[SDC-29] rebase continue work to align source
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-core / src / main / java / org / openecomp / sdc / vendorlicense / dao / impl / zusammen / LicenseAgreementDaoZusammenImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
22
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
25 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
26 import com.amdocs.zusammen.datatypes.Id;
27 import com.amdocs.zusammen.datatypes.SessionContext;
28 import com.amdocs.zusammen.datatypes.item.Action;
29 import com.amdocs.zusammen.datatypes.item.ElementContext;
30 import com.amdocs.zusammen.datatypes.item.Info;
31 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
32 import org.openecomp.core.zusammen.api.ZusammenUtil;
33 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
34 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
35 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
36 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
37 import org.openecomp.sdc.versioning.dao.types.Version;
38
39 import java.util.Collection;
40 import java.util.Map;
41 import java.util.Optional;
42 import java.util.Set;
43 import java.util.stream.Collectors;
44
45
46 public class LicenseAgreementDaoZusammenImpl implements LicenseAgreementDao {
47
48   private ZusammenAdaptor zusammenAdaptor;
49
50   public LicenseAgreementDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
51     this.zusammenAdaptor = zusammenAdaptor;
52   }
53
54   @Override
55   public void registerVersioning(String versionableEntityType) {
56     //no need
57   }
58
59   @Override
60   public void create(LicenseAgreementEntity licenseAgreement) {
61     ZusammenElement licenseAgreementElement =
62         buildLicenseAgreementElement(licenseAgreement, Action.CREATE);
63     ZusammenElement licenseAgreementsElement =
64         VlmZusammenUtil.buildStructuralElement(StructureElement.LicenseAgreements, null);
65     licenseAgreementsElement.addSubElement(licenseAgreementElement);
66
67     SessionContext context = ZusammenUtil.createSessionContext();
68     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
69     Optional<Element> savedElement = zusammenAdaptor.saveElement(context, new ElementContext(itemId,
70             VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
71         licenseAgreementsElement, "Create license agreement");
72     savedElement.ifPresent(element -> licenseAgreement
73         .setId(element.getSubElements().iterator().next().getElementId().getValue()));
74   }
75
76   @Override
77   public void update(LicenseAgreementEntity licenseAgreement) {
78     ZusammenElement licenseAgreementElement =
79         buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
80
81     SessionContext context = ZusammenUtil.createSessionContext();
82     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
83     zusammenAdaptor.saveElement(context, new ElementContext(itemId,
84             VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
85         licenseAgreementElement,
86         String.format("Update license agreement with id %s", licenseAgreement.getId()));
87   }
88
89   @Override
90   public LicenseAgreementEntity get(LicenseAgreementEntity licenseAgreement) {
91     SessionContext context = ZusammenUtil.createSessionContext();
92     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
93     ElementContext elementContext = new ElementContext(itemId,
94         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
95         VlmZusammenUtil.getVersionTag(licenseAgreement.getVersion()));
96
97     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()))
98         .map(elementInfo -> mapElementInfoToLicenseAgreement(
99             licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion(), elementInfo))
100         .orElse(null);
101   }
102
103   @Override
104   public void delete(LicenseAgreementEntity licenseAgreement) {
105     SessionContext context = ZusammenUtil.createSessionContext();
106     ZusammenElement zusammenElement = new ZusammenElement();
107     zusammenElement.setAction(Action.DELETE);
108     zusammenElement.setElementId(new Id(licenseAgreement.getId()));
109
110     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
111     ElementContext elementContext = new ElementContext(itemId,
112         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
113     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
114         "delete license agreement. id:" + licenseAgreement.getId() + ".");
115   }
116
117
118   @Override
119   public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity licenseAgreement) {
120     SessionContext context = ZusammenUtil.createSessionContext();
121     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
122     ElementContext elementContext = new ElementContext(itemId,
123         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
124         VlmZusammenUtil.getVersionTag(licenseAgreement.getVersion()));
125
126     return zusammenAdaptor
127         .listElementsByName(context, elementContext, null,
128             StructureElement.LicenseAgreements.name())
129         .stream().map(elementInfo -> mapElementInfoToLicenseAgreement(
130             licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion(), elementInfo))
131         .collect(Collectors.toList());
132   }
133
134   @Override
135   public long count(LicenseAgreementEntity licenseAgreement) {
136     SessionContext context = ZusammenUtil.createSessionContext();
137     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
138     ElementContext elementContext = new ElementContext(itemId,
139         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
140         VlmZusammenUtil.getVersionTag(licenseAgreement.getVersion()));
141
142     return zusammenAdaptor.listElementsByName(context, elementContext, null,
143         StructureElement.LicenseAgreements.name())
144         .size();
145   }
146
147   @Override
148   public void deleteAll(LicenseAgreementEntity entity) {
149     //not supported
150   }
151
152   @Override
153   public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
154     SessionContext context = ZusammenUtil.createSessionContext();
155     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
156     ElementContext elementContext = new ElementContext(itemId,
157         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
158
159     Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
160         elementContext, new Id(licenseAgreement.getId()));
161     if (elementInfo.isPresent()) {
162       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
163       zusammenElement.setAction(Action.UPDATE);
164       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
165           .filter(relation -> !featureGroupId.equals(relation.getEdge2().getElementId().getValue()))
166           .collect(Collectors.toList()));
167       zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "remove feature group");
168     }
169   }
170
171   @Override
172   public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
173                                                    Set<String> addedFeatureGroupIds,
174                                                    Set<String> removedFeatureGroupIds) {
175     ZusammenElement licenseAgreementElement =
176         buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
177
178     SessionContext context = ZusammenUtil.createSessionContext();
179     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
180     ElementContext elementContext = new ElementContext(itemId,
181         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
182
183     Optional<ElementInfo> elementInfo =
184         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()));
185     if (elementInfo.isPresent()) {
186       LicenseAgreementEntity currentLicenseAgreement =
187           mapElementInfoToLicenseAgreement(licenseAgreement.getId(), licenseAgreement.getVersion(),
188               elementInfo.get());
189
190       if (! (removedFeatureGroupIds == null) ) {
191         currentLicenseAgreement.getFeatureGroupIds().removeAll(removedFeatureGroupIds);
192       }
193
194       if (! (addedFeatureGroupIds == null)) {
195         currentLicenseAgreement.getFeatureGroupIds().addAll(addedFeatureGroupIds);
196       }
197       licenseAgreementElement.setRelations(currentLicenseAgreement.getFeatureGroupIds().stream()
198           .map(relation -> VlmZusammenUtil
199               .createRelation(RelationType.LicenseAgreementToFeatureGroup, relation))
200           .collect(Collectors.toList()));
201       zusammenAdaptor.saveElement(context, elementContext, licenseAgreementElement,
202           "update license agreement");
203     }
204   }
205
206   private LicenseAgreementEntity mapElementInfoToLicenseAgreement(String vlmId, Version version,
207                                                                   ElementInfo elementInfo) {
208     LicenseAgreementEntity licenseAgreement =
209         new LicenseAgreementEntity(vlmId, version, elementInfo.getId().getValue());
210     licenseAgreement.setName(elementInfo.getInfo().getName());
211     licenseAgreement.setDescription(elementInfo.getInfo().getDescription());
212
213     licenseAgreement
214         .setLicenseTerm(getCoiceOrOther(elementInfo.getInfo().getProperty("licenseTerm")));
215     licenseAgreement.setRequirementsAndConstrains(
216         elementInfo.getInfo().getProperty("requirementsAndConstrains"));
217     if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
218       licenseAgreement.setFeatureGroupIds(elementInfo.getRelations().stream()
219           .map(relation -> relation.getEdge2().getElementId().getValue())
220           .collect(Collectors.toSet()));
221     }
222     return licenseAgreement;
223   }
224
225   private ChoiceOrOther<LicenseTerm> getCoiceOrOther(Map licenseTerm) {
226     return new ChoiceOrOther(LicenseTerm.valueOf((String) licenseTerm.get("choice")),
227         (String) licenseTerm.get("other"));
228   }
229
230
231   private ZusammenElement buildLicenseAgreementElement(LicenseAgreementEntity licenseAgreement,
232                                                        Action action) {
233     ZusammenElement licenseAgreementElement = new ZusammenElement();
234     licenseAgreementElement.setAction(action);
235     if (licenseAgreement.getId() != null) {
236       licenseAgreementElement.setElementId(new Id(licenseAgreement.getId()));
237     }
238     Info info = new Info();
239     info.setName(licenseAgreement.getName());
240     info.setDescription(licenseAgreement.getDescription());
241     info.addProperty("licenseTerm", licenseAgreement.getLicenseTerm());
242     info.addProperty("requirementsAndConstrains", licenseAgreement.getRequirementsAndConstrains());
243     licenseAgreementElement.setInfo(info);
244
245     if (licenseAgreement.getFeatureGroupIds() != null &&
246         licenseAgreement.getFeatureGroupIds().size() > 0) {
247       licenseAgreementElement.setRelations(licenseAgreement.getFeatureGroupIds().stream()
248           .map(rel -> VlmZusammenUtil
249               .createRelation(RelationType.LicenseAgreementToFeatureGroup, rel))
250           .collect(Collectors.toList()));
251     }
252     return licenseAgreementElement;
253   }
254 }