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.nodetypes.reqandcapdefs;
14 import java.lang.reflect.Method;
15 import java.util.List;
17 import javax.ws.rs.FormParam;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import javax.xml.namespace.QName;
26 import org.eclipse.winery.model.tosca.TCapabilityDefinition;
27 import org.eclipse.winery.model.tosca.TConstraint;
28 import org.eclipse.winery.model.tosca.TRequirementDefinition;
29 import org.eclipse.winery.repository.backend.BackendUtils;
30 import org.eclipse.winery.repository.resources.ConstraintsResource;
31 import org.eclipse.winery.repository.resources._support.collections.IIdDetermination;
32 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdResource;
33 import org.eclipse.winery.repository.resources.entitytypes.nodetypes.NodeTypeResource;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Bundles common properties of TRequirementDefinition and TCapabilityDefinition
40 * We agreed in the project not to modify org.eclipse.winery.model.tosca.
41 * Therefore, this resource models the common properties of a
42 * TRequirementDefinition and a TCapabilityDefinition
44 public abstract class AbstractReqOrCapDefResource<ReqOrCapDef> extends EntityWithIdResource<ReqOrCapDef> implements IIdDetermination<ReqOrCapDef> {
46 private static final Logger logger = LoggerFactory.getLogger(AbstractReqOrCapDefResource.class);
48 protected NodeTypeResource parent;
50 // the capability or the requirement
51 private Object reqOrCapDef;
53 private List<TConstraint> constraints;
57 * @param constraints additional parameter (in comparison to the constructor
58 * of EntityWithIdResource) as we require that sublist for the
59 * constrinats sub resource
61 public AbstractReqOrCapDefResource(IIdDetermination<ReqOrCapDef> idDetermination, ReqOrCapDef reqOrCapDef, int idx, List<ReqOrCapDef> list, NodeTypeResource res, List<TConstraint> constraints) {
62 super(idDetermination, reqOrCapDef, idx, list, res);
63 assert ((reqOrCapDef instanceof TRequirementDefinition) || (reqOrCapDef instanceof TCapabilityDefinition));
65 this.reqOrCapDef = reqOrCapDef;
66 this.constraints = constraints;
71 public String getName() {
72 return (String) this.invokeGetter("getName");
75 static String getName(Object reqOrCapDef) {
76 return (String) AbstractReqOrCapDefResource.invokeGetter(reqOrCapDef, "getName");
81 public int getLowerBound() {
82 return (int) this.invokeGetter("getLowerBound");
87 public String getUpperBound() {
88 return (String) this.invokeGetter("getUpperBound");
93 public Response setName(@FormParam(value = "name") String name) {
94 // TODO: type check - see also min/max Instance of a node template
95 this.invokeSetter("setName", name);
96 return BackendUtils.persist(this.parent);
101 public Response setLowerBound(@FormParam(value = "lowerbound") String value) {
103 this.invokeSetter("setLowerBound", value);
104 return BackendUtils.persist(this.parent);
109 public Response setUpperBound(@FormParam(value = "upperbound") String value) {
111 this.invokeSetter("setUpperBound", value);
112 return BackendUtils.persist(this.parent);
115 @Path("constraints/")
116 public ConstraintsResource getConstraintsResource() {
117 return new ConstraintsResource(this.constraints, this.parent);
120 private static Object invokeGetter(Object reqOrCapDef, String getterName) {
124 method = reqOrCapDef.getClass().getMethod(getterName);
125 res = method.invoke(reqOrCapDef);
126 } catch (Exception e) {
127 AbstractReqOrCapDefResource.logger.error("Could not invoke getter {}", getterName, e);
128 throw new IllegalStateException(e);
133 private Object invokeGetter(String getterName) {
134 return AbstractReqOrCapDefResource.invokeGetter(this.reqOrCapDef, getterName);
138 * Quick hack method for RequirementOrCapabilityDefinitionsResource
140 static void invokeSetter(Object reqOrCapDef, String setterName, Object value) {
143 method = reqOrCapDef.getClass().getMethod(setterName, value.getClass());
144 method.invoke(reqOrCapDef, value);
145 } catch (Exception e) {
146 AbstractReqOrCapDefResource.logger.error("Could not invoke setter {}", setterName, e);
147 throw new IllegalStateException(e);
151 private void invokeSetter(String setterName, Object value) {
152 AbstractReqOrCapDefResource.invokeSetter(this.reqOrCapDef, setterName, value);
157 @Produces(MediaType.TEXT_PLAIN)
158 public String getTypeAsString() {
159 return this.getType().toString();
163 * required by the JSP.
165 * Therefore, we have two getters for the type: QName for the JSP and String
168 public abstract QName getType();
171 * Required by reqandcapdefs.jsp
173 public Object getDef() {
174 return this.reqOrCapDef;