aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.repository/src/main/java/org/eclipse/winery/repository/resources/entitytypes/relationshiptypes/VisualAppearanceResource.java
blob: c17c19bac2f7888a38157f05fce9603102c51c89 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*******************************************************************************
 * 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
 *     Jerome Tagliaferri - support for setting the color
 *******************************************************************************/
package org.eclipse.winery.repository.resources.entitytypes.relationshiptypes;

import java.io.StringWriter;
import java.util.Map;

import javax.ws.rs.Consumes;
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.ws.rs.core.Response.Status;
import javax.xml.namespace.QName;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.winery.common.constants.Defaults;
import org.eclipse.winery.common.constants.Namespaces;
import org.eclipse.winery.common.constants.QNames;
import org.eclipse.winery.common.ids.definitions.RelationshipTypeId;
import org.eclipse.winery.repository.backend.BackendUtils;
import org.eclipse.winery.repository.datatypes.ids.elements.VisualAppearanceId;
import org.eclipse.winery.repository.resources.GenericVisualAppearanceResource;
import org.restdoc.annotations.RestDoc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.sun.jersey.api.view.Viewable;

public class VisualAppearanceResource extends GenericVisualAppearanceResource {
	
	private static final Logger logger = LoggerFactory.getLogger(VisualAppearanceResource.class);
	
	private static final QName QNAME_ARROWHEAD_SOURCE = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "sourceArrowHead");
	private static final QName QNAME_ARROWHEAD_TARGET = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "targetArrowHead");
	private static final QName QNAME_DASH = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "dash");
	private static final QName QNAME_LINEWIDTH = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "linewidth");
	private static final QName QNAME_HOVER_COLOR = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "hovercolor");
	
	
	public VisualAppearanceResource(RelationshipTypeResource res, Map<QName, String> map, RelationshipTypeId parentId) {
		super(res, map, new VisualAppearanceId(parentId));
	}
	
	@GET
	@Produces(MediaType.TEXT_HTML)
	public Response getHTML() {
		Viewable viewable = new Viewable("/jsp/entitytypes/relationshiptypes/visualappearance.jsp", this);
		return Response.ok().entity(viewable).build();
	}
	
	@GET
	@RestDoc(methodDescription = "@return JSON object to be used at jsPlumb.registerConnectionType('NAME', <data>)")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getConnectionTypeForJsPlumbData() {
		JsonFactory jsonFactory = new JsonFactory();
		StringWriter sw = new StringWriter();
		try {
			JsonGenerator jg = jsonFactory.createGenerator(sw);
			jg.writeStartObject();
			
			jg.writeFieldName("connector");
			jg.writeString("Flowchart");
			
			jg.writeFieldName("paintStyle");
			jg.writeStartObject();
			jg.writeFieldName("lineWidth");
			jg.writeNumber(this.getLineWidth());
			jg.writeFieldName("strokeStyle");
			jg.writeObject(this.getColor());
			String dash = this.getDash();
			if (!StringUtils.isEmpty(dash)) {
				String dashStyle = null;
				switch (dash) {
				case "dotted":
					dashStyle = "1 5";
					break;
				case "dotted2":
					dashStyle = "3 4";
					break;
				case "plain":
					// default works
					// otherwise, "1 0" can be used
					break;
				}
				if (dashStyle != null) {
					jg.writeStringField("dashstyle", dashStyle);
				}
			}
			jg.writeEndObject();
			
			jg.writeFieldName("hoverPaintStyle");
			jg.writeStartObject();
			jg.writeFieldName("strokeStyle");
			jg.writeObject(this.getHoverColor());
			jg.writeEndObject();
			
			// BEGIN: Overlays
			
			jg.writeFieldName("overlays");
			jg.writeStartArray();
			
			// source arrow head
			String head = this.getSourceArrowHead();
			if (!head.equals("none")) {
				jg.writeStartArray();
				jg.writeString(head);
				
				jg.writeStartObject();
				
				jg.writeFieldName("location");
				jg.writeNumber(0);
				
				// arrow should point towards the node and not away from it
				jg.writeFieldName("direction");
				jg.writeNumber(-1);
				
				jg.writeFieldName("width");
				jg.writeNumber(20);
				
				jg.writeFieldName("length");
				jg.writeNumber(12);
				
				jg.writeEndObject();
				jg.writeEndArray();
			}
			
			// target arrow head
			head = this.getTargetArrowHead();
			if (!head.equals("none")) {
				jg.writeStartArray();
				jg.writeString(head);
				jg.writeStartObject();
				jg.writeFieldName("location");
				jg.writeNumber(1);
				jg.writeFieldName("width");
				jg.writeNumber(20);
				jg.writeFieldName("length");
				jg.writeNumber(12);
				jg.writeEndObject();
				jg.writeEndArray();
			}
			
			// Type in brackets on the arrow
			jg.writeStartArray();
			jg.writeString("Label");
			jg.writeStartObject();
			jg.writeStringField("id", "label");
			jg.writeStringField("label", "(" + ((RelationshipTypeResource) this.res).getName() + ")");
			jg.writeStringField("cssClass", "relationshipTypeLabel");
			jg.writeFieldName("location");
			jg.writeNumber(0.5);
			jg.writeEndObject();
			jg.writeEndArray();
			
			jg.writeEndArray();
			
			// END: Overlays
			
			jg.writeEndObject();
			
			jg.close();
		} catch (Exception e) {
			VisualAppearanceResource.logger.error(e.getMessage(), e);
			return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e).build();
		}
		String res = sw.toString();
		return Response.ok(res).build();
	}
	
	private String getOtherAttributeWithDefault(QName qname, String def) {
		String res = this.otherAttributes.get(qname);
		if (StringUtils.isEmpty(res)) {
			return def;
		} else {
			return res;
		}
	}
	
	/* * * source arrow head * * */
	
	public String getSourceArrowHead() {
		return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_ARROWHEAD_SOURCE, Defaults.DEFAULT_RT_ARROWHEAD_SOURCE);
	}
	
	@PUT
	@Consumes(MediaType.TEXT_PLAIN)
	@Path("sourcearrowhead")
	public Response onPutSourceHead(String config) {
		if (StringUtils.isEmpty(config)) {
			return Response.status(Status.BAD_REQUEST).entity("config must not be empty").build();
		}
		this.otherAttributes.put(VisualAppearanceResource.QNAME_ARROWHEAD_SOURCE, config);
		return BackendUtils.persist(this.res);
	}
	
	/* * * target arrow head * * */
	
	public String getTargetArrowHead() {
		return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_ARROWHEAD_TARGET, Defaults.DEFAULT_RT_ARROWHEAD_TARGET);
	}
	
	@PUT
	@Consumes(MediaType.TEXT_PLAIN)
	@Path("targetarrowhead")
	public Response onPutTargetHead(String config) {
		if (StringUtils.isEmpty(config)) {
			return Response.status(Status.BAD_REQUEST).entity("config must not be empty").build();
		}
		this.otherAttributes.put(VisualAppearanceResource.QNAME_ARROWHEAD_TARGET, config);
		return BackendUtils.persist(this.res);
	}
	
	/* * *
	 *
	 * stroke dash array / represents the line
	 *
	 * Attention: if a linewidth != 1 is chosen, the dash has to be multiplied somehow by the line width
	 * See: http://jsplumbtoolkit.com/doc/paint-styles:
	 * "The dashstyle attribute is specified as an array of strokes and spaces, where each value is some multiple of the width of the Connector"
	 *
	 * * * */
	
	public String getDash() {
		return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_DASH, Defaults.DEFAULT_RT_DASH);
	}
	
	@PUT
	@Consumes(MediaType.TEXT_PLAIN)
	@Path("dash")
	public Response onPutDash(String config) {
		if (StringUtils.isEmpty(config)) {
			return Response.status(Status.BAD_REQUEST).entity("config must not be empty").build();
		}
		this.otherAttributes.put(VisualAppearanceResource.QNAME_DASH, config);
		return BackendUtils.persist(this.res);
	}
	
	/* * * stroke/line width * * */
	
	public String getLineWidth() {
		return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_LINEWIDTH, Defaults.DEFAULT_RT_LINEWIDTH);
	}
	
	/* * * color * * */
	
	/**
	 * read by topologytemplateeditor.jsp via ${it.color}
	 */
	public String getColor() {
		return BackendUtils.getColorAndSetDefaultIfNotExisting(this.getId().getParent().getXmlId().getDecoded(), QNames.QNAME_COLOR, this.otherAttributes, this.res);
	}
	
	@PUT
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Path("color")
	public Response onPutColor(@FormParam("color") String color) {
		this.otherAttributes.put(QNames.QNAME_COLOR, color);
		return BackendUtils.persist(this.res);
	}
	
	/**
	 * read by topologytemplateeditor.jsp via ${it.hoverColor}
	 */
	public String getHoverColor() {
		return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_HOVER_COLOR, Defaults.DEFAULT_RT_HOVER_COLOR);
	}
	
	@PUT
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Path("hovercolor")
	public Response onPutHoverColor(@FormParam("color") String color) {
		this.otherAttributes.put(VisualAppearanceResource.QNAME_HOVER_COLOR, color);
		return BackendUtils.persist(this.res);
	}
}