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.*;
42 public class LicenseAgreementDaoZusammenImpl implements LicenseAgreementDao {
44 private ZusammenAdaptor zusammenAdaptor;
46 public LicenseAgreementDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
47 this.zusammenAdaptor = zusammenAdaptor;
51 public void registerVersioning(String versionableEntityType) {
56 public void create(LicenseAgreementEntity licenseAgreement) {
57 ZusammenElement licenseAgreementElement =
58 buildLicenseAgreementElement(licenseAgreement, Action.CREATE);
59 ZusammenElement licenseAgreementsElement =
60 buildStructuralElement(ElementType.LicenseAgreements, Action.IGNORE);
61 licenseAgreementsElement.addSubElement(licenseAgreementElement);
63 SessionContext context = createSessionContext();
64 Element licenseAgreementsSavedElement = zusammenAdaptor.saveElement(context,
65 new ElementContext(licenseAgreement.getVendorLicenseModelId(),
66 licenseAgreement.getVersion().getId()), licenseAgreementsElement,
67 "Create license agreement");
69 .setId(licenseAgreementsSavedElement.getSubElements().iterator().next().getElementId()
74 public void update(LicenseAgreementEntity licenseAgreement) {
75 ZusammenElement licenseAgreementElement =
76 buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
78 SessionContext context = createSessionContext();
79 zusammenAdaptor.saveElement(context,
80 new ElementContext(licenseAgreement.getVendorLicenseModelId(),
81 licenseAgreement.getVersion().getId()), licenseAgreementElement,
82 String.format("Update license agreement with id %s", licenseAgreement.getId()));
86 public LicenseAgreementEntity get(LicenseAgreementEntity licenseAgreement) {
87 SessionContext context = createSessionContext();
88 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
89 licenseAgreement.getVersion().getId());
90 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
91 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()))
93 LicenseAgreementEntity entity = convertor.convert(elementInfo);
94 entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
95 entity.setVersion(licenseAgreement.getVersion());
102 public void delete(LicenseAgreementEntity licenseAgreement) {
103 ZusammenElement zusammenElement = buildElement(new Id(licenseAgreement.getId()), Action.DELETE);
105 SessionContext context = createSessionContext();
106 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
107 licenseAgreement.getVersion().getId());
108 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
109 "delete license agreement. id:" + licenseAgreement.getId() + ".");
114 public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity licenseAgreement) {
115 SessionContext context = createSessionContext();
116 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
117 licenseAgreement.getVersion().getId());
118 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
119 return zusammenAdaptor
120 .listElementsByName(context, elementContext, null,
121 ElementType.LicenseAgreements.name())
122 .stream().map(elementInfo -> {
123 LicenseAgreementEntity entity = convertor.convert(elementInfo);
124 entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
125 entity.setVersion(licenseAgreement.getVersion());
128 .collect(Collectors.toList());
132 public long count(LicenseAgreementEntity licenseAgreement) {
133 SessionContext context = createSessionContext();
134 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
135 licenseAgreement.getVersion().getId());
137 return zusammenAdaptor.listElementsByName(context, elementContext, null,
138 ElementType.LicenseAgreements.name())
143 public void deleteAll(LicenseAgreementEntity entity) {
148 public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
149 SessionContext context = createSessionContext();
150 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
151 licenseAgreement.getVersion().getId());
153 Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
154 elementContext, new Id(licenseAgreement.getId()));
155 if (elementInfo.isPresent()) {
156 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
157 zusammenElement.setAction(Action.UPDATE);
158 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
159 .filter(relation -> !featureGroupId.equals(relation.getEdge2().getElementId().getValue()))
160 .collect(Collectors.toList()));
161 zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "remove feature group");
166 public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
167 Set<String> addedFeatureGroupIds,
168 Set<String> removedFeatureGroupIds) {
169 ZusammenElement licenseAgreementElement =
170 buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
172 SessionContext context = createSessionContext();
173 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
174 licenseAgreement.getVersion().getId());
175 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
176 Optional<ElementInfo> elementInfo =
177 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()));
178 if (elementInfo.isPresent()) {
179 LicenseAgreementEntity currentLicenseAgreement =
180 convertor.convert(elementInfo.get());
181 currentLicenseAgreement.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
182 currentLicenseAgreement.setVersion(licenseAgreement.getVersion());
183 if (!(removedFeatureGroupIds == null)) {
184 currentLicenseAgreement.getFeatureGroupIds().removeAll(removedFeatureGroupIds);
187 if (!(addedFeatureGroupIds == null)) {
188 currentLicenseAgreement.getFeatureGroupIds().addAll(addedFeatureGroupIds);
190 licenseAgreementElement.setRelations(currentLicenseAgreement.getFeatureGroupIds().stream()
191 .map(relation -> VlmZusammenUtil
192 .createRelation(RelationType.LicenseAgreementToFeatureGroup, relation))
193 .collect(Collectors.toList()));
194 zusammenAdaptor.saveElement(context, elementContext, licenseAgreementElement,
195 "update license agreement");
199 private ZusammenElement buildLicenseAgreementElement(LicenseAgreementEntity licenseAgreement,
201 ZusammenElement licenseAgreementElement =
202 buildElement(licenseAgreement.getId() == null ? null : new Id(licenseAgreement.getId()),
204 Info info = new Info();
205 info.setName(licenseAgreement.getName());
206 info.setDescription(licenseAgreement.getDescription());
207 info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseAgreement);
208 info.addProperty("licenseTerm", licenseAgreement.getLicenseTerm());
209 info.addProperty("requirementsAndConstrains", licenseAgreement.getRequirementsAndConstrains());
210 licenseAgreementElement.setInfo(info);
212 if (licenseAgreement.getFeatureGroupIds() != null &&
213 !licenseAgreement.getFeatureGroupIds().isEmpty()) {
214 licenseAgreementElement.setRelations(licenseAgreement.getFeatureGroupIds().stream()
215 .map(rel -> VlmZusammenUtil
216 .createRelation(RelationType.LicenseAgreementToFeatureGroup, rel))
217 .collect(Collectors.toList()));
219 return licenseAgreementElement;