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.Arrays;
25 import java.util.Collection;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29 import lombok.AllArgsConstructor;
31 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
32 import org.openecomp.core.zusammen.api.ZusammenUtil;
33 import org.openecomp.sdc.versioning.dao.ItemDao;
34 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
35 import org.openecomp.sdc.versioning.types.Item;
36 import org.openecomp.sdc.versioning.types.ItemStatus;
38 public class ItemZusammenDaoImpl implements ItemDao {
40 private final ZusammenAdaptor zusammenAdaptor;
42 public ItemZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
43 this.zusammenAdaptor = zusammenAdaptor;
47 public Collection<Item> list() {
48 return zusammenAdaptor.listItems(ZusammenUtil.createSessionContext()).stream().map(this::mapFromZusammenItem).collect(Collectors.toList());
52 public Item get(Item item) {
53 return mapFromZusammenItem(zusammenAdaptor.getItem(ZusammenUtil.createSessionContext(), new Id(item.getId())));
57 public Item create(Item item) {
58 Id itemId = zusammenAdaptor.createItem(ZusammenUtil.createSessionContext(), mapToZusammenItemInfo(item));
59 item.setId(itemId.getValue());
64 public void delete(Item item) {
65 zusammenAdaptor.deleteItem(ZusammenUtil.createSessionContext(), new Id(item.getId()));
69 public void update(Item item) {
70 zusammenAdaptor.updateItem(ZusammenUtil.createSessionContext(), new Id(item.getId()), mapToZusammenItemInfo(item));
73 private Item mapFromZusammenItem(com.amdocs.zusammen.datatypes.item.Item zusammenItem) {
74 if (zusammenItem == null) {
77 Item item = new Item();
78 item.setId(zusammenItem.getId().getValue());
79 item.setName(zusammenItem.getInfo().getName());
80 item.setDescription(zusammenItem.getInfo().getDescription());
81 zusammenItem.getInfo().getProperties().forEach((key, value) -> addPropertyToItem(key, value, item));
82 item.setCreationTime(zusammenItem.getCreationTime());
83 item.setModificationTime(zusammenItem.getModificationTime());
84 if (item.getStatus() == null) {
85 item.setStatus(ItemStatus.ACTIVE);
91 private void addPropertyToItem(String propertyKey, Object propertyValue, Item item) {
92 final ItemInfoProperty itemInfoProperty = ItemInfoProperty.findByName(propertyKey).orElse(null);
93 if (itemInfoProperty == null) {
94 item.addProperty(propertyKey, propertyValue);
98 switch (itemInfoProperty) {
100 item.setType((String) propertyValue);
103 item.setOwner((String) propertyValue);
106 item.setStatus(ItemStatus.valueOf((String) propertyValue));
108 case ITEM_VERSIONS_STATUSES:
109 for (Map.Entry<String, Number> statusCounter : ((Map<String, Number>) propertyValue).entrySet()) {
110 item.getVersionStatusCounters().put(VersionStatus.valueOf(statusCounter.getKey()), statusCounter.getValue().intValue());
114 item.addProperty(propertyKey, propertyValue);
118 private Info mapToZusammenItemInfo(Item item) {
119 Info info = new Info();
120 info.setName(item.getName());
121 info.setDescription(item.getDescription());
122 info.addProperty(ItemInfoProperty.ITEM_TYPE.getName(), item.getType());
123 info.addProperty(ItemInfoProperty.ITEM_OWNER.getName(), item.getOwner());
124 if (item.getStatus() != null) {
125 info.addProperty(ItemInfoProperty.ITEM_STATUS.getName(), item.getStatus());
127 info.addProperty(ItemInfoProperty.ITEM_VERSIONS_STATUSES.getName(), item.getVersionStatusCounters());
128 item.getProperties().forEach(info::addProperty);
134 public enum ItemInfoProperty {
135 ITEM_TYPE("item_type"),
136 ITEM_VERSIONS_STATUSES("item_versions_statuses"),
138 ITEM_STATUS("status");
140 private final String name;
142 public static Optional<ItemInfoProperty> findByName(final String name) {
143 return Arrays.stream(values()).filter(itemInfoProperty -> itemInfoProperty.getName().equals(name)).findFirst();