2 * Copyright © 2016-2017 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.openecomp.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.openecomp.activityspec.be.dao.ActivitySpecDao;
27 import org.openecomp.activityspec.be.dao.types.ActivitySpecEntity;
28 import org.openecomp.activityspec.utils.ActivitySpecConstant;
29 import org.openecomp.activityspec.be.datatypes.ActivitySpecData;
30 import org.openecomp.activityspec.be.datatypes.ElementType;
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 =
56 new ElementContext(entity.getId(), entity.getVersion().getId());
57 zusammenAdaptor.saveElement(context, elementContext, generalElement,
58 "Create Activity Spec General Info Element");
62 public ActivitySpecEntity get(ActivitySpecEntity entity) {
63 SessionContext context = createSessionContext();
65 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId());
66 Optional<Element> element = zusammenAdaptor.getElementByName(context, elementContext,
67 null, ElementType.ACTIVITYSPEC.name());
68 if (element.isPresent()) {
69 return mapZusammenElementToActivityDetails(element.get());
76 public void update(ActivitySpecEntity entity) {
77 SessionContext context = createSessionContext();
78 ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.UPDATE);
80 ElementContext elementContext =
81 new ElementContext(entity.getId(), entity.getVersion().getId());
82 zusammenAdaptor.saveElement(context, elementContext, generalElement,
83 "Update Activity Spec General Info Element");
87 private ActivitySpecEntity mapZusammenElementToActivityDetails(Element element) {
88 ActivitySpecEntity entity = new ActivitySpecEntity();
89 entity.setId(element.getElementId().getValue());
90 enrichEntityFromElementData(entity, element.getData());
91 enrichEntityFromElementInfo(entity, element.getInfo());
95 private ZusammenElement mapActivityDetailsToZusammenElement(ActivitySpecEntity entity,
97 ZusammenElement generalElement =
98 buildStructuralElement(ElementType.ACTIVITYSPEC.name(), action);
100 enrichElementInfoFromEntity(generalElement, entity);
101 enrichElementDataFromEntity(generalElement, entity);
102 return generalElement;
106 private void enrichEntityFromElementInfo(ActivitySpecEntity entity, Info info) {
107 entity.setName(info.getProperty(InfoPropertyName.NAME.getValue()));
108 entity.setDescription(info.getProperty(InfoPropertyName.DESCRIPTION.getValue()));
109 entity.setCategoryList(info.getProperty(InfoPropertyName.CATEGORY.getValue()));
112 private void enrichEntityFromElementData(ActivitySpecEntity entity, InputStream data) {
113 ActivitySpecData activitySpecData = JsonUtil.json2Object(data, ActivitySpecData.class);
114 if (Objects.nonNull(activitySpecData)) {
115 entity.setInputParameters(activitySpecData.getInputParameters());
116 entity.setOutputParameters(activitySpecData.getOutputParameters());
120 private void enrichElementInfoFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
121 element.getInfo().addProperty(InfoPropertyName.DESCRIPTION.getValue(), entity.getDescription());
122 element.getInfo().addProperty(InfoPropertyName.NAME.getValue(), entity.getName());
123 element.getInfo().addProperty(InfoPropertyName.CATEGORY.getValue(),
124 entity.getCategoryList());
128 private void enrichElementDataFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
129 ActivitySpecData activitySpecData = new ActivitySpecData();
130 activitySpecData.setInputParameters(entity.getInputParameters());
131 activitySpecData.setOutputParameters(entity.getOutputParameters());
132 element.setData(new ByteArrayInputStream(JsonUtil.object2Json(activitySpecData).getBytes()));
135 public enum InfoPropertyName {
136 DESCRIPTION("description"),
138 CATEGORY(ActivitySpecConstant.CATEGORY_ATTRIBUTE_NAME);
140 private final String value;
142 InfoPropertyName(String value) {
146 public String getValue() {