aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.repository/src/main/java/org/eclipse/winery/repository/resources/entitytypes/nodetypes/reqandcapdefs/AbstractReqOrCapDefResource.java
blob: b7eea9258a94bd1c67a595216dd3b56ee9c54a7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*******************************************************************************
 * Copyright (c) 2012-2013 University of Stuttgart.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     Oliver Kopp - initial API and implementation
 *******************************************************************************/
package org.eclipse.winery.repository.resources.entitytypes.nodetypes.reqandcapdefs;

import java.lang.reflect.Method;
import java.util.List;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.namespace.QName;

import org.eclipse.winery.model.tosca.TCapabilityDefinition;
import org.eclipse.winery.model.tosca.TConstraint;
import org.eclipse.winery.model.tosca.TRequirementDefinition;
import org.eclipse.winery.repository.backend.BackendUtils;
import org.eclipse.winery.repository.resources.ConstraintsResource;
import org.eclipse.winery.repository.resources._support.collections.IIdDetermination;
import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdResource;
import org.eclipse.winery.repository.resources.entitytypes.nodetypes.NodeTypeResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Bundles common properties of TRequirementDefinition and TCapabilityDefinition
 * 
 * We agreed in the project not to modify org.eclipse.winery.model.tosca.
 * Therefore, this resource models the common properties of a
 * TRequirementDefinition and a TCapabilityDefinition
 */
public abstract class AbstractReqOrCapDefResource<ReqOrCapDef> extends EntityWithIdResource<ReqOrCapDef> implements IIdDetermination<ReqOrCapDef> {
	
	private static final Logger logger = LoggerFactory.getLogger(AbstractReqOrCapDefResource.class);
	
	protected NodeTypeResource parent;
	
	// the capability or the requirement
	private Object reqOrCapDef;
	
	private List<TConstraint> constraints;
	
	
	/**
	 * @param constraints additional parameter (in comparison to the constructor
	 *            of EntityWithIdResource) as we require that sublist for the
	 *            constrinats sub resource
	 */
	public AbstractReqOrCapDefResource(IIdDetermination<ReqOrCapDef> idDetermination, ReqOrCapDef reqOrCapDef, int idx, List<ReqOrCapDef> list, NodeTypeResource res, List<TConstraint> constraints) {
		super(idDetermination, reqOrCapDef, idx, list, res);
		assert ((reqOrCapDef instanceof TRequirementDefinition) || (reqOrCapDef instanceof TCapabilityDefinition));
		this.parent = res;
		this.reqOrCapDef = reqOrCapDef;
		this.constraints = constraints;
	}
	
	@GET
	@Path("name")
	public String getName() {
		return (String) this.invokeGetter("getName");
	}
	
	static String getName(Object reqOrCapDef) {
		return (String) AbstractReqOrCapDefResource.invokeGetter(reqOrCapDef, "getName");
	}
	
	@GET
	@Path("lowerbound")
	public int getLowerBound() {
		return (int) this.invokeGetter("getLowerBound");
	}
	
	@GET
	@Path("upperbound")
	public String getUpperBound() {
		return (String) this.invokeGetter("getUpperBound");
	}
	
	@PUT
	@Path("name")
	public Response setName(@FormParam(value = "name") String name) {
		// TODO: type check - see also min/max Instance of a node template
		this.invokeSetter("setName", name);
		return BackendUtils.persist(this.parent);
	}
	
	@PUT
	@Path("lowerbound")
	public Response setLowerBound(@FormParam(value = "lowerbound") String value) {
		// TODO: type check
		this.invokeSetter("setLowerBound", value);
		return BackendUtils.persist(this.parent);
	}
	
	@PUT
	@Path("upperbound")
	public Response setUpperBound(@FormParam(value = "upperbound") String value) {
		// TODO: type check
		this.invokeSetter("setUpperBound", value);
		return BackendUtils.persist(this.parent);
	}
	
	@Path("constraints/")
	public ConstraintsResource getConstraintsResource() {
		return new ConstraintsResource(this.constraints, this.parent);
	}
	
	private static Object invokeGetter(Object reqOrCapDef, String getterName) {
		Method method;
		Object res;
		try {
			method = reqOrCapDef.getClass().getMethod(getterName);
			res = method.invoke(reqOrCapDef);
		} catch (Exception e) {
			AbstractReqOrCapDefResource.logger.error("Could not invoke getter {}", getterName, e);
			throw new IllegalStateException(e);
		}
		return res;
	}
	
	private Object invokeGetter(String getterName) {
		return AbstractReqOrCapDefResource.invokeGetter(this.reqOrCapDef, getterName);
	}
	
	/**
	 * Quick hack method for RequirementOrCapabilityDefinitionsResource
	 */
	static void invokeSetter(Object reqOrCapDef, String setterName, Object value) {
		Method method;
		try {
			method = reqOrCapDef.getClass().getMethod(setterName, value.getClass());
			method.invoke(reqOrCapDef, value);
		} catch (Exception e) {
			AbstractReqOrCapDefResource.logger.error("Could not invoke setter {}", setterName, e);
			throw new IllegalStateException(e);
		}
	}
	
	private void invokeSetter(String setterName, Object value) {
		AbstractReqOrCapDefResource.invokeSetter(this.reqOrCapDef, setterName, value);
	}
	
	@GET
	@Path("type")
	@Produces(MediaType.TEXT_PLAIN)
	public String getTypeAsString() {
		return this.getType().toString();
	}
	
	/**
	 * required by the JSP.
	 * 
	 * Therefore, we have two getters for the type: QName for the JSP and String
	 * for REST clients
	 */
	public abstract QName getType();
	
	/**
	 * Required by reqandcapdefs.jsp
	 */
	public Object getDef() {
		return this.reqOrCapDef;
	}
	
}