6ac18d9ff9aabc37dcb735a8c774665021286e35
[sdc.git] /
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 java.util.Collection;
25 import java.util.Map;
26 import java.util.stream.Collectors;
27 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
28 import org.openecomp.core.zusammen.api.ZusammenUtil;
29 import org.openecomp.sdc.versioning.dao.ItemDao;
30 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
31 import org.openecomp.sdc.versioning.types.Item;
32 import org.openecomp.sdc.versioning.types.ItemStatus;
33
34 public class ItemZusammenDaoImpl implements ItemDao {
35
36     private ZusammenAdaptor zusammenAdaptor;
37
38     public ItemZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
39         this.zusammenAdaptor = zusammenAdaptor;
40     }
41
42     @Override
43     public Collection<Item> list() {
44         return zusammenAdaptor.listItems(ZusammenUtil.createSessionContext()).stream().map(this::mapFromZusammenItem).collect(Collectors.toList());
45     }
46
47     @Override
48     public Item get(Item item) {
49         return mapFromZusammenItem(zusammenAdaptor.getItem(ZusammenUtil.createSessionContext(), new Id(item.getId())));
50     }
51
52     @Override
53     public Item create(Item item) {
54         Id itemId = zusammenAdaptor.createItem(ZusammenUtil.createSessionContext(), mapToZusammenItemInfo(item));
55         item.setId(itemId.getValue());
56         return item;
57     }
58
59     @Override
60     public void delete(Item item) {
61         zusammenAdaptor.deleteItem(ZusammenUtil.createSessionContext(), new Id(item.getId()));
62     }
63
64     @Override
65     public void update(Item item) {
66         zusammenAdaptor.updateItem(ZusammenUtil.createSessionContext(), new Id(item.getId()), mapToZusammenItemInfo(item));
67     }
68
69     private Item mapFromZusammenItem(com.amdocs.zusammen.datatypes.item.Item zusammenItem) {
70         if (zusammenItem == null) {
71             return null;
72         }
73         Item item = new Item();
74         item.setId(zusammenItem.getId().getValue());
75         item.setName(zusammenItem.getInfo().getName());
76         item.setDescription(zusammenItem.getInfo().getDescription());
77         zusammenItem.getInfo().getProperties().entrySet().forEach(property -> addPropertyToItem(property.getKey(), property.getValue(), item));
78         item.setCreationTime(zusammenItem.getCreationTime());
79         item.setModificationTime(zusammenItem.getModificationTime());
80         if (item.getStatus() == null) {
81             item.setStatus(ItemStatus.ACTIVE);
82             update(item);
83         }
84         return item;
85     }
86
87     private void addPropertyToItem(String propertyKey, Object propertyValue, Item item) {
88         switch (propertyKey) {
89             case InfoPropertyName.ITEM_TYPE:
90                 item.setType((String) propertyValue);
91                 break;
92             case InfoPropertyName.ITEM_OWNER:
93                 item.setOwner((String) propertyValue);
94                 break;
95             case InfoPropertyName.ITEM_STATUS:
96                 item.setStatus(ItemStatus.valueOf((String) propertyValue));
97                 break;
98             case InfoPropertyName.ITEM_VERSIONS_STATUSES:
99                 for (Map.Entry<String, Number> statusCounter : ((Map<String, Number>) propertyValue).entrySet()) {
100                     item.getVersionStatusCounters().put(VersionStatus.valueOf(statusCounter.getKey()), statusCounter.getValue().intValue());
101                 }
102                 break;
103             default:
104                 item.addProperty(propertyKey, propertyValue);
105         }
106     }
107
108     private Info mapToZusammenItemInfo(Item item) {
109         Info info = new Info();
110         info.setName(item.getName());
111         info.setDescription(item.getDescription());
112         info.addProperty(InfoPropertyName.ITEM_TYPE, item.getType());
113         info.addProperty(InfoPropertyName.ITEM_OWNER, item.getOwner());
114         if (item.getStatus() != null) {
115             info.addProperty(InfoPropertyName.ITEM_STATUS, item.getStatus());
116         }
117         info.addProperty(InfoPropertyName.ITEM_VERSIONS_STATUSES, item.getVersionStatusCounters());
118         item.getProperties().entrySet().forEach(property -> info.addProperty(property.getKey(), property.getValue()));
119         return info;
120     }
121
122     private static final class InfoPropertyName {
123
124         private static final String ITEM_TYPE = "item_type";
125         private static final String ITEM_VERSIONS_STATUSES = "item_versions_statuses";
126         private static final String ITEM_OWNER = "Owner";
127         private static final String ITEM_STATUS = "status";
128
129         private InfoPropertyName() {
130             throw new IllegalStateException("Constants class");
131         }
132     }
133 }