From d378c37fbd1ecec7b43394926f1ca32a695e07de Mon Sep 17 00:00:00 2001 From: vasraz Date: Mon, 22 Mar 2021 15:33:06 +0000 Subject: Reformat openecomp-be Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3449 Change-Id: I13e02322f8e00820cc5a1d85752caaeda9bf10d1 --- .../sdc/vendorlicense/VendorLicenseManager.java | 86 +- .../vendorlicense/VendorLicenseManagerFactory.java | 14 +- .../vendorlicense/errors/LimitErrorBuilder.java | 48 +- .../impl/VendorLicenseManagerFactoryImpl.java | 35 +- .../impl/VendorLicenseManagerImpl.java | 1061 +++++++++----------- 5 files changed, 556 insertions(+), 688 deletions(-) (limited to 'openecomp-be/backend/openecomp-sdc-vendor-license-manager') diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManager.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManager.java index 60288b53c3..c5c93ae00c 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManager.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManager.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,82 +17,80 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.vendorlicense; -import org.openecomp.sdc.vendorlicense.dao.types.*; -import org.openecomp.sdc.versioning.dao.types.Version; - import java.util.Collection; import java.util.Set; +import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity; +import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity; +import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel; +import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity; +import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel; +import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity; +import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity; +import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity; +import org.openecomp.sdc.versioning.dao.types.Version; public interface VendorLicenseManager { - void validate(String vendorLicenseModelId, Version version); - - VendorLicenseModelEntity createVendorLicenseModel(VendorLicenseModelEntity licenseModel); - - void updateVendorLicenseModel(VendorLicenseModelEntity licenseModel); - - VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version); - - void deleteVendorLicenseModel(String vlmId, Version version); + void validate(String vendorLicenseModelId, Version version); + VendorLicenseModelEntity createVendorLicenseModel(VendorLicenseModelEntity licenseModel); - Collection listLicenseAgreements(String vlmId, Version version); + void updateVendorLicenseModel(VendorLicenseModelEntity licenseModel); - LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement); + VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version); - void updateLicenseAgreement(LicenseAgreementEntity licenseAgreement, - Set addedFeatureGroupIds, Set removedFeatureGroupIds); + void deleteVendorLicenseModel(String vlmId, Version version); - LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version, - String licenseAgreementId); + Collection listLicenseAgreements(String vlmId, Version version); - void deleteLicenseAgreement(String vlmId, Version version, String licenseAgreementId); + LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement); + void updateLicenseAgreement(LicenseAgreementEntity licenseAgreement, Set addedFeatureGroupIds, Set removedFeatureGroupIds); - Collection listFeatureGroups(String vlmId, Version version); + LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version, String licenseAgreementId); - FeatureGroupEntity createFeatureGroup(FeatureGroupEntity fg); + void deleteLicenseAgreement(String vlmId, Version version, String licenseAgreementId); - void updateFeatureGroup(FeatureGroupEntity featureGroup, - Set addedLicenseKeyGroups, Set removedLicenseKeyGroups, - Set addedEntitlementPools, Set removedEntitlementPools); + Collection listFeatureGroups(String vlmId, Version version); - FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup); + FeatureGroupEntity createFeatureGroup(FeatureGroupEntity fg); - void deleteFeatureGroup(FeatureGroupEntity featureGroup); + void updateFeatureGroup(FeatureGroupEntity featureGroup, Set addedLicenseKeyGroups, Set removedLicenseKeyGroups, + Set addedEntitlementPools, Set removedEntitlementPools); + FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup); - Collection listEntitlementPools(String vlmId, Version version); + void deleteFeatureGroup(FeatureGroupEntity featureGroup); - EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool); + Collection listEntitlementPools(String vlmId, Version version); - void updateEntitlementPool(EntitlementPoolEntity entitlementPool); + EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool); - EntitlementPoolEntity getEntitlementPool(EntitlementPoolEntity entitlementPool); + void updateEntitlementPool(EntitlementPoolEntity entitlementPool); - void deleteEntitlementPool(EntitlementPoolEntity entitlementPool); + EntitlementPoolEntity getEntitlementPool(EntitlementPoolEntity entitlementPool); + void deleteEntitlementPool(EntitlementPoolEntity entitlementPool); - Collection listLicenseKeyGroups(String vlmId, Version version); + Collection listLicenseKeyGroups(String vlmId, Version version); - LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup); + LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup); - void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup); + void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup); - LicenseKeyGroupEntity getLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup); + LicenseKeyGroupEntity getLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup); - void deleteLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup); + void deleteLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup); - LimitEntity createLimit(LimitEntity limitEntity); + LimitEntity createLimit(LimitEntity limitEntity); - Collection listLimits(String vlmId, Version version, String epLkgId); + Collection listLimits(String vlmId, Version version, String epLkgId); - void deleteLimit(LimitEntity limitEntity); + void deleteLimit(LimitEntity limitEntity); - void updateLimit(LimitEntity limitEntity); + void updateLimit(LimitEntity limitEntity); - LimitEntity getLimit(LimitEntity entitlementPool); + LimitEntity getLimit(LimitEntity entitlementPool); } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManagerFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManagerFactory.java index 68ea919d22..fa05c03b5e 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManagerFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/VendorLicenseManagerFactory.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.vendorlicense; import org.openecomp.core.factory.api.AbstractComponentFactory; @@ -26,10 +25,9 @@ import org.openecomp.core.factory.api.AbstractFactory; /** * Created by ayalaben on 8/3/2017 */ -public abstract class VendorLicenseManagerFactory extends - AbstractComponentFactory { +public abstract class VendorLicenseManagerFactory extends AbstractComponentFactory { - public static VendorLicenseManagerFactory getInstance() { - return AbstractFactory.getInstance(VendorLicenseManagerFactory.class); - } + public static VendorLicenseManagerFactory getInstance() { + return AbstractFactory.getInstance(VendorLicenseManagerFactory.class); + } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/errors/LimitErrorBuilder.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/errors/LimitErrorBuilder.java index b2a155dc21..3c0463596a 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/errors/LimitErrorBuilder.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/errors/LimitErrorBuilder.java @@ -13,38 +13,32 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.vendorlicense.errors; - import org.openecomp.sdc.common.errors.ErrorCategory; import org.openecomp.sdc.common.errors.ErrorCode; public class LimitErrorBuilder { - private static final String LIMIT_INVALID_ATTR_VALUE_MSG = "The %s value doesn't meet the " - + "expected attribute value."; - - private static final String DUPLICATE_LIMIT_NAME_NOT_ALLOWED_MSG = - "Invalid request, Limit with name %s already exists for type %s."; - - private LimitErrorBuilder(){ - - } - - public static ErrorCode getInvalidValueErrorBuilder(String attribute, String errorCode) { - ErrorCode.ErrorCodeBuilder builder = new ErrorCode.ErrorCodeBuilder(); - builder.withId(errorCode); - builder.withCategory(ErrorCategory.APPLICATION); - builder.withMessage(String.format(LIMIT_INVALID_ATTR_VALUE_MSG, attribute)); - return builder.build(); - } - - public static ErrorCode getDuplicateNameErrorbuilder(String name, String type) { - ErrorCode.ErrorCodeBuilder builder = new ErrorCode.ErrorCodeBuilder(); - builder.withId(VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED); - builder.withCategory(ErrorCategory.APPLICATION); - builder.withMessage(String.format (DUPLICATE_LIMIT_NAME_NOT_ALLOWED_MSG, name, type )); - return builder.build(); - } + private static final String LIMIT_INVALID_ATTR_VALUE_MSG = "The %s value doesn't meet the " + "expected attribute value."; + private static final String DUPLICATE_LIMIT_NAME_NOT_ALLOWED_MSG = "Invalid request, Limit with name %s already exists for type %s."; + + private LimitErrorBuilder() { + } + + public static ErrorCode getInvalidValueErrorBuilder(String attribute, String errorCode) { + ErrorCode.ErrorCodeBuilder builder = new ErrorCode.ErrorCodeBuilder(); + builder.withId(errorCode); + builder.withCategory(ErrorCategory.APPLICATION); + builder.withMessage(String.format(LIMIT_INVALID_ATTR_VALUE_MSG, attribute)); + return builder.build(); + } + + public static ErrorCode getDuplicateNameErrorbuilder(String name, String type) { + ErrorCode.ErrorCodeBuilder builder = new ErrorCode.ErrorCodeBuilder(); + builder.withId(VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED); + builder.withCategory(ErrorCategory.APPLICATION); + builder.withMessage(String.format(DUPLICATE_LIMIT_NAME_NOT_ALLOWED_MSG, name, type)); + return builder.build(); + } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerFactoryImpl.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerFactoryImpl.java index ac7eb115b1..16a789bd8d 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerFactoryImpl.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerFactoryImpl.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,33 +17,32 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.vendorlicense.impl; import org.openecomp.core.dao.UniqueValueDaoFactory; import org.openecomp.sdc.vendorlicense.VendorLicenseManager; import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory; -import org.openecomp.sdc.vendorlicense.dao.*; +import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory; +import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory; +import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory; +import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory; +import org.openecomp.sdc.vendorlicense.dao.LimitDaoFactory; +import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory; import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory; /** * Created by ayalaben on 8/3/2017 */ public class VendorLicenseManagerFactoryImpl extends VendorLicenseManagerFactory { - private static final VendorLicenseManager INSTANCE = - new VendorLicenseManagerImpl( - VendorLicenseFacadeFactory.getInstance().createInterface(), - VendorLicenseModelDaoFactory.getInstance().createInterface(), - LicenseAgreementDaoFactory.getInstance().createInterface(), - FeatureGroupDaoFactory.getInstance().createInterface(), - EntitlementPoolDaoFactory.getInstance().createInterface(), - LicenseKeyGroupDaoFactory.getInstance().createInterface(), - LimitDaoFactory.getInstance().createInterface(), - UniqueValueDaoFactory.getInstance().createInterface()); - @Override - public VendorLicenseManager createInterface() { - return INSTANCE; - } + private static final VendorLicenseManager INSTANCE = new VendorLicenseManagerImpl(VendorLicenseFacadeFactory.getInstance().createInterface(), + VendorLicenseModelDaoFactory.getInstance().createInterface(), LicenseAgreementDaoFactory.getInstance().createInterface(), + FeatureGroupDaoFactory.getInstance().createInterface(), EntitlementPoolDaoFactory.getInstance().createInterface(), + LicenseKeyGroupDaoFactory.getInstance().createInterface(), LimitDaoFactory.getInstance().createInterface(), + UniqueValueDaoFactory.getInstance().createInterface()); + @Override + public VendorLicenseManager createInterface() { + return INSTANCE; + } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerImpl.java b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerImpl.java index 6939eeed32..0fb5f58507 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerImpl.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-license-manager/src/main/java/org/openecomp/sdc/vendorlicense/impl/VendorLicenseManagerImpl.java @@ -13,9 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.vendorlicense.impl; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Collection; +import java.util.Optional; +import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.openecomp.core.dao.UniqueValueDao; import org.openecomp.core.util.UniqueValueUtil; @@ -45,596 +49,471 @@ import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade; import org.openecomp.sdc.versioning.VersioningUtil; import org.openecomp.sdc.versioning.dao.types.Version; -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; -import java.util.Collection; -import java.util.Optional; -import java.util.Set; - public class VendorLicenseManagerImpl implements VendorLicenseManager { - private UniqueValueUtil uniqueValueUtil; - private VendorLicenseFacade vendorLicenseFacade; - private VendorLicenseModelDao vendorLicenseModelDao; - private LicenseAgreementDao licenseAgreementDao; - private FeatureGroupDao featureGroupDao; - private EntitlementPoolDao entitlementPoolDao; - private LicenseKeyGroupDao licenseKeyGroupDao; - private LimitDao limitDao; - - private static final String EP_POOL_START_TIME = "T00:00:00Z"; - private static final String EP_POOL_EXPIRY_TIME = "T23:59:59Z"; - private static final DateTimeFormatter FORMATTER - = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'"); - public VendorLicenseManagerImpl(VendorLicenseFacade vendorLicenseFacade, - VendorLicenseModelDao vendorLicenseModelDao, - LicenseAgreementDao licenseAgreementDao, - FeatureGroupDao featureGroupDao, - EntitlementPoolDao entitlementPoolDao, - LicenseKeyGroupDao licenseKeyGroupDao, - LimitDao limitDao, - UniqueValueDao uniqueValueDao) { - this.vendorLicenseFacade = vendorLicenseFacade; - this.vendorLicenseModelDao = vendorLicenseModelDao; - this.licenseAgreementDao = licenseAgreementDao; - this.featureGroupDao = featureGroupDao; - this.entitlementPoolDao = entitlementPoolDao; - this.licenseKeyGroupDao = licenseKeyGroupDao; - this.limitDao = limitDao; - this.uniqueValueUtil = new UniqueValueUtil(uniqueValueDao); - } - - - @Override - public void validate(String vendorLicenseModelId, Version version) { - vendorLicenseFacade.validate(vendorLicenseModelId, version); - } - - @Override - public VendorLicenseModelEntity createVendorLicenseModel( - VendorLicenseModelEntity vendorLicenseModelEntity) { - vendorLicenseModelDao.create(vendorLicenseModelEntity); - return vendorLicenseModelEntity; - } - - @Override - public void updateVendorLicenseModel(VendorLicenseModelEntity vendorLicenseModelEntity) { - VendorLicenseModelEntity retrieved = vendorLicenseModelDao.get(vendorLicenseModelEntity); - if (retrieved == null){ - throw new CoreException((new ErrorCode.ErrorCodeBuilder() - .withMessage(String.format("Vlm with id %s and version %s does not exist.", - vendorLicenseModelEntity.getId(), vendorLicenseModelEntity.getVersion().getId()))).build()); - } - - String existingVendorName = retrieved.getVendorName(); - - updateUniqueName(VendorLicenseConstants.UniqueValues.VENDOR_NAME, existingVendorName, - vendorLicenseModelEntity.getVendorName()); - vendorLicenseModelDao.update(vendorLicenseModelEntity); - } - - @Override - public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) { - return vendorLicenseFacade.getVendorLicenseModel(vlmId, version); - } - - @Override - public void deleteVendorLicenseModel(String vlmId, Version version) { - throw new UnsupportedOperationException(VendorLicenseConstants.UNSUPPORTED_OPERATION_ERROR); - } - - @Override - public Collection listLicenseAgreements(String vlmId, Version version) { - return licenseAgreementDao.list(new LicenseAgreementEntity(vlmId, version, null)); - } - - @Override - public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) { - return vendorLicenseFacade.createLicenseAgreement(licenseAgreement); - } - - @Override - public void updateLicenseAgreement(LicenseAgreementEntity licenseAgreement, - Set addedFeatureGroupIds, - Set removedFeatureGroupIds) { - LicenseAgreementEntity retrieved = licenseAgreementDao.get(licenseAgreement); - VersioningUtil - .validateEntityExistence(retrieved, licenseAgreement, VendorLicenseModelEntity.ENTITY_TYPE); - VersioningUtil.validateContainedEntitiesExistence(new FeatureGroupEntity().getEntityType(), - removedFeatureGroupIds, retrieved, retrieved.getFeatureGroupIds()); - VersioningUtil.validateEntitiesExistence(addedFeatureGroupIds, - new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), - licenseAgreement.getVersion(), - null), - featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE); - - updateUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME, - retrieved.getName(), licenseAgreement.getName(), licenseAgreement.getVendorLicenseModelId(), - licenseAgreement.getVersion().getId()); - licenseAgreementDao.updateColumnsAndDeltaFeatureGroupIds(licenseAgreement, addedFeatureGroupIds, - removedFeatureGroupIds); - - addFeatureGroupsToLicenseAgreementRef(addedFeatureGroupIds, licenseAgreement); - removeFeatureGroupsToLicenseAgreementRef(removedFeatureGroupIds, licenseAgreement); - } - - @Override - public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version, - String licenseAgreementId) { - return vendorLicenseFacade.getLicenseAgreementModel(vlmId, version, licenseAgreementId); - } - - @Override - public void deleteLicenseAgreement(String vlmId, Version version, String licenseAgreementId) { - LicenseAgreementEntity input = - new LicenseAgreementEntity(vlmId, version, licenseAgreementId); - LicenseAgreementEntity retrieved = licenseAgreementDao.get(input); - VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE); - - removeFeatureGroupsToLicenseAgreementRef(retrieved.getFeatureGroupIds(), retrieved); - - licenseAgreementDao.delete(retrieved); - - deleteUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME, - retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(), - retrieved.getName()); - } - - @Override - public Collection listFeatureGroups(String vlmId, Version version) { - return vendorLicenseFacade.listFeatureGroups(vlmId, version); - } - - @Override - public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) { - return vendorLicenseFacade.createFeatureGroup(featureGroup); - } - - @Override - public void updateFeatureGroup(FeatureGroupEntity featureGroup, - Set addedLicenseKeyGroups, - Set removedLicenseKeyGroups, - Set addedEntitlementPools, - Set removedEntitlementPools) { - FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup); - VersioningUtil - .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE); - - VersioningUtil.validateContainedEntitiesExistence(new LicenseKeyGroupEntity().getEntityType(), - removedLicenseKeyGroups, retrieved, retrieved.getLicenseKeyGroupIds()); - VersioningUtil.validateContainedEntitiesExistence(new EntitlementPoolEntity().getEntityType(), - removedEntitlementPools, retrieved, retrieved.getEntitlementPoolIds()); - - VersioningUtil.validateEntitiesExistence(addedLicenseKeyGroups, - new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(), - null), - licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE); - VersioningUtil.validateEntitiesExistence(addedEntitlementPools, - new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(), - null), - entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE); - - updateUniqueName(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME, - retrieved.getName(), featureGroup.getName(), featureGroup.getVendorLicenseModelId(), - featureGroup.getVersion().getId()); - - addLicenseKeyGroupsToFeatureGroupsRef(addedLicenseKeyGroups, featureGroup); - removeLicenseKeyGroupsToFeatureGroupsRef(removedLicenseKeyGroups, featureGroup); - addEntitlementPoolsToFeatureGroupsRef(addedEntitlementPools, featureGroup); - removeEntitlementPoolsToFeatureGroupsRef(removedEntitlementPools, featureGroup); - - featureGroupDao.updateFeatureGroup(featureGroup, addedEntitlementPools, removedEntitlementPools, - addedLicenseKeyGroups, removedLicenseKeyGroups); - - } - - @Override - public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) { - return vendorLicenseFacade.getFeatureGroupModel(featureGroup); - } - - @Override - public void deleteFeatureGroup(FeatureGroupEntity featureGroup) { - FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup); - VersioningUtil - .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE); - - removeLicenseKeyGroupsToFeatureGroupsRef(retrieved.getLicenseKeyGroupIds(), featureGroup); - removeEntitlementPoolsToFeatureGroupsRef(retrieved.getEntitlementPoolIds(), featureGroup); - - for (String licenceAgreementId : retrieved.getReferencingLicenseAgreements()) { - licenseAgreementDao.removeFeatureGroup( - new LicenseAgreementEntity(featureGroup.getVendorLicenseModelId(), - featureGroup.getVersion(), - licenceAgreementId), featureGroup.getId()); - } - - featureGroupDao.delete(featureGroup); - - deleteUniqueName(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME, - retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(), - retrieved.getName()); - } - - @Override - public Collection listEntitlementPools(String vlmId, Version version) { - return vendorLicenseFacade.listEntitlementPools(vlmId, version); - } - - @Override - public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) { - entitlementPool.setStartDate(getDate(entitlementPool.getStartDate(), EP_POOL_START_TIME)); - entitlementPool.setExpiryDate(getDate(entitlementPool.getExpiryDate(), EP_POOL_EXPIRY_TIME)); - validateCreateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(), - entitlementPool.getVendorLicenseModelId()); - return vendorLicenseFacade.createEntitlementPool(entitlementPool); - } - - private String getDate(String date, String poolTime){ - return date != null ? (!date.trim().isEmpty() ? date + poolTime: null) : null; - } - - private void validateCreateDate(String startDate, String expiryDate, - String vendorLicenseModelId) { - //original logic allows both nulls - if(StringUtils.isEmpty(startDate) && StringUtils.isEmpty(expiryDate)){ - return; - } - - Optional parsedStartDate = parseLocalDate(startDate); - Optional parsedExpiryDate = parseLocalDate(expiryDate); - if (!parsedStartDate.isPresent()) { - throw new CoreException( - new InvalidDateErrorBuilder(vendorLicenseModelId) - .build()); - } - - if (!parsedExpiryDate.isPresent() - && parsedStartDate.get().atStartOfDay().isBefore - (LocalDate.now().atStartOfDay())) { - throw new CoreException( - new InvalidDateErrorBuilder(vendorLicenseModelId) - .build()); - } - - if(parsedExpiryDate.isPresent() && isNotValidatStartAndExpiryDate(parsedStartDate.get(), parsedExpiryDate.get())){ - throw new CoreException( - new InvalidDateErrorBuilder(vendorLicenseModelId) - .build()); - } - } - - private boolean isNotValidatStartAndExpiryDate(LocalDate parsedStartDate, - LocalDate parsedExpiryDate) { - return parsedStartDate.atStartOfDay().isBefore(LocalDate.now().atStartOfDay()) - || parsedExpiryDate.atStartOfDay().isEqual(parsedStartDate.atStartOfDay()) - || parsedExpiryDate.isBefore(parsedStartDate); - } - - private static Optional parseLocalDate(String date) { - if (StringUtils.isEmpty(date)) { - return Optional.empty(); - } - - return Optional.of(LocalDate.parse(date, FORMATTER )); - } - - private void validateUpdateDate(String startDate, String expiryDate, - String vendorLicenseModelId) { - Optional parsedStartDate = parseLocalDate(startDate); - Optional parsedExpiryDate = parseLocalDate(expiryDate); - - if (parsedStartDate.isPresent() && parsedExpiryDate.isPresent() - && (parsedExpiryDate.get().atStartOfDay() - .isEqual(parsedStartDate.get().atStartOfDay()) - || parsedExpiryDate.get().isBefore(parsedStartDate.get() ))) { - throw new CoreException( - new InvalidDateErrorBuilder(vendorLicenseModelId) - .build()); - } - - if (startDate == null && expiryDate != null) { - throw new CoreException( - new InvalidDateErrorBuilder(vendorLicenseModelId) - .build()); - } - } - - @Override - public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) { - entitlementPool.setStartDate(getDate(entitlementPool.getStartDate(), EP_POOL_START_TIME)); - entitlementPool.setExpiryDate(getDate(entitlementPool.getExpiryDate(), EP_POOL_EXPIRY_TIME)); - validateUpdateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(), - entitlementPool.getVendorLicenseModelId()); - vendorLicenseFacade.updateEntitlementPool(entitlementPool); - } - - @Override - public EntitlementPoolEntity getEntitlementPool(EntitlementPoolEntity entitlementPool) { - EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool); - VersioningUtil - .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE); - DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); - if (retrieved.getStartDate() != null) { - retrieved.setStartDate(LocalDate.parse(retrieved.getStartDate(), FORMATTER ).format - (targetFormatter)); - } - - if (retrieved.getExpiryDate() != null) { - retrieved.setExpiryDate(LocalDate.parse(retrieved.getExpiryDate(), FORMATTER ).format - (targetFormatter)); - } - return retrieved; - } - - @Override - public void deleteEntitlementPool(EntitlementPoolEntity entitlementPool) { - EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool); - VersioningUtil - .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE); - - for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) { - featureGroupDao.removeEntitlementPool( - new FeatureGroupEntity(entitlementPool.getVendorLicenseModelId(), - entitlementPool.getVersion(), - referencingFeatureGroupId), entitlementPool.getId()); - } - - deleteChildLimits(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), - entitlementPool.getId()); - - entitlementPoolDao.delete(entitlementPool); - - deleteUniqueName(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME, - retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(), - retrieved.getName()); - } - - protected void deleteChildLimits(String vlmId, Version version, String epLkgId) { - Optional> limitEntities = Optional.ofNullable( - listLimits(vlmId, version, epLkgId)); - limitEntities.ifPresent(entities -> entities.forEach(this::deleteLimit)); - } - - @Override - public Collection listLicenseKeyGroups(String vlmId, Version version) { - return vendorLicenseFacade.listLicenseKeyGroups(vlmId, version); - } - - @Override - public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) { - licenseKeyGroup.setStartDate(licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup - .getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + EP_POOL_START_TIME - : null) : null); - licenseKeyGroup.setExpiryDate(licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup - .getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + EP_POOL_EXPIRY_TIME - : null) : null); - - validateCreateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(), - licenseKeyGroup.getVendorLicenseModelId()); - return vendorLicenseFacade.createLicenseKeyGroup(licenseKeyGroup); - } - - @Override - public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) { - licenseKeyGroup.setStartDate(licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup - .getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + EP_POOL_START_TIME - : null) : null); - licenseKeyGroup.setExpiryDate(licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup - .getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + EP_POOL_EXPIRY_TIME - : null) : null); - - validateUpdateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(), - licenseKeyGroup.getVendorLicenseModelId()); - vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroup); - } - - @Override - public LicenseKeyGroupEntity getLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) { - LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup); - VersioningUtil - .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE); - return retrieved; - } - - @Override - public void deleteLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) { - LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup); - VersioningUtil - .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE); - - for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) { - featureGroupDao.removeLicenseKeyGroup( - new FeatureGroupEntity(licenseKeyGroup.getVendorLicenseModelId(), - licenseKeyGroup.getVersion(), - referencingFeatureGroupId), licenseKeyGroup.getId()); - } - - deleteChildLimits(licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), - licenseKeyGroup.getId()); - - licenseKeyGroupDao.delete(licenseKeyGroup); - - deleteUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME, - retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(), - retrieved.getName()); - } - - @Override - public LimitEntity createLimit(LimitEntity limit) { - validateLimit(limit); - LimitEntity createdLimit = vendorLicenseFacade.createLimit(limit); - updateParentForLimit(limit); - return createdLimit; - } - - private void validateLimit(LimitEntity limit) { - Collection limitList = - listLimits(limit.getVendorLicenseModelId(), limit.getVersion() - , limit.getEpLkgId()); - - if (!isLimitNameUnique(limitList, limit.getName(), limit.getType(), limit.getId())) { - final ErrorCode duplicateLimitNameErrorBuilder = - LimitErrorBuilder.getDuplicateNameErrorbuilder(limit.getName(), limit.getType().name()); - throw new CoreException(duplicateLimitNameErrorBuilder); - } - } - - private boolean isLimitNameUnique(Collection limitList, String name, LimitType - type, String id) { - for (LimitEntity limit : limitList) { - if (limit.getName().equalsIgnoreCase(name) && - limit.getType().name().equalsIgnoreCase(type.name())) { - if (id != null && limit.getId().equals(id)) { - continue; + + private static final String EP_POOL_START_TIME = "T00:00:00Z"; + private static final String EP_POOL_EXPIRY_TIME = "T23:59:59Z"; + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'"); + private UniqueValueUtil uniqueValueUtil; + private VendorLicenseFacade vendorLicenseFacade; + private VendorLicenseModelDao vendorLicenseModelDao; + private LicenseAgreementDao licenseAgreementDao; + private FeatureGroupDao featureGroupDao; + private EntitlementPoolDao entitlementPoolDao; + private LicenseKeyGroupDao licenseKeyGroupDao; + private LimitDao limitDao; + + public VendorLicenseManagerImpl(VendorLicenseFacade vendorLicenseFacade, VendorLicenseModelDao vendorLicenseModelDao, + LicenseAgreementDao licenseAgreementDao, FeatureGroupDao featureGroupDao, EntitlementPoolDao entitlementPoolDao, + LicenseKeyGroupDao licenseKeyGroupDao, LimitDao limitDao, UniqueValueDao uniqueValueDao) { + this.vendorLicenseFacade = vendorLicenseFacade; + this.vendorLicenseModelDao = vendorLicenseModelDao; + this.licenseAgreementDao = licenseAgreementDao; + this.featureGroupDao = featureGroupDao; + this.entitlementPoolDao = entitlementPoolDao; + this.licenseKeyGroupDao = licenseKeyGroupDao; + this.limitDao = limitDao; + this.uniqueValueUtil = new UniqueValueUtil(uniqueValueDao); + } + + private static Optional parseLocalDate(String date) { + if (StringUtils.isEmpty(date)) { + return Optional.empty(); + } + return Optional.of(LocalDate.parse(date, FORMATTER)); + } + + @Override + public void validate(String vendorLicenseModelId, Version version) { + vendorLicenseFacade.validate(vendorLicenseModelId, version); + } + + @Override + public VendorLicenseModelEntity createVendorLicenseModel(VendorLicenseModelEntity vendorLicenseModelEntity) { + vendorLicenseModelDao.create(vendorLicenseModelEntity); + return vendorLicenseModelEntity; + } + + @Override + public void updateVendorLicenseModel(VendorLicenseModelEntity vendorLicenseModelEntity) { + VendorLicenseModelEntity retrieved = vendorLicenseModelDao.get(vendorLicenseModelEntity); + if (retrieved == null) { + throw new CoreException((new ErrorCode.ErrorCodeBuilder().withMessage(String + .format("Vlm with id %s and version %s does not exist.", vendorLicenseModelEntity.getId(), + vendorLicenseModelEntity.getVersion().getId()))).build()); + } + String existingVendorName = retrieved.getVendorName(); + updateUniqueName(VendorLicenseConstants.UniqueValues.VENDOR_NAME, existingVendorName, vendorLicenseModelEntity.getVendorName()); + vendorLicenseModelDao.update(vendorLicenseModelEntity); + } + + @Override + public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) { + return vendorLicenseFacade.getVendorLicenseModel(vlmId, version); + } + + @Override + public void deleteVendorLicenseModel(String vlmId, Version version) { + throw new UnsupportedOperationException(VendorLicenseConstants.UNSUPPORTED_OPERATION_ERROR); + } + + @Override + public Collection listLicenseAgreements(String vlmId, Version version) { + return licenseAgreementDao.list(new LicenseAgreementEntity(vlmId, version, null)); + } + + @Override + public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) { + return vendorLicenseFacade.createLicenseAgreement(licenseAgreement); + } + + @Override + public void updateLicenseAgreement(LicenseAgreementEntity licenseAgreement, Set addedFeatureGroupIds, + Set removedFeatureGroupIds) { + LicenseAgreementEntity retrieved = licenseAgreementDao.get(licenseAgreement); + VersioningUtil.validateEntityExistence(retrieved, licenseAgreement, VendorLicenseModelEntity.ENTITY_TYPE); + VersioningUtil.validateContainedEntitiesExistence(new FeatureGroupEntity().getEntityType(), removedFeatureGroupIds, retrieved, + retrieved.getFeatureGroupIds()); + VersioningUtil.validateEntitiesExistence(addedFeatureGroupIds, + new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion(), null), featureGroupDao, + VendorLicenseModelEntity.ENTITY_TYPE); + updateUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME, retrieved.getName(), licenseAgreement.getName(), + licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId()); + licenseAgreementDao.updateColumnsAndDeltaFeatureGroupIds(licenseAgreement, addedFeatureGroupIds, removedFeatureGroupIds); + addFeatureGroupsToLicenseAgreementRef(addedFeatureGroupIds, licenseAgreement); + removeFeatureGroupsToLicenseAgreementRef(removedFeatureGroupIds, licenseAgreement); + } + + @Override + public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version, String licenseAgreementId) { + return vendorLicenseFacade.getLicenseAgreementModel(vlmId, version, licenseAgreementId); + } + + @Override + public void deleteLicenseAgreement(String vlmId, Version version, String licenseAgreementId) { + LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId); + LicenseAgreementEntity retrieved = licenseAgreementDao.get(input); + VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE); + removeFeatureGroupsToLicenseAgreementRef(retrieved.getFeatureGroupIds(), retrieved); + licenseAgreementDao.delete(retrieved); + deleteUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME, retrieved.getVendorLicenseModelId(), + retrieved.getVersion().toString(), retrieved.getName()); + } + + @Override + public Collection listFeatureGroups(String vlmId, Version version) { + return vendorLicenseFacade.listFeatureGroups(vlmId, version); + } + + @Override + public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) { + return vendorLicenseFacade.createFeatureGroup(featureGroup); + } + + @Override + public void updateFeatureGroup(FeatureGroupEntity featureGroup, Set addedLicenseKeyGroups, Set removedLicenseKeyGroups, + Set addedEntitlementPools, Set removedEntitlementPools) { + FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup); + VersioningUtil.validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE); + VersioningUtil.validateContainedEntitiesExistence(new LicenseKeyGroupEntity().getEntityType(), removedLicenseKeyGroups, retrieved, + retrieved.getLicenseKeyGroupIds()); + VersioningUtil.validateContainedEntitiesExistence(new EntitlementPoolEntity().getEntityType(), removedEntitlementPools, retrieved, + retrieved.getEntitlementPoolIds()); + VersioningUtil.validateEntitiesExistence(addedLicenseKeyGroups, + new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(), null), licenseKeyGroupDao, + VendorLicenseModelEntity.ENTITY_TYPE); + VersioningUtil.validateEntitiesExistence(addedEntitlementPools, + new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(), null), entitlementPoolDao, + VendorLicenseModelEntity.ENTITY_TYPE); + updateUniqueName(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME, retrieved.getName(), featureGroup.getName(), + featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId()); + addLicenseKeyGroupsToFeatureGroupsRef(addedLicenseKeyGroups, featureGroup); + removeLicenseKeyGroupsToFeatureGroupsRef(removedLicenseKeyGroups, featureGroup); + addEntitlementPoolsToFeatureGroupsRef(addedEntitlementPools, featureGroup); + removeEntitlementPoolsToFeatureGroupsRef(removedEntitlementPools, featureGroup); + featureGroupDao + .updateFeatureGroup(featureGroup, addedEntitlementPools, removedEntitlementPools, addedLicenseKeyGroups, removedLicenseKeyGroups); + } + + @Override + public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) { + return vendorLicenseFacade.getFeatureGroupModel(featureGroup); + } + + @Override + public void deleteFeatureGroup(FeatureGroupEntity featureGroup) { + FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup); + VersioningUtil.validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE); + removeLicenseKeyGroupsToFeatureGroupsRef(retrieved.getLicenseKeyGroupIds(), featureGroup); + removeEntitlementPoolsToFeatureGroupsRef(retrieved.getEntitlementPoolIds(), featureGroup); + for (String licenceAgreementId : retrieved.getReferencingLicenseAgreements()) { + licenseAgreementDao + .removeFeatureGroup(new LicenseAgreementEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(), licenceAgreementId), + featureGroup.getId()); + } + featureGroupDao.delete(featureGroup); + deleteUniqueName(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME, retrieved.getVendorLicenseModelId(), + retrieved.getVersion().toString(), retrieved.getName()); + } + + @Override + public Collection listEntitlementPools(String vlmId, Version version) { + return vendorLicenseFacade.listEntitlementPools(vlmId, version); + } + + @Override + public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) { + entitlementPool.setStartDate(getDate(entitlementPool.getStartDate(), EP_POOL_START_TIME)); + entitlementPool.setExpiryDate(getDate(entitlementPool.getExpiryDate(), EP_POOL_EXPIRY_TIME)); + validateCreateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(), entitlementPool.getVendorLicenseModelId()); + return vendorLicenseFacade.createEntitlementPool(entitlementPool); + } + + private String getDate(String date, String poolTime) { + return date != null ? (!date.trim().isEmpty() ? date + poolTime : null) : null; + } + + private void validateCreateDate(String startDate, String expiryDate, String vendorLicenseModelId) { + //original logic allows both nulls + if (StringUtils.isEmpty(startDate) && StringUtils.isEmpty(expiryDate)) { + return; } - return false; - } - } - return true; - } - - @Override - public Collection listLimits(String vlmId, Version version, String epLkgId) { - return vendorLicenseFacade.listLimits(vlmId, version, epLkgId); - } - - @Override - public void deleteLimit(LimitEntity limitEntity) { - if (!isLimitPresent(limitEntity)) { - VersioningUtil - .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); - } - LimitEntity retrieved = limitDao.get(limitEntity); - VersioningUtil - .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); - - limitDao.delete(limitEntity); - - updateParentForLimit(limitEntity); - } - - @Override - public void updateLimit(LimitEntity limit) { - getLimit(limit); - validateLimit(limit); - LimitEntity retrieved = limitDao.get(limit); - if(!retrieved.equals(limit)){ - vendorLicenseFacade.updateLimit(limit); - updateParentForLimit(limit); - } - } - - private boolean isLimitPresent(LimitEntity limit) { - return limitDao.isLimitPresent(limit); - } - - @Override - public LimitEntity getLimit(LimitEntity limitEntity) { - if (!isLimitPresent(limitEntity)) { - VersioningUtil - .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); - } - LimitEntity retrieved = limitDao.get(limitEntity); - VersioningUtil - .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); - return retrieved; - } - - /** - * update Parent of limit (EP/LKG) versionuuid when limit is modified so that limit updates are - * captured in VLM XML - */ - private void updateParentForLimit(LimitEntity limit) { - if ("EntitlementPool".equals(limit.getParent())) { - EntitlementPoolEntity entitlementPoolEntity = - entitlementPoolDao.get(new EntitlementPoolEntity(limit.getVendorLicenseModelId(), - limit.getVersion(), limit.getEpLkgId())); - entitlementPoolEntity.setVersionUuId(CommonMethods.nextUuId()); - entitlementPoolDao.update(entitlementPoolEntity); - } - - if ("LicenseKeyGroup".equals(limit.getParent())) { - LicenseKeyGroupEntity licenseKeyGroupEntity = licenseKeyGroupDao.get( - new LicenseKeyGroupEntity(limit.getVendorLicenseModelId(), limit.getVersion(), - limit.getEpLkgId())); - licenseKeyGroupEntity.setVersionUuId(CommonMethods.nextUuId()); - licenseKeyGroupDao.update(licenseKeyGroupEntity); - } - } - - protected void addFeatureGroupsToLicenseAgreementRef(Set featureGroupIds, - LicenseAgreementEntity licenseAgreement) { - if (featureGroupIds != null) { - for (String featureGroupId : featureGroupIds) { - featureGroupDao.addReferencingLicenseAgreement( - new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), - licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId()); - } - } - } - - protected void removeFeatureGroupsToLicenseAgreementRef(Set featureGroupIds, - LicenseAgreementEntity licenseAgreement) { - if (featureGroupIds != null) { - for (String featureGroupId : featureGroupIds) { - featureGroupDao.removeReferencingLicenseAgreement( - new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), - licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId()); - } - } - } - - protected void addLicenseKeyGroupsToFeatureGroupsRef(Set licenseKeyGroupIds, - FeatureGroupEntity featureGroup) { - if (licenseKeyGroupIds != null) { - for (String licenseKeyGroupId : licenseKeyGroupIds) { - licenseKeyGroupDao.addReferencingFeatureGroup( - new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), - featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId()); - } - } - } - - protected void removeLicenseKeyGroupsToFeatureGroupsRef(Set licenseKeyGroupIds, - FeatureGroupEntity featureGroup) { - if (licenseKeyGroupIds != null) { - for (String licenseKeyGroupId : licenseKeyGroupIds) { - licenseKeyGroupDao.removeReferencingFeatureGroup( - new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), - featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId()); - } - } - } - - protected void addEntitlementPoolsToFeatureGroupsRef(Set entitlementPoolIds, - FeatureGroupEntity featureGroup) { - if (entitlementPoolIds != null) { - for (String entitlementPoolId : entitlementPoolIds) { - entitlementPoolDao.addReferencingFeatureGroup( - new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), - featureGroup.getVersion(), entitlementPoolId), featureGroup.getId()); - } - } - } - - protected void removeEntitlementPoolsToFeatureGroupsRef(Set entitlementPoolIds, - FeatureGroupEntity featureGroup) { - if (entitlementPoolIds != null) { - for (String entitlementPoolId : entitlementPoolIds) { - entitlementPoolDao.removeReferencingFeatureGroup( - new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), - featureGroup.getVersion(), entitlementPoolId), featureGroup.getId()); - } - } - } - - protected void updateUniqueName(String uniqueValueType, String oldName, String newName, String... - context) { - uniqueValueUtil - .updateUniqueValue(uniqueValueType, oldName, newName, context); - } - - protected void deleteUniqueName(String uniqueValueType, String... uniqueCombination) { - uniqueValueUtil.deleteUniqueValue(uniqueValueType, uniqueCombination); - } + Optional parsedStartDate = parseLocalDate(startDate); + Optional parsedExpiryDate = parseLocalDate(expiryDate); + if (!parsedStartDate.isPresent()) { + throw new CoreException(new InvalidDateErrorBuilder(vendorLicenseModelId).build()); + } + if (!parsedExpiryDate.isPresent() && parsedStartDate.get().atStartOfDay().isBefore(LocalDate.now().atStartOfDay())) { + throw new CoreException(new InvalidDateErrorBuilder(vendorLicenseModelId).build()); + } + if (parsedExpiryDate.isPresent() && isNotValidatStartAndExpiryDate(parsedStartDate.get(), parsedExpiryDate.get())) { + throw new CoreException(new InvalidDateErrorBuilder(vendorLicenseModelId).build()); + } + } + + private boolean isNotValidatStartAndExpiryDate(LocalDate parsedStartDate, LocalDate parsedExpiryDate) { + return parsedStartDate.atStartOfDay().isBefore(LocalDate.now().atStartOfDay()) || parsedExpiryDate.atStartOfDay() + .isEqual(parsedStartDate.atStartOfDay()) || parsedExpiryDate.isBefore(parsedStartDate); + } + + private void validateUpdateDate(String startDate, String expiryDate, String vendorLicenseModelId) { + Optional parsedStartDate = parseLocalDate(startDate); + Optional parsedExpiryDate = parseLocalDate(expiryDate); + if (parsedStartDate.isPresent() && parsedExpiryDate.isPresent() && ( + parsedExpiryDate.get().atStartOfDay().isEqual(parsedStartDate.get().atStartOfDay()) || parsedExpiryDate.get() + .isBefore(parsedStartDate.get()))) { + throw new CoreException(new InvalidDateErrorBuilder(vendorLicenseModelId).build()); + } + if (startDate == null && expiryDate != null) { + throw new CoreException(new InvalidDateErrorBuilder(vendorLicenseModelId).build()); + } + } + + @Override + public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) { + entitlementPool.setStartDate(getDate(entitlementPool.getStartDate(), EP_POOL_START_TIME)); + entitlementPool.setExpiryDate(getDate(entitlementPool.getExpiryDate(), EP_POOL_EXPIRY_TIME)); + validateUpdateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(), entitlementPool.getVendorLicenseModelId()); + vendorLicenseFacade.updateEntitlementPool(entitlementPool); + } + + @Override + public EntitlementPoolEntity getEntitlementPool(EntitlementPoolEntity entitlementPool) { + EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool); + VersioningUtil.validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE); + DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); + if (retrieved.getStartDate() != null) { + retrieved.setStartDate(LocalDate.parse(retrieved.getStartDate(), FORMATTER).format(targetFormatter)); + } + if (retrieved.getExpiryDate() != null) { + retrieved.setExpiryDate(LocalDate.parse(retrieved.getExpiryDate(), FORMATTER).format(targetFormatter)); + } + return retrieved; + } + + @Override + public void deleteEntitlementPool(EntitlementPoolEntity entitlementPool) { + EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool); + VersioningUtil.validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE); + for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) { + featureGroupDao.removeEntitlementPool( + new FeatureGroupEntity(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), referencingFeatureGroupId), + entitlementPool.getId()); + } + deleteChildLimits(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), entitlementPool.getId()); + entitlementPoolDao.delete(entitlementPool); + deleteUniqueName(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME, retrieved.getVendorLicenseModelId(), + retrieved.getVersion().toString(), retrieved.getName()); + } + + protected void deleteChildLimits(String vlmId, Version version, String epLkgId) { + Optional> limitEntities = Optional.ofNullable(listLimits(vlmId, version, epLkgId)); + limitEntities.ifPresent(entities -> entities.forEach(this::deleteLimit)); + } + + @Override + public Collection listLicenseKeyGroups(String vlmId, Version version) { + return vendorLicenseFacade.listLicenseKeyGroups(vlmId, version); + } + + @Override + public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) { + licenseKeyGroup.setStartDate( + licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup.getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + + EP_POOL_START_TIME : null) : null); + licenseKeyGroup.setExpiryDate( + licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup.getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + + EP_POOL_EXPIRY_TIME : null) : null); + validateCreateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(), licenseKeyGroup.getVendorLicenseModelId()); + return vendorLicenseFacade.createLicenseKeyGroup(licenseKeyGroup); + } + + @Override + public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) { + licenseKeyGroup.setStartDate( + licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup.getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + + EP_POOL_START_TIME : null) : null); + licenseKeyGroup.setExpiryDate( + licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup.getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + + EP_POOL_EXPIRY_TIME : null) : null); + validateUpdateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(), licenseKeyGroup.getVendorLicenseModelId()); + vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroup); + } + + @Override + public LicenseKeyGroupEntity getLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) { + LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup); + VersioningUtil.validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE); + return retrieved; + } + + @Override + public void deleteLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) { + LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup); + VersioningUtil.validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE); + for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) { + featureGroupDao.removeLicenseKeyGroup( + new FeatureGroupEntity(licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), referencingFeatureGroupId), + licenseKeyGroup.getId()); + } + deleteChildLimits(licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), licenseKeyGroup.getId()); + licenseKeyGroupDao.delete(licenseKeyGroup); + deleteUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME, retrieved.getVendorLicenseModelId(), + retrieved.getVersion().toString(), retrieved.getName()); + } + + @Override + public LimitEntity createLimit(LimitEntity limit) { + validateLimit(limit); + LimitEntity createdLimit = vendorLicenseFacade.createLimit(limit); + updateParentForLimit(limit); + return createdLimit; + } + + private void validateLimit(LimitEntity limit) { + Collection limitList = listLimits(limit.getVendorLicenseModelId(), limit.getVersion(), limit.getEpLkgId()); + if (!isLimitNameUnique(limitList, limit.getName(), limit.getType(), limit.getId())) { + final ErrorCode duplicateLimitNameErrorBuilder = LimitErrorBuilder.getDuplicateNameErrorbuilder(limit.getName(), limit.getType().name()); + throw new CoreException(duplicateLimitNameErrorBuilder); + } + } + + private boolean isLimitNameUnique(Collection limitList, String name, LimitType type, String id) { + for (LimitEntity limit : limitList) { + if (limit.getName().equalsIgnoreCase(name) && limit.getType().name().equalsIgnoreCase(type.name())) { + if (id != null && limit.getId().equals(id)) { + continue; + } + return false; + } + } + return true; + } + + @Override + public Collection listLimits(String vlmId, Version version, String epLkgId) { + return vendorLicenseFacade.listLimits(vlmId, version, epLkgId); + } + + @Override + public void deleteLimit(LimitEntity limitEntity) { + if (!isLimitPresent(limitEntity)) { + VersioningUtil.validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); + } + LimitEntity retrieved = limitDao.get(limitEntity); + VersioningUtil.validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); + limitDao.delete(limitEntity); + updateParentForLimit(limitEntity); + } + + @Override + public void updateLimit(LimitEntity limit) { + getLimit(limit); + validateLimit(limit); + LimitEntity retrieved = limitDao.get(limit); + if (!retrieved.equals(limit)) { + vendorLicenseFacade.updateLimit(limit); + updateParentForLimit(limit); + } + } + + private boolean isLimitPresent(LimitEntity limit) { + return limitDao.isLimitPresent(limit); + } + + @Override + public LimitEntity getLimit(LimitEntity limitEntity) { + if (!isLimitPresent(limitEntity)) { + VersioningUtil.validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); + } + LimitEntity retrieved = limitDao.get(limitEntity); + VersioningUtil.validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE); + return retrieved; + } + + /** + * update Parent of limit (EP/LKG) versionuuid when limit is modified so that limit updates are captured in VLM XML + */ + private void updateParentForLimit(LimitEntity limit) { + if ("EntitlementPool".equals(limit.getParent())) { + EntitlementPoolEntity entitlementPoolEntity = entitlementPoolDao + .get(new EntitlementPoolEntity(limit.getVendorLicenseModelId(), limit.getVersion(), limit.getEpLkgId())); + entitlementPoolEntity.setVersionUuId(CommonMethods.nextUuId()); + entitlementPoolDao.update(entitlementPoolEntity); + } + if ("LicenseKeyGroup".equals(limit.getParent())) { + LicenseKeyGroupEntity licenseKeyGroupEntity = licenseKeyGroupDao + .get(new LicenseKeyGroupEntity(limit.getVendorLicenseModelId(), limit.getVersion(), limit.getEpLkgId())); + licenseKeyGroupEntity.setVersionUuId(CommonMethods.nextUuId()); + licenseKeyGroupDao.update(licenseKeyGroupEntity); + } + } + + protected void addFeatureGroupsToLicenseAgreementRef(Set featureGroupIds, LicenseAgreementEntity licenseAgreement) { + if (featureGroupIds != null) { + for (String featureGroupId : featureGroupIds) { + featureGroupDao.addReferencingLicenseAgreement( + new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion(), featureGroupId), + licenseAgreement.getId()); + } + } + } + + protected void removeFeatureGroupsToLicenseAgreementRef(Set featureGroupIds, LicenseAgreementEntity licenseAgreement) { + if (featureGroupIds != null) { + for (String featureGroupId : featureGroupIds) { + featureGroupDao.removeReferencingLicenseAgreement( + new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion(), featureGroupId), + licenseAgreement.getId()); + } + } + } + + protected void addLicenseKeyGroupsToFeatureGroupsRef(Set licenseKeyGroupIds, FeatureGroupEntity featureGroup) { + if (licenseKeyGroupIds != null) { + for (String licenseKeyGroupId : licenseKeyGroupIds) { + licenseKeyGroupDao.addReferencingFeatureGroup( + new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(), licenseKeyGroupId), + featureGroup.getId()); + } + } + } + + protected void removeLicenseKeyGroupsToFeatureGroupsRef(Set licenseKeyGroupIds, FeatureGroupEntity featureGroup) { + if (licenseKeyGroupIds != null) { + for (String licenseKeyGroupId : licenseKeyGroupIds) { + licenseKeyGroupDao.removeReferencingFeatureGroup( + new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(), licenseKeyGroupId), + featureGroup.getId()); + } + } + } + + protected void addEntitlementPoolsToFeatureGroupsRef(Set entitlementPoolIds, FeatureGroupEntity featureGroup) { + if (entitlementPoolIds != null) { + for (String entitlementPoolId : entitlementPoolIds) { + entitlementPoolDao.addReferencingFeatureGroup( + new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(), entitlementPoolId), + featureGroup.getId()); + } + } + } + + protected void removeEntitlementPoolsToFeatureGroupsRef(Set entitlementPoolIds, FeatureGroupEntity featureGroup) { + if (entitlementPoolIds != null) { + for (String entitlementPoolId : entitlementPoolIds) { + entitlementPoolDao.removeReferencingFeatureGroup( + new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(), entitlementPoolId), + featureGroup.getId()); + } + } + } + + protected void updateUniqueName(String uniqueValueType, String oldName, String newName, String... context) { + uniqueValueUtil.updateUniqueValue(uniqueValueType, oldName, newName, context); + } + + protected void deleteUniqueName(String uniqueValueType, String... uniqueCombination) { + uniqueValueUtil.deleteUniqueValue(uniqueValueType, uniqueCombination); + } } -- cgit 1.2.3-korg