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
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.resources.entitytypes.properties.winery;
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;
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;
32 import com.sun.jersey.api.NotFoundException;
34 public class WinerysPropertiesDefinitionResource {
36 private final EntityTypeResource res;
37 private final WinerysPropertiesDefinition wpd;
41 * @param res the resource where winery's k/v properties are defined
42 * @param wpd winery's properties definition object, MAY be null
44 public WinerysPropertiesDefinitionResource(EntityTypeResource res, WinerysPropertiesDefinition wpd) {
50 @RestDoc(methodDescription = "switches the mode to winery properties instead of element/type properties")
51 public Response onPost() {
52 TEntityType et = this.res.getEntityType();
54 // clear current properties definition
55 et.setPropertiesDefinition(null);
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);
65 @Produces(MediaType.TEXT_PLAIN)
66 public String getNamespace() {
67 if (this.wpd == null) {
68 throw new NotFoundException();
70 return this.wpd.getNamespace();
75 @Consumes(MediaType.TEXT_PLAIN)
76 public Response setNamespace(String namespace) {
77 if (this.wpd == null) {
78 throw new NotFoundException();
80 this.wpd.setNamespace(namespace);
81 return BackendUtils.persist(this.res);
86 @Produces(MediaType.TEXT_PLAIN)
87 public String getElementName() {
88 if (this.wpd == null) {
89 throw new NotFoundException();
91 return this.wpd.getElementName();
96 @Consumes(MediaType.TEXT_PLAIN)
97 public Response setLocalname(String elementName) {
98 if (this.wpd == null) {
99 throw new NotFoundException();
101 this.wpd.setElementName(elementName);
102 return BackendUtils.persist(this.res);
107 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
108 public Response setLocalnameViaWebUI(@FormParam(value = "name") String elementName) {
109 if (this.wpd == null) {
110 throw new NotFoundException();
112 this.wpd.setElementName(elementName);
113 return BackendUtils.persist(this.res);
117 * Here, also the addition of k/v properties is handled.
120 public PropertyDefinitionKVListResource getListResource() {
121 if (this.wpd == null) {
122 throw new NotFoundException();
124 PropertyDefinitionKVList list = this.wpd.getPropertyDefinitionKVList();
126 list = new PropertyDefinitionKVList();
127 this.wpd.setPropertyDefinitionKVList(list);
129 return new PropertyDefinitionKVListResource(this.res, list);