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.activityspec.be.dao.impl;
19 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
20 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
21 import com.amdocs.zusammen.datatypes.SessionContext;
22 import com.amdocs.zusammen.datatypes.item.Action;
23 import com.amdocs.zusammen.datatypes.item.ElementContext;
24 import com.amdocs.zusammen.datatypes.item.Info;
26 import org.onap.sdc.activityspec.be.dao.ActivitySpecDao;
27 import org.onap.sdc.activityspec.be.dao.types.ActivitySpecEntity;
28 import org.onap.sdc.activityspec.be.datatypes.ActivitySpecData;
29 import org.onap.sdc.activityspec.be.datatypes.ElementType;
30 import org.onap.sdc.activityspec.utils.ActivitySpecConstant;
31 import org.openecomp.core.utilities.json.JsonUtil;
32 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
34 import java.io.ByteArrayInputStream;
35 import java.io.InputStream;
36 import java.util.Objects;
37 import java.util.Optional;
39 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
40 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
42 public class ActivitySpecDaoZusammenImpl implements ActivitySpecDao {
44 private final ZusammenAdaptor zusammenAdaptor;
46 public ActivitySpecDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
47 this.zusammenAdaptor = zusammenAdaptor;
51 public void create(ActivitySpecEntity entity) {
52 SessionContext context = createSessionContext();
53 ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.CREATE);
55 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId());
57 .saveElement(context, elementContext, generalElement, "Create Activity Spec General Info Element");
61 public ActivitySpecEntity get(ActivitySpecEntity entity) {
62 SessionContext context = createSessionContext();
64 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId());
65 Optional<Element> element =
66 zusammenAdaptor.getElementByName(context, elementContext, null, ElementType.ACTIVITYSPEC.name());
67 if (element.isPresent()) {
68 return mapZusammenElementToActivityDetails(element.get());
75 public void update(ActivitySpecEntity entity) {
76 SessionContext context = createSessionContext();
77 ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.UPDATE);
79 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId());
81 .saveElement(context, elementContext, generalElement, "Update Activity Spec General Info Element");
85 private ActivitySpecEntity mapZusammenElementToActivityDetails(Element element) {
86 ActivitySpecEntity entity = new ActivitySpecEntity();
87 entity.setId(element.getElementId().getValue());
88 enrichEntityFromElementData(entity, element.getData());
89 enrichEntityFromElementInfo(entity, element.getInfo());
93 private ZusammenElement mapActivityDetailsToZusammenElement(ActivitySpecEntity entity, Action action) {
94 ZusammenElement generalElement = buildStructuralElement(ElementType.ACTIVITYSPEC.name(), action);
96 enrichElementInfoFromEntity(generalElement, entity);
97 enrichElementDataFromEntity(generalElement, entity);
98 return generalElement;
102 private void enrichEntityFromElementInfo(ActivitySpecEntity entity, Info info) {
103 entity.setName(info.getProperty(InfoPropertyName.NAME.getValue()));
104 entity.setDescription(info.getProperty(InfoPropertyName.DESCRIPTION.getValue()));
105 entity.setCategoryList(info.getProperty(InfoPropertyName.CATEGORY.getValue()));
108 private void enrichEntityFromElementData(ActivitySpecEntity entity, InputStream data) {
109 ActivitySpecData activitySpecData = JsonUtil.json2Object(data, ActivitySpecData.class);
110 if (Objects.nonNull(activitySpecData)) {
111 entity.setInputs(activitySpecData.getInputs());
112 entity.setOutputs(activitySpecData.getOutputs());
113 entity.setType(activitySpecData.getType());
114 entity.setContent(activitySpecData.getContent());
118 private void enrichElementInfoFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
119 element.getInfo().addProperty(InfoPropertyName.DESCRIPTION.getValue(), entity.getDescription());
120 element.getInfo().addProperty(InfoPropertyName.NAME.getValue(), entity.getName());
121 element.getInfo().addProperty(InfoPropertyName.CATEGORY.getValue(), entity.getCategoryList());
125 private void enrichElementDataFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
126 ActivitySpecData activitySpecData = new ActivitySpecData();
127 activitySpecData.setInputs(entity.getInputs());
128 activitySpecData.setOutputs(entity.getOutputs());
129 activitySpecData.setType(entity.getType());
130 activitySpecData.setContent(entity.getContent());
131 element.setData(new ByteArrayInputStream(JsonUtil.object2Json(activitySpecData).getBytes()));
134 public enum InfoPropertyName {
135 DESCRIPTION("description"), NAME("name"), CATEGORY(ActivitySpecConstant.CATEGORY_ATTRIBUTE_NAME);
137 private final String value;
139 InfoPropertyName(String value) {
143 public String getValue() {