Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.topologymodeler / src / main / webapp / WEB-INF / tags / common / topologycompletion / relationshipTemplateSelector.tag
1 <%
2 /*******************************************************************************
3  * Copyright (c) 2013 Pascal Hirmer.
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  *    Pascal Hirmer - initial API and implementation
12  *******************************************************************************/
13 %>
14
15 <%@tag language="java" pageEncoding="UTF-8" description="This tag is used to render Relationship Templates for selection in a dialog."%>
16
17 <%-- attributes for the topology selection --%>
18 <%@attribute name="templateURL" type="java.lang.String"%>
19 <%@attribute name="topologyName" type="java.lang.String"%>
20 <%@attribute name="topologyNamespace" type="java.lang.String"%>
21 <%@attribute name="repositoryURL" type="java.lang.String" %>
22 <%@attribute name="stName" type="java.lang.String" %>
23 <%@attribute name="choices" type="java.util.List<org.eclipse.winery.model.tosca.TEntityTemplate>"%>
24
25 <%@tag import="java.util.ArrayList"%>
26 <%@tag import="java.util.HashMap"%>
27 <%@tag import="java.util.List"%>
28 <%@tag import="java.util.Map"%>
29 <%@tag import="java.util.UUID"%>
30 <%@tag import="javax.xml.namespace.QName"%>
31 <%@tag import="org.eclipse.winery.model.tosca.TEntityTemplate"%>
32 <%@tag import="org.eclipse.winery.model.tosca.TNodeTemplate"%>
33 <%@tag import="org.eclipse.winery.model.tosca.TRelationshipTemplate"%>
34 <%@tag import="org.eclipse.winery.model.tosca.TRelationshipType"%>
35 <%@tag import="org.eclipse.winery.repository.client.WineryRepositoryClientFactory"%>
36 <%@tag import="org.eclipse.winery.repository.client.IWineryRepositoryClient"%>
37 <%@tag import="org.eclipse.winery.common.Util"%>
38
39 <%@taglib prefix="ntrq" tagdir="/WEB-INF/tags/common/templates/nodetemplates/reqscaps" %>
40 <%@taglib prefix="nt"   tagdir="/WEB-INF/tags/common/templates/nodetemplates" %>
41 <%@taglib prefix="tc"   tagdir="/WEB-INF/tags/common/topologycompletion"%>
42
43 <div id="relationshipTemplateSelector">
44 <p> There are several possible Relationship Templates for a connection. <br> Please select your desired connection(s): </p>
45 <script>
46         // save all created connections in an array to be able to detach them after the selection
47         Connections = new Array();
48 </script>
49 <%
50         // the pixel distance between the displayed NodeTemplates
51         final int NODE_TEMPLATE_DISTANCE = 150;
52
53         IWineryRepositoryClient client = WineryRepositoryClientFactory.getWineryRepositoryClient();
54         client.addRepository(repositoryURL);
55
56         Map<String, String> idMap = new HashMap<String, String>();
57         String sourceId = "";
58         String id = "choice";
59
60         // used for the position of the NodeTemplate in the EditorArea
61         int topCounter = 0;
62
63         List<TRelationshipTemplate> possibleConnections = new ArrayList<TRelationshipTemplate>();
64
65         for (TEntityTemplate choice: choices) {
66                 if (choice instanceof TRelationshipTemplate) {
67                         possibleConnections.add((TRelationshipTemplate) choice);
68                 }
69         }
70         for (TRelationshipTemplate connector: possibleConnections) { %>
71                 <div id="proposalEditorArea">
72                 <div id="proposaldrawingarea">
73                 <div id="allRelationships">
74                 <%
75                 topCounter = 0;
76
77                 for (TEntityTemplate choice: choices) {
78                         if (choice instanceof TNodeTemplate) {
79                                 TNodeTemplate nodeTemplate = (TNodeTemplate) choice;
80
81                                 topCounter = topCounter + NODE_TEMPLATE_DISTANCE;
82                                 %>
83                                 <nt:nodeTemplateRenderer client="<%=client%>" relationshipTypes="<%=client.getAllTypes(TRelationshipType.class)%>" repositoryURL='<%=repositoryURL%>' nodeTemplate="<%=nodeTemplate%>" top="<%=Integer.toString(topCounter)%>" left='<%="0"%>'/>
84                                 <script>
85                                         //Map IDs here
86                                         <%
87                                                 String randomId = UUID.randomUUID().toString();
88                                         %>
89                                                 document.getElementById("<%=nodeTemplate.getId()%>").id = "<%=randomId%>";
90                                         <%
91                                                 idMap.put(nodeTemplate.getId(), randomId);
92                                         %>
93                                 </script>
94                                 <%
95                         }
96                 }
97
98                 sourceId = ((TNodeTemplate) connector.getSourceElement().getRef()).getId();
99                 String targetId = ((TNodeTemplate) connector.getTargetElement().getRef()).getId();
100                 QName type = connector.getType();
101
102                 String visualSourceId = idMap.get(sourceId);
103                 String visualTargetId = idMap.get(targetId);
104                 %>
105                 <script>
106                         // connect the rendered NodeTemplates
107                         require(["winery-common-topologyrendering"], function(wct) {
108                                 wct.initNodeTemplate(jsPlumb.getSelector(".NodeTemplateShape:not('.hidden')"), true);
109                         });
110                         var c;
111                         require(["jsplumb"], function(_jsPlumb) {
112                                 _jsPlumb.ready(function() {
113                                         c = _jsPlumb.connect({
114                                                 source:"<%=visualSourceId%>",
115                                                 target:"<%=visualTargetId%>",
116                                                 endpoint:"Blank",
117                                                 type: "<%=type%>"
118                                         });
119                                         Connections.push(c);
120                                 })
121                         });
122                 </script>
123                 </div>
124                 </div>
125                 </div>
126                 <input id="<%=id%>" name="<%=id%>" type="checkbox" value="<%=connector.getName()%>"> <%=connector.getName()%> <br>
127
128         <%}%>
129         <button type="button" class="btn btn-primary btn-default" id="btnUseSelection" onclick="useRelationshipTemplateSelection()">Use Selection</button>
130         <script>
131                 function useRelationshipTemplateSelection() {
132                         // add the selected RelationshipTemplates to the topology and restart the completion
133                         SelectedItems = new Array();
134                         for (var i= 0; i < document.getElementById("rtchoices").children[0].choice.length; i++) {
135                                 if (document.getElementById("rtchoices").children[0].choice[i].checked == true) {
136                                         SelectedItems.push(document.getElementById("rtchoices").children[0].choice[i].value);
137                                 }
138                         }
139
140                         if (SelectedItems.length == 0) {
141                                 vShowError("Please selected at least one Relationship Template.");
142                         } else {
143                                 $('#chooseRelationshipTemplateDiag').modal('hide');
144                                 var selectedRelationshipTemplates = JSON.stringify(SelectedItems);
145                                 // add selected RelationshipTemplate(s) to the topology
146                                 $.post("jsp/topologyCompletion/selectionHandler.jsp", {topology: topology, allChoices: choices, selectedRelationshipTemplates: selectedRelationshipTemplates},
147                                         function(data) {
148                                                 require(["winery-topologycompletion"], function(completer) {
149                                                         completer.restartCompletion(data, document.getElementById('overwriteTopology').checked,document.getElementById('openInNewWindow').checked,
150                                                                 topologyName, topologyNamespace, true, "<%=stName%>",
151                                                                 "<%=templateURL%>", "<%=repositoryURL%>");
152                                                 });
153                                         }
154                                 );
155                         }
156                 }
157         </script>
158 </div>