Package org.wso2.carbon.mediator.rule.ui.internal

Source Code of org.wso2.carbon.mediator.rule.ui.internal.RuleMediatorClientHelper

/*
*  Licensed to the Apache Software Foundation (ASF) under one
*  or more contributor license agreements.  See the NOTICE file
*  distributed with this work for additional information
*  regarding copyright ownership.  The ASF licenses this file
*  to you 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.mediator.rule.ui.internal;

import org.apache.axiom.om.xpath.AXIOMXPath;
import org.jaxen.BaseXPath;
import org.wso2.carbon.rulecep.commons.descriptions.PropertyDescription;
import org.wso2.carbon.rulecep.commons.descriptions.ResourceDescription;
import org.wso2.carbon.rulecep.commons.descriptions.rule.mediator.RuleMediatorDescription;
import org.wso2.carbon.sequences.ui.util.ns.NameSpacesRegistrar;
import org.wso2.carbon.sequences.ui.util.ns.XPathFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Locale;
import java.util.ResourceBundle;


/**
*
*/
public class RuleMediatorClientHelper {

    public static String getPropertyXML(String id,
                                        Collection<PropertyDescription> mediatorPropertyList,
                                        Locale locale) {

        String propertyTableStyle = !mediatorPropertyList.isEmpty() ? "" : "display:none;";
        ResourceBundle bundle = ResourceBundle.
                getBundle("org.wso2.carbon.mediator.rule.ui.i18n.Resources",
                        locale);
        String header = bundle.getString(id + ".properties");
        String name = bundle.getString("th.property.name");
        String valueExpr = bundle.getString("th.value");
        String type = bundle.getString("th.property.type");
        String nsEditor = bundle.getString("namespaceeditor");
        String action = bundle.getString("th.action");
        String addProperty = bundle.getString("add.property");
        String namespaces = bundle.getString("namespaces");
        String delete = bundle.getString("delete");
        String prefix = "<tr>\n" +
                "<td>\n" +
                "<h3 class=\"mediator\">" + header + "</h3>\n" +
                "<div style=\"margin-top:0px;\">\n" +
                "<table id=\"" + id + "propertytable\" style=\"" + propertyTableStyle + "\" " +
                "class=\"styledInner\">\n" +
                "<thead>\n" +
                "<tr>\n" +
                "<th width=\"15%\">" + name + "</th>\n" +
                "<th width=\"15%\">" + valueExpr + "</th>\n" +
                "<th>" + action + "</th>\n" +
                "</tr>\n" +
                "<tbody id=\"" + id + "propertytbody\">";

        String suffix = "</tbody>\n" +
                "</thead>\n" +
                "</table>\n" +
                "</div>\n" +
                "</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "<td>\n" +
                "<div style=\"margin-top:0px;\">\n" +
                "<a name=\"add" + id + "NameLink\"></a>\n" +
                "<a class=\"add-icon-link\" href=\"#add" + id + "NameLink\" " +
                "onclick=\"addProperty('" + id + "')\">" + addProperty + "</a>\n" +
                "</div>\n" +
                "</td>\n" +
                "</tr>";

        String body = "";
        int i = 0;
        String valueMsg = bundle.getString("value");
        String exprMsg = bundle.getString("expression");
        for (PropertyDescription mp : mediatorPropertyList) {
            if (mp != null) {
                String propertyValue = mp.getValue();
                body += "<tr id=\"" + id + "propertyRaw" + i + "\">\n" +
                        "<td><input type=\"text\" name=\"" + id + "propertyName" + i + "\" id=\"" +
                        id + "propertyName" + i + "\" " +
                        "value=\"" + mp.getName() + "\"/>\n" +
                        "</td>\n" +
                        "<td>\n";

                if (propertyValue == null) {
                    propertyValue = "";
                }
                body += "<input id=\"" + id + "propertyValue" + i + "\" name=\"" + id +
                        "propertyValue" + i + "\"" + " type=\"text\" value=\"" +
                        propertyValue + "\"" + " />\n";
                body += "</td>\n" +
                        "<td><a href=\"#\" class=\"delete-icon-link\"" +
                        " onclick=\"deleteProperty('" + i + "','" + id + "');return false;\">" +
                        delete + "</a></td>" +
                        "</tr>";
            }
            i++;
        }
        body += " <input type=\"hidden\" name=\"" + id + "propertyCount\" id=\"" +
                id + "propertyCount\" value=\"" + i + "\" />";

        return prefix + body + suffix;
    }

    public static void registerNameSpaces(Collection<PropertyDescription> properties, String baseId,
                                          HttpSession httpSession) {

        if (properties == null || baseId == null || "".equals(baseId)) {
            return;
        }

        int i = 0;
        for (PropertyDescription property : properties) {
            if (property != null) {
                BaseXPath xPath = property.getExpression();
                if (xPath instanceof AXIOMXPath) {
                    NameSpacesRegistrar.getInstance().registerNameSpaces((AXIOMXPath) xPath,
                            baseId + String.valueOf(i), httpSession);
                }
            }
            i++;
        }
    }

    public static void setProperty(HttpServletRequest request,
                                   Object configuration, String mName, String id) {

        String registrationPropertyCount = request.getParameter(id + "propertyCount");
        if (registrationPropertyCount != null && !"".equals(registrationPropertyCount)) {
            int propertyCount = 0;
            try {
                propertyCount = Integer.parseInt(registrationPropertyCount.trim());

                for (int i = 0; i <= propertyCount; i++) {
                    String name = request.getParameter(id + "propertyName" + i);
                    if (name != null && !"".equals(name)) {
                        String valueId = id + "propertyValue" + i;
                        String value = request.getParameter(valueId);
                        if (value == null || "".equals(value.trim())) {
                            continue;
                        }
                        PropertyDescription mp = new PropertyDescription();
                        mp.setName(name.trim());
                        mp.setValue(value.trim());
                        invokeInstanceProperty(mName, mp, configuration);
                    }
                }
            } catch (NumberFormatException ignored) {
            }
        }
    }

    public static void setFactsAndResults(HttpServletRequest request,
                                          RuleMediatorDescription mediatorDescription,
                                          String id) {
        HttpSession session = request.getSession();
        XPathFactory xPathFactory = XPathFactory.getInstance();
        String inputCountParameter = request.getParameter(id + "Count");
        if (inputCountParameter != null && !"".equals(inputCountParameter)) {
            int inputCount = 0;
            try {
                inputCount = Integer.parseInt(inputCountParameter.trim());

                for (int i = 0; i < inputCount; i++) {
                    String name = request.getParameter(id + "Name" + i);
                    String type = request.getParameter(id + "Type" + i);

                    if (type != null && !"".equals(type)) {

                        ResourceDescription description = new ResourceDescription();
                        if (name != null && !"".equals(name)) {
                            description.setName(name.trim());
                        }
                        description.setType(type.trim());
                        String valueID = id + "Value" + i;
                        String value = request.getParameter(valueID);
                        if (value != null && !"".equals(value.trim())) {
                            value = value.trim();
                            String typeSelector = request.getParameter(id + "TypeSelection" + i);
                            boolean isExpression = typeSelector != null &&
                                    "expression".equals(typeSelector.trim());
                            boolean isKey = typeSelector != null &&
                                    "key".equals(typeSelector.trim());
                            if (isExpression) {
                                description.setExpression(xPathFactory.createSynapseXPath(valueID,
                                        value.trim(), session));
                            } else if (isKey) {
                                description.setKey(value);
                            } else {
                                description.setValue(value);
                            }
                        }
                        if ("fact".equals(id)) {
                            mediatorDescription.addFactDescription(description);
                        } else {
                            mediatorDescription.addResultDescription(description);
                        }
                    }
                }
            } catch (NumberFormatException ignored) {
            }
        }
    }

    private static void invokeInstanceProperty(String mName, Object val, Object target) {
        Class<?> aClass = target.getClass();
        try {
            Method method = aClass.getMethod(mName, val.getClass());
            method.invoke(target, val);
        } catch (Exception e) {
            throw new RuntimeException("Error setting property : " + mName
                    + " into" + aClass + " : " + e.getMessage(), e);
        }
    }

}
TOP

Related Classes of org.wso2.carbon.mediator.rule.ui.internal.RuleMediatorClientHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.