[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-zusammen-lib / openecomp-zusammen-plugin / src / main / java / org / openecomp / core / zusammen / plugin / ZusammenPluginUtil.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.core.zusammen.plugin;
18
19 import com.amdocs.zusammen.datatypes.Id;
20 import com.amdocs.zusammen.datatypes.SessionContext;
21 import com.amdocs.zusammen.datatypes.Space;
22 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext;
23 import com.amdocs.zusammen.sdk.collaboration.types.CollaborationElement;
24 import com.amdocs.zusammen.utils.fileutils.FileUtils;
25 import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
26
27 import java.io.ByteArrayInputStream;
28 import java.nio.ByteBuffer;
29
30 public class ZusammenPluginUtil {
31
32   public static String getSpaceName(SessionContext context, Space space) {
33     switch (space) {
34       case PUBLIC:
35         return ZusammenPluginConstants.PUBLIC_SPACE;
36       case PRIVATE:
37         return ZusammenPluginUtil.getPrivateSpaceName(context);
38       default:
39         throw new IllegalArgumentException(String.format("Space %s is not supported.", space));
40     }
41   }
42
43   public static String getPrivateSpaceName(SessionContext context) {
44     return context.getUser().getUserName();
45   }
46
47   public static ElementEntity getElementEntity(CollaborationElement element) {
48     ElementEntity elementEntity = new ElementEntity(element.getId());
49     elementEntity.setNamespace(element.getNamespace());
50     elementEntity.setParentId(element.getParentId() == null
51         ? ZusammenPluginConstants.ROOT_ELEMENTS_PARENT_ID
52         : element.getParentId());
53     elementEntity.setInfo(element.getInfo());
54     elementEntity.setRelations(element.getRelations());
55     if (element.getData() != null) {
56       elementEntity.setData(ByteBuffer.wrap(FileUtils.toByteArray(element.getData())));
57     }
58     if (element.getSearchableData() != null) {
59       elementEntity.setSearchableData(
60           ByteBuffer.wrap(FileUtils.toByteArray(element.getSearchableData())));
61     }
62     if (element.getVisualization() != null) {
63       elementEntity.setVisualization(
64           ByteBuffer.wrap(FileUtils.toByteArray(element.getVisualization())));
65     }
66     return elementEntity;
67   }
68
69   public static CollaborationElement getCollaborationElement(
70       ElementEntityContext elementEntityContext, ElementEntity elementEntity) {
71     Id parentId =
72         ZusammenPluginConstants.ROOT_ELEMENTS_PARENT_ID.equals(elementEntity.getParentId())
73             ? null
74             : elementEntity.getParentId();
75     CollaborationElement element = new CollaborationElement(elementEntityContext.getItemId(),
76         elementEntityContext.getVersionId(), elementEntity.getNamespace(), elementEntity.getId());
77
78     element.setParentId(parentId);
79     element.setInfo(elementEntity.getInfo());
80     element.setRelations(elementEntity.getRelations());
81
82     if (elementEntity.getData() != null) {
83       element.setData(new ByteArrayInputStream(elementEntity.getData().array()));
84     }
85     if (elementEntity.getSearchableData() != null) {
86       element.setSearchableData(
87           new ByteArrayInputStream(elementEntity.getSearchableData().array()));
88     }
89     if (elementEntity.getVisualization() != null) {
90       element.setVisualization(new ByteArrayInputStream(elementEntity.getVisualization().array()));
91     }
92     element.setSubElements(elementEntity.getSubElementIds());
93     return element;
94   }
95 }