Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / interfaces / ParametersResource.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.interfaces;
13
14 import java.util.List;
15
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.FormParam;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.Produces;
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.tosca.TBoolean;
26 import org.eclipse.winery.model.tosca.TParameter;
27 import org.eclipse.winery.repository.backend.BackendUtils;
28 import org.eclipse.winery.repository.resources._support.IPersistable;
29 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdCollectionResource;
30 import org.restdoc.annotations.RestDocParam;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.sun.jersey.api.view.Viewable;
35
36 public class ParametersResource extends EntityWithIdCollectionResource<ParameterResource, TParameter> {
37         
38         private static final Logger logger = LoggerFactory.getLogger(ParametersResource.class);
39         
40         
41         public ParametersResource(List<TParameter> parameters, IPersistable typeResource) {
42                 super(ParameterResource.class, TParameter.class, parameters, typeResource);
43         }
44         
45         @POST
46         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
47         @Produces(MediaType.TEXT_PLAIN)
48         // @formatter:off
49         public Response createParamter(
50                         @FormParam("name") String name,
51                         @FormParam("type") String type,
52                         @FormParam("required") @RestDocParam(description="type tYesNo, not Boolean. For convenience, on/off is also supported. In case this parameter is not provided, 'off' is assumed. This is in contrast to the specification, but it eases implementing the UI") String required) {
53                 // @formatter:on
54                 if (StringUtils.isEmpty(name)) {
55                         return Response.status(Status.BAD_REQUEST).entity("name must not be null").build();
56                 }
57                 if (StringUtils.isEmpty(type)) {
58                         return Response.status(Status.BAD_REQUEST).entity("type must not be null").build();
59                 }
60                 
61                 TParameter param = new TParameter();
62                 param.setName(name);
63                 param.setType(type);
64                 TBoolean tb;
65                 if (required == null) {
66                         // The specification states that the default value is "yes"
67                         // We assume "no", because Chrome does not send the checkbox data if a checkbox is not checked
68                         tb = TBoolean.NO;
69                 } else {
70                         if (required.equalsIgnoreCase("on")) {
71                                 tb = TBoolean.YES;
72                         } else if (required.equalsIgnoreCase("off")) {
73                                 tb = TBoolean.NO;
74                         } else {
75                                 try {
76                                         tb = TBoolean.valueOf(required);
77                                 } catch (java.lang.IllegalArgumentException e) {
78                                         return Response.status(Status.BAD_REQUEST).entity("Wrong format of required").build();
79                                 }
80                         }
81                 }
82                 param.setRequired(tb);
83                 
84                 this.list.add(param);
85                 
86                 return BackendUtils.persist(this.res);
87         }
88         
89         @Override
90         public String getId(TParameter entity) {
91                 return entity.getName();
92         }
93         
94         @Override
95         public Viewable getHTML() {
96                 throw new IllegalStateException("Not yet implemented.");
97         }
98         
99 }