5553b784f6f64c9cbe5aa90bc8f674d4f39af8aa
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorlicense.impl;
18
19 import org.apache.commons.collections.CollectionUtils;
20 import org.openecomp.core.util.UniqueValueUtil;
21 import org.openecomp.core.utilities.CommonMethods;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.common.errors.ErrorCode;
24 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
25 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
26 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
27 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
28 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
29 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
30 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
31 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
32 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
33 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
34 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
35 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
36 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
37 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
38 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
39 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
40 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
41 import org.openecomp.sdc.vendorlicense.errors.InvalidDateErrorBuilder;
42 import org.openecomp.sdc.vendorlicense.errors.LimitErrorBuilder;
43 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
44 import org.openecomp.sdc.versioning.VersioningUtil;
45 import org.openecomp.sdc.versioning.dao.types.Version;
46
47 import java.time.LocalDate;
48 import java.time.format.DateTimeFormatter;
49 import java.util.Collection;
50 import java.util.Objects;
51 import java.util.Optional;
52 import java.util.Set;
53
54 public class VendorLicenseManagerImpl implements VendorLicenseManager {
55   private VendorLicenseFacade vendorLicenseFacade;
56   private VendorLicenseModelDao vendorLicenseModelDao;
57   private LicenseAgreementDao licenseAgreementDao;
58   private FeatureGroupDao featureGroupDao;
59   private EntitlementPoolDao entitlementPoolDao;
60   private LicenseKeyGroupDao licenseKeyGroupDao;
61   private LimitDao limitDao;
62   private static final String EP_POOL_START_TIME = "T00:00:00Z";
63   private static final String EP_POOL_EXPIRY_TIME = "T23:59:59Z";
64   private static final  DateTimeFormatter FORMATTER
65           = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
66   public VendorLicenseManagerImpl(VendorLicenseFacade vendorLicenseFacade,
67                                   VendorLicenseModelDao vendorLicenseModelDao,
68                                   LicenseAgreementDao licenseAgreementDao,
69                                   FeatureGroupDao featureGroupDao,
70                                   EntitlementPoolDao entitlementPoolDao,
71                                   LicenseKeyGroupDao licenseKeyGroupDao,
72                                   LimitDao limitDao) {
73     this.vendorLicenseFacade = vendorLicenseFacade;
74     this.vendorLicenseModelDao = vendorLicenseModelDao;
75     this.licenseAgreementDao = licenseAgreementDao;
76     this.featureGroupDao = featureGroupDao;
77     this.entitlementPoolDao = entitlementPoolDao;
78     this.licenseKeyGroupDao = licenseKeyGroupDao;
79     this.limitDao = limitDao;
80   }
81
82
83   @Override
84   public void validate(String vendorLicenseModelId, Version version) {
85     vendorLicenseFacade.validate(vendorLicenseModelId, version);
86   }
87
88   @Override
89   public VendorLicenseModelEntity createVendorLicenseModel(
90       VendorLicenseModelEntity vendorLicenseModelEntity) {
91     vendorLicenseModelDao.create(vendorLicenseModelEntity);
92     return vendorLicenseModelEntity;
93   }
94
95   @Override
96   public void updateVendorLicenseModel(VendorLicenseModelEntity vendorLicenseModelEntity) {
97     String existingVendorName = vendorLicenseModelDao.get(vendorLicenseModelEntity).getVendorName();
98
99     updateUniqueName(VendorLicenseConstants.UniqueValues.VENDOR_NAME, existingVendorName,
100         vendorLicenseModelEntity.getVendorName());
101     vendorLicenseModelDao.update(vendorLicenseModelEntity);
102   }
103
104   @Override
105   public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
106     return vendorLicenseFacade.getVendorLicenseModel(vlmId, version);
107   }
108
109   @Override
110   public void deleteVendorLicenseModel(String vlmId, Version version) {
111     throw new UnsupportedOperationException(VendorLicenseConstants.UNSUPPORTED_OPERATION_ERROR);
112   }
113
114   @Override
115   public Collection<LicenseAgreementEntity> listLicenseAgreements(String vlmId, Version version) {
116     return licenseAgreementDao.list(new LicenseAgreementEntity(vlmId, version, null));
117   }
118
119   @Override
120   public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
121     return vendorLicenseFacade.createLicenseAgreement(licenseAgreement);
122   }
123
124   @Override
125   public void updateLicenseAgreement(LicenseAgreementEntity licenseAgreement,
126                                      Set<String> addedFeatureGroupIds,
127                                      Set<String> removedFeatureGroupIds) {
128     LicenseAgreementEntity retrieved = licenseAgreementDao.get(licenseAgreement);
129     VersioningUtil
130         .validateEntityExistence(retrieved, licenseAgreement, VendorLicenseModelEntity.ENTITY_TYPE);
131     VersioningUtil.validateContainedEntitiesExistence(new FeatureGroupEntity().getEntityType(),
132         removedFeatureGroupIds, retrieved, retrieved.getFeatureGroupIds());
133     VersioningUtil.validateEntitiesExistence(addedFeatureGroupIds,
134         new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
135             licenseAgreement.getVersion(),
136             null),
137         featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
138
139     updateUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
140         retrieved.getName(), licenseAgreement.getName(), licenseAgreement.getVendorLicenseModelId(),
141         licenseAgreement.getVersion().getId());
142     licenseAgreementDao.updateColumnsAndDeltaFeatureGroupIds(licenseAgreement, addedFeatureGroupIds,
143         removedFeatureGroupIds);
144
145     addFeatureGroupsToLicenseAgreementRef(addedFeatureGroupIds, licenseAgreement);
146     removeFeatureGroupsToLicenseAgreementRef(removedFeatureGroupIds, licenseAgreement);
147   }
148
149   @Override
150   public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
151                                                         String licenseAgreementId) {
152     return vendorLicenseFacade.getLicenseAgreementModel(vlmId, version, licenseAgreementId);
153   }
154
155   @Override
156   public void deleteLicenseAgreement(String vlmId, Version version, String licenseAgreementId) {
157     LicenseAgreementEntity input =
158         new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
159     LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
160     VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
161
162     removeFeatureGroupsToLicenseAgreementRef(retrieved.getFeatureGroupIds(), retrieved);
163
164     licenseAgreementDao.delete(retrieved);
165
166     deleteUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
167         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
168         retrieved.getName());
169   }
170
171   @Override
172   public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
173     return vendorLicenseFacade.listFeatureGroups(vlmId, version);
174   }
175
176   @Override
177   public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
178     return vendorLicenseFacade.createFeatureGroup(featureGroup);
179   }
180
181   @Override
182   public void updateFeatureGroup(FeatureGroupEntity featureGroup,
183                                  Set<String> addedLicenseKeyGroups,
184                                  Set<String> removedLicenseKeyGroups,
185                                  Set<String> addedEntitlementPools,
186                                  Set<String> removedEntitlementPools) {
187     FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
188     VersioningUtil
189         .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
190
191     VersioningUtil.validateContainedEntitiesExistence(new LicenseKeyGroupEntity().getEntityType(),
192         removedLicenseKeyGroups, retrieved, retrieved.getLicenseKeyGroupIds());
193     VersioningUtil.validateContainedEntitiesExistence(new EntitlementPoolEntity().getEntityType(),
194         removedEntitlementPools, retrieved, retrieved.getEntitlementPoolIds());
195
196     VersioningUtil.validateEntitiesExistence(addedLicenseKeyGroups,
197         new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
198             null),
199         licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
200     VersioningUtil.validateEntitiesExistence(addedEntitlementPools,
201         new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
202             null),
203         entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
204
205     updateUniqueName(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
206         retrieved.getName(), featureGroup.getName(), featureGroup.getVendorLicenseModelId(),
207         featureGroup.getVersion().getId());
208
209     addLicenseKeyGroupsToFeatureGroupsRef(addedLicenseKeyGroups, featureGroup);
210     removeLicenseKeyGroupsToFeatureGroupsRef(removedLicenseKeyGroups, featureGroup);
211     addEntitlementPoolsToFeatureGroupsRef(addedEntitlementPools, featureGroup);
212     removeEntitlementPoolsToFeatureGroupsRef(removedEntitlementPools, featureGroup);
213
214     featureGroupDao.updateFeatureGroup(featureGroup, addedEntitlementPools, removedEntitlementPools,
215         addedLicenseKeyGroups, removedLicenseKeyGroups);
216
217     updateEpLkgOnMrnChange(featureGroup, addedLicenseKeyGroups, addedEntitlementPools, retrieved);
218   }
219
220   /**
221    * If MRN is updated in feature group then update all linked EPs and Lkgs with new versionUuId
222    * @param featureGroup - Feature Group entity which is requested for update
223    * @param addedLicenseKeyGroups - LicenseKeyGroups added with Feature Group
224    * @param addedEntitlementPools - EntitlementPools added with Feature Group
225    * @param retrieved - Feature Group entity fetched from database
226    */
227   private void updateEpLkgOnMrnChange(FeatureGroupEntity featureGroup,
228                                       Set<String> addedLicenseKeyGroups,
229                                       Set<String> addedEntitlementPools,
230                                       FeatureGroupEntity retrieved) {
231     if (Objects.nonNull(retrieved.getManufacturerReferenceNumber())
232         && !retrieved.getManufacturerReferenceNumber().equals(featureGroup
233         .getManufacturerReferenceNumber())) {
234       if (CollectionUtils.isEmpty(addedEntitlementPools)) {
235         updateEntitlementPool(featureGroup, retrieved.getEntitlementPoolIds());
236       } else {
237         updateEntitlementPool(featureGroup, addedEntitlementPools);
238       }
239
240       if (CollectionUtils.isEmpty(addedLicenseKeyGroups)) {
241         updateLicenseKeyGroup(featureGroup, retrieved.getLicenseKeyGroupIds());
242       } else {
243         updateLicenseKeyGroup(featureGroup, addedLicenseKeyGroups);
244       }
245     }
246   }
247
248   private void updateEntitlementPool(FeatureGroupEntity featureGroup,
249                                      Set<String> entitlementPoolIds) {
250     for (String epId: entitlementPoolIds) {
251       final EntitlementPoolEntity entitlementPoolEntity = entitlementPoolDao
252           .get(new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup
253               .getVersion(), epId));
254       if (Objects.nonNull(entitlementPoolEntity)) {
255         entitlementPoolEntity.setVersionUuId(CommonMethods.nextUuId());
256         entitlementPoolDao.update(entitlementPoolEntity);
257       }
258     }
259   }
260
261   private void updateLicenseKeyGroup(FeatureGroupEntity featureGroup,
262                                      Set<String> licenseKeyGroupIds) {
263     for (String lkgId: licenseKeyGroupIds) {
264       final LicenseKeyGroupEntity licenseKeyGroupEntity = licenseKeyGroupDao
265           .get(new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
266               featureGroup.getVersion(), lkgId));
267       if (Objects.nonNull(licenseKeyGroupEntity)) {
268         licenseKeyGroupEntity.setVersionUuId(CommonMethods.nextUuId());
269         licenseKeyGroupDao.update(licenseKeyGroupEntity);
270       }
271     }
272   }
273
274   @Override
275   public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
276     return vendorLicenseFacade.getFeatureGroupModel(featureGroup);
277   }
278
279   @Override
280   public void deleteFeatureGroup(FeatureGroupEntity featureGroup) {
281     FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
282     VersioningUtil
283         .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
284
285     removeLicenseKeyGroupsToFeatureGroupsRef(retrieved.getLicenseKeyGroupIds(), featureGroup);
286     removeEntitlementPoolsToFeatureGroupsRef(retrieved.getEntitlementPoolIds(), featureGroup);
287
288     for (String licenceAgreementId : retrieved.getReferencingLicenseAgreements()) {
289       licenseAgreementDao.removeFeatureGroup(
290           new LicenseAgreementEntity(featureGroup.getVendorLicenseModelId(),
291               featureGroup.getVersion(),
292               licenceAgreementId), featureGroup.getId());
293     }
294
295     featureGroupDao.delete(featureGroup);
296
297     deleteUniqueName(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
298         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
299         retrieved.getName());
300   }
301
302   @Override
303   public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
304     return vendorLicenseFacade.listEntitlementPools(vlmId, version);
305   }
306
307   @Override
308   public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
309     entitlementPool.setStartDate(entitlementPool.getStartDate() != null ? (entitlementPool
310         .getStartDate().trim().length() != 0 ? entitlementPool.getStartDate() + EP_POOL_START_TIME
311         : null) : null);
312     entitlementPool.setExpiryDate(entitlementPool.getExpiryDate() != null ? (entitlementPool
313         .getExpiryDate().trim().length() != 0 ? entitlementPool.getExpiryDate() + EP_POOL_EXPIRY_TIME 
314         : null) : null);
315
316     validateCreateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(),
317         entitlementPool.getVendorLicenseModelId());
318     return vendorLicenseFacade.createEntitlementPool(entitlementPool);
319   }
320
321   private void validateCreateDate(String startDate, String expiryDate,
322                                   String vendorLicenseModelId) {
323   LocalDate parsedStartDate = parseLocalDate(startDate);
324   LocalDate parsedExpiryDate = parseLocalDate(expiryDate);
325
326
327     validateIfStartAndExpiryDateIsNotNull(startDate, expiryDate,
328             vendorLicenseModelId, parsedStartDate, parsedExpiryDate);
329
330     if (startDate != null && expiryDate == null
331                       && parsedStartDate.atStartOfDay().isBefore
332           (LocalDate.now().atStartOfDay())) {
333         throw new CoreException(
334             new InvalidDateErrorBuilder(vendorLicenseModelId)
335                 .build());
336     }
337
338     if (startDate == null && expiryDate != null) {
339       throw new CoreException(
340           new InvalidDateErrorBuilder(vendorLicenseModelId)
341               .build());
342
343     }
344   }
345
346   private void validateIfStartAndExpiryDateIsNotNull(String startDate, String expiryDate,
347                                                      String vendorLicenseModelId,
348                                                      LocalDate parsedStartDate,
349                                                      LocalDate parsedExpiryDate) {
350     if (startDate != null && expiryDate != null
351             && isValidatStartAndExpiryDate(parsedStartDate, parsedExpiryDate)) {
352       throw new CoreException(
353               new InvalidDateErrorBuilder(vendorLicenseModelId)
354                       .build());
355     }
356   }
357
358   private boolean isValidatStartAndExpiryDate(LocalDate parsedStartDate,
359                                               LocalDate parsedExpiryDate) {
360     return parsedStartDate.atStartOfDay().isBefore(LocalDate.now().atStartOfDay())
361     || parsedExpiryDate.atStartOfDay().isEqual(parsedStartDate.atStartOfDay())
362     || parsedExpiryDate.isBefore(parsedStartDate);
363   }
364
365   private static LocalDate parseLocalDate(String date) {
366     if (date == null || date.isEmpty()) {
367       return null;
368     }
369
370     return LocalDate.parse(date, FORMATTER );
371   }
372
373   private void validateUpdateDate(String startDate, String expiryDate,
374                                   String vendorLicenseModelId) {
375     LocalDate parsedStartDate = parseLocalDate(startDate);
376     LocalDate parsedExpiryDate = parseLocalDate(expiryDate);
377
378     if (startDate != null && expiryDate != null
379             && (parsedExpiryDate.atStartOfDay()
380             .isEqual(parsedStartDate.atStartOfDay())
381             || parsedExpiryDate.isBefore(parsedStartDate ))) {
382       throw new CoreException(
383               new InvalidDateErrorBuilder(vendorLicenseModelId)
384                       .build());
385     }
386
387     if (startDate == null && expiryDate != null) {
388       throw new CoreException(
389           new InvalidDateErrorBuilder(vendorLicenseModelId)
390               .build());
391
392     }
393   }
394
395   @Override
396   public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
397     entitlementPool.setStartDate(entitlementPool.getStartDate() != null ? (entitlementPool
398         .getStartDate().trim().length() != 0 ? entitlementPool.getStartDate() + EP_POOL_START_TIME
399         : null) : null);
400     entitlementPool.setExpiryDate(entitlementPool.getExpiryDate() != null ? (entitlementPool
401         .getExpiryDate().trim().length() != 0 ? entitlementPool.getExpiryDate() + EP_POOL_EXPIRY_TIME 
402         : null) : null);
403
404     validateUpdateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(),
405         entitlementPool.getVendorLicenseModelId());
406     vendorLicenseFacade.updateEntitlementPool(entitlementPool);
407   }
408
409   @Override
410   public EntitlementPoolEntity getEntitlementPool(EntitlementPoolEntity entitlementPool) {
411     EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
412     VersioningUtil
413         .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
414     DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
415     if (retrieved.getStartDate() != null) {
416       retrieved.setStartDate(LocalDate.parse(retrieved.getStartDate(), FORMATTER ).format
417           (targetFormatter));
418     }
419
420     if (retrieved.getExpiryDate() != null) {
421       retrieved.setExpiryDate(LocalDate.parse(retrieved.getExpiryDate(), FORMATTER ).format
422           (targetFormatter));
423     }
424     return retrieved;
425   }
426
427   @Override
428   public void deleteEntitlementPool(EntitlementPoolEntity entitlementPool) {
429     EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
430     VersioningUtil
431         .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
432
433     for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
434       featureGroupDao.removeEntitlementPool(
435           new FeatureGroupEntity(entitlementPool.getVendorLicenseModelId(),
436               entitlementPool.getVersion(),
437               referencingFeatureGroupId), entitlementPool.getId());
438     }
439
440     deleteChildLimits(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(),
441         entitlementPool.getId());
442
443     entitlementPoolDao.delete(entitlementPool);
444
445     deleteUniqueName(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
446         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
447         retrieved.getName());
448   }
449
450   protected void deleteChildLimits(String vlmId, Version version, String epLkgId) {
451     Optional<Collection<LimitEntity>> limitEntities = Optional.ofNullable(
452         listLimits(vlmId, version, epLkgId));
453     limitEntities.ifPresent(entities -> entities.forEach(this::deleteLimit));
454   }
455
456   @Override
457   public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
458     return vendorLicenseFacade.listLicenseKeyGroups(vlmId, version);
459   }
460
461   @Override
462   public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
463     licenseKeyGroup.setStartDate(licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup
464         .getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + EP_POOL_START_TIME
465         : null) : null);
466     licenseKeyGroup.setExpiryDate(licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup
467         .getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + EP_POOL_EXPIRY_TIME 
468         : null) : null);
469
470     validateCreateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(),
471         licenseKeyGroup.getVendorLicenseModelId());
472     return vendorLicenseFacade.createLicenseKeyGroup(licenseKeyGroup);
473   }
474
475   @Override
476   public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
477     licenseKeyGroup.setStartDate(licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup
478         .getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + EP_POOL_START_TIME
479         : null) : null);
480     licenseKeyGroup.setExpiryDate(licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup
481         .getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + EP_POOL_EXPIRY_TIME 
482         : null) : null);
483
484     validateUpdateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(),
485         licenseKeyGroup.getVendorLicenseModelId());
486     vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroup);
487   }
488
489   @Override
490   public LicenseKeyGroupEntity getLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
491     LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
492     VersioningUtil
493         .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
494     return retrieved;
495   }
496
497   @Override
498   public void deleteLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
499     LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
500     VersioningUtil
501         .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
502
503     for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
504       featureGroupDao.removeLicenseKeyGroup(
505           new FeatureGroupEntity(licenseKeyGroup.getVendorLicenseModelId(),
506               licenseKeyGroup.getVersion(),
507               referencingFeatureGroupId), licenseKeyGroup.getId());
508     }
509
510     deleteChildLimits(licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(),
511         licenseKeyGroup.getId());
512
513     licenseKeyGroupDao.delete(licenseKeyGroup);
514
515     deleteUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
516         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
517         retrieved.getName());
518   }
519
520   @Override
521   public LimitEntity createLimit(LimitEntity limit) {
522     validateLimit(limit);
523     LimitEntity createdLimit = vendorLicenseFacade.createLimit(limit);
524     updateParentForLimit(limit);
525     return createdLimit;
526   }
527
528   private void validateLimit(LimitEntity limit) {
529     Collection<LimitEntity> limitList =
530         listLimits(limit.getVendorLicenseModelId(), limit.getVersion()
531             , limit.getEpLkgId());
532
533     if (!isLimitNameUnique(limitList, limit.getName(), limit.getType(), limit.getId())) {
534       final ErrorCode duplicateLimitNameErrorBuilder =
535           LimitErrorBuilder.getDuplicateNameErrorbuilder(limit.getName(), limit.getType().name());
536       throw new CoreException(duplicateLimitNameErrorBuilder);
537     }
538   }
539
540   private boolean isLimitNameUnique(Collection<LimitEntity> limitList, String name, LimitType
541       type, String id) {
542     for (LimitEntity limit : limitList) {
543       if (limit.getName().equalsIgnoreCase(name) &&
544           limit.getType().name().equalsIgnoreCase(type.name())) {
545         if (id != null && limit.getId().equals(id)) {
546           continue;
547         }
548         return false;
549       }
550     }
551     return true;
552   }
553
554   @Override
555   public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
556     return vendorLicenseFacade.listLimits(vlmId, version, epLkgId);
557   }
558
559   @Override
560   public void deleteLimit(LimitEntity limitEntity) {
561     if (!isLimitPresent(limitEntity)) {
562       VersioningUtil
563           .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
564     }
565     LimitEntity retrieved = limitDao.get(limitEntity);
566     VersioningUtil
567         .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
568
569     limitDao.delete(limitEntity);
570
571     updateParentForLimit(limitEntity);
572   }
573
574   @Override
575   public void updateLimit(LimitEntity limit) {
576     getLimit(limit);
577     validateLimit(limit);
578     vendorLicenseFacade.updateLimit(limit);
579     updateParentForLimit(limit);
580   }
581
582   private boolean isLimitPresent(LimitEntity limit) {
583     return limitDao.isLimitPresent(limit);
584   }
585
586   @Override
587   public LimitEntity getLimit(LimitEntity limitEntity) {
588     if (!isLimitPresent(limitEntity)) {
589       VersioningUtil
590           .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
591     }
592     LimitEntity retrieved = limitDao.get(limitEntity);
593     VersioningUtil
594         .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
595     return retrieved;
596   }
597
598   /**
599    * update Parent of limit (EP/LKG) versionuuid when limit is modified so that limit updates are
600    * captured in VLM XML
601    */
602   private void updateParentForLimit(LimitEntity limit) {
603     if ("EntitlementPool".equals(limit.getParent())) {
604       EntitlementPoolEntity entitlementPoolEntity =
605           entitlementPoolDao.get(new EntitlementPoolEntity(limit.getVendorLicenseModelId(),
606               limit.getVersion(), limit.getEpLkgId()));
607       vendorLicenseFacade.updateEntitlementPool(entitlementPoolEntity);
608     }
609
610     if ("LicenseKeyGroup".equals(limit.getParent())) {
611       LicenseKeyGroupEntity licenseKeyGroupEntity = licenseKeyGroupDao.get(
612           new LicenseKeyGroupEntity(limit.getVendorLicenseModelId(), limit.getVersion(),
613               limit.getEpLkgId()));
614       vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroupEntity);
615     }
616   }
617
618   protected void addFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
619                                                        LicenseAgreementEntity licenseAgreement) {
620     if (featureGroupIds != null) {
621       for (String featureGroupId : featureGroupIds) {
622         featureGroupDao.addReferencingLicenseAgreement(
623             new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
624                 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
625       }
626     }
627   }
628
629   protected void removeFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
630                                                           LicenseAgreementEntity licenseAgreement) {
631     if (featureGroupIds != null) {
632       for (String featureGroupId : featureGroupIds) {
633         featureGroupDao.removeReferencingLicenseAgreement(
634             new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
635                 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
636       }
637     }
638   }
639
640   protected void addLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
641                                                        FeatureGroupEntity featureGroup) {
642     if (licenseKeyGroupIds != null) {
643       for (String licenseKeyGroupId : licenseKeyGroupIds) {
644         licenseKeyGroupDao.addReferencingFeatureGroup(
645             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
646                 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
647       }
648     }
649   }
650
651   protected void removeLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
652                                                           FeatureGroupEntity featureGroup) {
653     if (licenseKeyGroupIds != null) {
654       for (String licenseKeyGroupId : licenseKeyGroupIds) {
655         licenseKeyGroupDao.removeReferencingFeatureGroup(
656             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
657                 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
658       }
659     }
660   }
661
662   protected void addEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
663                                                        FeatureGroupEntity featureGroup) {
664     if (entitlementPoolIds != null) {
665       for (String entitlementPoolId : entitlementPoolIds) {
666         entitlementPoolDao.addReferencingFeatureGroup(
667             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
668                 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
669       }
670     }
671   }
672
673   protected void removeEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
674                                                           FeatureGroupEntity featureGroup) {
675     if (entitlementPoolIds != null) {
676       for (String entitlementPoolId : entitlementPoolIds) {
677         entitlementPoolDao.removeReferencingFeatureGroup(
678             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
679                 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
680       }
681     }
682   }
683
684   protected void updateUniqueName(String uniqueValueType, String oldName, String newName, String...
685       context) {
686     UniqueValueUtil
687         .updateUniqueValue(uniqueValueType, oldName, newName, context);
688   }
689
690   protected void deleteUniqueName(String uniqueValueType, String... uniqueCombination) {
691     UniqueValueUtil.deleteUniqueValue(uniqueValueType, uniqueCombination);
692   }
693 }