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