fix incorrect dependency
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-zusammen-lib / openecomp-zusammen-plugin / src / main / java / org / openecomp / core / zusammen / plugin / collaboration / ElementCollaborationStore.java
1 package org.openecomp.core.zusammen.plugin.collaboration;
2
3 import com.amdocs.zusammen.datatypes.Id;
4 import com.amdocs.zusammen.datatypes.SessionContext;
5 import com.amdocs.zusammen.datatypes.item.ElementContext;
6 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext;
7 import com.amdocs.zusammen.sdk.collaboration.types.CollaborationElement;
8 import org.openecomp.core.zusammen.plugin.ZusammenPluginConstants;
9 import org.openecomp.core.zusammen.plugin.ZusammenPluginUtil;
10 import org.openecomp.core.zusammen.plugin.dao.ElementRepository;
11 import org.openecomp.core.zusammen.plugin.dao.ElementRepositoryFactory;
12 import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Optional;
17
18 import static org.openecomp.core.zusammen.plugin.ZusammenPluginUtil.getCollaborationElement;
19 import static org.openecomp.core.zusammen.plugin.ZusammenPluginUtil.getSpaceName;
20
21
22 public class ElementCollaborationStore {
23
24   private static final String SUB_ELEMENT_NOT_EXIST_ERROR_MSG =
25       "List sub elements error: item %s, version %s - " +
26           "element %s, which appears as sub element of element %s, does not exist";
27
28   public Collection<CollaborationElement> listElements(SessionContext context,
29                                                        ElementContext elementContext,
30                                                        Id elementId) {
31     ElementEntityContext elementEntityContext =
32         new ElementEntityContext(ZusammenPluginUtil.getPrivateSpaceName(context), elementContext);
33
34     if (elementId == null) {
35       elementId = ZusammenPluginConstants.ROOT_ELEMENTS_PARENT_ID;
36     }
37
38     ElementRepository elementRepository = getElementRepository(context);
39     String elementIdValue = elementId.getValue();
40     String versionIdValue = elementContext.getChangeRef() == null
41         ? elementContext.getVersionId().getValue()
42         : elementContext.getChangeRef();
43     Collection<CollaborationElement> subElements = new ArrayList<>();
44
45     Optional<ElementEntity> element =
46         elementRepository.get(context, elementEntityContext, new ElementEntity(elementId));
47     if (element.isPresent() && element.get().getSubElementIds() != null) {
48       for (Id subElementId : element.get().getSubElementIds()) {
49         ElementEntity subElement =
50             elementRepository.get(context, elementEntityContext, new ElementEntity(subElementId))
51                 .orElseThrow(
52                     () -> new IllegalStateException(String.format(SUB_ELEMENT_NOT_EXIST_ERROR_MSG,
53                         elementContext.getItemId().getValue(), versionIdValue,
54                         subElementId.getValue(), elementIdValue)));
55         subElements.add(getCollaborationElement(elementEntityContext, subElement));
56       }
57     }
58     return subElements;
59   }
60
61   public CollaborationElement getElement(SessionContext context, ElementContext elementContext,
62                                          Id elementId) {
63     ElementEntityContext elementEntityContext =
64         new ElementEntityContext(ZusammenPluginUtil.getPrivateSpaceName(context), elementContext);
65     return getElementRepository(context)
66         .get(context, elementEntityContext, new ElementEntity(elementId))
67         .map(elementEntity -> getCollaborationElement(elementEntityContext, elementEntity))
68         .orElse(null);
69   }
70
71   public void createElement(SessionContext context, CollaborationElement element) {
72     getElementRepository(context)
73         .create(context,
74             new ElementEntityContext(getSpaceName(context, element.getSpace()),
75                 element.getItemId(), element.getVersionId()),
76             ZusammenPluginUtil.getElementEntity(element));
77   }
78
79   public void updateElement(SessionContext context, CollaborationElement element) {
80     getElementRepository(context)
81         .update(context,
82             new ElementEntityContext(getSpaceName(context, element.getSpace()),
83                 element.getItemId(), element.getVersionId()),
84             ZusammenPluginUtil.getElementEntity(element));
85   }
86
87   public void deleteElement(SessionContext context, CollaborationElement element) {
88     deleteElementHierarchy(getElementRepository(context),
89         context,
90         new ElementEntityContext(getSpaceName(context, element.getSpace()),
91             element.getItemId(), element.getVersionId()),
92         ZusammenPluginUtil.getElementEntity(element));
93   }
94
95   public boolean checkHealth(SessionContext sessionContext) {
96     return getElementRepository(sessionContext).checkHealth(sessionContext);
97   }
98
99   private void deleteElementHierarchy(ElementRepository elementRepository, SessionContext context,
100                                       ElementEntityContext elementEntityContext,
101                                       ElementEntity elementEntity) {
102     Optional<ElementEntity> retrieved =
103         elementRepository.get(context, elementEntityContext, elementEntity);
104     if (!retrieved.isPresent()) {
105       return;
106     }
107     retrieved.get().getSubElementIds().stream()
108         .map(ElementEntity::new)
109         .forEach(subElementEntity -> deleteElementHierarchy(
110             elementRepository, context, elementEntityContext, subElementEntity));
111
112     // only for the first one the parentId will populated (so it'll be removed from its parent)
113     elementRepository.delete(context, elementEntityContext, elementEntity);
114   }
115
116   protected ElementRepository getElementRepository(SessionContext context) {
117     return ElementRepositoryFactory.getInstance().createInterface(context);
118   }
119 }