aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.topologymodeler/src/main/webapp/WEB-INF/tags/common/topologycompletion/relationshipTemplateSelector.tag
blob: 58e2d854cfeb26920f30eadbabb2225a7185361a (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
<%
/*******************************************************************************
 * 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 Relationship Templates for selection in a dialog."%>

<%-- attributes for the topology 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.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" %>
<%@taglib prefix="tc"   tagdir="/WEB-INF/tags/common/topologycompletion"%>

<div id="relationshipTemplateSelector">
<p> There are several possible Relationship Templates for a connection. <br> Please select your desired connection(s): </p>
<script>
	// save all created connections in an array to be able to detach them after the selection
	Connections = new Array();
</script>
<%
	// the pixel distance between the displayed NodeTemplates
	final int NODE_TEMPLATE_DISTANCE = 150;

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

	Map<String, String> idMap = new HashMap<String, String>();
	String sourceId = "";
	String id = "choice";

	// used for the position of the NodeTemplate in the EditorArea
	int topCounter = 0;

	List<TRelationshipTemplate> possibleConnections = new ArrayList<TRelationshipTemplate>();

	for (TEntityTemplate choice: choices) {
		if (choice instanceof TRelationshipTemplate) {
			possibleConnections.add((TRelationshipTemplate) choice);
		}
	}
	for (TRelationshipTemplate connector: possibleConnections) { %>
		<div id="proposalEditorArea">
		<div id="proposaldrawingarea">
		<div id="allRelationships">
		<%
		topCounter = 0;

		for (TEntityTemplate choice: choices) {
			if (choice instanceof TNodeTemplate) {
				TNodeTemplate nodeTemplate = (TNodeTemplate) choice;

				topCounter = topCounter + NODE_TEMPLATE_DISTANCE;
				%>
				<nt:nodeTemplateRenderer client="<%=client%>" relationshipTypes="<%=client.getAllTypes(TRelationshipType.class)%>" repositoryURL='<%=repositoryURL%>' nodeTemplate="<%=nodeTemplate%>" top="<%=Integer.toString(topCounter)%>" left='<%="0"%>'/>
				<script>
					//Map IDs here
					<%
						String randomId = UUID.randomUUID().toString();
					%>
						document.getElementById("<%=nodeTemplate.getId()%>").id = "<%=randomId%>";
					<%
						idMap.put(nodeTemplate.getId(), randomId);
					%>
				</script>
				<%
			}
		}

		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>
		</div>
		</div>
		</div>
		<input id="<%=id%>" name="<%=id%>" type="checkbox" value="<%=connector.getName()%>"> <%=connector.getName()%> <br>

	<%}%>
	<button type="button" class="btn btn-primary btn-default" id="btnUseSelection" onclick="useRelationshipTemplateSelection()">Use Selection</button>
	<script>
		function useRelationshipTemplateSelection() {
			// add the selected RelationshipTemplates to the topology and restart the completion
			SelectedItems = new Array();
			for (var i= 0; i < document.getElementById("rtchoices").children[0].choice.length; i++) {
				if (document.getElementById("rtchoices").children[0].choice[i].checked == true) {
					SelectedItems.push(document.getElementById("rtchoices").children[0].choice[i].value);
				}
			}

			if (SelectedItems.length == 0) {
				vShowError("Please selected at least one Relationship Template.");
			} else {
				$('#chooseRelationshipTemplateDiag').modal('hide');
				var selectedRelationshipTemplates = JSON.stringify(SelectedItems);
				// add selected RelationshipTemplate(s) to the topology
				$.post("jsp/topologyCompletion/selectionHandler.jsp", {topology: topology, allChoices: choices, selectedRelationshipTemplates: selectedRelationshipTemplates},
					function(data) {
						require(["winery-topologycompletion"], function(completer) {
							completer.restartCompletion(data, document.getElementById('overwriteTopology').checked,document.getElementById('openInNewWindow').checked,
								topologyName, topologyNamespace, true, "<%=stName%>",
								"<%=templateURL%>", "<%=repositoryURL%>");
						});
					}
				);
			}
		}
	</script>
</div>