0d219e357713d1d46ca4f6394d0c4752bcd8c2f9
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / entitytypes / properties / winery / WinerysPropertiesDefinitionResource.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.entitytypes.properties.winery;
13
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.FormParam;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.POST;
18 import javax.ws.rs.PUT;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.Produces;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.Response;
23
24 import org.eclipse.winery.model.tosca.TEntityType;
25 import org.eclipse.winery.common.ModelUtilities;
26 import org.eclipse.winery.common.propertydefinitionkv.PropertyDefinitionKVList;
27 import org.eclipse.winery.common.propertydefinitionkv.WinerysPropertiesDefinition;
28 import org.eclipse.winery.repository.backend.BackendUtils;
29 import org.eclipse.winery.repository.resources.EntityTypeResource;
30 import org.restdoc.annotations.RestDoc;
31
32 import com.sun.jersey.api.NotFoundException;
33
34 public class WinerysPropertiesDefinitionResource {
35         
36         private final EntityTypeResource res;
37         private final WinerysPropertiesDefinition wpd;
38         
39         
40         /**
41          * @param res the resource where winery's k/v properties are defined
42          * @param wpd winery's properties definition object, MAY be null
43          */
44         public WinerysPropertiesDefinitionResource(EntityTypeResource res, WinerysPropertiesDefinition wpd) {
45                 this.res = res;
46                 this.wpd = wpd;
47         }
48         
49         @POST
50         @RestDoc(methodDescription = "switches the mode to winery properties instead of element/type properties")
51         public Response onPost() {
52                 TEntityType et = this.res.getEntityType();
53                 
54                 // clear current properties definition
55                 et.setPropertiesDefinition(null);
56                 
57                 // create empty winery properties definition and persist it
58                 WinerysPropertiesDefinition wpd = new WinerysPropertiesDefinition();
59                 ModelUtilities.replaceWinerysPropertiesDefinition(et, wpd);
60                 return BackendUtils.persist(this.res);
61         }
62         
63         @Path("namespace")
64         @GET
65         @Produces(MediaType.TEXT_PLAIN)
66         public String getNamespace() {
67                 if (this.wpd == null) {
68                         throw new NotFoundException();
69                 }
70                 return this.wpd.getNamespace();
71         }
72         
73         @Path("namespace")
74         @PUT
75         @Consumes(MediaType.TEXT_PLAIN)
76         public Response setNamespace(String namespace) {
77                 if (this.wpd == null) {
78                         throw new NotFoundException();
79                 }
80                 this.wpd.setNamespace(namespace);
81                 return BackendUtils.persist(this.res);
82         }
83         
84         @Path("elementname")
85         @GET
86         @Produces(MediaType.TEXT_PLAIN)
87         public String getElementName() {
88                 if (this.wpd == null) {
89                         throw new NotFoundException();
90                 }
91                 return this.wpd.getElementName();
92         }
93         
94         @Path("elementname")
95         @PUT
96         @Consumes(MediaType.TEXT_PLAIN)
97         public Response setLocalname(String elementName) {
98                 if (this.wpd == null) {
99                         throw new NotFoundException();
100                 }
101                 this.wpd.setElementName(elementName);
102                 return BackendUtils.persist(this.res);
103         }
104         
105         @Path("elementname")
106         @PUT
107         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
108         public Response setLocalnameViaWebUI(@FormParam(value = "name") String elementName) {
109                 if (this.wpd == null) {
110                         throw new NotFoundException();
111                 }
112                 this.wpd.setElementName(elementName);
113                 return BackendUtils.persist(this.res);
114         }
115         
116         /**
117          * Here, also the addition of k/v properties is handled.
118          */
119         @Path("list/")
120         public PropertyDefinitionKVListResource getListResource() {
121                 if (this.wpd == null) {
122                         throw new NotFoundException();
123                 }
124                 PropertyDefinitionKVList list = this.wpd.getPropertyDefinitionKVList();
125                 if (list == null) {
126                         list = new PropertyDefinitionKVList();
127                         this.wpd.setPropertyDefinitionKVList(list);
128                 }
129                 return new PropertyDefinitionKVListResource(this.res, list);
130         }
131         
132 }