Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.topologymodeler / src / main / webapp / js / winery-topologycompletion.js
1 /*******************************************************************************
2  * Copyright (c) 2013 Pascal Hirmer.
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
8  *
9  * Contributors:
10  *    Pascal Hirmer - initial API and implementation
11  *******************************************************************************/
12
13 define(function() {
14
15         /**
16          * This module handles a click on "Complete Topology" in the enterTopologyCompletionInformation dialog.
17          */
18         var module = {
19                 complete: complete,
20                 restartCompletion: restartCompletion
21         };
22         return module;
23
24         /**
25          * This function start the topology completion. With information from the
26          * PolicyInformationDialog the topologyCompletion.jsp is called which will invoke the
27          * Topology Completion java component. The parameters determine if the current topology
28          * will be overwritten or if a new topology is created.
29          *
30          * @param (String)  overwriteTopology
31          *                                              determines how to save the topology. If true, the current topology is overwritten.
32          * @param (Boolean) openInNewWindow
33          *                                              determines if the result is opened in a new window
34          * @param (String)  topologyName
35          *                                              the name of the new topology
36          * @param (String)  topologyNamespace
37          *                                              the namespace of the new topology
38          * @param (Boolean) stepByStep
39          *                                              if true, the completion will be processed step by step
40          * @param (String)  repositoryURL
41          *                                              the URL to the repository the topology is saved to
42          * @param (String)      toscaNamespace
43          *                                              the namespace URL of TOSCA
44          * @param (String)      wineryNamespace
45          *                                              the namespace URL of winery
46          * @param (String)      serviceTemplateName
47          *                                              the name of the service template containing the displayed topology template
48          * @param (String)      topologyTemplateURL
49          *                                              the URL to the displayed topology template
50          */
51         function complete(overwriteTopology, openInNewWindow, topologyName, topologyNamespace, stepByStep, repositoryURL, serviceTemplateName, topologyTemplateURL) {
52
53                 // if the user wants to create a new topology, a post call is sent to the repository adding
54                 // the new ServiceTemplate
55                 if (!overwriteTopology) {
56                         var dataToSend = "name=" + topologyName + "&namespace=" + topologyNamespace;
57                         var url = repositoryURL + "/servicetemplates/";
58                         $.ajax(
59                                 {
60                                         type: "POST",
61                                         async: false,
62                                         url: url,
63                                         "data": dataToSend,
64                                         dataType: "text",
65                                         error: function(jqXHR, textStatus, errorThrown) {
66                                                 vShowAJAXError("Could not add Service Template.");
67                                         }
68                                 }
69                         );
70                 }
71
72                 var topology;
73                 require(["winery-topologymodeler-AMD"], function(wt) {
74                         topology = wt.getTopologyTemplateAsXML(true);
75                 
76                         // call to the completion JSP which will call the java component
77                         $.post("jsp/topologyCompletion/topologyCompletion.jsp", {topology: topology, stName: serviceTemplateName, templateURL: topologyTemplateURL, overwriteTopology: overwriteTopology, topologyName: topologyName, topologyNamespace: topologyNamespace, repositoryURL: repositoryURL, stepByStep: stepByStep, openInNewWindow: openInNewWindow, restarted: "false"},
78                                 /**
79                                  * Callback function which will either open a new window if a new topology was created or
80                                  * refresh the current browser window
81                                  *
82                                  * @param (String) data
83                                  *                      the answer of the post call
84                                  */
85                                 function(data){
86                                         // checks the message returned by the CompletionInterface
87                                         if (data.indexOf("topologyComplete") != -1) {
88                                                 vShowSuccess('The topology is already complete.');
89                                         } else if (data.indexOf("failure") != -1) {
90                                                 vShowError(data);
91                                         } else if (data.indexOf("userTopologySelection") != -1) {
92                                                 $(chooseTopologyDiag[0].children[0].children[0].children[1]).html(data);
93                                                 window.setTimeout(jsPlumb.repaintEverything, JQUERY_ANIMATION_DURATION);
94                                                 chooseTopologyDiag.modal("show");
95                                         } else if (data.indexOf("topologyComplete") == -1 && data.indexOf("userTopologySelection") == -1 && data.indexOf("userInteraction") != -1) {
96                                                 $(chooseRelationshipTemplateDiag[0].children[0].children[0].children[1]).html(data);
97                                                 window.setTimeout(jsPlumb.repaintEverything, JQUERY_ANIMATION_DURATION);
98                                                 chooseRelationshipTemplateDiag.modal("show");
99                                         } else if (data.indexOf("topologyComplete") == -1 && data.indexOf("stepByStep") != -1) {
100                                                 $(chooseNodeTemplateDiag[0].children[0].children[0].children[1]).html(data);
101                                                 window.setTimeout(jsPlumb.repaintEverything, JQUERY_ANIMATION_DURATION);
102                                                 chooseNodeTemplateDiag.modal("show");
103                                         } else {
104                                                 if (openInNewWindow) {
105                                                         // a new topology has been created, open it in a new window
106                                                         var win=window.open('?repositoryURL=' + repositoryURL + '&ns='+ topologyNamespace + '&id=' + topologyName, '_blank');
107                                                         win.focus();
108                                                 } else if (overwriteTopology) {
109                                                         // refresh page
110                                                         document.location.reload(true);
111                                                 }
112                                         }
113                                 }
114                         );
115                 });
116         }
117
118         /**
119          * This function restarts the topology completion when it has been stopped to get a
120          * user decision.
121          *
122          * @param (String) topology
123          *                                      the topology as XML string
124          * @param (String) overwriteTopology
125          *                                      determines how to save the topology. Can contain the values "createNew" or "overwrite"
126          * @param (Boolean) openInNewWindow
127          *                                      determines if the result is opened in a new window
128          * @param (String) topologyName
129          *                                      the name of the new topology
130          * @param (String) topologyNamespace
131          *                                      the namespace of the new topology
132          * @param (Boolean) stepByStep
133          *                                      if true, the completion will be processed step by step
134          * @param (String)      serviceTemplateName
135          *                                              the name of the service template containing the displayed topology template
136          * @param (String)      topologyTemplateURL
137          *                                              the URL to the displayed topology template
138          * @param (String)  repositoryURL
139          *                                              the URL to the repository the topology is saved to
140          */
141         function restartCompletion(topology, overwriteTopology, openInNewWindow, topologyName, topologyNamespace, stepByStep, serviceTemplateName, topologyTemplateURL, repositoryURL) {
142
143                 // remove whitespaces in the topology XML string
144                 topology = topology.replace(/\s+/g, ' ');
145                 topology = topology.substr(1);
146
147                 // call to the completion JSP which will call the java component
148                 $.post("jsp/topologyCompletion/topologyCompletion.jsp", { topology: topology, stName: serviceTemplateName, templateURL: topologyTemplateURL, overwriteTopology: overwriteTopology, topologyName: topologyName, topologyNamespace: topologyNamespace, repositoryURL: repositoryURL, stepByStep: stepByStep, restarted: "true"},
149                         /**
150                          * Callback function which will either open a new window if a new topology was created or
151                          * refresh the current browser window
152                          *
153                          * @param (String) data
154                          *                      the answer of the post call
155                          */
156                         function(data){
157                                 if (data.indexOf("Successful") == -1 && data.indexOf("stepByStep") == -1) {
158                                         $(chooseRelationshipTemplateDiag[0].children[0].children[0].children[1]).html(data);
159                                         window.setTimeout(jsPlumb.repaintEverything, JQUERY_ANIMATION_DURATION);
160                                         chooseRelationshipTemplateDiag.modal("show");
161                                 } else if (data.indexOf("Successful") == -1 && data.indexOf("stepByStep") != -1) {
162                                         $(chooseNodeTemplateDiag[0].children[0].children[0].children[1]).html(data);
163                                         window.setTimeout(jsPlumb.repaintEverything, JQUERY_ANIMATION_DURATION);
164                                         chooseNodeTemplateDiag.modal("show");
165                                 } else {
166                                         if (openInNewWindow) {
167                                                 // a new topology has been created, open it in a new window
168                                                 var win = window.open('?repositoryURL=' + "<%=repositoryURL%>" + '&ns='+ topologyNamespace + '&id=' + topologyName, '_blank');
169                                                 win.focus();
170                                         } else if (overwriteTopology) {
171                                                 // refresh page
172                                                 document.location.reload(true);
173                                         }
174                                 }
175                         }
176                 );
177         }
178 });