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 lombok.AllArgsConstructor;
28 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
29 import org.openecomp.sdc.datatypes.model.ElementType;
30 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
31 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToLicenseAgreementConvertor;
32 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
33 import org.openecomp.types.ElementPropertyName;
35 import java.util.Collection;
36 import java.util.Optional;
38 import java.util.stream.Collectors;
40 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
43 public class LicenseAgreementDaoZusammenImpl implements LicenseAgreementDao {
45 private ZusammenAdaptor zusammenAdaptor;
48 public void registerVersioning(String versionableEntityType) {
53 public void create(LicenseAgreementEntity licenseAgreement) {
54 ZusammenElement licenseAgreementElement =
55 buildLicenseAgreementElement(licenseAgreement, Action.CREATE);
56 ZusammenElement licenseAgreementsElement =
57 buildStructuralElement(ElementType.LicenseAgreements, Action.IGNORE);
58 licenseAgreementsElement.addSubElement(licenseAgreementElement);
60 SessionContext context = createSessionContext();
61 Element licenseAgreementsSavedElement = zusammenAdaptor.saveElement(context,
62 new ElementContext(licenseAgreement.getVendorLicenseModelId(),
63 licenseAgreement.getVersion().getId()), licenseAgreementsElement,
64 "Create license agreement");
66 .setId(licenseAgreementsSavedElement.getSubElements().iterator().next().getElementId()
71 public void update(LicenseAgreementEntity licenseAgreement) {
72 ZusammenElement licenseAgreementElement =
73 buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
75 SessionContext context = createSessionContext();
76 zusammenAdaptor.saveElement(context,
77 new ElementContext(licenseAgreement.getVendorLicenseModelId(),
78 licenseAgreement.getVersion().getId()), licenseAgreementElement,
79 String.format("Update license agreement with id %s", licenseAgreement.getId()));
83 public LicenseAgreementEntity get(LicenseAgreementEntity licenseAgreement) {
84 SessionContext context = createSessionContext();
85 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
86 licenseAgreement.getVersion().getId());
87 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
88 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()))
90 LicenseAgreementEntity entity = convertor.convert(elementInfo);
91 entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
92 entity.setVersion(licenseAgreement.getVersion());
99 public void delete(LicenseAgreementEntity licenseAgreement) {
100 ZusammenElement zusammenElement = buildElement(new Id(licenseAgreement.getId()), Action.DELETE);
102 SessionContext context = createSessionContext();
103 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
104 licenseAgreement.getVersion().getId());
105 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
106 "delete license agreement. id:" + licenseAgreement.getId() + ".");
111 public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity licenseAgreement) {
112 SessionContext context = createSessionContext();
113 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
114 licenseAgreement.getVersion().getId());
115 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
116 return zusammenAdaptor
117 .listElementsByName(context, elementContext, null,
118 ElementType.LicenseAgreements.name())
119 .stream().map(elementInfo -> {
120 LicenseAgreementEntity entity = convertor.convert(elementInfo);
121 entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
122 entity.setVersion(licenseAgreement.getVersion());
125 .collect(Collectors.toList());
129 public long count(LicenseAgreementEntity licenseAgreement) {
130 SessionContext context = createSessionContext();
131 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
132 licenseAgreement.getVersion().getId());
134 return zusammenAdaptor.listElementsByName(context, elementContext, null,
135 ElementType.LicenseAgreements.name())
140 public void deleteAll(LicenseAgreementEntity entity) {
145 public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
146 SessionContext context = createSessionContext();
147 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
148 licenseAgreement.getVersion().getId());
150 Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
151 elementContext, new Id(licenseAgreement.getId()));
152 if (elementInfo.isPresent()) {
153 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
154 zusammenElement.setAction(Action.UPDATE);
155 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
156 .filter(relation -> !featureGroupId.equals(relation.getEdge2().getElementId().getValue()))
157 .collect(Collectors.toList()));
158 zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "remove feature group");
163 public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
164 Set<String> addedFeatureGroupIds,
165 Set<String> removedFeatureGroupIds) {
166 ZusammenElement licenseAgreementElement =
167 buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
169 SessionContext context = createSessionContext();
170 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
171 licenseAgreement.getVersion().getId());
172 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
173 Optional<ElementInfo> elementInfo =
174 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()));
175 if (elementInfo.isPresent()) {
176 LicenseAgreementEntity currentLicenseAgreement =
177 convertor.convert(elementInfo.get());
178 currentLicenseAgreement.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
179 currentLicenseAgreement.setVersion(licenseAgreement.getVersion());
180 if (!(removedFeatureGroupIds == null)) {
181 currentLicenseAgreement.getFeatureGroupIds().removeAll(removedFeatureGroupIds);
184 if (!(addedFeatureGroupIds == null)) {
185 currentLicenseAgreement.getFeatureGroupIds().addAll(addedFeatureGroupIds);
187 licenseAgreementElement.setRelations(currentLicenseAgreement.getFeatureGroupIds().stream()
188 .map(relation -> VlmZusammenUtil
189 .createRelation(RelationType.LicenseAgreementToFeatureGroup, relation))
190 .collect(Collectors.toList()));
191 zusammenAdaptor.saveElement(context, elementContext, licenseAgreementElement,
192 "update license agreement");
196 private ZusammenElement buildLicenseAgreementElement(LicenseAgreementEntity licenseAgreement,
198 ZusammenElement licenseAgreementElement =
199 buildElement(licenseAgreement.getId() == null ? null : new Id(licenseAgreement.getId()),
201 Info info = new Info();
202 info.setName(licenseAgreement.getName());
203 info.setDescription(licenseAgreement.getDescription());
204 info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseAgreement);
205 info.addProperty("licenseTerm", licenseAgreement.getLicenseTerm());
206 info.addProperty("requirementsAndConstrains", licenseAgreement.getRequirementsAndConstrains());
207 licenseAgreementElement.setInfo(info);
209 if (licenseAgreement.getFeatureGroupIds() != null &&
210 !licenseAgreement.getFeatureGroupIds().isEmpty()) {
211 licenseAgreementElement.setRelations(licenseAgreement.getFeatureGroupIds().stream()
212 .map(rel -> VlmZusammenUtil
213 .createRelation(RelationType.LicenseAgreementToFeatureGroup, rel))
214 .collect(Collectors.toList()));
216 return licenseAgreementElement;