86fa102fef209afbe1703b4f9d636b4894cd48d3
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / servicetemplates / selfserviceportal / OptionsResource.java
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.resources.servicetemplates.selfserviceportal;
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.List;
17
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
22 import javax.ws.rs.core.Response.Status;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.eclipse.winery.model.selfservice.ApplicationOption;
26 import org.eclipse.winery.common.RepositoryFileReference;
27 import org.eclipse.winery.repository.Utils;
28 import org.eclipse.winery.repository.backend.BackendUtils;
29 import org.eclipse.winery.repository.backend.Repository;
30 import org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId;
31 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdCollectionResource;
32 import org.restdoc.annotations.RestDoc;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.sun.jersey.api.view.Viewable;
37 import com.sun.jersey.core.header.FormDataContentDisposition;
38 import com.sun.jersey.multipart.FormDataBodyPart;
39 import com.sun.jersey.multipart.FormDataParam;
40
41 public class OptionsResource extends EntityWithIdCollectionResource<OptionResource, ApplicationOption> {
42         
43         private static final Logger logger = LoggerFactory.getLogger(OptionsResource.class);
44         
45         
46         public OptionsResource(List<ApplicationOption> list, SelfServicePortalResource res) {
47                 super(OptionResource.class, ApplicationOption.class, list, res);
48         }
49         
50         @Override
51         public String getId(ApplicationOption entity) {
52                 return entity.getId();
53         }
54         
55         @Override
56         public Viewable getHTML() {
57                 throw new IllegalStateException("Not yet implemented.");
58         }
59         
60         @POST
61         @RestDoc(methodDescription = "Adds a new option<p>TODO: @return JSON with .tableData: Array with row data for dataTable</p>")
62         @Consumes(MediaType.MULTIPART_FORM_DATA)
63         // @formatter:off
64         public Response onPost(
65                 @FormDataParam("name") String name,
66                 @FormDataParam("description") String description,
67                 @FormDataParam("planServiceName") String planServiceName,
68                 @FormDataParam("planInputMessage") String planInputMessage,
69                 @FormDataParam("file") InputStream uploadedInputStream,
70                 @FormDataParam("file") FormDataContentDisposition fileDetail,
71                 @FormDataParam("file") FormDataBodyPart body
72         ) {
73         // @formatter:on
74                 if (StringUtils.isEmpty(name)) {
75                         return Response.status(Status.BAD_REQUEST).entity("planName must be given").build();
76                 }
77                 if (StringUtils.isEmpty(description)) {
78                         return Response.status(Status.BAD_REQUEST).entity("description must be given").build();
79                 }
80                 if (StringUtils.isEmpty(planServiceName)) {
81                         return Response.status(Status.BAD_REQUEST).entity("planServiceName must be given").build();
82                 }
83                 if (StringUtils.isEmpty(planInputMessage)) {
84                         return Response.status(Status.BAD_REQUEST).entity("planInputMessage must be given").build();
85                 }
86                 if (uploadedInputStream == null) {
87                         return Response.status(Status.BAD_REQUEST).entity("file has to be provided").build();
88                 }
89                 ApplicationOption option = new ApplicationOption();
90                 
91                 String id = Utils.createXMLidAsString(name);
92                 
93                 String fileNamePrefix = OptionResource.getFileNamePrefix(id);
94                 String iconFileName = fileNamePrefix + OptionResource.ICON_JPG;
95                 String planInputMessageFileName = fileNamePrefix + OptionResource.PLAN_INPUT_XML;
96                 
97                 // create option data
98                 option.setId(id);
99                 option.setName(name);
100                 option.setDescription(description);
101                 option.setIconUrl(iconFileName);
102                 option.setPlanInputMessageUrl(planInputMessageFileName);
103                 option.setPlanServiceName(planServiceName);
104                 
105                 // BEGIN: store icon and planInputMessage
106                 
107                 SelfServiceMetaDataId ssmdId = ((SelfServicePortalResource) this.res).getId();
108                 
109                 RepositoryFileReference iconRef = new RepositoryFileReference(ssmdId, iconFileName);
110                 try {
111                         Repository.INSTANCE.putContentToFile(iconRef, uploadedInputStream, body.getMediaType());
112                 } catch (IOException e) {
113                         OptionsResource.logger.error(e.getMessage(), e);
114                         return Response.serverError().entity(e.getMessage()).build();
115                 }
116                 
117                 RepositoryFileReference planInputMessageRef = new RepositoryFileReference(ssmdId, planInputMessageFileName);
118                 try {
119                         Repository.INSTANCE.putContentToFile(planInputMessageRef, planInputMessage, MediaType.TEXT_XML_TYPE);
120                 } catch (IOException e) {
121                         OptionsResource.logger.error(e.getMessage(), e);
122                         return Response.serverError().entity(e.getMessage()).build();
123                 }
124                 
125                 // END: store icon and planInputMessage
126                 
127                 this.list.add(option);
128                 Response response = BackendUtils.persist(this.res);
129                 return response;
130         }
131 }