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