rsanchez
2017-04-03 347803bd8d8349baa0577156896a1ec924a69e6d
securis/src/main/java/net/curisit/securis/services/helpers/MetadataHelper.java
....@@ -16,6 +16,8 @@
1616
1717 import net.curisit.securis.db.Application;
1818 import net.curisit.securis.db.ApplicationMetadata;
19
+import net.curisit.securis.db.License;
20
+import net.curisit.securis.db.LicenseStatus;
1921 import net.curisit.securis.db.LicenseType;
2022 import net.curisit.securis.db.LicenseTypeMetadata;
2123 import net.curisit.securis.db.Pack;
....@@ -108,13 +110,11 @@
108110 public void propagateMetadata(EntityManager em, Application app) {
109111 Set<ApplicationMetadata> appMd = app.getApplicationMetadata();
110112 Set<String> keys = appMd.parallelStream().map(md -> md.getKey()).collect(Collectors.toSet());
111
- log.info("App metadata keys: {}", keys);
112113 for (LicenseType lt : app.getLicenseTypes()) {
113114 log.info("Lic type to update: {}", lt.getCode());
114115 this.mergeMetadata(em, appMd, lt.getMetadata(), keys);
115116 Set<LicenseTypeMetadata> newMdList = createNewMetadata(appMd, lt.getMetadata(), lt);
116117 for (LicenseTypeMetadata newMetadata : newMdList) {
117
- log.info("MD key to add to lt: {}", newMetadata.getKey());
118118 em.persist(newMetadata);
119119 }
120120 em.detach(lt);
....@@ -142,14 +142,29 @@
142142 List<Pack> packs = packsQuery.getResultList();
143143 log.info("Packs to update the metadata: {}", packs.size());
144144 for (Pack pack : packs) {
145
+ if (pack.isFrozen()) {
146
+ log.warn("Metadata in LicenseType {} has changed but the Pack {} is frozen and won't be updated.", lt.getCode(), pack.getCode());
147
+ continue;
148
+ }
145149 this.mergeMetadata(em, ltMd, pack.getMetadata(), keys);
146150 Set<PackMetadata> newMdList = createNewMetadata(ltMd, pack.getMetadata(), pack);
147151 for (PackMetadata newMetadata : newMdList) {
148
- log.info("MD key to add to pack: {}", newMetadata.getKey());
149152 em.persist(newMetadata);
150153 }
154
+ markObsoleteMetadata(em, pack);
151155 em.detach(pack);
152156 }
153157 }
154158
159
+ public void markObsoleteMetadata(EntityManager em, Pack pack) {
160
+ TypedQuery<License> existingPackLicenses = em.createNamedQuery("list-licenses-by-pack", License.class);
161
+ existingPackLicenses.setParameter("packId", pack.getId());
162
+ for (License lic : existingPackLicenses.getResultList()) {
163
+ log.info("License from pack: {}, status: {}", lic.getCode(), lic.getStatus());
164
+ if (lic.getStatus() == LicenseStatus.ACTIVE || lic.getStatus() == LicenseStatus.PRE_ACTIVE || lic.getStatus() == LicenseStatus.CANCELLED) {
165
+ lic.setMetadataObsolete(true);
166
+ em.merge(lic);
167
+ }
168
+ }
169
+ }
155170 }