push addional code
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-license-rest / vendor-license-rest-services / src / main / java / org / openecomp / sdcrests / vendorlicense / rest / services / VendorLicenseModelsImpl.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.sdcrests.vendorlicense.rest.services;
22
23 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
24 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
25 import org.openecomp.sdc.vendorlicense.types.VersionedVendorLicenseModel;
26 import org.openecomp.sdc.versioning.dao.types.Version;
27 import org.openecomp.sdcrests.vendorlicense.rest.VendorLicenseModels;
28 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapVendorLicenseModelRequestDtoToVendorLicenseModelEntity;
29 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapVersionedVendorLicenseModelToVendorLicenseModelEntityDto;
30 import org.openecomp.sdcrests.vendorlicense.types.VendorLicenseModelActionRequestDto;
31 import org.openecomp.sdcrests.vendorlicense.types.VendorLicenseModelEntityDto;
32 import org.openecomp.sdcrests.vendorlicense.types.VendorLicenseModelRequestDto;
33 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
34 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.context.annotation.Scope;
37 import org.springframework.stereotype.Service;
38 import org.springframework.validation.annotation.Validated;
39
40 import java.util.Collection;
41 import javax.inject.Named;
42 import javax.ws.rs.core.Response;
43
44
45
46 @Named
47 @Service("vendorLicenseModels")
48 @Scope(value = "prototype")
49 @Validated
50 public class VendorLicenseModelsImpl implements VendorLicenseModels {
51
52   @Autowired
53   private VendorLicenseManager vendorLicenseManager;
54
55   @Override
56   public Response listLicenseModels(String versionFilter, String user) {
57     Collection<VersionedVendorLicenseModel> versionedVendorLicenseModels =
58         vendorLicenseManager.listVendorLicenseModels(versionFilter, user);
59
60     GenericCollectionWrapper<VendorLicenseModelEntityDto> results =
61         new GenericCollectionWrapper<>();
62     MapVersionedVendorLicenseModelToVendorLicenseModelEntityDto outputMapper =
63         new MapVersionedVendorLicenseModelToVendorLicenseModelEntityDto();
64     for (VersionedVendorLicenseModel versionedVlm : versionedVendorLicenseModels) {
65       results.add(outputMapper.applyMapping(versionedVlm, VendorLicenseModelEntityDto.class));
66     }
67
68     return Response.ok(results).build();
69   }
70
71   @Override
72   public Response createLicenseModel(VendorLicenseModelRequestDto request, String user) {
73     VendorLicenseModelEntity vendorLicenseModelEntity =
74         new MapVendorLicenseModelRequestDtoToVendorLicenseModelEntity()
75             .applyMapping(request, VendorLicenseModelEntity.class);
76     VendorLicenseModelEntity createdVendorLicenseModel =
77         vendorLicenseManager.createVendorLicenseModel(vendorLicenseModelEntity, user);
78     StringWrapperResponse result = createdVendorLicenseModel != null ? new StringWrapperResponse(
79         createdVendorLicenseModel.getId()) : null;
80
81     return Response.ok(result).build();
82   }
83
84   @Override
85   public Response updateLicenseModel(VendorLicenseModelRequestDto request, String vlmId,
86                                      String user) {
87     VendorLicenseModelEntity vendorLicenseModelEntity =
88         new MapVendorLicenseModelRequestDtoToVendorLicenseModelEntity()
89             .applyMapping(request, VendorLicenseModelEntity.class);
90     vendorLicenseModelEntity.setId(vlmId);
91
92     vendorLicenseManager.updateVendorLicenseModel(vendorLicenseModelEntity, user);
93     return Response.ok().build();
94   }
95
96   @Override
97   public Response getLicenseModel(String vlmId, String version, String user) {
98     VersionedVendorLicenseModel versionedVlm =
99         vendorLicenseManager.getVendorLicenseModel(vlmId, Version.valueOf(version), user);
100
101     VendorLicenseModelEntityDto vlmDto = versionedVlm == null ? null :
102         new MapVersionedVendorLicenseModelToVendorLicenseModelEntityDto()
103             .applyMapping(versionedVlm, VendorLicenseModelEntityDto.class);
104     return Response.ok(vlmDto).build();
105   }
106
107   @Override
108   public Response deleteLicenseModel(String vlmId, String user) {
109     vendorLicenseManager.deleteVendorLicenseModel(vlmId, user);
110     return Response.ok().build();
111   }
112
113   @Override
114   public Response actOnLicenseModel(VendorLicenseModelActionRequestDto request, String vlmId,
115                                     String user) {
116
117     switch (request.getAction()) {
118       case Checkout:
119         vendorLicenseManager.checkout(vlmId, user);
120         break;
121       case Undo_Checkout:
122         vendorLicenseManager.undoCheckout(vlmId, user);
123         break;
124       case Checkin:
125         vendorLicenseManager.checkin(vlmId, user);
126         break;
127       case Submit:
128         vendorLicenseManager.submit(vlmId, user);
129         break;
130       default:
131     }
132
133     return Response.ok().build();
134   }
135 }