2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
19 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
20 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
21 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
22 import com.amdocs.zusammen.datatypes.Id;
23 import com.amdocs.zusammen.datatypes.SessionContext;
24 import com.amdocs.zusammen.datatypes.item.Action;
25 import com.amdocs.zusammen.datatypes.item.ElementContext;
26 import com.amdocs.zusammen.datatypes.item.Info;
27 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
28 import org.openecomp.sdc.datatypes.model.ElementType;
29 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
30 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToLicenseAgreementConvertor;
31 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
32 import org.openecomp.types.ElementPropertyName;
34 import java.util.Collection;
35 import java.util.Optional;
37 import java.util.stream.Collectors;
39 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
40 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
41 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
44 public class LicenseAgreementDaoZusammenImpl implements LicenseAgreementDao {
46 private ZusammenAdaptor zusammenAdaptor;
48 public LicenseAgreementDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
49 this.zusammenAdaptor = zusammenAdaptor;
53 public void registerVersioning(String versionableEntityType) {
58 public void create(LicenseAgreementEntity licenseAgreement) {
59 ZusammenElement licenseAgreementElement =
60 buildLicenseAgreementElement(licenseAgreement, Action.CREATE);
61 ZusammenElement licenseAgreementsElement =
62 buildStructuralElement(ElementType.LicenseAgreements, Action.IGNORE);
63 licenseAgreementsElement.addSubElement(licenseAgreementElement);
65 SessionContext context = createSessionContext();
66 Element licenseAgreementsSavedElement = zusammenAdaptor.saveElement(context,
67 new ElementContext(licenseAgreement.getVendorLicenseModelId(),
68 licenseAgreement.getVersion().getId()), licenseAgreementsElement,
69 "Create license agreement");
71 .setId(licenseAgreementsSavedElement.getSubElements().iterator().next().getElementId()
76 public void update(LicenseAgreementEntity licenseAgreement) {
77 ZusammenElement licenseAgreementElement =
78 buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
80 SessionContext context = createSessionContext();
81 zusammenAdaptor.saveElement(context,
82 new ElementContext(licenseAgreement.getVendorLicenseModelId(),
83 licenseAgreement.getVersion().getId()), licenseAgreementElement,
84 String.format("Update license agreement with id %s", licenseAgreement.getId()));
88 public LicenseAgreementEntity get(LicenseAgreementEntity licenseAgreement) {
89 SessionContext context = createSessionContext();
90 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
91 licenseAgreement.getVersion().getId());
92 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
93 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()))
95 LicenseAgreementEntity entity = convertor.convert(elementInfo);
96 entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
97 entity.setVersion(licenseAgreement.getVersion());
104 public void delete(LicenseAgreementEntity licenseAgreement) {
105 ZusammenElement zusammenElement = buildElement(new Id(licenseAgreement.getId()), Action.DELETE);
107 SessionContext context = createSessionContext();
108 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
109 licenseAgreement.getVersion().getId());
110 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
111 "delete license agreement. id:" + licenseAgreement.getId() + ".");
116 public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity licenseAgreement) {
117 SessionContext context = createSessionContext();
118 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
119 licenseAgreement.getVersion().getId());
120 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
121 return zusammenAdaptor
122 .listElementsByName(context, elementContext, null,
123 ElementType.LicenseAgreements.name())
124 .stream().map(elementInfo -> {
125 LicenseAgreementEntity entity = convertor.convert(elementInfo);
126 entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
127 entity.setVersion(licenseAgreement.getVersion());
130 .collect(Collectors.toList());
134 public long count(LicenseAgreementEntity licenseAgreement) {
135 SessionContext context = createSessionContext();
136 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
137 licenseAgreement.getVersion().getId());
139 return zusammenAdaptor.listElementsByName(context, elementContext, null,
140 ElementType.LicenseAgreements.name())
145 public void deleteAll(LicenseAgreementEntity entity) {
150 public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
151 SessionContext context = createSessionContext();
152 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
153 licenseAgreement.getVersion().getId());
155 Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
156 elementContext, new Id(licenseAgreement.getId()));
157 if (elementInfo.isPresent()) {
158 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
159 zusammenElement.setAction(Action.UPDATE);
160 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
161 .filter(relation -> !featureGroupId.equals(relation.getEdge2().getElementId().getValue()))
162 .collect(Collectors.toList()));
163 zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "remove feature group");
168 public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
169 Set<String> addedFeatureGroupIds,
170 Set<String> removedFeatureGroupIds) {
171 ZusammenElement licenseAgreementElement =
172 buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
174 SessionContext context = createSessionContext();
175 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
176 licenseAgreement.getVersion().getId());
177 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
178 Optional<ElementInfo> elementInfo =
179 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()));
180 if (elementInfo.isPresent()) {
181 LicenseAgreementEntity currentLicenseAgreement =
182 convertor.convert(elementInfo.get());
183 currentLicenseAgreement.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
184 currentLicenseAgreement.setVersion(licenseAgreement.getVersion());
185 if (!(removedFeatureGroupIds == null)) {
186 currentLicenseAgreement.getFeatureGroupIds().removeAll(removedFeatureGroupIds);
189 if (!(addedFeatureGroupIds == null)) {
190 currentLicenseAgreement.getFeatureGroupIds().addAll(addedFeatureGroupIds);
192 licenseAgreementElement.setRelations(currentLicenseAgreement.getFeatureGroupIds().stream()
193 .map(relation -> VlmZusammenUtil
194 .createRelation(RelationType.LicenseAgreementToFeatureGroup, relation))
195 .collect(Collectors.toList()));
196 zusammenAdaptor.saveElement(context, elementContext, licenseAgreementElement,
197 "update license agreement");
201 private ZusammenElement buildLicenseAgreementElement(LicenseAgreementEntity licenseAgreement,
203 ZusammenElement licenseAgreementElement =
204 buildElement(licenseAgreement.getId() == null ? null : new Id(licenseAgreement.getId()),
206 Info info = new Info();
207 info.setName(licenseAgreement.getName());
208 info.setDescription(licenseAgreement.getDescription());
209 info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseAgreement);
210 info.addProperty("licenseTerm", licenseAgreement.getLicenseTerm());
211 info.addProperty("requirementsAndConstrains", licenseAgreement.getRequirementsAndConstrains());
212 licenseAgreementElement.setInfo(info);
214 if (licenseAgreement.getFeatureGroupIds() != null &&
215 !licenseAgreement.getFeatureGroupIds().isEmpty()) {
216 licenseAgreementElement.setRelations(licenseAgreement.getFeatureGroupIds().stream()
217 .map(rel -> VlmZusammenUtil
218 .createRelation(RelationType.LicenseAgreementToFeatureGroup, rel))
219 .collect(Collectors.toList()));
221 return licenseAgreementElement;