push addional code
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / main / java / org / openecomp / sdcrests / vsp / rest / services / ComponentProcessesImpl.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.vsp.rest.services;
22
23 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
24 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
25 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
26 import org.openecomp.sdc.versioning.dao.types.Version;
27 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ProcessEntityDto;
28 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ProcessRequestDto;
29 import org.openecomp.sdcrests.vsp.rest.ComponentProcesses;
30 import org.openecomp.sdcrests.vsp.rest.mapping.MapProcessEntityToProcessEntityDto;
31 import org.openecomp.sdcrests.vsp.rest.mapping.MapProcessRequestDtoToProcessEntity;
32 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
33 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
34
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.context.annotation.Scope;
37 import org.springframework.stereotype.Service;
38
39 import java.io.File;
40 import java.io.InputStream;
41 import java.util.Collection;
42 import javax.inject.Named;
43 import javax.ws.rs.core.Response;
44
45
46 @Named
47 @Service("componentProcesses")
48 @Scope(value = "prototype")
49 public class ComponentProcessesImpl implements ComponentProcesses {
50   @Autowired
51   private VendorSoftwareProductManager vendorSoftwareProductManager;
52
53   @Override
54   public Response list(String vspId, String componentId, String version, String user) {
55     Collection<ProcessEntity> processes = vendorSoftwareProductManager
56         .listProcesses(vspId, Version.valueOf(version), componentId, user);
57
58     MapProcessEntityToProcessEntityDto mapper = new MapProcessEntityToProcessEntityDto();
59     GenericCollectionWrapper<ProcessEntityDto> results = new GenericCollectionWrapper<>();
60     for (ProcessEntity process : processes) {
61       results.add(mapper.applyMapping(process, ProcessEntityDto.class));
62     }
63
64     return Response.ok(results).build();
65   }
66
67   @Override
68   public Response deleteList(String vspId, String componentId, String user) {
69     vendorSoftwareProductManager.deleteProcesses(vspId, componentId, user);
70     return Response.ok().build();
71   }
72
73   @Override
74   public Response create(ProcessRequestDto request, String vspId, String componentId, String user) {
75     ProcessEntity process =
76         new MapProcessRequestDtoToProcessEntity().applyMapping(request, ProcessEntity.class);
77     process.setVspId(vspId);
78     process.setComponentId(componentId);
79
80     ProcessEntity createdProcess = vendorSoftwareProductManager.createProcess(process, user);
81     return Response
82         .ok(createdProcess != null ? new StringWrapperResponse(createdProcess.getId()) : null)
83         .build();
84   }
85
86   @Override
87   public Response get(String vspId, String componentId, String processId, String version,
88                       String user) {
89     ProcessEntity process = vendorSoftwareProductManager
90         .getProcess(vspId, Version.valueOf(version), componentId, processId, user);
91     ProcessEntityDto result =
92         new MapProcessEntityToProcessEntityDto().applyMapping(process, ProcessEntityDto.class);
93     return Response.ok(result).build();
94   }
95
96   @Override
97   public Response delete(String vspId, String componentId, String processId, String user) {
98     vendorSoftwareProductManager.deleteProcess(vspId, componentId, processId, user);
99     return Response.ok().build();
100   }
101
102   @Override
103   public Response update(ProcessRequestDto request, String vspId, String componentId,
104                          String processId, String user) {
105     ProcessEntity processEntity =
106         new MapProcessRequestDtoToProcessEntity().applyMapping(request, ProcessEntity.class);
107     processEntity.setVspId(vspId);
108     processEntity.setComponentId(componentId);
109     processEntity.setId(processId);
110
111     vendorSoftwareProductManager.updateProcess(processEntity, user);
112     return Response.ok().build();
113   }
114
115   @Override
116   public Response getUploadedFile(String vspId, String componentId, String processId,
117                                   String version, String user) {
118     File file = vendorSoftwareProductManager
119         .getProcessArtifact(vspId, Version.valueOf(version), componentId, processId, user);
120
121     Response.ResponseBuilder response = Response.ok(file);
122     if (file == null) {
123       return Response.status(Response.Status.NOT_FOUND).build();
124     }
125     response.header("Content-Disposition", "attachment; filename=" + file.getName());
126     return response.build();
127   }
128
129   @Override
130   public Response deleteUploadedFile(String vspId, String componentId, String processId,
131                                      String user) {
132     vendorSoftwareProductManager.deleteProcessArtifact(vspId, componentId, processId, user);
133     return Response.ok().build();
134   }
135
136   @Override
137   public Response uploadFile(Attachment attachment, String vspId, String componentId,
138                              String processId, String user) {
139     vendorSoftwareProductManager.uploadProcessArtifact(attachment.getObject(InputStream.class),
140         attachment.getContentDisposition().getParameter("filename"), vspId, componentId, processId,
141         user);
142     return Response.ok().build();
143   }
144 }