Use versioning, zusammen and session libs from sdc-common-be
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / main / java / org / onap / sdc / workflow / persistence / impl / ActivitySpecRepositoryImpl.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.sdc.workflow.persistence.impl;
18
19 import static org.onap.sdc.common.zusammen.services.ZusammenElementUtil.buildStructuralElement;
20
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;
40
41 @Repository
42 public class ActivitySpecRepositoryImpl implements ActivitySpecRepository {
43
44     private final ZusammenAdaptor zusammenAdaptor;
45     private final ZusammenSessionContextCreator contextCreator;
46
47     @Autowired
48     public ActivitySpecRepositoryImpl(ZusammenAdaptor zusammenAdaptor, ZusammenSessionContextCreator contextCreator) {
49         this.zusammenAdaptor = zusammenAdaptor;
50         this.contextCreator = contextCreator;
51     }
52
53     @Override
54     public void create(ActivitySpecEntity activitySpec) {
55         ZusammenElement generalElement = mapActivityDetailsToZusammenElement(activitySpec, Action.CREATE);
56
57         ElementContext elementContext = new ElementContext(activitySpec.getId(), activitySpec.getVersionId());
58         zusammenAdaptor
59                 .saveElement(contextCreator.create(), elementContext, generalElement, "Create Activity Spec General Info Element");
60     }
61
62     @Override
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);
68     }
69
70     @Override
71     public void update(ActivitySpecEntity entity) {
72         
73         ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.UPDATE);
74
75         ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersionId());
76         zusammenAdaptor
77                 .saveElement(contextCreator.create(), elementContext, generalElement, "Update Activity Spec General Info Element");
78     }
79
80     private ZusammenElement mapActivityDetailsToZusammenElement(ActivitySpecEntity entity, Action action) {
81         ZusammenElement generalElement = buildStructuralElement(ActivitySpecElementType.ACTIVITYSPEC.name(), action);
82
83         enrichElementInfoFromEntity(generalElement, entity);
84         enrichElementDataFromEntity(generalElement, entity);
85         return generalElement;
86     }
87
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());
92     }
93
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()));
101     }
102
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());
108         return entity;
109     }
110
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());
118         }
119     }
120
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()));
125     }
126
127     public enum InfoPropertyName {
128         DESCRIPTION("description"), NAME("name"), CATEGORY(ActivitySpecConstant.CATEGORY_ATTRIBUTE_NAME);
129
130         private final String value;
131
132         InfoPropertyName(String value) {
133             this.value = value;
134         }
135
136         public String getValue() {
137             return value;
138         }
139     }
140
141 }