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