1 /*******************************************************************************
2 * Copyright (c) 2012-2015 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
10 * Oliver Kopp - initial API and implementation and/or initial documentation
11 *******************************************************************************/
14 * This file contains supporting functions for the topoplogy modeler
17 // although XMLWriter ist not an AMD module, requirejs does not complain when loading it
18 ["winery-support-common", "XMLWriter"],
20 // has to be consistent with {@link org.eclipse.winery.common.constants.Namespaces}
21 var TOSCA_NAMESPACE = "http://docs.oasis-open.org/tosca/ns/2011/12";
22 var TOSCA_WINERY_EXTENSIONS_NAMESPACE ="http://www.opentosca.org/winery/extensions/tosca/2013/02/12";
24 var topologyTemplateURL;
28 setTopologyTemplateURL: function(url) {
29 topologyTemplateURL = url;
31 getTopologyTemplateAsXML: getTopologyTemplateAsXML,
33 TOSCA_NAMESPACE: TOSCA_NAMESPACE,
34 TOSCA_WINERY_EXTENSIONS_NAMESPACE: TOSCA_WINERY_EXTENSIONS_NAMESPACE
38 function writeReqOrCaps(elements, xmlw, globalWrapperElementName, singleElementWrapperName) {
39 if (elements.length != 0) {
40 xmlw.writeStartElement(globalWrapperElementName);
42 $.each(elements, function(i,e) {
43 xmlw.writeStartElement(singleElementWrapperName);
45 xmlw.writeAttributeString("id", e.children(".id").text());
46 xmlw.writeAttributeString("name", e.children(".name").text());
47 writeType(xmlw, e.children(".type").children("a").data("qname"));
48 savePropertiesFromDivToXMLWriter(e.children("div.propertiesContainer"), xmlw);
49 xmlw.writeEndElement();
52 xmlw.writeEndElement();
61 $("#saveBtn").button('loading');
64 url: topologyTemplateURL,
66 contentType: 'text/xml',
67 data: getTopologyTemplateAsXML(false),
68 success: function(data, textStatus, jqXHR) {
69 $("#saveBtn").button('reset');
70 vShowSuccess("successfully saved.");
72 error: function(jqXHR, textStatus, errorThrown) {
73 $("#saveBtn").button('reset');
74 vShowAJAXError("Could not save", jqXHR, errorThrown);
80 * Creates an XML String of the modelled topology template.
82 function getTopologyTemplateAsXML(needsDefinitionsTag) {
84 var xmlw = new XMLWriter("utf-8");
85 xmlw.writeStartDocument();
87 if (needsDefinitionsTag) {
88 xmlw.writeStartElement("Definitions");
89 xmlw.writeAttributeString("xmlns", TOSCA_NAMESPACE);
90 xmlw.writeAttributeString("xmlns:winery", TOSCA_WINERY_EXTENSIONS_NAMESPACE);
92 xmlw.writeStartElement("ServiceTemplate");
93 xmlw.writeAttributeString("xmlns", TOSCA_NAMESPACE);
94 xmlw.writeAttributeString("xmlns:winery", TOSCA_WINERY_EXTENSIONS_NAMESPACE);
96 xmlw.writeStartElement("TopologyTemplate");
97 xmlw.writeAttributeString("xmlns", TOSCA_NAMESPACE);
98 xmlw.writeAttributeString("xmlns:winery", TOSCA_WINERY_EXTENSIONS_NAMESPACE);
99 $("div.NodeTemplateShape").not(".hidden").each (function() {
100 xmlw.writeStartElement("NodeTemplate");
102 var id = $(this).attr("id");
104 var headerContainer = $(this).children("div.headerContainer");
105 var name = headerContainer.children("div.name").text();
106 var typeQNameStr = headerContainer.children("span.typeQName").text();
107 var minmaxdiv = headerContainer.children("div.minMaxInstances");
108 var min = minmaxdiv.children("span.minInstances").text();
109 var max = minmaxdiv.children("span.maxInstances").text();
113 var x = $(this).css("left");
114 x = x.substring(0, x.indexOf("px"));
115 var y = $(this).css("top");
116 y = y.substring(0, y.indexOf("px"));
118 xmlw.writeAttributeString("id", id);
120 xmlw.writeAttributeString("name", name);
122 writeType(xmlw, typeQNameStr);
124 xmlw.writeAttributeString("minInstances", min);
127 xmlw.writeAttributeString("maxInstances", max);
129 xmlw.writeAttributeString("winery:x", x);
130 xmlw.writeAttributeString("winery:y", y);
133 savePropertiesFromDivToXMLWriter($(this).children("div.propertiesContainer"), xmlw);
137 $(this).children("div.requirementsContainer").children("div.content").children("div.reqorcap"),
144 $(this).children("div.capabilitiesContainer").children("div.content").children("div.reqorcap"),
150 w.writeCollectionDefinedByATextArea(xmlw,
151 $(this).children("div.policiesContainer").children("div.content").children("div.policy"),
154 /** Deployment Artifacts **/
155 var das = $(this).children("div.deploymentArtifactsContainer").children("div.content").children("div.deploymentArtifact");
156 if (das.length != 0) {
157 xmlw.writeStartElement("DeploymentArtifacts");
158 das.each(function(i,e) {
159 // the textarea contains a valid deployment artifact xml
160 var xml = $(e).children("textarea").val();
163 xmlw.writeEndElement();
167 xmlw.writeEndElement();
169 jsPlumb.select().each(function(connection) {
170 xmlw.writeStartElement("RelationshipTemplate");
171 var id = connection.id;
172 var typeQNameStr = connection.getType()[0];
174 var connData = winery.connections[id];
176 vShowError("Error in the internal data structure: Id " + id + " not found");
180 xmlw.writeAttributeString("id", connData.id);
181 if (connData.name != "") {
182 xmlw.writeAttributeString("name", connData.name);
184 writeType(xmlw, typeQNameStr);
186 if (typeof connData.propertiesContainer !== "undefined") {
187 savePropertiesFromDivToXMLWriter(connData.propertiesContainer, xmlw);
190 xmlw.writeStartElement("SourceElement");
192 // conn starts at a requirement
193 xmlw.writeAttributeString("ref", connData.req);
195 // conn starts at a node template
196 xmlw.writeAttributeString("ref", connection.sourceId);
198 xmlw.writeEndElement();
199 xmlw.writeStartElement("TargetElement");
201 // conn ends at a capability
202 xmlw.writeAttributeString("ref", connData.cap);
204 // conn ends at a node template
205 xmlw.writeAttributeString("ref", connection.targetId);
207 xmlw.writeEndElement();
209 xmlw.writeEndElement();
212 if (needsDefinitionsTag) {
213 xmlw.writeEndElement();
214 xmlw.writeEndElement();
217 xmlw.writeEndDocument();
222 function writeQNameAttribute(w, nsPrefix, qnameStr) {
223 var qname = getQName(qnameStr);
224 w.writeAttributeString("xmlns:" + nsPrefix, qname.namespace);
225 w.writeAttributeString("type", nsPrefix + ":" + qname.localName);
228 function writeType(w, typeQNameStr) {
229 writeQNameAttribute(w, "ty", typeQNameStr);