2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.versioning.dao.impl.zusammen;
22 import com.amdocs.zusammen.datatypes.Id;
23 import com.amdocs.zusammen.datatypes.item.Info;
24 import java.util.Collection;
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;
34 public class ItemZusammenDaoImpl implements ItemDao {
36 private ZusammenAdaptor zusammenAdaptor;
38 public ItemZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
39 this.zusammenAdaptor = zusammenAdaptor;
43 public Collection<Item> list() {
44 return zusammenAdaptor.listItems(ZusammenUtil.createSessionContext()).stream().map(this::mapFromZusammenItem).collect(Collectors.toList());
48 public Item get(Item item) {
49 return mapFromZusammenItem(zusammenAdaptor.getItem(ZusammenUtil.createSessionContext(), new Id(item.getId())));
53 public Item create(Item item) {
54 Id itemId = zusammenAdaptor.createItem(ZusammenUtil.createSessionContext(), mapToZusammenItemInfo(item));
55 item.setId(itemId.getValue());
60 public void delete(Item item) {
61 zusammenAdaptor.deleteItem(ZusammenUtil.createSessionContext(), new Id(item.getId()));
65 public void update(Item item) {
66 zusammenAdaptor.updateItem(ZusammenUtil.createSessionContext(), new Id(item.getId()), mapToZusammenItemInfo(item));
69 private Item mapFromZusammenItem(com.amdocs.zusammen.datatypes.item.Item zusammenItem) {
70 if (zusammenItem == null) {
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);
87 private void addPropertyToItem(String propertyKey, Object propertyValue, Item item) {
88 switch (propertyKey) {
89 case InfoPropertyName.ITEM_TYPE:
90 item.setType((String) propertyValue);
92 case InfoPropertyName.ITEM_OWNER:
93 item.setOwner((String) propertyValue);
95 case InfoPropertyName.ITEM_STATUS:
96 item.setStatus(ItemStatus.valueOf((String) propertyValue));
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());
104 item.addProperty(propertyKey, propertyValue);
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());
117 info.addProperty(InfoPropertyName.ITEM_VERSIONS_STATUSES, item.getVersionStatusCounters());
118 item.getProperties().entrySet().forEach(property -> info.addProperty(property.getKey(), property.getValue()));
122 private static final class InfoPropertyName {
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";
129 private InfoPropertyName() {
130 throw new IllegalStateException("Constants class");