Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / entitytypes / relationshiptypes / VisualAppearanceResource.java
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
8  *
9  * Contributors:
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;
14
15 import java.io.StringWriter;
16 import java.util.Map;
17
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;
28
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;
40
41 import com.fasterxml.jackson.core.JsonFactory;
42 import com.fasterxml.jackson.core.JsonGenerator;
43 import com.sun.jersey.api.view.Viewable;
44
45 public class VisualAppearanceResource extends GenericVisualAppearanceResource {
46         
47         private static final Logger logger = LoggerFactory.getLogger(VisualAppearanceResource.class);
48         
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");
54         
55         
56         public VisualAppearanceResource(RelationshipTypeResource res, Map<QName, String> map, RelationshipTypeId parentId) {
57                 super(res, map, new VisualAppearanceId(parentId));
58         }
59         
60         @GET
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();
65         }
66         
67         @GET
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();
73                 try {
74                         JsonGenerator jg = jsonFactory.createGenerator(sw);
75                         jg.writeStartObject();
76                         
77                         jg.writeFieldName("connector");
78                         jg.writeString("Flowchart");
79                         
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;
89                                 switch (dash) {
90                                 case "dotted":
91                                         dashStyle = "1 5";
92                                         break;
93                                 case "dotted2":
94                                         dashStyle = "3 4";
95                                         break;
96                                 case "plain":
97                                         // default works
98                                         // otherwise, "1 0" can be used
99                                         break;
100                                 }
101                                 if (dashStyle != null) {
102                                         jg.writeStringField("dashstyle", dashStyle);
103                                 }
104                         }
105                         jg.writeEndObject();
106                         
107                         jg.writeFieldName("hoverPaintStyle");
108                         jg.writeStartObject();
109                         jg.writeFieldName("strokeStyle");
110                         jg.writeObject(this.getHoverColor());
111                         jg.writeEndObject();
112                         
113                         // BEGIN: Overlays
114                         
115                         jg.writeFieldName("overlays");
116                         jg.writeStartArray();
117                         
118                         // source arrow head
119                         String head = this.getSourceArrowHead();
120                         if (!head.equals("none")) {
121                                 jg.writeStartArray();
122                                 jg.writeString(head);
123                                 
124                                 jg.writeStartObject();
125                                 
126                                 jg.writeFieldName("location");
127                                 jg.writeNumber(0);
128                                 
129                                 // arrow should point towards the node and not away from it
130                                 jg.writeFieldName("direction");
131                                 jg.writeNumber(-1);
132                                 
133                                 jg.writeFieldName("width");
134                                 jg.writeNumber(20);
135                                 
136                                 jg.writeFieldName("length");
137                                 jg.writeNumber(12);
138                                 
139                                 jg.writeEndObject();
140                                 jg.writeEndArray();
141                         }
142                         
143                         // target arrow head
144                         head = this.getTargetArrowHead();
145                         if (!head.equals("none")) {
146                                 jg.writeStartArray();
147                                 jg.writeString(head);
148                                 jg.writeStartObject();
149                                 jg.writeFieldName("location");
150                                 jg.writeNumber(1);
151                                 jg.writeFieldName("width");
152                                 jg.writeNumber(20);
153                                 jg.writeFieldName("length");
154                                 jg.writeNumber(12);
155                                 jg.writeEndObject();
156                                 jg.writeEndArray();
157                         }
158                         
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");
167                         jg.writeNumber(0.5);
168                         jg.writeEndObject();
169                         jg.writeEndArray();
170                         
171                         jg.writeEndArray();
172                         
173                         // END: Overlays
174                         
175                         jg.writeEndObject();
176                         
177                         jg.close();
178                 } catch (Exception e) {
179                         VisualAppearanceResource.logger.error(e.getMessage(), e);
180                         return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e).build();
181                 }
182                 String res = sw.toString();
183                 return Response.ok(res).build();
184         }
185         
186         private String getOtherAttributeWithDefault(QName qname, String def) {
187                 String res = this.otherAttributes.get(qname);
188                 if (StringUtils.isEmpty(res)) {
189                         return def;
190                 } else {
191                         return res;
192                 }
193         }
194         
195         /* * * source arrow head * * */
196         
197         public String getSourceArrowHead() {
198                 return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_ARROWHEAD_SOURCE, Defaults.DEFAULT_RT_ARROWHEAD_SOURCE);
199         }
200         
201         @PUT
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();
207                 }
208                 this.otherAttributes.put(VisualAppearanceResource.QNAME_ARROWHEAD_SOURCE, config);
209                 return BackendUtils.persist(this.res);
210         }
211         
212         /* * * target arrow head * * */
213         
214         public String getTargetArrowHead() {
215                 return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_ARROWHEAD_TARGET, Defaults.DEFAULT_RT_ARROWHEAD_TARGET);
216         }
217         
218         @PUT
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();
224                 }
225                 this.otherAttributes.put(VisualAppearanceResource.QNAME_ARROWHEAD_TARGET, config);
226                 return BackendUtils.persist(this.res);
227         }
228         
229         /* * *
230          *
231          * stroke dash array / represents the line
232          *
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"
236          *
237          * * * */
238         
239         public String getDash() {
240                 return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_DASH, Defaults.DEFAULT_RT_DASH);
241         }
242         
243         @PUT
244         @Consumes(MediaType.TEXT_PLAIN)
245         @Path("dash")
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();
249                 }
250                 this.otherAttributes.put(VisualAppearanceResource.QNAME_DASH, config);
251                 return BackendUtils.persist(this.res);
252         }
253         
254         /* * * stroke/line width * * */
255         
256         public String getLineWidth() {
257                 return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_LINEWIDTH, Defaults.DEFAULT_RT_LINEWIDTH);
258         }
259         
260         /* * * color * * */
261         
262         /**
263          * read by topologytemplateeditor.jsp via ${it.color}
264          */
265         public String getColor() {
266                 return BackendUtils.getColorAndSetDefaultIfNotExisting(this.getId().getParent().getXmlId().getDecoded(), QNames.QNAME_COLOR, this.otherAttributes, this.res);
267         }
268         
269         @PUT
270         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
271         @Path("color")
272         public Response onPutColor(@FormParam("color") String color) {
273                 this.otherAttributes.put(QNames.QNAME_COLOR, color);
274                 return BackendUtils.persist(this.res);
275         }
276         
277         /**
278          * read by topologytemplateeditor.jsp via ${it.hoverColor}
279          */
280         public String getHoverColor() {
281                 return this.getOtherAttributeWithDefault(VisualAppearanceResource.QNAME_HOVER_COLOR, Defaults.DEFAULT_RT_HOVER_COLOR);
282         }
283         
284         @PUT
285         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
286         @Path("hovercolor")
287         public Response onPutHoverColor(@FormParam("color") String color) {
288                 this.otherAttributes.put(VisualAppearanceResource.QNAME_HOVER_COLOR, color);
289                 return BackendUtils.persist(this.res);
290         }
291 }