2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.sdc.workflow.persistence.impl;
19 import static org.onap.sdc.common.zusammen.services.ZusammenElementUtil.buildStructuralElement;
21 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
22 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
23 import com.amdocs.zusammen.datatypes.item.Action;
24 import com.amdocs.zusammen.datatypes.item.ElementContext;
25 import com.amdocs.zusammen.datatypes.item.Info;
26 import java.io.ByteArrayInputStream;
27 import java.io.InputStream;
28 import java.util.Objects;
29 import java.util.Optional;
30 import org.onap.sdc.common.versioning.persistence.zusammen.ZusammenSessionContextCreator;
31 import org.onap.sdc.common.zusammen.services.ZusammenAdaptor;
32 import org.onap.sdc.workflow.persistence.ActivitySpecRepository;
33 import org.onap.sdc.workflow.persistence.impl.types.ActivitySpecData;
34 import org.onap.sdc.workflow.persistence.impl.types.ActivitySpecElementType;
35 import org.onap.sdc.workflow.persistence.types.ActivitySpecEntity;
36 import org.onap.sdc.workflow.services.ActivitySpecConstant;
37 import org.onap.sdc.workflow.services.utilities.JsonUtil;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Repository;
42 public class ActivitySpecRepositoryImpl implements ActivitySpecRepository {
44 private final ZusammenAdaptor zusammenAdaptor;
45 private final ZusammenSessionContextCreator contextCreator;
48 public ActivitySpecRepositoryImpl(ZusammenAdaptor zusammenAdaptor, ZusammenSessionContextCreator contextCreator) {
49 this.zusammenAdaptor = zusammenAdaptor;
50 this.contextCreator = contextCreator;
54 public void create(ActivitySpecEntity activitySpec) {
55 ZusammenElement generalElement = mapActivityDetailsToZusammenElement(activitySpec, Action.CREATE);
57 ElementContext elementContext = new ElementContext(activitySpec.getId(), activitySpec.getVersionId());
59 .saveElement(contextCreator.create(), elementContext, generalElement, "Create Activity Spec General Info Element");
63 public ActivitySpecEntity get(ActivitySpecEntity entity) {
64 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersionId());
65 Optional<Element> element =
66 zusammenAdaptor.getElementByName(contextCreator.create(), elementContext, null, ActivitySpecElementType.ACTIVITYSPEC.name());
67 return element.map(this::mapZusammenElementToActivityDetails).orElse(null);
71 public void update(ActivitySpecEntity entity) {
73 ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.UPDATE);
75 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersionId());
77 .saveElement(contextCreator.create(), elementContext, generalElement, "Update Activity Spec General Info Element");
80 private ZusammenElement mapActivityDetailsToZusammenElement(ActivitySpecEntity entity, Action action) {
81 ZusammenElement generalElement = buildStructuralElement(ActivitySpecElementType.ACTIVITYSPEC.name(), action);
83 enrichElementInfoFromEntity(generalElement, entity);
84 enrichElementDataFromEntity(generalElement, entity);
85 return generalElement;
88 private void enrichElementInfoFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
89 element.getInfo().addProperty(InfoPropertyName.DESCRIPTION.getValue(), entity.getDescription());
90 element.getInfo().addProperty(InfoPropertyName.NAME.getValue(), entity.getName());
91 element.getInfo().addProperty(InfoPropertyName.CATEGORY.getValue(), entity.getCategoryList());
94 private void enrichElementDataFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
95 ActivitySpecData activitySpecData = new ActivitySpecData();
96 activitySpecData.setInputs(entity.getInputs());
97 activitySpecData.setOutputs(entity.getOutputs());
98 activitySpecData.setType(entity.getType());
99 activitySpecData.setContent(entity.getContent());
100 element.setData(new ByteArrayInputStream(JsonUtil.object2Json(activitySpecData).getBytes()));
103 private ActivitySpecEntity mapZusammenElementToActivityDetails(Element element) {
104 ActivitySpecEntity entity = new ActivitySpecEntity();
105 entity.setId(element.getElementId().getValue());
106 enrichEntityFromElementData(entity, element.getData());
107 enrichEntityFromElementInfo(entity, element.getInfo());
111 private void enrichEntityFromElementData(ActivitySpecEntity entity, InputStream data) {
112 ActivitySpecData activitySpecData = JsonUtil.json2Object(data, ActivitySpecData.class);
113 if (Objects.nonNull(activitySpecData)) {
114 entity.setInputs(activitySpecData.getInputs());
115 entity.setOutputs(activitySpecData.getOutputs());
116 entity.setType(activitySpecData.getType());
117 entity.setContent(activitySpecData.getContent());
121 private void enrichEntityFromElementInfo(ActivitySpecEntity entity, Info info) {
122 entity.setName(info.getProperty(InfoPropertyName.NAME.getValue()));
123 entity.setDescription(info.getProperty(InfoPropertyName.DESCRIPTION.getValue()));
124 entity.setCategoryList(info.getProperty(InfoPropertyName.CATEGORY.getValue()));
127 public enum InfoPropertyName {
128 DESCRIPTION("description"), NAME("name"), CATEGORY(ActivitySpecConstant.CATEGORY_ATTRIBUTE_NAME);
130 private final String value;
132 InfoPropertyName(String value) {
136 public String getValue() {