1   package com.trendmicro.grid.acl.ds.jpa;
2   
3   import com.trendmicro.grid.acl.ds.ProcessingResultReceiver;
4   import com.trendmicro.grid.acl.ds.config.DataSourcePropertySection;
5   import com.trendmicro.grid.acl.l0.datatypes.ProcessPackageDataSet;
6   import net.sf.tinyjee.config.PropertySection;
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   import org.springframework.stereotype.Repository;
10  import org.springframework.transaction.annotation.Transactional;
11  
12  import javax.annotation.PostConstruct;
13  import javax.annotation.Resource;
14  import javax.persistence.EntityManager;
15  import javax.persistence.PersistenceContext;
16  import java.util.Collection;
17  
18  import static com.trendmicro.grid.acl.ds.config.DataSourcePropertySection.*;
19  import static com.trendmicro.grid.acl.ds.jpa.AbstractReceivedResultsHandler.StorageContext;
20  import static com.trendmicro.grid.acl.ds.jpa.AbstractReceivedResultsHandler.getValue;
21  
22  /**
23   * Implements ProcessingResultReceiver using JPA.
24   * <p/>
25   * Note: This is the primary implementation that stores content inside the CoreDB.
26   *
27   * @author Juergen_Kellerer, 2010-06-12
28   * @version 1.0
29   */
30  @Repository
31  @Transactional
32  public class JpaProcessingResultReceiver implements ProcessingResultReceiver {
33  
34  	private static final Logger log = LoggerFactory.getLogger(JpaProcessingResultReceiver.class);
35  
36  	@PersistenceContext(unitName = "CoreDB")
37  	EntityManager em;
38  
39  	@Resource
40  	JpaVendorRepository vendorRepository;
41  	@Resource
42  	JpaSourceRepository sourceRepository;
43  	@Resource
44  	JpaPackageFamilyRepository packageFamilyRepository;
45  	@Resource
46  	JpaPackageRepository packageRepository;
47  	@Resource
48  	JpaFileRepository fileRepository;
49  
50  	ReceivedDataSetsHandler dataSetsHandler;
51  	boolean ignoreNullMetadataUpdates, ignoreAllUpdates, enableHistoryRecording;
52  
53  	@PostConstruct
54  	void configure() {
55  		PropertySection section = getPropertySection();
56  
57  		ignoreNullMetadataUpdates = getValue(section, KEY_IGNORE_NULL_METADATA_UPDATES);
58  		ignoreAllUpdates = getValue(section, KEY_IGNORE_ALL_UPDATES);
59  		enableHistoryRecording = getValue(section, KEY_ENABLE_HISTORY_RECORDING);
60  
61  		// Building a handler chain.
62  		final ReceivedSourcesHandler sourcesHandler = new ReceivedSourcesHandler(sourceRepository);
63  		final ReceivedPackageFamiliesHandler familiesHandler = new ReceivedPackageFamiliesHandler(
64  			vendorRepository, packageFamilyRepository);
65  		final ReceivedPreparedPackagesHandler packagesHandler = new ReceivedPreparedPackagesHandler(
66  			fileRepository, packageRepository, familiesHandler);
67  
68  		dataSetsHandler = new ReceivedDataSetsHandler(
69  			fileRepository, packageRepository, packagesHandler, sourcesHandler);
70  	}
71  
72  	public void setEnableHistoryRecording(boolean enableHistoryRecording) {
73  		this.enableHistoryRecording = enableHistoryRecording;
74  	}
75  
76  	/**
77  	 * {@inheritDoc}
78  	 */
79  	public void receive(Collection<ProcessPackageDataSet> dataSets) {
80  		StorageContext context = new StorageContext(em, dataSets, ignoreAllUpdates, ignoreNullMetadataUpdates, enableHistoryRecording);
81  		dataSetsHandler.handle(context);
82  	}
83  
84  	/**
85  	 * {@inheritDoc}
86  	 */
87  	public void checkpoint() {
88  		// nothing to do as receive(..) is already transactional.
89  	}
90  }