diff options
Diffstat (limited to 'services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be')
9 files changed, 624 insertions, 0 deletions
diff --git a/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/ActivitySpecManager.java b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/ActivitySpecManager.java new file mode 100644 index 0000000000..6c1a4d0526 --- /dev/null +++ b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/ActivitySpecManager.java @@ -0,0 +1,36 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.activityspec.be; + +import java.util.Collection; + +import org.onap.sdc.activityspec.api.rest.types.ActivitySpecAction; +import org.onap.sdc.activityspec.be.dao.types.ActivitySpecEntity; +import org.openecomp.sdc.versioning.types.Item; + +public interface ActivitySpecManager { + + ActivitySpecEntity createActivitySpec(ActivitySpecEntity activitySpecEntity); + + ActivitySpecEntity get(ActivitySpecEntity activitySpec); + + void update(ActivitySpecEntity activitySpec); + + void actOnAction(String activitySpecId, String versionId, ActivitySpecAction action); + + Collection<Item> list(String versionStatus); +} diff --git a/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/dao/ActivitySpecDao.java b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/dao/ActivitySpecDao.java new file mode 100644 index 0000000000..c71f296f1a --- /dev/null +++ b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/dao/ActivitySpecDao.java @@ -0,0 +1,28 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.activityspec.be.dao; + +import org.onap.sdc.activityspec.be.dao.types.ActivitySpecEntity; + +public interface ActivitySpecDao { + + void create(ActivitySpecEntity activitySpecEntity); + + ActivitySpecEntity get(ActivitySpecEntity activitySpec); + + void update(ActivitySpecEntity activitySpec); +} diff --git a/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/dao/impl/ActivitySpecDaoZusammenImpl.java b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/dao/impl/ActivitySpecDaoZusammenImpl.java new file mode 100644 index 0000000000..9da4cd4835 --- /dev/null +++ b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/dao/impl/ActivitySpecDaoZusammenImpl.java @@ -0,0 +1,148 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.activityspec.be.dao.impl; + +import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement; +import com.amdocs.zusammen.datatypes.SessionContext; +import com.amdocs.zusammen.datatypes.item.Action; +import com.amdocs.zusammen.datatypes.item.ElementContext; +import com.amdocs.zusammen.datatypes.item.Info; + +import org.onap.sdc.activityspec.be.dao.ActivitySpecDao; +import org.onap.sdc.activityspec.be.dao.types.ActivitySpecEntity; +import org.onap.sdc.activityspec.be.datatypes.ActivitySpecData; +import org.onap.sdc.activityspec.be.datatypes.ElementType; +import org.onap.sdc.activityspec.utils.ActivitySpecConstant; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.core.zusammen.api.ZusammenAdaptor; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.Objects; +import java.util.Optional; + +import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement; +import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext; + +public class ActivitySpecDaoZusammenImpl implements ActivitySpecDao { + + private final ZusammenAdaptor zusammenAdaptor; + + public ActivitySpecDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) { + this.zusammenAdaptor = zusammenAdaptor; + } + + @Override + public void create(ActivitySpecEntity entity) { + SessionContext context = createSessionContext(); + ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.CREATE); + + ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId()); + zusammenAdaptor + .saveElement(context, elementContext, generalElement, "Create Activity Spec General Info Element"); + } + + @Override + public ActivitySpecEntity get(ActivitySpecEntity entity) { + SessionContext context = createSessionContext(); + + ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId()); + Optional<Element> element = + zusammenAdaptor.getElementByName(context, elementContext, null, ElementType.ACTIVITYSPEC.name()); + if (element.isPresent()) { + return mapZusammenElementToActivityDetails(element.get()); + } else { + return null; + } + } + + @Override + public void update(ActivitySpecEntity entity) { + SessionContext context = createSessionContext(); + ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.UPDATE); + + ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId()); + zusammenAdaptor + .saveElement(context, elementContext, generalElement, "Update Activity Spec General Info Element"); + } + + + private ActivitySpecEntity mapZusammenElementToActivityDetails(Element element) { + ActivitySpecEntity entity = new ActivitySpecEntity(); + entity.setId(element.getElementId().getValue()); + enrichEntityFromElementData(entity, element.getData()); + enrichEntityFromElementInfo(entity, element.getInfo()); + return entity; + } + + private ZusammenElement mapActivityDetailsToZusammenElement(ActivitySpecEntity entity, Action action) { + ZusammenElement generalElement = buildStructuralElement(ElementType.ACTIVITYSPEC.name(), action); + + enrichElementInfoFromEntity(generalElement, entity); + enrichElementDataFromEntity(generalElement, entity); + return generalElement; + } + + + private void enrichEntityFromElementInfo(ActivitySpecEntity entity, Info info) { + entity.setName(info.getProperty(InfoPropertyName.NAME.getValue())); + entity.setDescription(info.getProperty(InfoPropertyName.DESCRIPTION.getValue())); + entity.setCategoryList(info.getProperty(InfoPropertyName.CATEGORY.getValue())); + } + + private void enrichEntityFromElementData(ActivitySpecEntity entity, InputStream data) { + ActivitySpecData activitySpecData = JsonUtil.json2Object(data, ActivitySpecData.class); + if (Objects.nonNull(activitySpecData)) { + entity.setInputs(activitySpecData.getInputs()); + entity.setOutputs(activitySpecData.getOutputs()); + entity.setType(activitySpecData.getType()); + entity.setContent(activitySpecData.getContent()); + } + } + + private void enrichElementInfoFromEntity(ZusammenElement element, ActivitySpecEntity entity) { + element.getInfo().addProperty(InfoPropertyName.DESCRIPTION.getValue(), entity.getDescription()); + element.getInfo().addProperty(InfoPropertyName.NAME.getValue(), entity.getName()); + element.getInfo().addProperty(InfoPropertyName.CATEGORY.getValue(), entity.getCategoryList()); + } + + + private void enrichElementDataFromEntity(ZusammenElement element, ActivitySpecEntity entity) { + ActivitySpecData activitySpecData = new ActivitySpecData(); + activitySpecData.setInputs(entity.getInputs()); + activitySpecData.setOutputs(entity.getOutputs()); + activitySpecData.setType(entity.getType()); + activitySpecData.setContent(entity.getContent()); + element.setData(new ByteArrayInputStream(JsonUtil.object2Json(activitySpecData).getBytes())); + } + + public enum InfoPropertyName { + DESCRIPTION("description"), NAME("name"), CATEGORY(ActivitySpecConstant.CATEGORY_ATTRIBUTE_NAME); + + private final String value; + + InfoPropertyName(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } + +} diff --git a/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/dao/types/ActivitySpecEntity.java b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/dao/types/ActivitySpecEntity.java new file mode 100644 index 0000000000..7dc66123f9 --- /dev/null +++ b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/dao/types/ActivitySpecEntity.java @@ -0,0 +1,40 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.activityspec.be.dao.types; + +import org.onap.sdc.activityspec.be.datatypes.ActivitySpecParameter; +import org.openecomp.sdc.versioning.dao.types.Version; + +import java.util.List; + +@lombok.Data +public class ActivitySpecEntity { + + private String id; + private Version version; + private String name; + private String description; + + private List<String> categoryList; + private List<ActivitySpecParameter> inputs; + private List<ActivitySpecParameter> outputs; + private String type; + private String content; + + //Not to be maintained in activityspec element + private String status; +} diff --git a/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ActivitySpecData.java b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ActivitySpecData.java new file mode 100644 index 0000000000..2c76ed86d3 --- /dev/null +++ b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ActivitySpecData.java @@ -0,0 +1,30 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.activityspec.be.datatypes; + +import java.util.Collections; +import java.util.List; + +@lombok.Data +public class ActivitySpecData { + + private List<ActivitySpecParameter> inputs = Collections.emptyList(); + private List<ActivitySpecParameter> outputs = Collections.emptyList(); + private String type; + private String content; +} + diff --git a/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ActivitySpecParameter.java b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ActivitySpecParameter.java new file mode 100644 index 0000000000..72273dd649 --- /dev/null +++ b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ActivitySpecParameter.java @@ -0,0 +1,35 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.activityspec.be.datatypes; + +@lombok.Data +public class ActivitySpecParameter { + + private String name; + private String type; + private String value; + + @SuppressWarnings("unused") + public ActivitySpecParameter() { + // default constructor for automatic tools that use reflection + } + + public ActivitySpecParameter(String name, String type) { + this.name = name; + this.type = type; + } +} diff --git a/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ElementType.java b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ElementType.java new file mode 100644 index 0000000000..d063530145 --- /dev/null +++ b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ElementType.java @@ -0,0 +1,21 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.activityspec.be.datatypes; + +public enum ElementType { + ACTIVITYSPEC +} diff --git a/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ItemType.java b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ItemType.java new file mode 100644 index 0000000000..f9d8fa33f3 --- /dev/null +++ b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/datatypes/ItemType.java @@ -0,0 +1,21 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.activityspec.be.datatypes; + +public enum ItemType { + ACTIVITYSPEC +} diff --git a/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/impl/ActivitySpecManagerImpl.java b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/impl/ActivitySpecManagerImpl.java new file mode 100644 index 0000000000..6853b855d2 --- /dev/null +++ b/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/be/impl/ActivitySpecManagerImpl.java @@ -0,0 +1,265 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.activityspec.be.impl; + +import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Certified; +import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Deleted; +import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Deprecated; +import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Draft; + +import java.util.Collection; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Predicate; + +import org.onap.sdc.activityspec.api.rest.types.ActivitySpecAction; +import org.onap.sdc.activityspec.be.dao.ActivitySpecDao; +import org.onap.sdc.activityspec.utils.ActivitySpecConstant; +import org.onap.sdc.activityspec.be.ActivitySpecManager; +import org.onap.sdc.activityspec.be.dao.types.ActivitySpecEntity; +import org.onap.sdc.activityspec.be.datatypes.ItemType; +import org.onap.sdc.activityspec.errors.ActivitySpecNotFoundException; +import org.openecomp.core.dao.UniqueValueDao; +import org.openecomp.core.util.UniqueValueUtil; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; +import org.openecomp.sdc.common.errors.SdcRuntimeException; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.versioning.ItemManager; +import org.openecomp.sdc.versioning.VersioningManager; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.dao.types.VersionStatus; +import org.openecomp.sdc.versioning.types.Item; +import org.openecomp.sdc.versioning.types.VersionCreationMethod; + +public class ActivitySpecManagerImpl implements ActivitySpecManager { + + private static final Map<VersionStatus, VersionStatus> EXPECTED_PREV_STATUS; + + static { + EXPECTED_PREV_STATUS = new EnumMap<>(VersionStatus.class); + EXPECTED_PREV_STATUS.put(Certified, Draft); + EXPECTED_PREV_STATUS.put(Deprecated, Certified); + EXPECTED_PREV_STATUS.put(Deleted, Deprecated); + } + + private final ItemManager itemManager; + private final VersioningManager versioningManager; + private final ActivitySpecDao activitySpecDao; + private final UniqueValueUtil uniqueValueUtil; + private static final String ACTIVITY_SPEC_NAME = "ActivitySpec.Name"; + private static final Logger LOGGER = LoggerFactory.getLogger(ActivitySpecManagerImpl.class); + + public ActivitySpecManagerImpl(ItemManager itemManager, VersioningManager versioningManager, + ActivitySpecDao activitySpecDao, UniqueValueDao uniqueValueDao) { + this.itemManager = itemManager; + this.versioningManager = versioningManager; + this.activitySpecDao = activitySpecDao; + this.uniqueValueUtil = new UniqueValueUtil(uniqueValueDao); + } + + @Override + public ActivitySpecEntity createActivitySpec(ActivitySpecEntity activitySpecEntity) { + + validateUniqueValue(activitySpecEntity); + + Item item = getActivitySpecItem(activitySpecEntity); + item = itemManager.create(item); + + Version version = getActivitySpecVersion(activitySpecEntity); + versioningManager.create(item.getId(), version, VersionCreationMethod.major); + + enrichActivitySpec(item, version, activitySpecEntity); + activitySpecDao.create(activitySpecEntity); + + uniqueValueUtil.createUniqueValue(ACTIVITY_SPEC_NAME, activitySpecEntity.getName()); + return activitySpecEntity; + } + + private void validateUniqueValue(ActivitySpecEntity activitySpecEntity) { + try { + uniqueValueUtil.validateUniqueValue(ACTIVITY_SPEC_NAME, activitySpecEntity.getName()); + } catch (CoreException exception) { + LOGGER.debug("Unique value error for activity spec name :" + activitySpecEntity.getName(), exception); + throw new CoreException(new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION) + .withId(exception.code().id()) + .withMessage("name already in use").build()); + } + } + + private Version getActivitySpecVersion(ActivitySpecEntity activitySpecEntity) { + return activitySpecEntity.getVersion() == null ? new Version() : activitySpecEntity.getVersion(); + + } + + @Override + public ActivitySpecEntity get(ActivitySpecEntity activitySpec) { + activitySpec.setVersion(calculateLatestVersion(activitySpec.getId(), activitySpec.getVersion())); + ActivitySpecEntity retrieved; + try { + retrieved = activitySpecDao.get(activitySpec); + } catch (SdcRuntimeException runtimeException) { + LOGGER.debug( + "Failed to retrieve activity spec for activitySpecId: " + activitySpec.getId() + " and version: " + + activitySpec.getVersion().getId(), runtimeException); + throw new ActivitySpecNotFoundException(ActivitySpecConstant.ACTIVITY_SPEC_NOT_FOUND, runtimeException); + } + if (retrieved != null) { + final Version retrievedVersion = versioningManager.get(activitySpec.getId(), activitySpec.getVersion()); + retrieved.setStatus(Objects.nonNull(retrievedVersion) ? retrievedVersion.getStatus().name() : null); + } + return retrieved; + } + + @Override + public void update(ActivitySpecEntity activitySpec) { + + ActivitySpecEntity previousActivitySpec = get(activitySpec); + + if (!activitySpec.getName().equals(previousActivitySpec.getName())) { + validateUniqueValue(activitySpec); + } + + activitySpecDao.update(activitySpec); + + if (!activitySpec.getName().equals(previousActivitySpec.getName())) { + uniqueValueUtil.createUniqueValue(ACTIVITY_SPEC_NAME, activitySpec.getName()); + itemManager.updateName(activitySpec.getId(), activitySpec.getName()); + uniqueValueUtil.deleteUniqueValue(ACTIVITY_SPEC_NAME, previousActivitySpec.getName()); + } + } + + @Override + public void actOnAction(String activitySpecId, String versionId, ActivitySpecAction action) { + Version version = new Version(versionId); + version = calculateLatestVersion(activitySpecId, version); + if (action == ActivitySpecAction.CERTIFY) { + version.setStatus(Certified); + } + if (action == ActivitySpecAction.DEPRECATE) { + version.setStatus(Deprecated); + } + if (action == ActivitySpecAction.DELETE) { + version.setStatus(Deleted); + } + + updateVersionStatus(activitySpecId, action, version); + if (action == ActivitySpecAction.DELETE) { + ActivitySpecEntity entity = new ActivitySpecEntity(); + entity.setId(activitySpecId); + entity.setVersion(version); + final ActivitySpecEntity activitySpecEntity = get(entity); + uniqueValueUtil.deleteUniqueValue(ACTIVITY_SPEC_NAME, activitySpecEntity.getName()); + } + } + + private void updateVersionStatus(String activitySpecId, ActivitySpecAction action, Version version) { + VersionStatus prevVersionStatus = null; + Version retrievedVersion; + try { + retrievedVersion = versioningManager.get(activitySpecId, version); + } catch (SdcRuntimeException exception) { + LOGGER.debug( + "Failed to get version for activitySpecId: " + activitySpecId + " and version: " + version.getId(), + exception); + throw new ActivitySpecNotFoundException(ActivitySpecConstant.ACTIVITY_SPEC_NOT_FOUND, exception); + + } + + VersionStatus status = version.getStatus(); + VersionStatus expectedPrevStatus = EXPECTED_PREV_STATUS.get(status); + if (expectedPrevStatus != null) { + + VersionStatus retrievedStatus = Objects.nonNull(retrievedVersion) ? retrievedVersion.getStatus() : null; + if (retrievedStatus != expectedPrevStatus) { + LOGGER.debug("Failed to " + version.getStatus() + " since activity spec is in " + retrievedStatus); + throw new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage(ActivitySpecConstant.INVALID_STATE) + .build()); + } + prevVersionStatus = expectedPrevStatus; + } + + if (Objects.nonNull(retrievedVersion)) { + retrievedVersion.setStatus(status); + versioningManager.updateVersion(activitySpecId, retrievedVersion); + itemManager.updateVersionStatus(activitySpecId, status, prevVersionStatus); + versioningManager.publish(activitySpecId, retrievedVersion, "actionOnActivitySpec :" + action.name()); + } + } + + private Version calculateLatestVersion(String activitySpecId, Version version) { + if (ActivitySpecConstant.VERSION_ID_DEFAULT_VALUE.equalsIgnoreCase(version.getId())) { + List<Version> list; + try { + list = versioningManager.list(activitySpecId); + } catch (SdcRuntimeException runtimeException) { + LOGGER.debug("Failed to list versions for activitySpecId " + activitySpecId, runtimeException); + throw new ActivitySpecNotFoundException(ActivitySpecConstant.ACTIVITY_SPEC_NOT_FOUND, runtimeException); + } + if (Objects.nonNull(list) && !list.isEmpty()) { + return list.get(0); + } + } + return version; + } + + @Override + public Collection<Item> list(String versionStatus) { + Predicate<Item> itemPredicate; + if (Objects.nonNull(versionStatus)) { + + VersionStatus statusEnumValue; + + try { + statusEnumValue = VersionStatus.valueOf(versionStatus); + } catch (IllegalArgumentException e) { + LOGGER.debug("Unexpected value of VersionStatus {}", versionStatus); + return Collections.emptyList(); + } + + itemPredicate = + item -> ItemType.ACTIVITYSPEC.name().equals(item.getType()) && item.getVersionStatusCounters() + .containsKey(statusEnumValue); + + } else { + itemPredicate = item -> ItemType.ACTIVITYSPEC.name().equals(item.getType()); + } + + return itemManager.list(itemPredicate); + } + + private void enrichActivitySpec(Item item, Version version, ActivitySpecEntity activitySpecEntity) { + activitySpecEntity.setId(item.getId()); + activitySpecEntity.setVersion(version); + } + + private Item getActivitySpecItem(ActivitySpecEntity activitySpecEntity) { + Item item = new Item(); + item.setType(ItemType.ACTIVITYSPEC.name()); + item.setName(activitySpecEntity.getName()); + if (activitySpecEntity.getId() != null) { + item.setId(activitySpecEntity.getId()); + } + item.addProperty(ActivitySpecConstant.CATEGORY_ATTRIBUTE_NAME, activitySpecEntity.getCategoryList()); + return item; + } +} |