Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-sdc-versioning-lib / openecomp-sdc-versioning-core / src / main / java / org / openecomp / sdc / versioning / dao / impl / zusammen / ItemZusammenDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.versioning.dao.impl.zusammen;
21
22 import com.amdocs.zusammen.datatypes.Id;
23 import com.amdocs.zusammen.datatypes.item.Info;
24 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
25 import org.openecomp.core.zusammen.api.ZusammenUtil;
26 import org.openecomp.sdc.versioning.dao.ItemDao;
27 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
28 import org.openecomp.sdc.versioning.types.Item;
29 import org.openecomp.sdc.versioning.types.ItemStatus;
30
31 import java.util.Collection;
32 import java.util.Map;
33 import java.util.stream.Collectors;
34
35 public class ItemZusammenDaoImpl implements ItemDao {
36
37   private ZusammenAdaptor zusammenAdaptor;
38
39   public ItemZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
40     this.zusammenAdaptor = zusammenAdaptor;
41   }
42
43   @Override
44   public Collection<Item> list() {
45     return zusammenAdaptor.listItems(ZusammenUtil.createSessionContext())
46         .stream().map(this::mapFromZusammenItem)
47         .collect(Collectors.toList());
48   }
49
50   @Override
51   public Item get(Item item) {
52     return mapFromZusammenItem(
53         zusammenAdaptor.getItem(ZusammenUtil.createSessionContext(), new Id(item.getId())));
54   }
55
56   @Override
57   public Item create(Item item) {
58     Id itemId = zusammenAdaptor
59         .createItem(ZusammenUtil.createSessionContext(), mapToZusammenItemInfo(item));
60     item.setId(itemId.getValue());
61     return item;
62   }
63
64   @Override
65   public void delete(Item item){
66     zusammenAdaptor.deleteItem(ZusammenUtil.createSessionContext(),new Id(item.getId()));
67   }
68
69   @Override
70   public void update(Item item) {
71     zusammenAdaptor
72         .updateItem(ZusammenUtil.createSessionContext(), new Id(item.getId()),
73             mapToZusammenItemInfo(item));
74   }
75
76   private Item mapFromZusammenItem(com.amdocs.zusammen.datatypes.item.Item zusammenItem) {
77     if (zusammenItem == null) {
78       return null;
79     }
80     Item item = new Item();
81     item.setId(zusammenItem.getId().getValue());
82     item.setName(zusammenItem.getInfo().getName());
83     item.setDescription(zusammenItem.getInfo().getDescription());
84
85     zusammenItem.getInfo().getProperties().entrySet()
86         .forEach(property -> addPropertyToItem(property.getKey(), property.getValue(), item));
87
88     item.setCreationTime(zusammenItem.getCreationTime());
89     item.setModificationTime(zusammenItem.getModificationTime());
90
91     if(item.getStatus() == null){
92       item.setStatus(ItemStatus.ACTIVE);
93       update(item);
94     }
95
96     return item;
97   }
98
99   private void addPropertyToItem(String propertyKey, Object propertyValue, Item item) {
100     switch (propertyKey) {
101       case InfoPropertyName.ITEM_TYPE:
102         item.setType((String) propertyValue);
103         break;
104       case InfoPropertyName.ITEM_OWNER:
105         item.setOwner((String) propertyValue);
106         break;
107       case InfoPropertyName.ITEM_STATUS:
108         item.setStatus(ItemStatus.valueOf((String)propertyValue));
109         break;
110       case InfoPropertyName.ITEM_VERSIONS_STATUSES:
111         for (Map.Entry<String, Number> statusCounter :
112             ((Map<String, Number>) propertyValue).entrySet()) {
113           item.getVersionStatusCounters().put(VersionStatus.valueOf(statusCounter.getKey()),
114               statusCounter.getValue().intValue());
115         }
116         break;
117       default:
118         item.addProperty(propertyKey, propertyValue);
119     }
120   }
121
122   private Info mapToZusammenItemInfo(Item item) {
123     Info info = new Info();
124     info.setName(item.getName());
125     info.setDescription(item.getDescription());
126     info.addProperty(InfoPropertyName.ITEM_TYPE, item.getType());
127     info.addProperty(InfoPropertyName.ITEM_OWNER,item.getOwner());
128     if (item.getStatus() != null) {
129       info.addProperty(InfoPropertyName.ITEM_STATUS, item.getStatus());
130     }
131     info.addProperty(InfoPropertyName.ITEM_VERSIONS_STATUSES, item.getVersionStatusCounters());
132     item.getProperties().entrySet()
133         .forEach(property -> info.addProperty(property.getKey(), property.getValue()));
134     return info;
135   }
136
137   private static final class InfoPropertyName {
138     private static final String ITEM_TYPE = "item_type";
139     private static final String ITEM_VERSIONS_STATUSES = "item_versions_statuses";
140     private static final String ITEM_OWNER = "Owner";
141     private static final String ITEM_STATUS = "status";
142
143     private InfoPropertyName() {
144       throw new IllegalStateException("Constants class");
145     }
146   }
147 }