aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.topologymodeler/src/main/webapp/WEB-INF/tags/common/topologycompletion/nodeTemplateSelector.tag
blob: 9d09fd3d8c3584cc13e0921e9e58caaa06105d94 (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
<%
/*******************************************************************************
 * Copyright (c) 2013 Pascal Hirmer.
 * 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:
 *    Pascal Hirmer - initial API and implementation
 *******************************************************************************/
%>
<%@tag language="java" pageEncoding="UTF-8" description="This tag is used to render Node and Relationship Templates for selection in a dialog."%>

<%-- attributes for the NodeTemplate selection --%>
<%@attribute name="templateURL" type="java.lang.String"%>
<%@attribute name="topologyName" type="java.lang.String"%>
<%@attribute name="topologyNamespace" type="java.lang.String"%>
<%@attribute name="repositoryURL" type="java.lang.String" %>
<%@attribute name="stName" type="java.lang.String" %>
<%@attribute name="choices" type="java.util.Map<org.eclipse.winery.model.tosca.TNodeTemplate, java.util.Map<org.eclipse.winery.model.tosca.TNodeTemplate, java.util.List<org.eclipse.winery.model.tosca.TEntityTemplate>>>"%>

<%@tag import="java.util.ArrayList"%>
<%@tag import="java.util.HashMap"%>
<%@tag import="java.util.List"%>
<%@tag import="java.util.Map"%>
<%@tag import="java.util.UUID"%>
<%@tag import="javax.xml.namespace.QName"%>
<%@tag import="org.eclipse.winery.model.tosca.TEntityTemplate"%>
<%@tag import="org.eclipse.winery.model.tosca.TNodeTemplate"%>
<%@tag import="org.eclipse.winery.model.tosca.TRelationshipTemplate"%>
<%@tag import="org.eclipse.winery.model.tosca.TRelationshipType"%>
<%@tag import="org.eclipse.winery.repository.client.WineryRepositoryClientFactory"%>
<%@tag import="org.eclipse.winery.repository.client.IWineryRepositoryClient"%>
<%@tag import="org.eclipse.winery.common.Util"%>

<%@taglib prefix="ntrq" tagdir="/WEB-INF/tags/common/templates/nodetemplates/reqscaps" %>
<%@taglib prefix="nt"   tagdir="/WEB-INF/tags/common/templates/nodetemplates" %>

<div id="nodeTemplateSelector">
	<p> There are several possible Node Templates to be inserted. <br> Please select your desired NodeTemplate: </p>

	<%
		// the pixel distance between the displayed NodeTemplates
		final int NODE_TEMPLATE_DISTANCE = 150;

		IWineryRepositoryClient client = WineryRepositoryClientFactory.getWineryRepositoryClient();
		client.addRepository(repositoryURL);

		// instantiate  variables
		Map<String, String> idMap = new HashMap<String, String>();
		List<TRelationshipTemplate> possibleConnections = new ArrayList<TRelationshipTemplate>();
		String sourceId = "";
		String randomId = "";
		String id = "";

		// a counter used for an ID
		int counter = 0;

		// used for the position of the NodeTemplate in the EditorArea
		int topCounter = 0;
	%>
	<script>
		// array to collect the created IDs
		IDs = new Array();

		// save all created connections in an array to be able to detach them after the selection
		Connections = new Array();
	</script>
	<%
		// render a topology for every choice to be displayed in the dialog
		for (TNodeTemplate nt: choices.keySet()) {

			Map<TNodeTemplate, List<TEntityTemplate>> entityTemplates = choices.get(nt);

			for (TNodeTemplate choice: entityTemplates.keySet()) {
				id = "choice" + Integer.toString(counter);

				%>
				<div id="proposalEditorArea">
				<div id="proposaldrawingarea">
				<%

				topCounter = 0;
			%>
			<nt:nodeTemplateRenderer client="<%=client%>" relationshipTypes="<%=client.getAllTypes(TRelationshipType.class)%>" repositoryURL='<%=repositoryURL%>' nodeTemplate="<%=nt%>" top="<%=Integer.toString(topCounter)%>" left='<%="0"%>'/>
			<script>

				//Map IDs here. ID mapping is necessary to avoid conflict with the modelled NodeTemplates in the background.
				<%
					randomId = UUID.randomUUID().toString();
				%>
					document.getElementById("<%=nt.getId()%>").id = "<%=randomId%>";
					IDs.push("<%=randomId%>");
				<%
					idMap.put(nt.getId(), randomId);
				%>
			</script>
			<%

			topCounter = topCounter + NODE_TEMPLATE_DISTANCE;
			%>
			<!-- use the nodeTemplateRenderer tag to render NodeTemplates-->
			<nt:nodeTemplateRenderer client="<%=client%>" relationshipTypes="<%=client.getAllTypes(TRelationshipType.class)%>" repositoryURL='<%=repositoryURL%>' nodeTemplate="<%=choice%>" top="<%=Integer.toString(topCounter)%>" left='<%="0"%>'/>
			<script>
			    //Map IDs here
				<%
					randomId = UUID.randomUUID().toString();
				%>
					document.getElementById("<%=choice.getId()%>").id = "<%=randomId%>";
					IDs.push("<%=randomId%>");
				<%
					idMap.put(choice.getId(), randomId);
				%>
			</script>
			</div>
			</div>
			<% if (entityTemplates.get(choice).size() > 1) { %>
				<p> There are several possible Relationship Templates to connect the Node Templates <%=nt.getName()%> and <%=choice.getName()%>. Please choose at least one connection: </p>
			<%}
			for (TEntityTemplate rtChoice: entityTemplates.get(choice)) {

				TRelationshipTemplate connector = (TRelationshipTemplate) rtChoice;
				if (entityTemplates.get(choice).size() > 1) {
				%>
					<input checked="checked" id="<%=connector.getName()%>" name="<%=connector.getName()%>" type="checkbox" value="<%=connector.getName()%>">	<%=connector.getName()%> <br/>
				<%
				}
				sourceId = ((TNodeTemplate) connector.getSourceElement().getRef()).getId();
				String targetId = ((TNodeTemplate) connector.getTargetElement().getRef()).getId();
				QName type = connector.getType();
				String visualSourceId = idMap.get(sourceId);
				String visualTargetId = idMap.get(targetId);
				%>
				<script>
					// connect the rendered NodeTemplates
					require(["winery-common-topologyrendering"], function(wct) {
						wct.initNodeTemplate(jsPlumb.getSelector(".NodeTemplateShape:not('.hidden')"), true);
					});
					var c;
					require(["jsplumb"], function(_jsPlumb) {
						_jsPlumb.ready(function() {
							c = _jsPlumb.connect({
								source:"<%=visualSourceId%>",
								target:"<%=visualTargetId%>",
								endpoint:"Blank",
								type: "<%=type%>"
							});
							Connections.push(c);
						})
					});

				</script>
			<%}
		%>
		<br>
		<button type="button" class="btn btn-primary btn-default" data-dismiss="modal" id="<%=id%>" value='<%=choice.getName()%>' onclick="onSelected<%=choice.getName()%>()">Use Template: <%=choice.getName()%></button>
		<script>

			/**
			 * Handles a click on the "Select" button.
			 *
			 * This selection handler method is created for every NodeTemplate that can be chosen by the user.
			 * This is realized by inserting the unique names of the NodeTemplate choices in the method name via JSP scriptlet.
			 */
			function onSelected<%=choice.getName()%>() {

				SelectedRTs = new Array();

				for (var i = 0; i < Connections.length; i++) {
					jsPlumb.detach(Connections[i]);
				}

				<%
				if (entityTemplates.get(choice).size() == 1) {
				%>
					SelectedRTs.push("<%=((TRelationshipTemplate) entityTemplates.get(choice).get(0)).getName()%>");
				<%
				} else if (entityTemplates.get(choice).size() > 1) {
					for (TEntityTemplate rtChoice: entityTemplates.get(choice)) {
					TRelationshipTemplate connector = (TRelationshipTemplate) rtChoice;
					%>
					if (document.getElementById("<%=connector.getName()%>").checked) {
						SelectedRTs.push(document.getElementById("<%=connector.getName()%>").value);
					}
				<%
				}}
				%>
				SelectedItems = new Array();
				SelectedItems.push(document.getElementById("<%=id%>").value);

				if (SelectedItems.length == 0) {
					vShowError("Please selected at least one Relationship Template.");
				} else {
					// add the selected Templates to the topology and restart the completion
					var selectedNodeTemplates = JSON.stringify(SelectedItems);
					var selectedRelationshipTemplates = JSON.stringify(SelectedRTs);
					$.post("jsp/topologyCompletion/selectionHandler.jsp", {topology: topology, allChoices: choices, selectedNodeTemplates: selectedNodeTemplates, selectedRelationshipTemplates: selectedRelationshipTemplates},
						function(data){
							require(["winery-topologycompletion"], function(completer) {
								completer.restartCompletion(data, document.getElementById('overwriteTopology').checked,document.getElementById('openInNewWindow').checked,
									document.getElementById('topologyName').value, document.getElementById('topologyNamespace').value, true, "<%=stName%>",
									"<%=templateURL%>", "<%=repositoryURL%>");
							});
						}
					);
				}
			}
		</script>
		<%
		counter++;
		}
	}%>
	<br>
	<br>
	<br>
	<button type="button" class="btn btn-primary btn-default" data-dismiss="modal" id="cancel" onclick="onSelectedCancel()">Cancel Automatic Completion</button>
	<p><i> Press this button if you want to continue the completion manually.</i> </p>
	<script>
		// save topology and refresh the page
		function onSelectedCancel() {
			$.post("jsp/topologyCompletion/topologySaver.jsp", {topology: topology, templateURL: "<%=templateURL%>", repositoryURL: "<%=repositoryURL%>", topologyName: "<%=topologyName%>", topologyNamespace: "<%=topologyNamespace%>", overwriteTopology: "true"},
				function(callback){
					document.location.reload(true);
				}
			);
		}
	</script>
</div>