f27e6c05c467c8357348e3a795783740d9152636
[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(getDate(entitlementPool.getStartDate(), EP_POOL_START_TIME));
252     entitlementPool.setExpiryDate(getDate(entitlementPool.getExpiryDate(), EP_POOL_EXPIRY_TIME));
253     validateCreateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(),
254         entitlementPool.getVendorLicenseModelId());
255     return vendorLicenseFacade.createEntitlementPool(entitlementPool);
256   }
257
258   private String getDate(String date, String poolTime){
259     return date != null ? (!date.trim().isEmpty() ? date + poolTime: null) : null;
260   }
261
262   private void validateCreateDate(String startDate, String expiryDate,
263                                   String vendorLicenseModelId) {
264     if(isNull(startDate, expiryDate) || isEmpty(startDate, expiryDate) ||
265             isInvalidStartEndDate(startDate, expiryDate)){
266       throw new CoreException(
267               new InvalidDateErrorBuilder(vendorLicenseModelId)
268                       .build());
269     }
270   }
271
272   private boolean isInvalidStartEndDate(String startDate, String expiryDate) {
273     LocalDate parsedStartDate = parseLocalDate(startDate);
274     LocalDate parsedExpiryDate = parseLocalDate(expiryDate);
275
276     return parsedStartDate.atStartOfDay().isBefore(LocalDate.now().atStartOfDay())
277             || parsedExpiryDate.atStartOfDay().isEqual(parsedStartDate.atStartOfDay())
278             || parsedExpiryDate.isBefore(parsedStartDate);
279   }
280
281   private boolean isEmpty(String startDate, String expiryDate) {
282     return startDate.isEmpty() || expiryDate.isEmpty();
283   }
284
285   private boolean isNull(String startDate, String expiryDate) {
286     return startDate == null || expiryDate == null;
287   }
288
289   private static LocalDate parseLocalDate(String date) {
290     return LocalDate.parse(date, FORMATTER );
291   }
292
293   private void validateUpdateDate(String startDate, String expiryDate,
294                                   String vendorLicenseModelId) {
295
296     if(isNull(startDate, expiryDate) || isEmpty(startDate, expiryDate)
297      || isInvalidUpdateDate(startDate, expiryDate)){
298       throw new CoreException(
299               new InvalidDateErrorBuilder(vendorLicenseModelId)
300                       .build());
301     }
302   }
303
304   private boolean isInvalidUpdateDate(String startDate, String expiryDate) {
305
306     LocalDate parsedStartDate = parseLocalDate(startDate);
307     LocalDate parsedExpiryDate = parseLocalDate(expiryDate);
308
309     return parsedExpiryDate.atStartOfDay()
310             .isEqual(parsedStartDate.atStartOfDay())
311             || parsedExpiryDate.isBefore(parsedStartDate);
312   }
313
314   @Override
315   public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
316     entitlementPool.setStartDate(getDate(entitlementPool.getStartDate(), EP_POOL_START_TIME));
317     entitlementPool.setExpiryDate(getDate(entitlementPool.getExpiryDate(), EP_POOL_EXPIRY_TIME));
318     validateUpdateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(),
319         entitlementPool.getVendorLicenseModelId());
320     vendorLicenseFacade.updateEntitlementPool(entitlementPool);
321   }
322
323   @Override
324   public EntitlementPoolEntity getEntitlementPool(EntitlementPoolEntity entitlementPool) {
325     EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
326     VersioningUtil
327         .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
328     DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
329     if (retrieved.getStartDate() != null) {
330       retrieved.setStartDate(LocalDate.parse(retrieved.getStartDate(), FORMATTER ).format
331           (targetFormatter));
332     }
333
334     if (retrieved.getExpiryDate() != null) {
335       retrieved.setExpiryDate(LocalDate.parse(retrieved.getExpiryDate(), FORMATTER ).format
336           (targetFormatter));
337     }
338     return retrieved;
339   }
340
341   @Override
342   public void deleteEntitlementPool(EntitlementPoolEntity entitlementPool) {
343     EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
344     VersioningUtil
345         .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
346
347     for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
348       featureGroupDao.removeEntitlementPool(
349           new FeatureGroupEntity(entitlementPool.getVendorLicenseModelId(),
350               entitlementPool.getVersion(),
351               referencingFeatureGroupId), entitlementPool.getId());
352     }
353
354     deleteChildLimits(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(),
355         entitlementPool.getId());
356
357     entitlementPoolDao.delete(entitlementPool);
358
359     deleteUniqueName(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
360         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
361         retrieved.getName());
362   }
363
364   protected void deleteChildLimits(String vlmId, Version version, String epLkgId) {
365     Optional<Collection<LimitEntity>> limitEntities = Optional.ofNullable(
366         listLimits(vlmId, version, epLkgId));
367     limitEntities.ifPresent(entities -> entities.forEach(this::deleteLimit));
368   }
369
370   @Override
371   public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
372     return vendorLicenseFacade.listLicenseKeyGroups(vlmId, version);
373   }
374
375   @Override
376   public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
377     licenseKeyGroup.setStartDate(licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup
378         .getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + EP_POOL_START_TIME
379         : null) : null);
380     licenseKeyGroup.setExpiryDate(licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup
381         .getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + EP_POOL_EXPIRY_TIME 
382         : null) : null);
383
384     validateCreateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(),
385         licenseKeyGroup.getVendorLicenseModelId());
386     return vendorLicenseFacade.createLicenseKeyGroup(licenseKeyGroup);
387   }
388
389   @Override
390   public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
391     licenseKeyGroup.setStartDate(licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup
392         .getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + EP_POOL_START_TIME
393         : null) : null);
394     licenseKeyGroup.setExpiryDate(licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup
395         .getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + EP_POOL_EXPIRY_TIME 
396         : null) : null);
397
398     validateUpdateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(),
399         licenseKeyGroup.getVendorLicenseModelId());
400     vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroup);
401   }
402
403   @Override
404   public LicenseKeyGroupEntity getLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
405     LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
406     VersioningUtil
407         .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
408     return retrieved;
409   }
410
411   @Override
412   public void deleteLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
413     LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
414     VersioningUtil
415         .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
416
417     for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
418       featureGroupDao.removeLicenseKeyGroup(
419           new FeatureGroupEntity(licenseKeyGroup.getVendorLicenseModelId(),
420               licenseKeyGroup.getVersion(),
421               referencingFeatureGroupId), licenseKeyGroup.getId());
422     }
423
424     deleteChildLimits(licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(),
425         licenseKeyGroup.getId());
426
427     licenseKeyGroupDao.delete(licenseKeyGroup);
428
429     deleteUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
430         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
431         retrieved.getName());
432   }
433
434   @Override
435   public LimitEntity createLimit(LimitEntity limit) {
436     validateLimit(limit);
437     LimitEntity createdLimit = vendorLicenseFacade.createLimit(limit);
438     updateParentForLimit(limit);
439     return createdLimit;
440   }
441
442   private void validateLimit(LimitEntity limit) {
443     Collection<LimitEntity> limitList =
444         listLimits(limit.getVendorLicenseModelId(), limit.getVersion()
445             , limit.getEpLkgId());
446
447     if (!isLimitNameUnique(limitList, limit.getName(), limit.getType(), limit.getId())) {
448       final ErrorCode duplicateLimitNameErrorBuilder =
449           LimitErrorBuilder.getDuplicateNameErrorbuilder(limit.getName(), limit.getType().name());
450       throw new CoreException(duplicateLimitNameErrorBuilder);
451     }
452   }
453
454   private boolean isLimitNameUnique(Collection<LimitEntity> limitList, String name, LimitType
455       type, String id) {
456     for (LimitEntity limit : limitList) {
457       if (limit.getName().equalsIgnoreCase(name) &&
458           limit.getType().name().equalsIgnoreCase(type.name())) {
459         if (id != null && limit.getId().equals(id)) {
460           continue;
461         }
462         return false;
463       }
464     }
465     return true;
466   }
467
468   @Override
469   public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
470     return vendorLicenseFacade.listLimits(vlmId, version, epLkgId);
471   }
472
473   @Override
474   public void deleteLimit(LimitEntity limitEntity) {
475     if (!isLimitPresent(limitEntity)) {
476       VersioningUtil
477           .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
478     }
479     LimitEntity retrieved = limitDao.get(limitEntity);
480     VersioningUtil
481         .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
482
483     limitDao.delete(limitEntity);
484
485     updateParentForLimit(limitEntity);
486   }
487
488   @Override
489   public void updateLimit(LimitEntity limit) {
490     getLimit(limit);
491     validateLimit(limit);
492     LimitEntity retrieved = limitDao.get(limit);
493     if(!retrieved.equals(limit)){
494       vendorLicenseFacade.updateLimit(limit);
495       updateParentForLimit(limit);
496     }
497   }
498
499   private boolean isLimitPresent(LimitEntity limit) {
500     return limitDao.isLimitPresent(limit);
501   }
502
503   @Override
504   public LimitEntity getLimit(LimitEntity limitEntity) {
505     if (!isLimitPresent(limitEntity)) {
506       VersioningUtil
507           .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
508     }
509     LimitEntity retrieved = limitDao.get(limitEntity);
510     VersioningUtil
511         .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
512     return retrieved;
513   }
514
515   /**
516    * update Parent of limit (EP/LKG) versionuuid when limit is modified so that limit updates are
517    * captured in VLM XML
518    */
519   private void updateParentForLimit(LimitEntity limit) {
520     if ("EntitlementPool".equals(limit.getParent())) {
521       EntitlementPoolEntity entitlementPoolEntity =
522           entitlementPoolDao.get(new EntitlementPoolEntity(limit.getVendorLicenseModelId(),
523               limit.getVersion(), limit.getEpLkgId()));
524       entitlementPoolEntity.setVersionUuId(CommonMethods.nextUuId());
525       entitlementPoolDao.update(entitlementPoolEntity);
526     }
527
528     if ("LicenseKeyGroup".equals(limit.getParent())) {
529       LicenseKeyGroupEntity licenseKeyGroupEntity = licenseKeyGroupDao.get(
530           new LicenseKeyGroupEntity(limit.getVendorLicenseModelId(), limit.getVersion(),
531               limit.getEpLkgId()));
532       licenseKeyGroupEntity.setVersionUuId(CommonMethods.nextUuId());
533       licenseKeyGroupDao.update(licenseKeyGroupEntity);
534     }
535   }
536
537   protected void addFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
538                                                        LicenseAgreementEntity licenseAgreement) {
539     if (featureGroupIds != null) {
540       for (String featureGroupId : featureGroupIds) {
541         featureGroupDao.addReferencingLicenseAgreement(
542             new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
543                 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
544       }
545     }
546   }
547
548   protected void removeFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
549                                                           LicenseAgreementEntity licenseAgreement) {
550     if (featureGroupIds != null) {
551       for (String featureGroupId : featureGroupIds) {
552         featureGroupDao.removeReferencingLicenseAgreement(
553             new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
554                 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
555       }
556     }
557   }
558
559   protected void addLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
560                                                        FeatureGroupEntity featureGroup) {
561     if (licenseKeyGroupIds != null) {
562       for (String licenseKeyGroupId : licenseKeyGroupIds) {
563         licenseKeyGroupDao.addReferencingFeatureGroup(
564             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
565                 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
566       }
567     }
568   }
569
570   protected void removeLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
571                                                           FeatureGroupEntity featureGroup) {
572     if (licenseKeyGroupIds != null) {
573       for (String licenseKeyGroupId : licenseKeyGroupIds) {
574         licenseKeyGroupDao.removeReferencingFeatureGroup(
575             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
576                 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
577       }
578     }
579   }
580
581   protected void addEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
582                                                        FeatureGroupEntity featureGroup) {
583     if (entitlementPoolIds != null) {
584       for (String entitlementPoolId : entitlementPoolIds) {
585         entitlementPoolDao.addReferencingFeatureGroup(
586             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
587                 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
588       }
589     }
590   }
591
592   protected void removeEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
593                                                           FeatureGroupEntity featureGroup) {
594     if (entitlementPoolIds != null) {
595       for (String entitlementPoolId : entitlementPoolIds) {
596         entitlementPoolDao.removeReferencingFeatureGroup(
597             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
598                 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
599       }
600     }
601   }
602
603   protected void updateUniqueName(String uniqueValueType, String oldName, String newName, String...
604       context) {
605     uniqueValueUtil
606         .updateUniqueValue(uniqueValueType, oldName, newName, context);
607   }
608
609   protected void deleteUniqueName(String uniqueValueType, String... uniqueCombination) {
610     uniqueValueUtil.deleteUniqueValue(uniqueValueType, uniqueCombination);
611   }
612 }