aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImpl.java
blob: b6316638136605ddbbdcd2473d13c53a67fb815e (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.versioning.dao.impl.zusammen;

import com.amdocs.zusammen.datatypes.Id;
import com.amdocs.zusammen.datatypes.item.Info;
import org.openecomp.core.zusammen.api.ZusammenAdaptor;
import org.openecomp.core.zusammen.api.ZusammenUtil;
import org.openecomp.sdc.versioning.dao.ItemDao;
import org.openecomp.sdc.versioning.dao.types.VersionStatus;
import org.openecomp.sdc.versioning.types.Item;
import org.openecomp.sdc.versioning.types.ItemStatus;

import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

public class ItemZusammenDaoImpl implements ItemDao {

  private ZusammenAdaptor zusammenAdaptor;

  public ItemZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
    this.zusammenAdaptor = zusammenAdaptor;
  }

  @Override
  public Collection<Item> list() {
    return zusammenAdaptor.listItems(ZusammenUtil.createSessionContext())
        .stream().map(this::mapFromZusammenItem)
        .collect(Collectors.toList());
  }

  @Override
  public Item get(Item item) {
    return mapFromZusammenItem(
        zusammenAdaptor.getItem(ZusammenUtil.createSessionContext(), new Id(item.getId())));
  }

  @Override
  public Item create(Item item) {
    Id itemId = zusammenAdaptor
        .createItem(ZusammenUtil.createSessionContext(), mapToZusammenItemInfo(item));
    item.setId(itemId.getValue());
    return item;
  }

  @Override
  public void delete(Item item){
    zusammenAdaptor.deleteItem(ZusammenUtil.createSessionContext(),new Id(item.getId()));
  }

  @Override
  public void update(Item item) {
    zusammenAdaptor
        .updateItem(ZusammenUtil.createSessionContext(), new Id(item.getId()),
            mapToZusammenItemInfo(item));
  }

  private Item mapFromZusammenItem(com.amdocs.zusammen.datatypes.item.Item zusammenItem) {
    if (zusammenItem == null) {
      return null;
    }
    Item item = new Item();
    item.setId(zusammenItem.getId().getValue());
    item.setName(zusammenItem.getInfo().getName());
    item.setDescription(zusammenItem.getInfo().getDescription());

    zusammenItem.getInfo().getProperties().entrySet()
        .forEach(property -> addPropertyToItem(property.getKey(), property.getValue(), item));

    item.setCreationTime(zusammenItem.getCreationTime());
    item.setModificationTime(zusammenItem.getModificationTime());

    if(item.getStatus() == null){
      item.setStatus(ItemStatus.ACTIVE);
      update(item);
    }

    return item;
  }

  private void addPropertyToItem(String propertyKey, Object propertyValue, Item item) {
    switch (propertyKey) {
      case InfoPropertyName.ITEM_TYPE:
        item.setType((String) propertyValue);
        break;
      case InfoPropertyName.ITEM_OWNER:
        item.setOwner((String) propertyValue);
        break;
      case InfoPropertyName.ITEM_STATUS:
        item.setStatus(ItemStatus.valueOf((String)propertyValue));
        break;
      case InfoPropertyName.ITEM_VERSIONS_STATUSES:
        for (Map.Entry<String, Number> statusCounter :
            ((Map<String, Number>) propertyValue).entrySet()) {
          item.getVersionStatusCounters().put(VersionStatus.valueOf(statusCounter.getKey()),
              statusCounter.getValue().intValue());
        }
        break;
      default:
        item.addProperty(propertyKey, propertyValue);
    }
  }

  private Info mapToZusammenItemInfo(Item item) {
    Info info = new Info();
    info.setName(item.getName());
    info.setDescription(item.getDescription());
    info.addProperty(InfoPropertyName.ITEM_TYPE, item.getType());
    info.addProperty(InfoPropertyName.ITEM_OWNER,item.getOwner());
    if (item.getStatus() != null) {
      info.addProperty(InfoPropertyName.ITEM_STATUS, item.getStatus());
    }
    info.addProperty(InfoPropertyName.ITEM_VERSIONS_STATUSES, item.getVersionStatusCounters());
    item.getProperties().entrySet()
        .forEach(property -> info.addProperty(property.getKey(), property.getValue()));
    return info;
  }

  private static final class InfoPropertyName {
    private static final String ITEM_TYPE = "item_type";
    private static final String ITEM_VERSIONS_STATUSES = "item_versions_statuses";
    private static final String ITEM_OWNER = "Owner";
    private static final String ITEM_STATUS = "status";

    private InfoPropertyName() {
      throw new IllegalStateException("Constants class");
    }
  }
}