/*
* Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.carbon.governance.services.ui.utils;
import org.apache.axiom.om.*;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.wso2.carbon.governance.services.ui.clients.AddServicesServiceClient;
import org.wso2.carbon.ui.CarbonUIUtil;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.namespace.QName;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletConfig;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
/* This is the class which generate the service UI by reading service-config.xml */
public class AddServiceUIGenerator {
//StringBuffer serviceUI;
public OMElement getUIConfiguration(String content, HttpServletRequest request,
ServletConfig config, HttpSession session) throws Exception {
OMElement omElement = null;
try {
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(content));
StAXOMBuilder builder = new StAXOMBuilder(reader);
omElement = builder.getDocumentElement();
addLifeCycleElements(omElement, request, config, session);
} catch (XMLStreamException e) {
e.printStackTrace();
}
return omElement;
}
public String printWidgetWithValues(OMElement widget, OMElement data,
boolean isFilterOperation, HttpServletRequest request) {
if (isFilterOperation && Boolean.toString(false).equals(
widget.getAttributeValue(new QName(null, UIGeneratorConstants.FILTER_ATTRIBUTE)))) {
return "";
}
int columns = 2; //default value of number of columns is 2
String widgetName = widget.getAttributeValue(new QName(null, UIGeneratorConstants.ARGUMENT_NAME));
OMElement dataHead = null;
if (data != null) {
dataHead = AddServicesUtil.getChildWithName(data, widgetName);
}
if (widget.getAttributeValue(new QName(null, UIGeneratorConstants.WIDGET_COLUMN)) != null) {
columns = Integer.parseInt(widget.getAttributeValue(new QName(null, UIGeneratorConstants.WIDGET_COLUMN)));
}
Iterator subHeadingIt = widget.getChildrenWithName(new QName(null, UIGeneratorConstants.SUBHEADING_ELEMENT));
StringBuilder table = new StringBuilder();
table.append("<table class=\"normal-nopadding\" cellspacing=\"0\">");
List<String> subList = new ArrayList<String>();
OMElement sub = null;
if (subHeadingIt != null && subHeadingIt.hasNext()) {
sub = (OMElement) subHeadingIt.next(); // NO need to have multiple subheading elements in a single widget element
}
if (sub != null && UIGeneratorConstants.SUBHEADING_ELEMENT.equals(sub.getLocalName())) {
Iterator headingList = sub.getChildrenWithLocalName(UIGeneratorConstants.HEADING_ELEMENT);
while (headingList.hasNext()) {
OMElement subheading = (OMElement) headingList.next();
subList.add(subheading.getText());
}
if (subList.size() > columns) {
/*This is the place where special scenario comes in to play with number of columns other
than having two columns
*/
return ""; // TODO: throw an exception
}
}
table.append(printMainHeader(widgetName, columns));
if (subList.size() > 2) {
//if the column size is not 2 we print sub-headers first before going in to loop
//In this table there should not be any field with maxOccurs unbounded//
table.append(printSubHeaders(subList.toArray(new String[subList.size()])));
}
Iterator arguments = widget.getChildrenWithLocalName(UIGeneratorConstants.ARGUMENT_ELMENT);
int columnCount = 0;
int rowCount = 0;
OMElement inner = null;
while (arguments.hasNext()) {
OMElement arg = (OMElement) arguments.next();
String maxOccurs = "";
if (UIGeneratorConstants.ARGUMENT_ELMENT.equals(arg.getLocalName())) {
if (isFilterOperation && Boolean.toString(false).equals(
arg.getAttributeValue(new QName(null, UIGeneratorConstants.FILTER_ATTRIBUTE)))) {
continue;
}
rowCount++; //this variable used to find the which raw is in and use this to print the sub header
String elementType = arg.getAttributeValue(new QName(null, UIGeneratorConstants.TYPE_ATTRIBUTE));
//Read the maxOccurs value
maxOccurs = arg.getAttributeValue(new QName(null, UIGeneratorConstants.MAXOCCUR_ELEMENT));
if (maxOccurs != null) {
if (!UIGeneratorConstants.MAXOCCUR_BOUNDED.equals(maxOccurs) && !UIGeneratorConstants.MAXOCCUR_UNBOUNDED.equals(maxOccurs)) {
//if user has given something else other than unbounded
return ""; //TODO: throw an exception
}
if (!UIGeneratorConstants.MAXOCCUR_UNBOUNDED.equals(maxOccurs)) {
//if maxOccurs is not unbounded then print the sub header otherwise we will show the adding link
if (rowCount == 1) {
// We print the sub header only when we parse the first element otherwise we'll print sub header for each field element
table.append(printSubHeaders(subList.toArray(new String[subList.size()])));
}
}
} else {
if (subList.size() == 2 && rowCount == 1) {
// We print the sub header only when we parse the first element otherwise we'll print sub header for each field element
// sub headers are printed in this position only if column number is exactly 2//
table.append(printSubHeaders(subList.toArray(new String[subList.size()])));
}
}
if (dataHead != null) {
//if the data xml contains the main element then get the element contains value
inner = AddServicesUtil.getChildWithName(dataHead, arg.getFirstChildWithName
(new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText().replaceAll(" ", "-"));
}
String value = null;
String optionValue = null;
if (UIGeneratorConstants.TEXT_FIELD.equals(elementType)) {
String mandat = arg.getAttributeValue(new QName(null, UIGeneratorConstants.MANDETORY_ATTRIBUTE));
if (isFilterOperation) {
mandat = "false";
}
boolean isURL = Boolean.toString(true).equals(arg.getAttributeValue(
new QName(null, UIGeneratorConstants.URL_ATTRIBUTE)));
if (inner != null) {
//if the element contains value is not null get the value
value = inner.getText();
}
if (columns > 2) {
if (columnCount == 0) {
table.append("<tr>");
}
if (value != null) {
table.append(printTextSkipName(arg.getFirstChildWithName(new QName(null,
UIGeneratorConstants.ARGUMENT_NAME)).getText(), widgetName, value, isURL, request));
} else {
table.append(printTextSkipName(arg.getFirstChildWithName(new QName(null,
UIGeneratorConstants.ARGUMENT_NAME)).getText(), widgetName));
}
columnCount++;
if (columnCount == columns) {
table.append("</tr>");
columnCount = 0;
}
} else {
if (value != null) {
table.append(printTextField(arg.getFirstChildWithName(new QName(null,
UIGeneratorConstants.ARGUMENT_NAME)).getText(), mandat, widgetName, value, isURL, request));
} else {
table.append(printTextField(arg.getFirstChildWithName(new QName(null,
UIGeneratorConstants.ARGUMENT_NAME)).getText(), mandat, widgetName));
}
}
} else if (UIGeneratorConstants.OPTION_FIELD.equals(elementType)) {
String name = arg.getFirstChildWithName(new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText();
if (inner != null) {
//if the element contains value is not null get the value
optionValue = inner.getText();
}
Iterator values = arg.getFirstChildWithName(new QName(null,
UIGeneratorConstants.OPTION_VALUES)).getChildrenWithLocalName(UIGeneratorConstants.OPTION_VALUE);
if (values != null) {
List<String> optionValues = new ArrayList<String>();
while (values.hasNext()) {
optionValues.add(((OMElement) values.next()).getText());
}
if (columns > 2) {
if (columnCount == 0) {
table.append("<tr>");
}
if (optionValue != null) {
table.append(printDropDownSkipName(name,
optionValues.toArray(new String[optionValues.size()]), widgetName, optionValue));
} else {
table.append(printDropDownSkipName(name, optionValues.toArray(new String[optionValues.size()]), widgetName));
}
columnCount++;
if (columnCount == columns) {
table.append("</tr>");
columnCount = 0;
}
} else {
if (optionValue != null) {
table.append(printDropDown(name, optionValues.toArray(new String[optionValues.size()]), widgetName, optionValue));
} else {
table.append(printDropDown(name, optionValues.toArray(new String[optionValues.size()]), widgetName));
}
}
} else {
return "";
}
} else if (UIGeneratorConstants.CHECKBOX_FIELD.equals(elementType)) {
String name = arg.getFirstChildWithName(
new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText();
if (inner != null) {
//if the element contains value is not null get the value
optionValue = inner.getText();
}
if (columns > 2) {
if (columnCount == 0) {
table.append("<tr>");
}
table.append(printCheckboxSkipName(name, widgetName, optionValue));
columnCount++;
if (columnCount == columns) {
table.append("</tr>");
columnCount = 0;
}
} else {
table.append(printCheckbox(name, widgetName, optionValue));
}
} else if (UIGeneratorConstants.TEXT_AREA_FIELD.equals(elementType)) {
String mandet = arg.getAttributeValue(new QName(null, UIGeneratorConstants.MANDETORY_ATTRIBUTE));
if (isFilterOperation) {
mandet = "false";
}
if (inner != null) {
//if the element contains value is not null get the value
value = inner.getText();
}
int height = -1;
int width = -1;
String heightString = arg.getAttributeValue(new QName(null, UIGeneratorConstants.HEIGHT_ATTRIBUTE));
if (heightString != null) {
try {
height = Integer.parseInt(heightString);
} catch (NumberFormatException ignored) {
}
}
String widthString = arg.getAttributeValue(new QName(null, UIGeneratorConstants.WIDTH_ATTRIBUTE));
if (widthString != null) {
try {
width = Integer.parseInt(widthString);
} catch (NumberFormatException ignored) {
}
}
if (columns > 2) {
if (columnCount == 0) {
table.append("<tr>");
}
if (value != null) {
table.append(printTextAreaSkipName(arg.getFirstChildWithName(
new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText(), widgetName, value, height, width));
} else {
table.append(printTextAreaSkipName(arg.getFirstChildWithName(
new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText(), widgetName, height, width));
}
columnCount++;
if (columnCount == columns) {
table.append("</tr>");
columnCount = 0;
}
} else {
if (value != null) {
table.append(printTextArea(arg.getFirstChildWithName(
new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText(), mandet, widgetName, value, height, width));
} else {
table.append(printTextArea(arg.getFirstChildWithName(
new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText(), mandet, widgetName, height, width));
}
}
} else if (UIGeneratorConstants.OPTION_TEXT_FIELD.equals(elementType)) {
if (UIGeneratorConstants.MAXOCCUR_UNBOUNDED.equals(maxOccurs)) {
// This is the code segment to run in maxoccur unbounded situation
// String addedItems = "0";
// if(dataHead != null){
// addedItems = dataHead.getFirstChildWithName(new QName(null,UIGeneratorConstants.COUNT)).getText();
// }
String name = arg.getFirstChildWithName(new QName(null,
UIGeneratorConstants.ARGUMENT_NAME)).getText();
// String addedOptionValues [] = new String[Integer.parseInt(addedItems)];
// String addedValues[] = new String[Integer.parseInt(addedItems)];
List<String> addedOptionValues = new ArrayList<String>();
List<String> addedValues = new ArrayList<String>();
int addedItemsCount = 0;
if (dataHead != null) {
//if the element contains value is not null get the value
// with option-text field we put text value like this text_value.replaceAll(" ","-")
Iterator itemChildIt = dataHead.getChildElements();
int i = 0;
while (itemChildIt.hasNext()) {
// get all the filled values to the newly added fields
Object itemChildObj = itemChildIt.next();
if (!(itemChildObj instanceof OMElement)) {
continue;
}
OMElement itemChildEle = (OMElement) itemChildObj;
if (!(itemChildEle.getQName().equals(new QName(UIGeneratorConstants.DATA_NAMESPACE,
UIGeneratorConstants.ENTRY_FIELD)))) {
continue;
}
String entryText = itemChildEle.getText();
String entryKey = null;
String entryVal;
int colonIndex = entryText.indexOf(":");
if (colonIndex < entryText.length() - 1) {
entryKey = entryText.substring(0, colonIndex);
entryText = entryText.substring(colonIndex + 1);
}
entryVal = entryText;
if (entryKey != null && !entryKey.equals("")) {
addedOptionValues.add(entryKey);
} else {
addedOptionValues.add("0");
}
if (entryVal != null) {
addedValues.add(entryVal);
}
i++;
}
addedItemsCount = i;
}
/* if there are no added items headings of the table will hide,else display */
if (addedItemsCount == 0) {
table.append(printAddLink(name,
UIGeneratorConstants.ADD_ICON_PATH,
widgetName,
subList.toArray(new String[subList.size()])));
} else if (addedItemsCount > 0) {
table.append(printAddLinkWithDisplay(name,
UIGeneratorConstants.ADD_ICON_PATH,
widgetName,
subList.toArray(new String[subList.size()])));
}
Iterator values = arg.getFirstChildWithName(new QName(null, UIGeneratorConstants.OPTION_VALUES)).
getChildrenWithLocalName(UIGeneratorConstants.OPTION_VALUE);
if (values != null) {
List<String> optionValues = new ArrayList<String>();
while (values.hasNext()) {
optionValues.add(((OMElement) values.next()).getText());
}
if (addedItemsCount > 0) {
// This is the place where we fill already added entries
for (int i = 0; i < addedItemsCount; i++) {
String addedOptionValue = addedOptionValues.get(i);
String addedValue = addedValues.get(i);
if (addedOptionValue != null && addedValue != null) {
table.append(printOptionTextWithId(name + (i + 1),
optionValues.toArray(new String[optionValues.size()]),
widgetName,
addedOptionValue,
addedValue,
i + 1));
}
}
} else {
//just show the add button only
}
}
table.append(printCloseAddLink(name, addedItemsCount)); // add the previously added items and then close the tbody
} else {
String name = arg.getFirstChildWithName(new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText();
if (dataHead != null) {
//if the element contains value is not null get the value
// with option-text field we put text value like this text_value.replaceAll(" ","-")
inner = AddServicesUtil.getChildWithName(dataHead, UIGeneratorConstants.TEXT_FIELD +
arg.getFirstChildWithName(
new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText());
if (inner != null) {
value = inner.getText();
}
OMElement optionValueElement = AddServicesUtil.getChildWithName(dataHead, arg.getFirstChildWithName
(new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText());
if (optionValueElement != null) {
optionValue = optionValueElement.getText();
}
}
Iterator values = arg.getFirstChildWithName(new QName(null, UIGeneratorConstants.OPTION_VALUES)).
getChildrenWithLocalName(UIGeneratorConstants.OPTION_VALUE);
if (values != null) {
List<String> optionValues = new ArrayList<String>();
while (values.hasNext()) {
optionValues.add(((OMElement) values.next()).getText());
}
if (optionValue != null && value != null) {
table.append(printOptionText(name, optionValues.toArray(new String[optionValues.size()]), widgetName, optionValue, value));
} else {
table.append(printOptionText(name, optionValues.toArray(new String[optionValues.size()]), widgetName));
}
}
}
}
}
}
table.append("</table>");
return table.toString();
}
public String printMainHeader(String header, int columns) {
StringBuilder head = new StringBuilder();
head.append("<thead><tr><th colspan=\"" + columns + "\">");
head.append(header);
head.append("</th></tr></thead>");
return head.toString();
}
public String printSubHeaders(String[] headers) {
StringBuilder subHeaders = new StringBuilder();
subHeaders.append("<tr>");
for (String header : headers) {
subHeaders.append("<td class=\"sub-header\">");
subHeaders.append(header);
subHeaders.append("</td>");
}
subHeaders.append("</tr>");
return subHeaders.toString();
}
public String printTextField(String name, String mandatory, String widget) {
StringBuilder element = new StringBuilder();
if ("true".equals(mandatory)) {
element.append("<tr><td class=\"leftCol-big\">" + name + "<span class=\"required\">*</span></td>\n" +
" <td><input type=\"text\" name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-")
+ "\" id=\"id_" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" style=\"width:" + UIGeneratorConstants.DEFAULT_WIDTH + "px\"/></td></tr>");
} else {
element.append("<tr><td class=\"leftCol-big\">" + name + "</td>\n" +
" <td><input type=\"text\" name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-")
+ "\" id=\"id_" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" style=\"width:" + UIGeneratorConstants.DEFAULT_WIDTH + "px\"/></td></tr>");
}
return element.toString();
}
public String printDropDown(String name, String[] values, String widget) {
StringBuilder dropDown = new StringBuilder();
dropDown.append("<tr><td class=\"leftCol-big\">" + name + "</td>\n" +
"<td><select name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\">");
for (int i = 0; i < values.length; i++) {
dropDown.append("<option value=\"" + values[i] + "\">");
dropDown.append(values[i]);
dropDown.append("</option>");
}
dropDown.append("</select></td></tr>");
return dropDown.toString();
}
public String printTextArea(String name, String mandatory, String widget, int height, int width) {
StringBuilder element = new StringBuilder();
StringBuilder size = new StringBuilder("style=\"");
if (height > 0) {
size.append("height:").append(height).append("px;");
}
if (width > 0) {
size.append("width:").append(width).append("px\"");
} else {
size.append("width:").append(UIGeneratorConstants.DEFAULT_WIDTH).append("px\"");
}
if ("true".equals(mandatory)) {
element.append("<tr><td class=\"leftCol-big\">" + name + "<span class=\"required\">*</span></td>\n" +
" <td><textarea name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" id=\"id_"
+ widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" " + size + "></textarea></td></tr>");
} else {
element.append("<tr><td class=\"leftCol-big\">" + name + "</td>\n" +
" <td><textarea name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" id=\"id_"
+ widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" " + size + "></textarea></td></tr>");
}
return element.toString();
}
public String printTextAreaSkipName(String name, String widget, int height, int width) {
StringBuilder element = new StringBuilder();
StringBuilder size = new StringBuilder();
if (height > 0 || width > 0) {
size = size.append("");
if (height > 0) {
size.append("height:").append(height).append("px;");
}
if (width > 0) {
size.append("width:").append(width).append("px\"");
}
size.append("\"");
}
element.append("<td><textarea name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" " +
"id=\"id_" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" " + size + "></textarea></td>");
return element.toString();
}
public String printTextSkipName(String name, String widget) {
StringBuilder element = new StringBuilder();
element.append("<td><input type=\"text\" name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-")
+ "\" id=\"id_" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" /></td>");
return element.toString();
}
public String printDropDownSkipName(String name, String[] values, String widget) {
StringBuilder dropDown = new StringBuilder();
dropDown.append("<td><select name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\">");
for (int i = 0; i < values.length; i++) {
dropDown.append("<option value=\"" + i + "\">");
dropDown.append(values[i]);
dropDown.append("</option>");
}
dropDown.append("</select></td>");
return dropDown.toString();
}
public String printOptionText(String name, String[] values, String widget) {
StringBuilder dropDown = new StringBuilder();
dropDown.append("<tr><td class=\"leftCol-big\"><select name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\">");
for (int i = 0; i < values.length; i++) {
dropDown.append("<option value=\"" + values[i] + "\">");
dropDown.append(values[i]);
dropDown.append("</option>");
}
dropDown.append("</select></td>");
dropDown.append("<td><input type=\"text\" name=\"" + widget.replaceAll(" ", "_") + UIGeneratorConstants.TEXT_FIELD
+ "_" + name.replaceAll(" ", "-") + "\" id=\"id_" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" style=\"width:" + UIGeneratorConstants.DEFAULT_WIDTH + "px\"/></td></tr>");
return dropDown.toString();
}
public String printTextField(String name, String mandatory, String widget, String value, boolean isURL, HttpServletRequest request) {
StringBuilder element = new StringBuilder();
String id = "id_" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-");
String div = "<div id=\"" + id + "_link\"><a target=\"_blank\" href=\"" + value + "\">" + value + "</a>" +
" <a onclick=\"$('" + id + "_link').style.display='none';$('" + id +
"')." +
"style.display='';\" title=\"" + CarbonUIUtil.geti18nString("edit",
"org.wso2.carbon.governance.services.ui.i18n.Resources", request.getLocale()) +
"\" " +
"class=\"icon-link\" style=\"background-image: url('../admin/images/edit.gif');float: none\"></a></div>";
if ("true".equals(mandatory)) {
element.append("<tr><td class=\"leftCol-big\">" + name + "<span class=\"required\">*</span></td>\n" +
" <td>" + (isURL ? div : "") + "<input type=\"text\" name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-")
+ "\" value=\"" + value + "\" id=\"" + id + "\" style=\"width:" + UIGeneratorConstants.DEFAULT_WIDTH + "px\"/></td></tr>");
} else {
element.append("<tr><td class=\"leftCol-big\">" + name + "</td>\n" +
" <td>" + (isURL ? div : "") + "<input type=\"text\" name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-")
+ "\" value=\"" + value + "\" id=\"" + id + "\" style=\"width:" + UIGeneratorConstants.DEFAULT_WIDTH + "px\"/></td></tr>");
}
return element.toString();
}
public String printDropDown(String name, String[] values, String widget, String value) {
StringBuilder dropDown = new StringBuilder();
dropDown.append("<tr><td class=\"leftCol-big\">" + name + "</td>\n" +
"<td><select name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\">");
for (int i = 0; i < values.length; i++) {
dropDown.append("<option value=\"" + values[i] + "\"");
if (values[i].equals(value)) {
dropDown.append(" selected>");
} else {
dropDown.append(">");
}
dropDown.append(values[i]);
dropDown.append("</option>");
}
dropDown.append("</select></td></tr>");
return dropDown.toString();
}
public String printCheckbox(String name, String widget, String value) {
if (Boolean.toString(true).equals(value)) {
return "<tr><td class=\"leftCol-big\">" + name + "</td>\n" +
"<td><input type=\"checkbox\" checked=\"checked\" name=\"" +
widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") +
"\" value=\"true\" /></td>";
} else {
return "<tr><td class=\"leftCol-big\">" + name + "</td>\n" +
"<td><input type=\"checkbox\" name=\"" + widget.replaceAll(" ", "_") + "_" +
name.replaceAll(" ", "-") + "\" value=\"true\" /></td>";
}
}
public String printTextArea(String name, String mandatory, String widget, String value, int height, int width) {
StringBuilder element = new StringBuilder();
StringBuilder size = new StringBuilder("style=\"");
if (height > 0) {
size.append("height:").append(height).append("px;");
}
if (width > 0) {
size.append("width:").append(width).append("px\"");
} else {
size.append("width:").append(UIGeneratorConstants.DEFAULT_WIDTH).append("px\"");
}
if ("true".equals(mandatory)) {
element.append("<tr><td class=\"leftCol-big\">" + name + "<span class=\"required\">*</span></td>\n" +
" <td><textarea name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" id=\"id_"
+ widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" " + size + ">" + value + "</textarea></td></tr>");
} else {
element.append("<tr><td class=\"leftCol-big\">" + name + "</td>\n" +
" <td><textarea name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" id=\"id_"
+ widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" " + size + ">" + value + "</textarea></td></tr>");
}
return element.toString();
}
public String printTextAreaSkipName(String name, String widget, String value, int height, int width) {
StringBuilder element = new StringBuilder();
StringBuilder size = new StringBuilder();
if (height > 0 || width > 0) {
size = size.append("");
if (height > 0) {
size.append("height:").append(height).append("px;");
}
if (width > 0) {
size.append("width:").append(width).append("px\"");
}
size.append("\"");
}
element.append("<td><textarea name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" " +
"id=\"id_" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" " + size + ">" + value + "</textarea></td>");
return element.toString();
}
public String printTextSkipName(String name, String widget, String value, boolean isURL, HttpServletRequest request) {
StringBuilder element = new StringBuilder();
if (isURL) {
String id = "id_" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-");
String div = "<div id=\"" + id + "_link\"><a target=\"_blank\" href=\"" + value + "\">" + value + "</a>" +
" <a onclick=\"$('" + id + "_link').style.display='none';$('" + id +
"')." +
"style.display='';\" title=\"" + CarbonUIUtil.geti18nString("edit",
"org.wso2.carbon.governance.services.ui.i18n.Resources", request.getLocale()) +
"\" " +
"class=\"icon-link\" style=\"background-image: url('../admin/images/edit.gif');float: none\"></a></div>";
element.append("<td>" + div + "<input style=\"display:none\" type=\"text\" name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-")
+ "\" value=\"" + value + "\" id=\"" + id + "\" /></td>");
} else {
element.append("<td><input type=\"text\" name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-")
+ "\" value=\"" + value + "\" id=\"id_" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" /></td>");
}
return element.toString();
}
public String printDropDownSkipName(String name, String[] values, String widget, String value) {
StringBuilder dropDown = new StringBuilder();
dropDown.append("<td><select name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\">");
for (int i = 0; i < values.length; i++) {
dropDown.append("<option value=\"" + values[i] + "\"");
if (values[i].equals(value)) {
dropDown.append(" selected>");
} else {
dropDown.append(">");
}
dropDown.append(values[i]);
dropDown.append("</option>");
}
dropDown.append("</select></td>");
return dropDown.toString();
}
public String printCheckboxSkipName(String name, String widget, String value) {
if (Boolean.toString(true).equals(value)) {
return "<td><input type=\"checkbox\" checked=\"checked\" name=\"" +
widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") +
"\" value=\"true\" /></td>";
} else {
return "<td><input type=\"checkbox\" name=\"" + widget.replaceAll(" ", "_") + "_" +
name.replaceAll(" ", "-") + "\" value=\"true\" /></td>";
}
}
public String printOptionText(String name, String[] values, String widget, String option, String text) {
StringBuilder dropDown = new StringBuilder();
dropDown.append("<tr><td class=\"leftCol-big\"><select name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\">");
for (int i = 0; i < values.length; i++) {
dropDown.append("<option value=\"" + values[i] + "\"");
if (values[i].equals(option)) {
dropDown.append(" selected>");
} else {
dropDown.append(">");
}
dropDown.append(values[i]);
dropDown.append("</option>");
}
dropDown.append("</select></td>");
dropDown.append("<td><input type=\"text\" name=\"" + widget.replaceAll(" ", "_") + UIGeneratorConstants.TEXT_FIELD
+ "_" + name.replaceAll(" ", "-") + "\" value=\"" + text + "\" id=\"id_" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\" style=\"width:400px\"/></td></tr>");
return dropDown.toString();
}
public String printOptionTextWithId(String name, String[] values, String widget, String option, String text, int index) {
StringBuilder dropDown = new StringBuilder();
dropDown.append("<tr><td class=\"leftCol-big\"><select name=\"" + widget.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-") + "\">");
for (int i = 0; i < values.length; i++) {
dropDown.append("<option value=\"" + values[i] + "\"");
if (values[i].equals(option)) {
dropDown.append(" selected>");
} else {
dropDown.append(">");
}
dropDown.append(values[i]);
dropDown.append("</option>");
}
dropDown.append("</select></td>");
dropDown.append("<td><input type=\"text\" name=\"" + widget.replaceAll(" ", "_") + UIGeneratorConstants.TEXT_FIELD
+ "_" + name.replaceAll(" ", "-") + "\" value=\"" + text + "\" id=\"" + index + "\" style=\"width:400px\"/></td></tr>");
return dropDown.toString();
}
public String printAddLink(String name, String addIconPath, String widget, String[] subList) {
StringBuilder link = new StringBuilder();
link.append("<tr><td colspan=\"3\"><a class=\"icon-link\" style=\"background-image: url(");
link.append(addIconPath);
link.append(");\" onclick=\"");
link.append("add" + name.replaceAll(" ", "-") + "_" + widget.replaceAll(" ", "_") + "()\">"); //creating a JavaScript onclick method name which should be identical ex: addEndpoint_Endpoint
link.append("Add " + name.replaceAll(" ", "-")); //This is the display string for add item ex: Add EndPoint
link.append("</a></td></tr>");
link.append("<tr><td colspan=\"3\">");
link.append("<table class=\"styledLeft\" style=\"display:none;border: 1px solid rgb(204, 204, 204) ! important;\"><thead>" +
printSubHeaders(subList) +
"</thead><tbody id=\"" + name.replaceAll(" ", "-") + "Mgt\">");
return link.toString();
}
public String printAddLinkWithDisplay(String name, String addIconPath, String widget, String[] subList) {
StringBuilder link = new StringBuilder();
link.append("<tr><td colspan=\"3\"><a class=\"icon-link\" style=\"background-image: url(");
link.append(addIconPath);
link.append(");\" onclick=\"");
link.append("add" + name.replaceAll(" ", "-") + "_" + widget.replaceAll(" ", "_") + "()\">"); //creating a JavaScript onclick method name which should be identical ex: addEndpoint_Endpoint
link.append("Add " + name.replaceAll(" ", "-")); //This is the display string for add item ex: Add EndPoint
link.append("</a></td></tr>");
link.append("<tr><td colspan=\"3\">");
link.append("<table class=\"styledLeft\" style=\"border: 1px solid rgb(204, 204, 204) ! important;\"><thead>" +
printSubHeaders(subList) +
"</thead><tbody id=\"" + name.replaceAll(" ", "-") + "Mgt\">");
return link.toString();
}
public String printCloseAddLink(String name, int count) {
StringBuilder link = new StringBuilder();
link.append("</tbody></table>");
link.append("<input id=\"" + name.replaceAll(" ", "-") + "CountTaker\" type=\"hidden\" value=\"" +
count + "\" name=\"");
link.append(name.replaceAll(" ", "-") + UIGeneratorConstants.COUNT + "\"/>\n");
link.append("</td></tr>");
return link.toString();
}
/* This is the method which extract information from the UI and embedd them to xml using value elements */
public static OMElement getDataFromUI(OMElement head, HttpServletRequest request) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace namespace = fac.createOMNamespace(UIGeneratorConstants.DATA_NAMESPACE, "");
OMElement data = fac.createOMElement(UIGeneratorConstants.DATA_ELEMENT, namespace);
Iterator it = head.getChildrenWithName(new QName(UIGeneratorConstants.WIDGET_ELEMENT));
while (it.hasNext()) {
OMElement widget = (OMElement) it.next();
String widgetName = widget.getAttributeValue(new QName(null, UIGeneratorConstants.ARGUMENT_NAME));
OMElement widgetData = fac.createOMElement(AddServicesUtil.getDataElementName(widgetName),
namespace);
Iterator arguments = widget.getChildrenWithLocalName(UIGeneratorConstants.ARGUMENT_ELMENT);
OMElement arg = null;
while (arguments.hasNext()) {
arg = (OMElement) arguments.next();
if (UIGeneratorConstants.ARGUMENT_ELMENT.equals(arg.getLocalName())) {
String elementType = arg.getAttributeValue(new QName(null, UIGeneratorConstants.TYPE_ATTRIBUTE));
String name = arg.getFirstChildWithName(new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText();
if (UIGeneratorConstants.OPTION_TEXT_FIELD.equals(elementType)) {
if (UIGeneratorConstants.MAXOCCUR_UNBOUNDED.equals(
arg.getAttributeValue(new QName(null, UIGeneratorConstants.MAXOCCUR_ELEMENT)))) {
//implement the new way of extracting data if the maxoccurs unbounded happend in option-text field
String count = request.getParameter(name.replaceAll(" ", "-") + UIGeneratorConstants.COUNT);
for (int i = 0; i < Integer.parseInt(count); i++) {
String entryValue = "";
String input = request.getParameter(widgetName.replaceAll(" ", "_") +
"_" + name.replaceAll(" ", "-") + (i + 1));
if (input != null && !("".equals(input)) && !("None".equals(input))) {
entryValue += input;
}
entryValue += ":";
String inputTextValue = request.getParameter(widgetName.replaceAll(" ", "_") +
UIGeneratorConstants.TEXT_FIELD +
"_" + name.replaceAll(" ", "-") + (i + 1));
if (inputTextValue != null && !("".equals(inputTextValue))) {
entryValue += inputTextValue;
}
if (!":".equals(entryValue)) {
OMElement entryElement = fac.createOMElement(UIGeneratorConstants.ENTRY_FIELD,
namespace);
entryElement.setText(entryValue);
widgetData.addChild(entryElement);
}
}
}
// if maxoccurs unbounded is not mentioned use the default behaviour
else {
String input = request.getParameter(widgetName.replaceAll(" ", "_") + "_" +
name.replaceAll(" ", "-"));
if (input != null && !("".equals(input)) && !("None".equals(input))) {
OMElement text = fac.createOMElement(AddServicesUtil.getDataElementName(name),
namespace);
text.setText(input);
widgetData.addChild(text);
}
String inputOption = request.getParameter(widgetName.replaceAll(" ", "_") +
UIGeneratorConstants.TEXT_FIELD +
"_" + name.replaceAll(" ", "-"));
if (inputOption != null && !("".equals(inputOption))) {
OMElement value = fac.createOMElement(
AddServicesUtil.getDataElementName(UIGeneratorConstants.TEXT_FIELD + name),
namespace);
value.setText(inputOption);
widgetData.addChild(value);
}
}
} else {
String input = request.getParameter(widgetName.replaceAll(" ", "_") + "_" +
name.replaceAll(" ", "-"));
if (input != null && !("".equals(input)) && !("None".equals(input))) {
OMElement text = fac.createOMElement(AddServicesUtil.getDataElementName(name), namespace);
text.setText(input);
widgetData.addChild(text);
}
}
}
}
data.addChild(widgetData);
}
return AddServicesUtil.addExtraElements(data, request);
}
public String[] getMandatoryIdList(OMElement head) {
List<String> id = new ArrayList<String>();
Iterator it = head.getChildrenWithName(new QName(UIGeneratorConstants.WIDGET_ELEMENT));
while (it.hasNext()) {
OMElement widget = (OMElement) it.next();
String widgetName = widget.getAttributeValue(new QName(null, UIGeneratorConstants.ARGUMENT_NAME));
Iterator arguments = widget.getChildrenWithLocalName(UIGeneratorConstants.ARGUMENT_ELMENT);
OMElement arg = null;
while (arguments.hasNext()) {
arg = (OMElement) arguments.next();
if (UIGeneratorConstants.ARGUMENT_ELMENT.equals(arg.getLocalName())) {
String name = arg.getFirstChildWithName(new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText();
//check the mandatory fields and get the id's of them
if (arg.getAttributeValue(new QName(null, UIGeneratorConstants.MANDETORY_ATTRIBUTE)) != null) {
id.add("id_" + widgetName.replaceAll(" ", "_") + "_" + name.replaceAll(" ", "-"));
}
}
}
}
return id.toArray(new String[id.size()]);
}
public String[] getMandatoryNameList(OMElement head) {
List<String> name = new ArrayList<String>();
Iterator it = head.getChildrenWithName(new QName(UIGeneratorConstants.WIDGET_ELEMENT));
while (it.hasNext()) {
OMElement widget = (OMElement) it.next();
Iterator arguments = widget.getChildrenWithLocalName(UIGeneratorConstants.ARGUMENT_ELMENT);
OMElement arg = null;
while (arguments.hasNext()) {
arg = (OMElement) arguments.next();
if (UIGeneratorConstants.ARGUMENT_ELMENT.equals(arg.getLocalName())) {
String name_element = arg.getFirstChildWithName(new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText();
if (arg.getAttributeValue(new QName(null, UIGeneratorConstants.MANDETORY_ATTRIBUTE)) != null) {
name.add(name_element);
}
}
}
}
return name.toArray(new String[name.size()]);
}
public String[] getUnboundedNameList(OMElement head) {
List<String> name = new ArrayList<String>();
Iterator it = head.getChildrenWithName(new QName(UIGeneratorConstants.WIDGET_ELEMENT));
while (it.hasNext()) {
OMElement widget = (OMElement) it.next();
Iterator arguments = widget.getChildrenWithLocalName(UIGeneratorConstants.ARGUMENT_ELMENT);
OMElement arg = null;
while (arguments.hasNext()) {
arg = (OMElement) arguments.next();
if (UIGeneratorConstants.ARGUMENT_ELMENT.equals(arg.getLocalName())) {
//check the unbounded fields and get the names of them
if (UIGeneratorConstants.OPTION_TEXT_FIELD.equals(arg.getAttributeValue(new QName(null, UIGeneratorConstants.TYPE_ATTRIBUTE)))) {
//previous check is used to check the max occur unbounded only with option-text fields with other fields it will ignore
if (UIGeneratorConstants.MAXOCCUR_UNBOUNDED.equals(arg.getAttributeValue(new QName(null,
UIGeneratorConstants.MAXOCCUR_ELEMENT)))) {
name.add(arg.getFirstChildWithName(new QName(null, UIGeneratorConstants.ARGUMENT_NAME)).getText());
}
}
}
}
}
return name.toArray(new String[name.size()]);
}
public String[] getUnboundedWidgetList(OMElement head) {
List<String> widgetList = new ArrayList<String>();
Iterator it = head.getChildrenWithName(new QName(UIGeneratorConstants.WIDGET_ELEMENT));
while (it.hasNext()) {
OMElement widget = (OMElement) it.next();
Iterator arguments = widget.getChildrenWithLocalName(UIGeneratorConstants.ARGUMENT_ELMENT);
OMElement arg = null;
while (arguments.hasNext()) {
arg = (OMElement) arguments.next();
if (UIGeneratorConstants.ARGUMENT_ELMENT.equals(arg.getLocalName())) {
//check the unbounded fields and get the widget names of them
if (UIGeneratorConstants.OPTION_TEXT_FIELD.equals(arg.getAttributeValue(new QName(null, UIGeneratorConstants.TYPE_ATTRIBUTE)))) {
//previous check is used to check the max occur unbounded only with option-text fields with other fields it will ignore
if (UIGeneratorConstants.MAXOCCUR_UNBOUNDED.equals(arg.getAttributeValue(
new QName(null, UIGeneratorConstants.MAXOCCUR_ELEMENT)))) {
widgetList.add(widget.getAttributeValue(new QName(null, UIGeneratorConstants.WIDGET_NAME)));
}
}
}
}
}
return widgetList.toArray(new String[widgetList.size()]);
}
public String[][] getUnboundedValues(OMElement head) {
List<String[]> values = new ArrayList<String[]>();
Iterator it = head.getChildrenWithName(new QName(UIGeneratorConstants.WIDGET_ELEMENT));
while (it.hasNext()) {
OMElement widget = (OMElement) it.next();
Iterator arguments = widget.getChildrenWithLocalName(UIGeneratorConstants.ARGUMENT_ELMENT);
OMElement arg = null;
while (arguments.hasNext()) {
arg = (OMElement) arguments.next();
if (UIGeneratorConstants.ARGUMENT_ELMENT.equals(arg.getLocalName())) {
//check the unbounded fields and get the values of drop-down in option-text type
if (UIGeneratorConstants.OPTION_TEXT_FIELD.equals(arg.getAttributeValue(new QName(null, UIGeneratorConstants.TYPE_ATTRIBUTE)))) {
//previous check is used to check the max occur unbounded only with option-text fields with other fields it will ignore
if (UIGeneratorConstants.MAXOCCUR_UNBOUNDED.equals(
arg.getAttributeValue(new QName(null, UIGeneratorConstants.MAXOCCUR_ELEMENT)))) {
Iterator iterator = arg.getFirstChildWithName(new QName(null,
UIGeneratorConstants.OPTION_VALUES)).getChildrenWithLocalName(UIGeneratorConstants.OPTION_VALUE);
List<String> inner = new ArrayList<String>();
while (iterator.hasNext()) {
inner.add(((OMElement) iterator.next()).getText());
}
values.add(inner.toArray(new String[inner.size()]));
}
}
}
}
}
return values.toArray(new String[0][0]);
}
/**
* This method will take the default config of service UI and add lifecycle configuration dynamically to the service
* service-config.xml
*
* @param head
* @param request
* @param config
* @param session
* @throws Exception
*/
public void addLifeCycleElements(OMElement head, HttpServletRequest request,
ServletConfig config, HttpSession session) throws Exception {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMElement lifeCycleTable = getLifecycleConfigElement(head);
if (lifeCycleTable != null) {
AddServicesServiceClient client = new AddServicesServiceClient(config, session);
String[] lifeCycleList = client.getAvailableAspects();
if (lifeCycleList == null || lifeCycleList.length == 0) {
return;
}
for (String lifeCycle : lifeCycleList) {
OMElement value = fac.createOMElement(UIGeneratorConstants.OPTION_VALUE, null);
value.setText(lifeCycle);
lifeCycleTable.addChild(value);
}
}
}
public OMElement getLifecycleConfigElement(OMElement head) throws Exception {
Iterator widgets = head.getChildrenWithName(new QName(null, UIGeneratorConstants.WIDGET_ELEMENT));
OMElement lifeCycleTable = null;
while (widgets.hasNext()) {
OMElement widget = (OMElement) widgets.next();
if ("Service Lifecycle".equals(widget.getAttributeValue(new QName(null, "name")))) {
widget = widget.getFirstChildWithName(new QName(null, UIGeneratorConstants.ARGUMENT_ELMENT));
lifeCycleTable = widget.getFirstChildWithName(new QName(null, UIGeneratorConstants.OPTION_VALUES));
break;
}
}
return lifeCycleTable;
}
}