1 package com.trendmicro.grid.acl.ds.jpa;
2
3 import com.trendmicro.grid.acl.ds.jpa.entities.JpaPackageDetails;
4 import com.trendmicro.grid.acl.l0.datatypes.ProcessPackageDataSet;
5 import com.trendmicro.grid.acl.metadata.Metadata;
6 import net.sf.tinyjee.config.PropertySection;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import javax.persistence.EntityManager;
11 import javax.persistence.FlushModeType;
12 import java.util.Collection;
13 import java.util.Date;
14
15 import static com.trendmicro.grid.acl.ds.jpa.entities.JpaPackageInformation.ROOT_PACKAGE_NAME;
16 import static net.sf.tinyjee.util.Assert.assertNotEmpty;
17 import static net.sf.tinyjee.util.Assert.assertNotNull;
18
19
20
21
22
23
24
25 public abstract class AbstractReceivedResultsHandler<T> {
26
27 private static final Logger log = LoggerFactory.getLogger(AbstractReceivedResultsHandler.class);
28
29
30
31
32
33 protected static class StorageContext {
34
35 protected final EntityManager em;
36 protected final Collection<ProcessPackageDataSet> receivedDataSets;
37 protected final boolean ignoreAllUpdates, ignoreNullMetadataUpdates, enableHistoryRecording;
38
39 private JpaPackageDetails rootPackageReference;
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public StorageContext(EntityManager em, Collection<ProcessPackageDataSet> receivedDataSets,
54 boolean ignoreAllUpdates, boolean ignoreNullMetadataUpdates, boolean enableHistoryRecording) {
55 assertNotNull("EntityManager", em);
56 assertNotEmpty("Collection<ProcessPackageDataSet>", receivedDataSets);
57
58 this.em = em;
59 this.receivedDataSets = receivedDataSets;
60 this.ignoreAllUpdates = ignoreAllUpdates;
61 this.ignoreNullMetadataUpdates = ignoreAllUpdates || ignoreNullMetadataUpdates;
62 this.enableHistoryRecording = enableHistoryRecording;
63
64
65
66 if (em.getFlushMode() != FlushModeType.COMMIT)
67 em.setFlushMode(FlushModeType.COMMIT);
68 }
69
70
71
72
73
74
75
76 protected JpaPackageDetails getRootPackageReference(JpaPackageRepository repository) {
77 if (rootPackageReference == null) {
78 final Integer rootPk = repository.getPackagePrimaryKeyByName(ROOT_PACKAGE_NAME);
79 if (rootPk != null)
80 rootPackageReference = em.getReference(JpaPackageDetails.class, rootPk);
81 }
82 return rootPackageReference;
83 }
84 }
85
86
87
88
89
90
91
92 protected static boolean isNull(Metadata metadata) {
93 return metadata == null || metadata.getMetaElements().isEmpty();
94 }
95
96
97
98
99
100
101
102
103 protected static boolean isAfter(Date reference, Date dateToCheck) {
104 return dateToCheck != null && (reference == null || dateToCheck.after(reference));
105 }
106
107
108
109
110
111
112
113
114 protected static boolean getValue(PropertySection section, String property) {
115 try {
116 return section.getProperty(property).getValue().getBoolean();
117 } catch (Exception e) {
118 log.warn("TMACL-01050:Failed getting property {}, using 'false'", property);
119 return false;
120 }
121 }
122
123
124
125
126
127
128
129 protected abstract T handle(StorageContext context);
130 }