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 / VersionCollaborationStore.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.Space;
6 import com.amdocs.zusammen.datatypes.item.Action;
7 import com.amdocs.zusammen.datatypes.itemversion.Tag;
8 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext;
9 import com.amdocs.zusammen.sdk.collaboration.types.CollaborationElementChange;
10 import com.amdocs.zusammen.sdk.collaboration.types.CollaborationMergeChange;
11 import org.openecomp.core.zusammen.plugin.ZusammenPluginUtil;
12 import org.openecomp.core.zusammen.plugin.dao.ElementRepository;
13 import org.openecomp.core.zusammen.plugin.dao.ElementRepositoryFactory;
14 import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
15
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.stream.Collectors;
19
20 import static org.openecomp.core.zusammen.plugin.ZusammenPluginConstants.ROOT_ELEMENTS_PARENT_ID;
21 import static org.openecomp.core.zusammen.plugin.ZusammenPluginUtil.getSpaceName;
22
23 public class VersionCollaborationStore {
24
25   public void tagItemVersion(SessionContext context, Id itemId, Id versionId, Id changeId,
26                              Tag tag) {
27     if (changeId != null) {
28       throw new UnsupportedOperationException(
29           "In this plugin implementation tag is supported only on versionId");
30     }
31     String space = getSpaceName(context, Space.PRIVATE);
32     ElementEntityContext targetContext = new ElementEntityContext(space, itemId, versionId);
33     targetContext.setChangeRef(tag.getName());
34     copyElements(context, new ElementEntityContext(space, itemId, versionId), targetContext,
35         getElementRepository(context));
36   }
37
38   public CollaborationMergeChange resetItemVersionHistory(SessionContext context, Id itemId,
39                                                           Id versionId, String changeRef) {
40     ElementRepository elementRepository = getElementRepository(context);
41
42     String spaceName = getSpaceName(context, Space.PRIVATE);
43     ElementEntityContext versionContext = new ElementEntityContext(spaceName, itemId, versionId);
44
45     Collection<ElementEntity> deletedElements =
46         deleteElements(context, versionContext, elementRepository);
47
48     ElementEntityContext changeRefContext = new ElementEntityContext(spaceName, itemId, versionId);
49     changeRefContext.setChangeRef(changeRef);
50
51     Collection<ElementEntity> createdElements =
52         copyElements(context, changeRefContext, versionContext, elementRepository);
53
54     // TODO: 4/19/2017 version change...
55     return createCollaborationMergeChange(versionContext, deletedElements, createdElements);
56   }
57
58   private Collection<ElementEntity> deleteElements(SessionContext context,
59                                                    ElementEntityContext elementContext,
60                                                    ElementRepository elementRepository) {
61     Collection<ElementEntity> elements = elementRepository.list(context, elementContext);
62     elements.forEach(element -> elementRepository
63         .delete(context, elementContext, new ElementEntity(element.getId())));
64     elementRepository.delete(context, elementContext, new ElementEntity(ROOT_ELEMENTS_PARENT_ID));
65     return elements;
66   }
67
68   private Collection<ElementEntity> copyElements(SessionContext context,
69                                                  ElementEntityContext sourceElementContext,
70                                                  ElementEntityContext targetElementContext,
71                                                  ElementRepository elementRepository) {
72     Collection<ElementEntity> elements = elementRepository.list(context, sourceElementContext);
73     elements.forEach(elementEntity ->
74         elementRepository.create(context, targetElementContext, elementEntity));
75     return elements;
76   }
77
78   private CollaborationMergeChange createCollaborationMergeChange(
79       ElementEntityContext versionContext,
80       Collection<ElementEntity> deletedElements,
81       Collection<ElementEntity> createdElements) {
82     CollaborationMergeChange mergeChange = new CollaborationMergeChange();
83     mergeChange.getChangedElements().addAll(
84         convertToCollaborationElementChanges(versionContext, deletedElements, Action.DELETE));
85     mergeChange.getChangedElements().addAll(
86         convertToCollaborationElementChanges(versionContext, createdElements, Action.CREATE));
87     return mergeChange;
88   }
89
90   private List<CollaborationElementChange> convertToCollaborationElementChanges(
91       ElementEntityContext elementContext, Collection<ElementEntity> changedElements,
92       Action action) {
93     return changedElements.stream()
94         .map(element -> convertToCollaborationElementChange(element, elementContext, action))
95         .collect(Collectors.toList());
96   }
97
98   private CollaborationElementChange convertToCollaborationElementChange(
99       ElementEntity elementEntity, ElementEntityContext elementContext, Action action) {
100     CollaborationElementChange elementChange = new CollaborationElementChange();
101     elementChange
102         .setElement(ZusammenPluginUtil.getCollaborationElement(elementContext, elementEntity));
103     elementChange.setAction(action);
104     return elementChange;
105   }
106
107   protected ElementRepository getElementRepository(SessionContext context) {
108     return ElementRepositoryFactory.getInstance().createInterface(context);
109   }
110 }