2 /*******************************************************************************
3 * Copyright (c) 2012-2014 University of Stuttgart.
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
11 * Oliver Kopp - initial API and implementation and/or initial documentation
12 *******************************************************************************/
15 <%@tag import="org.eclipse.winery.model.tosca.TPolicyTemplate"%>
16 <%@tag description="Dialog to add or update a policy. Offers function showUpdateDiagForPolicy(policyElement) / showAddDiagForPolicy(nodeTemplateElement)" pageEncoding="UTF-8"%>
17 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
19 <%@taglib prefix="o" tagdir="/WEB-INF/tags/common/orioneditor"%>
20 <%@taglib prefix="w" tagdir="/WEB-INF/tags"%>
21 <%@taglib prefix="ct" tagdir="/WEB-INF/tags/common"%>
22 <%@taglib prefix="wc" uri="http://www.eclipse.org/winery/functions"%>
24 <%@attribute name="allPolicyTypes" required="true" type="java.util.Collection" description="Collection<QName> of all available policy types" %>
25 <%@attribute name="repositoryURL" required="true" type="java.lang.String" description="The URL of winery's repository"%>
27 <div class="modal fade" id="PolicyDiag">
28 <div class="modal-dialog">
29 <div class="modal-content">
30 <div class="modal-header">
31 <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
32 <h4 class="modal-title">Policy</h4>
34 <div class="modal-body">
35 <ct:id_name_type idPrefix="policy" allTypes="${allPolicyTypes}" hideIdField="true" />
37 <div class="form-group">
38 <label for="policyTemplate" class="control-label">Policy Template:</label>
40 <input id="policyTemplate" class="form-control" name="policyTemplate"></input>
43 <o:orioneditorarea areaid="OrionpolicyXML" withoutsavebutton="true" />
45 <div class="modal-footer">
46 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
47 <button type="button" id="deletePolicy" class="btn btn-danger" onclick="deletePolicy();">Delete</button>
48 <button type="button" id="updatePolicy" class="btn btn-primary" onclick="addOrUpdatePolicy(false);">Update</button>
49 <button type="button" id="addPolicy" class="btn btn-primary" onclick="addOrUpdatePolicy(true);">Add</button>
55 <c:set var="clazz" value="<%=org.eclipse.winery.model.tosca.TPolicy.class%>" />
56 <textarea id="emptyPolicy" class="hidden">${wc:XMLAsString(clazz, null)}</textarea>
59 //global variable set by showUpdateDiagForPolicy and read by addOrUpdatePolicy
60 var currentPolicyElement;
62 // possibly this is a duplicate information as we also have "currentlySelectedNodeTemplate" (or similar)
63 var currentNodeTemplateElement;
65 function updatePolicyTemplateSelect(valueToSelect) {
66 require(["winery-support-common"], function(w) {
67 var type = $("#policyType").val();
68 var fragment = w.getURLFragmentOutOfFullQName(type);
69 var url = "${repositoryURL}/policytypes/" + fragment + "/instances/";
72 }).fail(function(jqXHR, textStatus, errorThrown) {
73 vShowAJAXError("Could not get policy templates", jqXHR, errorThrown);
74 }).done(function(data) {
75 // add "(none)" to available items
82 if (typeof valueToSelect === "undefined") {
83 valueToSelect = "(none)";
87 .select2({data: data})
88 .select2("val", valueToSelect);
93 function showUpdateDiagForPolicy(policyElement) {
94 currentPolicyElement = policyElement;
96 $("#deletePolicy").show();
97 $("#updatePolicy").show();
98 $("#addPolicy").hide();
100 var name = policyElement.children("div.name").text();
101 var type = policyElement.children("span.type").text();
103 $("#policyName").val(name);
104 $("#policyType").val(type);
106 // onchange of type is not called, we have to update the template selection field for ourselves
107 // we also have to select the current user's choice
108 updatePolicyTemplateSelect(policyElement.children("span.template").text());
110 var diag = $("#PolicyDiag");
111 require(["winery-support-common"], function(w) {
112 w.replaceDialogShownHookForOrionUpdate(diag, "OrionpolicyXML", currentPolicyElement.children("textarea").val());
117 function showAddDiagForPolicy(nodeTemplateElement) {
118 currentNodeTemplateElement = nodeTemplateElement;
120 $("#deletePolicy").hide();
121 $("#updatePolicy").hide();
122 $("#addPolicy").show();
124 $("#policyName").val("");
126 // fill policy template select field
127 updatePolicyTemplateSelect();
129 var diag = $("#PolicyDiag");
130 require(["winery-support-common"], function(w) {
131 w.replaceDialogShownHookForOrionUpdate(diag, "OrionpolicyXML", $("#emptyPolicy").val());
136 function addOrUpdatePolicy(doAdd) {
137 if (highlightRequiredFields()) {
138 vShowError("Please fill in all required fields");
142 require(["winery-support-common", "tmpl"], function(wsc, tmpl) {
143 var res = wsc.synchronizeNameAndType("policy", false, [{
144 attribute: "policyType",
147 attribute: "policyRef",
148 fieldSuffix: "Template"
152 var policyTemplate = $("#policyTemplate").select2("data");
154 name: $("#policyName").val(),
156 policyTypeText: $("#policyType :selected").text(),
157 policyTypeVal: $("#policyType").val(),
159 policyTemplateText: policyTemplate.text,
160 policyTemplateVal: policyTemplate.id,
164 var div = tmpl("tmpl-policy", renderData);
166 currentNodeTemplateElement.children("div.policiesContainer").children("div.content").children("div.addnewpolicy").before(div);
168 currentPolicyElement.replaceWith(div);
170 $("#PolicyDiag").modal("hide");
172 vShowError("Could not synchronize XML fields");
178 function deletePolicy() {
179 // We just have to remove the HTML element:
180 // The save operation converts the information in the HTML to XML
181 currentPolicyElement.remove();
182 $("#PolicyDiag").modal("hide");
188 .on("change", updatePolicyTemplateSelect);
191 <%-- parameters: o.id, o.name, o.policyType, o.policyRef, o.xml. Has to be consistent with the HTML generated by policies.tag --%>
192 <script type="text/x-tmpl" id="tmpl-policy">
193 <div class="policy row"> <%-- "even"/"odd" is not set. Could be done by using $.prev() --%>
194 <div class="col-xs-4 policy name">{%=o.name%}</div>
196 <%-- we do not provide a link here. Link is only available at reload. Makes life easier here. makeArtifactTemplateURL at winery-common.js is a first hint of how to generate links --%>
197 <div class="col-xs-4 policy type">{%=o.policyTypeText%}</div>
198 <span class="type">{%=o.policyTypeVal%}</span>
200 <div class="col-xs-4 policy template">{%=o.policyTemplateText%}</div>
201 <span class="template">{%=o.policyTemplateVal%}</span>
203 <textarea class="policy_xml">{%=o.xml%}</textarea>