Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.topologymodeler / src / main / webapp / WEB-INF / tags / common / templates / registerConnectionTypesAndConnectNodeTemplates.tag
1 <%--
2 /*******************************************************************************
3  * Copyright (c) 2012-2014 University of Stuttgart.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and the Apache License 2.0 which both accompany this distribution,
7  * and are available at http://www.eclipse.org/legal/epl-v10.html
8  * and http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Contributors:
11  *    Oliver Kopp - initial API and implementation and/or initial documentation
12  *******************************************************************************/
13 --%>
14 <%@tag description="Registers the connection types to jsPlumb" pageEncoding="UTF-8"%>
15
16 <%@attribute name="relationshipTypes" description="the known relationship types" required="true" type="java.util.Collection"%>
17 <%@attribute name="relationshipTemplates" description="the relationship templates to render" required="true" type="java.util.Collection"%>
18 <%@attribute name="ondone" description="JavaScript code executed when everything has been done" required="false"%>
19 <%@attribute name="repositoryURL" required="true" %>
20 <%@attribute name="readOnly" required="false" type="java.lang.Boolean"%>
21
22 <%@tag import="java.util.Collection"%>
23 <%@tag import="org.eclipse.winery.model.tosca.TRequirement"%>
24 <%@tag import="org.eclipse.winery.model.tosca.TCapability"%>
25 <%@tag import="org.eclipse.winery.model.tosca.TNodeTemplate"%>
26 <%@tag import="org.eclipse.winery.model.tosca.TRelationshipType"%>
27 <%@tag import="org.eclipse.winery.model.tosca.TRelationshipTemplate"%>
28 <%@tag import="org.eclipse.winery.model.tosca.TRelationshipTemplate.SourceElement"%>
29 <%@tag import="org.eclipse.winery.model.tosca.TRelationshipTemplate.TargetElement"%>
30 <%@tag import="org.eclipse.winery.common.Util"%>
31
32 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
33
34 <%
35 Collection<TRelationshipType> relationshipTypesCasted = (Collection<TRelationshipType>) relationshipTypes;
36 Collection<TRelationshipTemplate> relationshipTemplatesCasted = (Collection<TRelationshipTemplate>) relationshipTemplates;
37 %>
38
39 <script>
40 // we should load jquery and jquery.ui to have it available at jsPlumb
41 // however, jquery.ui cannot be loaded as it conflicts with bootstrap (when using toogle buttons)
42 require(["jquery", "jsplumb", "winery-common-topologyrendering"], function(globallyavailablea, jsPlumb, wct) {
43         jsPlumb.bind("ready", function() {
44                 /**
45                  * Shows error if req or cap does not exist
46                  */
47                 function getNodeTemplateIdForReqOrCapId(id) {
48                         var reqOrCap = $("#" + id);
49                         if (reqOrCap.length == 0) {
50                                 vShowError("Requirement/Capability with id " + id + " not found");
51                         }
52                         var res = reqOrCap.parent().parent().parent().attr("id");
53                         return res;
54                 }
55
56                 // register the "selected" type to enable selection of arrows
57                 jsPlumb.registerConnectionTypes({
58                         "selected":{
59                                 paintStyle:{ strokeStyle:"red", lineWidth:5 },
60                                 hoverPaintStyle:{ lineWidth: 7 }
61                         }
62                 });
63
64 <%
65                 int i=0;
66                 String when = "";
67                 String whenRes = "";
68                 if (relationshipTypesCasted.isEmpty()) {
69 %>
70                         vShowError("No relationship types exist. Please add relationship types to the repository");
71 <%
72                 }
73                 for (TRelationshipType relationshipType: relationshipTypesCasted) {
74                         String fnName = "ajaxRTdata" + i;
75                         when = when + fnName + "(), ";
76                         whenRes = whenRes + fnName + ", ";
77 %>
78                         function <%=fnName%>() { return $.ajax({
79                                 url: "<%=repositoryURL%>/relationshiptypes/<%=Util.DoubleURLencode(relationshipType.getTargetNamespace())%>/<%=Util.DoubleURLencode(relationshipType.getName())%>/visualappearance/",
80                                 dataType: "json",
81                                 success: function(data, textStatus, jqXHR) {
82                                         jsPlumb.registerConnectionType(
83                                                 "{<%=relationshipType.getTargetNamespace()%>}<%=relationshipType.getName()%>",
84                                                 data);
85                                 },
86                                 error: function(jqXHR, textStatus, errorThrown) {
87                                         vShowAJAXError("Could not load relationship type {<%=relationshipType.getTargetNamespace()%>}<%=relationshipType.getName()%>", jqXHR, errorThrown);
88                                 }
89                         });};
90 <%
91                         i++;
92                 }
93
94                 if (!relationshipTypesCasted.isEmpty()) {
95                         // strip last comma
96                         when = when.substring(0, when.length()-2);
97                         whenRes = whenRes.substring(0, whenRes.length()-2);
98                 }
99 %>
100                 // as soon as all relationship types are registered as jsPlumb object,
101                 // create connection end points and connect the nodes
102                 $.when(<%=when%>).done(function(<%=whenRes%>){
103                         require(["winery-common-topologyrendering"], function(wct) {
104                                 // A NodeTemplateShape also appears in the palette. There, it is hidden.
105                                 // These should not be initialized as the template will be initialized later on
106
107                                 <c:if test="${readOnly}">wct.setReadOnly();</c:if>
108
109                                 // Quick hack: All node templates are draggable, even in the readonly view
110                                 wct.initNodeTemplate(jsPlumb.getSelector(".NodeTemplateShape:not('.hidden')"), true);
111
112                                 var sourceId;
113                                 var targetId;
114 <%
115                                 for (TRelationshipTemplate relationshipTemplate : relationshipTemplatesCasted) {
116 %>
117                                         var req = undefined;
118                                         var cap = undefined;
119 <%
120                                         // Source: Either NodeTemplate or Requirement
121                                         SourceElement sourceElement = relationshipTemplate.getSourceElement();
122                                         if (sourceElement == null) {
123                                                 %>vShowError("sourceElement is null for <%=relationshipTemplate.getId()%>");<%
124                                                 continue;
125                                         }
126                                         Object source = sourceElement.getRef();
127                                         if (source instanceof TRequirement) {
128 %>
129                                                 req = "<%=((TRequirement)source).getId()%>";
130                                                 sourceId = getNodeTemplateIdForReqOrCapId(req);
131 <%
132                                         } else {
133                                                 TNodeTemplate sourceT = (TNodeTemplate) source;
134                                                 if (sourceT == null) {
135                                                         %>vShowError("sourceElement.getRef() is null for <%=relationshipTemplate.getId()%>");<%
136                                                         continue;
137                                                 }
138 %>
139                                                 sourceId = "<%=sourceT.getId()%>";
140 <%
141                                         }
142
143                                         // Target: Either NodeTemplate or Requirement
144                                         TargetElement targetElement = relationshipTemplate.getTargetElement();
145                                         if (targetElement == null) {
146                                                 %>vShowError("targetElement is null for <%=relationshipTemplate.getId()%>");<%
147                                                 continue;
148                                         }
149                                         Object target = targetElement.getRef();
150                                         if (target instanceof TCapability) {
151 %>
152                                                 cap = "<%=((TCapability)target).getId()%>";
153                                                 targetId = getNodeTemplateIdForReqOrCapId(cap);
154 <%
155                                         } else {
156                                                 TNodeTemplate targetT = (TNodeTemplate) target;
157                                                 if (targetT == null) {
158                                                         %>vShowError("targetElement.getRef() is null for <%=relationshipTemplate.getId()%>");<%
159                                                         continue;
160                                                 }
161 %>
162                                                 targetId = "<%=targetT.getId()%>";
163 <%
164                                         }
165 %>
166                                         var c = jsPlumb.connect({
167                                                 source: sourceId,
168                                                 target: targetId,
169                                                 type:"<%=relationshipTemplate.getType()%>"
170                                         });
171                                         wct.handleConnectionCreated(c);
172                                         // we have to store the TOSCA id as jsPlumb does not allow to pass ids from user's side
173                                         // we could overwrite c.id, but we are not aware the side effects...
174                                         winery.connections[c.id].id = "<%=relationshipTemplate.getId()%>";
175                                         if (req) {
176                                                 winery.connections[c.id].req = req;
177                                         }
178                                         if (cap) {
179                                                 winery.connections[c.id].cap = cap;
180                                         }
181 <%
182                                         if (relationshipTemplate.getName() != null) {
183 %>
184                                                 winery.connections[c.id].name = "<%=relationshipTemplate.getName()%>";
185 <%
186                                         }
187                                 }
188 %>
189
190                                 // all connections are there
191                                 // we can register the events now
192
193                                 jsPlumb.bind("connection", wct.handleConnectionCreated);
194
195                                 ${ondone}
196
197                         // end of the when waiting for all relationship types
198                         });
199                 // end of require binding
200                 });
201         // jsPlumb.ready
202         });
203 // requirejs
204 });
205 </script>