1 package org.openecomp.core.zusammen.plugin.collaboration;
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;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Optional;
18 import static org.openecomp.core.zusammen.plugin.ZusammenPluginUtil.getCollaborationElement;
19 import static org.openecomp.core.zusammen.plugin.ZusammenPluginUtil.getSpaceName;
22 public class ElementCollaborationStore {
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";
28 public Collection<CollaborationElement> listElements(SessionContext context,
29 ElementContext elementContext,
31 ElementEntityContext elementEntityContext =
32 new ElementEntityContext(ZusammenPluginUtil.getPrivateSpaceName(context), elementContext);
34 if (elementId == null) {
35 elementId = ZusammenPluginConstants.ROOT_ELEMENTS_PARENT_ID;
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<>();
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))
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));
61 public CollaborationElement getElement(SessionContext context, ElementContext elementContext,
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))
71 public void createElement(SessionContext context, CollaborationElement element) {
72 getElementRepository(context)
74 new ElementEntityContext(getSpaceName(context, element.getSpace()),
75 element.getItemId(), element.getVersionId()),
76 ZusammenPluginUtil.getElementEntity(element));
79 public void updateElement(SessionContext context, CollaborationElement element) {
80 getElementRepository(context)
82 new ElementEntityContext(getSpaceName(context, element.getSpace()),
83 element.getItemId(), element.getVersionId()),
84 ZusammenPluginUtil.getElementEntity(element));
87 public void deleteElement(SessionContext context, CollaborationElement element) {
88 deleteElementHierarchy(getElementRepository(context),
90 new ElementEntityContext(getSpaceName(context, element.getSpace()),
91 element.getItemId(), element.getVersionId()),
92 ZusammenPluginUtil.getElementEntity(element));
95 public boolean checkHealth(SessionContext sessionContext) {
96 return getElementRepository(sessionContext).checkHealth(sessionContext);
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()) {
107 retrieved.get().getSubElementIds().stream()
108 .map(ElementEntity::new)
109 .forEach(subElementEntity -> deleteElementHierarchy(
110 elementRepository, context, elementEntityContext, subElementEntity));
112 // only for the first one the parentId will populated (so it'll be removed from its parent)
113 elementRepository.delete(context, elementEntityContext, elementEntity);
116 protected ElementRepository getElementRepository(SessionContext context) {
117 return ElementRepositoryFactory.getInstance().createInterface(context);