summaryrefslogtreecommitdiffstats
path: root/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/openecomp/activityspec/be/dao/impl/ActivitySpecDaoZusammenImpl.java
blob: 0dabcca9847f4bfeded4613b94e402d0ec414e9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright © 2016-2017 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.openecomp.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.openecomp.activityspec.be.dao.ActivitySpecDao;
import org.openecomp.activityspec.be.dao.types.ActivitySpecEntity;
import org.openecomp.activityspec.utils.ActivitySpecConstant;
import org.openecomp.activityspec.be.datatypes.ActivitySpecData;
import org.openecomp.activityspec.be.datatypes.ElementType;
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;
    }
  }

}