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 * Jerome Tagliaferri - support for setting the color
12 *******************************************************************************/
13 package org.eclipse.winery.repository.resources.entitytypes.relationshiptypes;
15 import java.io.StringWriter;
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.FormParam;
20 import javax.ws.rs.GET;
21 import javax.ws.rs.PUT;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.core.Response.Status;
27 import javax.xml.namespace.QName;
29 import org.apache.commons.lang3.StringUtils;
30 import org.eclipse.winery.common.constants.Defaults;
31 import org.eclipse.winery.common.constants.Namespaces;
32 import org.eclipse.winery.common.constants.QNames;
33 import org.eclipse.winery.common.ids.definitions.RelationshipTypeId;
34 import org.eclipse.winery.repository.backend.BackendUtils;
35 import org.eclipse.winery.repository.datatypes.ids.elements.VisualAppearanceId;
36 import org.eclipse.winery.repository.resources.GenericVisualAppearanceResource;
37 import org.restdoc.annotations.RestDoc;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.fasterxml.jackson.core.JsonFactory;
42 import com.fasterxml.jackson.core.JsonGenerator;
43 import com.sun.jersey.api.view.Viewable;
45 public class VisualAppearanceResource extends GenericVisualAppearanceResource {
47 private static final Logger logger = LoggerFactory.getLogger(VisualAppearanceResource.class);
49 private static final QName QNAME_ARROWHEAD_SOURCE = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "sourceArrowHead");
50 private static final QName QNAME_ARROWHEAD_TARGET = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "targetArrowHead");
51 private static final QName QNAME_DASH = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "dash");
52 private static final QName QNAME_LINEWIDTH = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "linewidth");
53 private static final QName QNAME_HOVER_COLOR = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "hovercolor");
56 public VisualAppearanceResource(RelationshipTypeResource res, Map<QName, String> map, RelationshipTypeId parentId) {
57 super(res, map, new VisualAppearanceId(parentId));
61 @Produces(MediaType.TEXT_HTML)
62 public Response getHTML() {
63 Viewable viewable = new Viewable("/jsp/entitytypes/relationshiptypes/visualappearance.jsp", this);
64 return Response.ok().entity(viewable).build();
68 @RestDoc(methodDescription = "@return JSON object to be used at jsPlumb.registerConnectionType('NAME', <data>)")
69 @Produces(MediaType.APPLICATION_JSON)
70 public Response getConnectionTypeForJsPlumbData() {
71 JsonFactory jsonFactory = new JsonFactory();
72 StringWriter sw = new StringWriter();
74 JsonGenerator jg = jsonFactory.createGenerator(sw);
75 jg.writeStartObject();
77 jg.writeFieldName("connector");
78 jg.writeString("Flowchart");
80 jg.writeFieldName("paintStyle");
81 jg.writeStartObject();
82 jg.writeFieldName("lineWidth");
83 jg.writeNumber(this.getLineWidth());
84 jg.writeFieldName("strokeStyle");
85 jg.writeObject(this.getColor());
86 String dash = this.getDash();
87 if (!StringUtils.isEmpty(dash)) {
88 String dashStyle = null;
98 // otherwise, "1 0" can be used
101 if (dashStyle != null) {
102 jg.writeStringField("dashstyle", dashStyle);
107 jg.writeFieldName("hoverPaintStyle");
108 jg.writeStartObject();
109 jg.writeFieldName("strokeStyle");
110 jg.writeObject(this.getHoverColor());
115 jg.writeFieldName("overlays");
116 jg.writeStartArray();
119 String head = this.getSourceArrowHead();
120 if (!head.equals("none")) {
121 jg.writeStartArray();
122 jg.writeString(head);
124 jg.writeStartObject();
126 jg.writeFieldName("location");
129 // arrow should point towards the node and not away from it
130 jg.writeFieldName("direction");
133 jg.writeFieldName("width");
136 jg.writeFieldName("length");
144 head = this.getTargetArrowHead();
145 if (!head.equals("none")) {
146 jg.writeStartArray();
147 jg.writeString(head);
148 jg.writeStartObject();
149 jg.writeFieldName("location");
151 jg.writeFieldName("width");
153 jg.writeFieldName("length");
159 // Type in brackets on the arrow
160 jg.writeStartArray();
161 jg.writeString("Label");
162 jg.writeStartObject();
163 jg.writeStringField("id", "label");
164 jg.writeStringField("label", "(" + ((RelationshipTypeResource) this.res).getName() + ")");
165 jg.writeStringField("cssClass", "relationshipTypeLabel");
166 jg.writeFieldName("location");
178 } catch (Exception e) {
179 VisualAppearanceResource.logger.error(e.getMessage(), e);
180 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e).build();
182 String res = sw.toString();
183 return Response.ok(res).build();
186 private String getOtherAttributeWithDefault(QName qname, String def) {
187 String res = this.otherAttributes.get(qname);
188 if (StringUtils.isEmpty(res)) {
195 /* * * source arrow head * * */
197 public String getSourceArrowHead() {
198 return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_ARROWHEAD_SOURCE, Defaults.DEFAULT_RT_ARROWHEAD_SOURCE);
202 @Consumes(MediaType.TEXT_PLAIN)
203 @Path("sourcearrowhead")
204 public Response onPutSourceHead(String config) {
205 if (StringUtils.isEmpty(config)) {
206 return Response.status(Status.BAD_REQUEST).entity("config must not be empty").build();
208 this.otherAttributes.put(VisualAppearanceResource.QNAME_ARROWHEAD_SOURCE, config);
209 return BackendUtils.persist(this.res);
212 /* * * target arrow head * * */
214 public String getTargetArrowHead() {
215 return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_ARROWHEAD_TARGET, Defaults.DEFAULT_RT_ARROWHEAD_TARGET);
219 @Consumes(MediaType.TEXT_PLAIN)
220 @Path("targetarrowhead")
221 public Response onPutTargetHead(String config) {
222 if (StringUtils.isEmpty(config)) {
223 return Response.status(Status.BAD_REQUEST).entity("config must not be empty").build();
225 this.otherAttributes.put(VisualAppearanceResource.QNAME_ARROWHEAD_TARGET, config);
226 return BackendUtils.persist(this.res);
231 * stroke dash array / represents the line
233 * Attention: if a linewidth != 1 is chosen, the dash has to be multiplied somehow by the line width
234 * See: http://jsplumbtoolkit.com/doc/paint-styles:
235 * "The dashstyle attribute is specified as an array of strokes and spaces, where each value is some multiple of the width of the Connector"
239 public String getDash() {
240 return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_DASH, Defaults.DEFAULT_RT_DASH);
244 @Consumes(MediaType.TEXT_PLAIN)
246 public Response onPutDash(String config) {
247 if (StringUtils.isEmpty(config)) {
248 return Response.status(Status.BAD_REQUEST).entity("config must not be empty").build();
250 this.otherAttributes.put(VisualAppearanceResource.QNAME_DASH, config);
251 return BackendUtils.persist(this.res);
254 /* * * stroke/line width * * */
256 public String getLineWidth() {
257 return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_LINEWIDTH, Defaults.DEFAULT_RT_LINEWIDTH);
263 * read by topologytemplateeditor.jsp via ${it.color}
265 public String getColor() {
266 return BackendUtils.getColorAndSetDefaultIfNotExisting(this.getId().getParent().getXmlId().getDecoded(), QNames.QNAME_COLOR, this.otherAttributes, this.res);
270 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
272 public Response onPutColor(@FormParam("color") String color) {
273 this.otherAttributes.put(QNames.QNAME_COLOR, color);
274 return BackendUtils.persist(this.res);
278 * read by topologytemplateeditor.jsp via ${it.hoverColor}
280 public String getHoverColor() {
281 return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_HOVER_COLOR, Defaults.DEFAULT_RT_HOVER_COLOR);
285 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
287 public Response onPutHoverColor(@FormParam("color") String color) {
288 this.otherAttributes.put(VisualAppearanceResource.QNAME_HOVER_COLOR, color);
289 return BackendUtils.persist(this.res);