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 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;
31 import java.util.Collection;
33 import java.util.stream.Collectors;
35 public class ItemZusammenDaoImpl implements ItemDao {
37 private ZusammenAdaptor zusammenAdaptor;
39 public ItemZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
40 this.zusammenAdaptor = zusammenAdaptor;
44 public Collection<Item> list() {
45 return zusammenAdaptor.listItems(ZusammenUtil.createSessionContext())
46 .stream().map(this::mapFromZusammenItem)
47 .collect(Collectors.toList());
51 public Item get(Item item) {
52 return mapFromZusammenItem(
53 zusammenAdaptor.getItem(ZusammenUtil.createSessionContext(), new Id(item.getId())));
57 public Item create(Item item) {
58 Id itemId = zusammenAdaptor
59 .createItem(ZusammenUtil.createSessionContext(), mapToZusammenItemInfo(item));
60 item.setId(itemId.getValue());
65 public void delete(Item item){
66 zusammenAdaptor.deleteItem(ZusammenUtil.createSessionContext(),new Id(item.getId()));
70 public void update(Item item) {
72 .updateItem(ZusammenUtil.createSessionContext(), new Id(item.getId()),
73 mapToZusammenItemInfo(item));
76 private Item mapFromZusammenItem(com.amdocs.zusammen.datatypes.item.Item zusammenItem) {
77 if (zusammenItem == null) {
80 Item item = new Item();
81 item.setId(zusammenItem.getId().getValue());
82 item.setName(zusammenItem.getInfo().getName());
83 item.setDescription(zusammenItem.getInfo().getDescription());
85 zusammenItem.getInfo().getProperties().entrySet()
86 .forEach(property -> addPropertyToItem(property.getKey(), property.getValue(), item));
88 item.setCreationTime(zusammenItem.getCreationTime());
89 item.setModificationTime(zusammenItem.getModificationTime());
91 if(item.getStatus() == null){
92 item.setStatus(ItemStatus.ACTIVE);
99 private void addPropertyToItem(String propertyKey, Object propertyValue, Item item) {
100 switch (propertyKey) {
101 case InfoPropertyName.ITEM_TYPE:
102 item.setType((String) propertyValue);
104 case InfoPropertyName.ITEM_OWNER:
105 item.setOwner((String) propertyValue);
107 case InfoPropertyName.ITEM_STATUS:
108 item.setStatus(ItemStatus.valueOf((String)propertyValue));
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());
118 item.addProperty(propertyKey, propertyValue);
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());
131 info.addProperty(InfoPropertyName.ITEM_VERSIONS_STATUSES, item.getVersionStatusCounters());
132 item.getProperties().entrySet()
133 .forEach(property -> info.addProperty(property.getKey(), property.getValue()));
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";
143 private InfoPropertyName() {
144 throw new IllegalStateException("Constants class");