Search in sources :

Example 31 with XMLVector

use of com.twinsoft.convertigo.beans.common.XMLVector in project convertigo by convertigo.

the class WsReference method createSoapTransaction.

private static XmlHttpTransaction createSoapTransaction(XmlSchema xmlSchema, WsdlInterface iface, WsdlOperation operation, Project project, HttpConnector httpConnector) throws ParserConfigurationException, SAXException, IOException, EngineException {
    XmlHttpTransaction xmlHttpTransaction = null;
    WsdlRequest request;
    String requestXml;
    String transactionName, comment;
    String operationName;
    if (operation != null) {
        comment = operation.getDescription();
        try {
            comment = (comment.equals("") ? operation.getBindingOperation().getDocumentationElement().getTextContent() : comment);
        } catch (Exception e) {
        }
        operationName = operation.getName();
        transactionName = StringUtils.normalize("C" + operationName);
        xmlHttpTransaction = new XmlHttpTransaction();
        xmlHttpTransaction.bNew = true;
        xmlHttpTransaction.setHttpVerb(HttpMethodType.POST);
        xmlHttpTransaction.setName(transactionName);
        xmlHttpTransaction.setComment(comment);
        // Set encoding (UTF-8 by default)
        xmlHttpTransaction.setEncodingCharSet("UTF-8");
        xmlHttpTransaction.setXmlEncoding("UTF-8");
        // Ignore SOAP elements in response
        xmlHttpTransaction.setIgnoreSoapEnveloppe(true);
        // Adds parameters
        XMLVector<XMLVector<String>> parameters = new XMLVector<XMLVector<String>>();
        XMLVector<String> xmlv;
        xmlv = new XMLVector<String>();
        xmlv.add(HeaderName.ContentType.value());
        xmlv.add(MimeType.TextXml.value());
        parameters.add(xmlv);
        xmlv = new XMLVector<String>();
        xmlv.add("Host");
        xmlv.add(httpConnector.getServer());
        parameters.add(xmlv);
        xmlv = new XMLVector<String>();
        xmlv.add("SOAPAction");
        // fix #4215 - SOAPAction header must be empty
        xmlv.add("");
        parameters.add(xmlv);
        xmlv = new XMLVector<String>();
        xmlv.add("user-agent");
        xmlv.add("Convertigo EMS " + Version.fullProductVersion);
        parameters.add(xmlv);
        xmlHttpTransaction.setHttpParameters(parameters);
        QName qname = null;
        boolean bRPC = false;
        String style = operation.getStyle();
        if (style.toUpperCase().equals("RPC"))
            bRPC = true;
        // Set SOAP response element
        if (bRPC) {
            try {
                MessagePart[] parts = operation.getDefaultResponseParts();
                if (parts.length > 0) {
                    String ename = parts[0].getName();
                    if (parts[0].getPartType().name().equals("CONTENT")) {
                        MessagePart.ContentPart mpcp = (MessagePart.ContentPart) parts[0];
                        qname = mpcp.getSchemaType().getName();
                        if (qname != null) {
                            // response is based on an element defined with a type
                            // operationResponse element name; element name; element type
                            String responseQName = operationName + "Response;" + ename + ";" + "{" + qname.getNamespaceURI() + "}" + qname.getLocalPart();
                            xmlHttpTransaction.setResponseElementQName(responseQName);
                        }
                    }
                }
            } catch (Exception e) {
            }
        } else {
            try {
                qname = operation.getResponseBodyElementQName();
                if (qname != null) {
                    QName refName = new QName(qname.getNamespaceURI(), qname.getLocalPart());
                    xmlHttpTransaction.setXmlElementRefAffectation(new XmlQName(refName));
                }
            } catch (Exception e) {
            }
        }
        // Create request/response
        request = operation.addNewRequest("Test" + transactionName);
        requestXml = operation.createRequest(true);
        request.setRequestContent(requestXml);
        // responseXml = operation.createResponse(true);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document requestDoc = db.parse(new InputSource(new StringReader(requestXml)));
        // Document responseDoc = db.parse(new InputSource(new StringReader(responseXml)));
        Element enveloppe = requestDoc.getDocumentElement();
        String soapenvNamespace = enveloppe.getNamespaceURI();
        // Retrieve variables
        Element header = (Element) requestDoc.getDocumentElement().getElementsByTagNameNS(soapenvNamespace, "Header").item(0);
        Element body = (Element) requestDoc.getDocumentElement().getElementsByTagNameNS(soapenvNamespace, "Body").item(0);
        // System.out.println(XMLUtils.prettyPrintDOM(requestDoc));
        // Extract variables
        List<RequestableHttpVariable> variables = new ArrayList<RequestableHttpVariable>();
        extractSoapVariables(xmlSchema, variables, header, null, false, null);
        extractSoapVariables(xmlSchema, variables, body, null, false, null);
        // Serialize request/response into template xml files
        String projectName = project.getName();
        String connectorName = httpConnector.getName();
        String templateDir = Engine.projectDir(projectName) + "/soap-templates/" + connectorName;
        File dir = new File(templateDir);
        if (!dir.exists())
            dir.mkdirs();
        String requestTemplateName = "/soap-templates/" + connectorName + "/" + xmlHttpTransaction.getName() + ".xml";
        String requestTemplate = Engine.PROJECTS_PATH + "/" + projectName + requestTemplateName;
        xmlHttpTransaction.setRequestTemplate(requestTemplateName);
        saveTemplate(requestDoc, requestTemplate);
        // Adds variables
        for (RequestableHttpVariable variable : variables) {
            // System.out.println("adding "+ variable.getName());
            xmlHttpTransaction.add(variable);
        }
        xmlHttpTransaction.hasChanged = true;
    }
    return xmlHttpTransaction;
}
Also used : InputSource(org.xml.sax.InputSource) RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XmlHttpTransaction(com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction) XmlQName(com.twinsoft.convertigo.beans.common.XmlQName) QName(javax.xml.namespace.QName) MessagePart(com.eviware.soapui.model.iface.MessagePart) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) EngineException(com.twinsoft.convertigo.engine.EngineException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XmlQName(com.twinsoft.convertigo.beans.common.XmlQName) WsdlRequest(com.eviware.soapui.impl.wsdl.WsdlRequest) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) File(java.io.File)

Example 32 with XMLVector

use of com.twinsoft.convertigo.beans.common.XMLVector in project convertigo by convertigo.

the class SwaggerUtils method createRestConnector.

@SuppressWarnings("unused")
private static HttpConnector createRestConnector(JSONObject json) throws Exception {
    try {
        HttpConnector httpConnector = new HttpConnector();
        httpConnector.bNew = true;
        JSONObject info = json.getJSONObject("info");
        httpConnector.setName(StringUtils.normalize(info.getString("title")));
        String host = json.getString("host");
        int index = host.indexOf(":");
        String server = index == -1 ? host : host.substring(0, index);
        int port = index == -1 ? 0 : Integer.parseInt(host.substring(index + 1, 10));
        httpConnector.setServer(server);
        httpConnector.setPort(port <= 0 ? 80 : port);
        String basePath = json.getString("basePath");
        httpConnector.setBaseDir(basePath);
        JSONArray _consumes = new JSONArray();
        if (json.has("consumes")) {
            _consumes = json.getJSONArray("consumes");
        }
        JSONArray _produces = new JSONArray();
        if (json.has("produces")) {
            _produces = json.getJSONArray("produces");
        }
        Map<String, JSONObject> models = new HashMap<String, JSONObject>();
        JSONObject definitions = new JSONObject();
        if (json.has("definitions")) {
            definitions = json.getJSONObject("definitions");
            for (Iterator<String> i = GenericUtils.cast(definitions.keys()); i.hasNext(); ) {
                String key = i.next();
                JSONObject model = definitions.getJSONObject(key);
                models.put(key, model);
            }
        }
        JSONObject paths = json.getJSONObject("paths");
        for (Iterator<String> i1 = GenericUtils.cast(paths.keys()); i1.hasNext(); ) {
            String subDir = i1.next();
            JSONObject path = paths.getJSONObject(subDir);
            for (Iterator<String> i2 = GenericUtils.cast(path.keys()); i2.hasNext(); ) {
                String httpVerb = i2.next();
                JSONObject verb = path.getJSONObject(httpVerb);
                XMLVector<XMLVector<String>> httpParameters = new XMLVector<XMLVector<String>>();
                AbstractHttpTransaction transaction = new HttpTransaction();
                JSONArray consumes = verb.has("consumes") ? verb.getJSONArray("consumes") : _consumes;
                List<String> consumeList = new ArrayList<String>();
                for (int i = 0; i < consumes.length(); i++) {
                    consumeList.add(consumes.getString(i));
                }
                String h_ContentType = null;
                if (consumeList.contains(MimeType.Xml.value())) {
                    h_ContentType = MimeType.Xml.value();
                } else if (consumeList.contains(MimeType.Json.value())) {
                    h_ContentType = MimeType.Json.value();
                } else {
                    h_ContentType = consumeList.size() > 0 ? consumeList.get(0) : MimeType.WwwForm.value();
                }
                JSONArray produces = verb.has("produces") ? verb.getJSONArray("produces") : _produces;
                List<String> produceList = new ArrayList<String>();
                for (int i = 0; i < produces.length(); i++) {
                    produceList.add(produces.getString(i));
                }
                String h_Accept = null;
                if (produceList.contains(h_ContentType)) {
                    h_Accept = h_ContentType;
                } else {
                    if (produceList.contains(MimeType.Xml.value())) {
                        h_Accept = MimeType.Xml.value();
                    } else if (produceList.contains(MimeType.Json.value())) {
                        h_Accept = MimeType.Json.value();
                    }
                }
                if (h_Accept != null) {
                    XMLVector<String> xmlv = new XMLVector<String>();
                    xmlv.add("Accept");
                    xmlv.add(h_Accept);
                    httpParameters.add(xmlv);
                    if (h_Accept.equals(MimeType.Xml.value())) {
                        transaction = new XmlHttpTransaction();
                        ((XmlHttpTransaction) transaction).setXmlEncoding("UTF-8");
                    } else if (h_Accept.equals(MimeType.Json.value())) {
                        transaction = new JsonHttpTransaction();
                        ((JsonHttpTransaction) transaction).setIncludeDataType(false);
                    }
                }
                if (h_ContentType != null) {
                    XMLVector<String> xmlv = new XMLVector<String>();
                    xmlv.add(HeaderName.ContentType.value());
                    xmlv.add(h_ContentType);
                    httpParameters.add(xmlv);
                }
                String operationId = "";
                if (verb.has("operationId")) {
                    operationId = verb.getString("operationId");
                }
                String summary = "";
                if (verb.has("summary")) {
                    summary = verb.getString("summary");
                }
                String description = "";
                if (verb.has("description")) {
                    description = verb.getString("description");
                }
                String name = StringUtils.normalize(operationId);
                if (name.isEmpty()) {
                    name = StringUtils.normalize(summary);
                    if (name.isEmpty()) {
                        name = "operation";
                    }
                }
                String comment = summary;
                if (comment.isEmpty()) {
                    comment = description;
                }
                JSONArray parameters = new JSONArray();
                if (verb.has("parameters")) {
                    parameters = verb.getJSONArray("parameters");
                    for (int i = 0; i < parameters.length(); i++) {
                        JSONObject parameter = (JSONObject) parameters.get(i);
                        String type = "string";
                        if (parameter.has("collectionFormat")) {
                            type = parameter.getString("type");
                        }
                        String collectionFormat = "csv";
                        if (parameter.has("collectionFormat")) {
                            collectionFormat = parameter.getString("collectionFormat");
                        }
                        boolean isMultiValued = type.equalsIgnoreCase("array") && collectionFormat.equals("multi");
                        RequestableHttpVariable httpVariable = isMultiValued ? new RequestableHttpMultiValuedVariable() : new RequestableHttpVariable();
                        httpVariable.bNew = true;
                        httpVariable.setName(parameter.getString("name"));
                        httpVariable.setHttpName(parameter.getString("name"));
                        String in = parameter.getString("in");
                        if (in.equals("query") || in.equals("path") || in.equals("header")) {
                            httpVariable.setHttpMethod(HttpMethodType.GET.name());
                            if (in.equals("header")) {
                                // overrides variable's name : will be treated as dynamic header
                                httpVariable.setName(com.twinsoft.convertigo.engine.enums.Parameter.HttpHeader.getName() + parameter.getString("name"));
                                // do not post on target server
                                httpVariable.setHttpName("");
                            }
                        } else if (in.equals("formData") || in.equals("body")) {
                            httpVariable.setHttpMethod(HttpMethodType.POST.name());
                            if (in.equals("body")) {
                                // overrides variable's name for internal use
                                httpVariable.setName(com.twinsoft.convertigo.engine.enums.Parameter.HttpBody.getName());
                                // add internal __contentType variable
                                RequestableHttpVariable ct = new RequestableHttpVariable();
                                ct.setName(com.twinsoft.convertigo.engine.enums.Parameter.HttpContentType.getName());
                                // do not post on target server
                                ct.setHttpName("");
                                ct.setHttpMethod(HttpMethodType.POST.name());
                                ct.setValueOrNull(null);
                                ct.bNew = true;
                                transaction.addVariable(ct);
                                // 
                                if (parameter.has("schema")) {
                                // String schema = parameter.getString("schema");
                                }
                            }
                        } else {
                            httpVariable.setHttpMethod("");
                        }
                        Object defaultValue = null;
                        if (parameter.has("default")) {
                            defaultValue = parameter.get("default");
                        }
                        if (defaultValue == null && type.equalsIgnoreCase("array")) {
                            JSONObject items = parameter.getJSONObject("items");
                            if (items.has("default")) {
                                defaultValue = items.get("default");
                            }
                        }
                        httpVariable.setValueOrNull(defaultValue);
                        if (parameter.has("description")) {
                            httpVariable.setDescription(parameter.getString("description"));
                        }
                        transaction.addVariable(httpVariable);
                    }
                }
                transaction.bNew = true;
                transaction.setName(name);
                transaction.setComment(comment);
                transaction.setSubDir(subDir);
                transaction.setHttpVerb(HttpMethodType.valueOf(httpVerb.toUpperCase()));
                transaction.setHttpParameters(httpParameters);
                transaction.setHttpInfo(true);
                httpConnector.add(transaction);
            }
        }
        return httpConnector;
    } catch (Throwable t) {
        System.out.println(t);
        throw new Exception("Invalid Swagger format", t);
    }
}
Also used : XmlHttpTransaction(com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction) HttpTransaction(com.twinsoft.convertigo.beans.transactions.HttpTransaction) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) JsonHttpTransaction(com.twinsoft.convertigo.beans.transactions.JsonHttpTransaction) HttpConnector(com.twinsoft.convertigo.beans.connectors.HttpConnector) RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) HashMap(java.util.HashMap) XmlHttpTransaction(com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction) JSONArray(org.codehaus.jettison.json.JSONArray) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) JSONObject(org.codehaus.jettison.json.JSONObject) RequestableHttpMultiValuedVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable) JsonHttpTransaction(com.twinsoft.convertigo.beans.transactions.JsonHttpTransaction) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 33 with XMLVector

use of com.twinsoft.convertigo.beans.common.XMLVector in project convertigo by convertigo.

the class ChangeToXMLConcatStepAction method run.

/* (non-Javadoc)
	 * @see com.twinsoft.convertigo.eclipse.popup.actions.MyAbstractAction#run()
	 */
@Override
public void run() {
    Display display = Display.getDefault();
    Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
    Shell shell = getParentShell();
    shell.setCursor(waitCursor);
    try {
        ProjectExplorerView explorerView = getProjectExplorerView();
        if (explorerView != null) {
            TreeObject treeObject = explorerView.getFirstSelectedTreeObject();
            Object databaseObject = treeObject.getObject();
            if ((databaseObject != null) && (databaseObject instanceof XMLElementStep)) {
                XMLElementStep elementStep = (XMLElementStep) databaseObject;
                TreeParent treeParent = treeObject.getParent();
                DatabaseObjectTreeObject parentTreeObject = null;
                if (treeParent instanceof DatabaseObjectTreeObject)
                    parentTreeObject = (DatabaseObjectTreeObject) treeParent;
                else
                    parentTreeObject = (DatabaseObjectTreeObject) treeParent.getParent();
                if (parentTreeObject != null) {
                    // New XMLConcatStep step
                    XMLConcatStep concatStep = new XMLConcatStep();
                    if (DatabaseObjectsManager.acceptDatabaseObjects(elementStep.getParent(), concatStep)) {
                        XMLVector<XMLVector<Object>> sources = new XMLVector<XMLVector<Object>>();
                        XMLVector<Object> source = new XMLVector<Object>();
                        source.add("description");
                        source.add(elementStep.getSourceDefinition());
                        source.add(elementStep.getNodeText());
                        sources.add(source);
                        concatStep.setOutput(elementStep.isOutput());
                        concatStep.setEnabled(elementStep.isEnabled());
                        concatStep.setComment(elementStep.getComment());
                        // elementStep.getNodeText();
                        concatStep.setNodeName(elementStep.getNodeName());
                        concatStep.setSourcesDefinition(sources);
                        concatStep.bNew = true;
                        concatStep.hasChanged = true;
                        // Add new XMLConcatStep step to parent
                        DatabaseObject parentDbo = elementStep.getParent();
                        parentDbo.add(concatStep);
                        // Set correct order
                        if (parentDbo instanceof StepWithExpressions)
                            ((StepWithExpressions) parentDbo).insertAtOrder(concatStep, elementStep.priority);
                        else if (parentDbo instanceof Sequence)
                            ((Sequence) parentDbo).insertAtOrder(concatStep, elementStep.priority);
                        // Add new XMLConcatStep step in Tree
                        StepTreeObject stepTreeObject = new StepTreeObject(explorerView.viewer, concatStep);
                        treeParent.addChild(stepTreeObject);
                        // Delete XMLElementStep step
                        long oldPriority = elementStep.priority;
                        elementStep.delete();
                        concatStep.getSequence().fireStepMoved(new StepEvent(concatStep, String.valueOf(oldPriority)));
                        parentTreeObject.hasBeenModified(true);
                        explorerView.reloadTreeObject(parentTreeObject);
                        explorerView.setSelectedTreeObject(explorerView.findTreeObjectByUserObject(concatStep));
                    } else {
                        throw new EngineException("You cannot paste to a " + elementStep.getParent().getClass().getSimpleName() + " a database object of type " + concatStep.getClass().getSimpleName());
                    }
                }
            }
        }
    } catch (Throwable e) {
        ConvertigoPlugin.logException(e, "Unable to change XMLElement to XMLConcat step!");
    } finally {
        shell.setCursor(null);
        waitCursor.dispose();
    }
}
Also used : StepEvent(com.twinsoft.convertigo.beans.core.StepEvent) ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) DatabaseObjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) XMLElementStep(com.twinsoft.convertigo.beans.steps.XMLElementStep) TreeParent(com.twinsoft.convertigo.eclipse.views.projectexplorer.TreeParent) StepTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.StepTreeObject) StepWithExpressions(com.twinsoft.convertigo.beans.core.StepWithExpressions) EngineException(com.twinsoft.convertigo.engine.EngineException) XMLConcatStep(com.twinsoft.convertigo.beans.steps.XMLConcatStep) Sequence(com.twinsoft.convertigo.beans.core.Sequence) Cursor(org.eclipse.swt.graphics.Cursor) Shell(org.eclipse.swt.widgets.Shell) TreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject) DatabaseObjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject) StepTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.StepTreeObject) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) TreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject) DatabaseObjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject) StepTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.StepTreeObject) Display(org.eclipse.swt.widgets.Display)

Example 34 with XMLVector

use of com.twinsoft.convertigo.beans.common.XMLVector in project convertigo by convertigo.

the class ChangeToXMLElementStepAction method run.

/* (non-Javadoc)
	 * @see com.twinsoft.convertigo.eclipse.popup.actions.MyAbstractAction#run()
	 */
@SuppressWarnings("unchecked")
@Override
public void run() {
    Display display = Display.getDefault();
    Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
    Shell shell = getParentShell();
    shell.setCursor(waitCursor);
    try {
        ProjectExplorerView explorerView = getProjectExplorerView();
        if (explorerView != null) {
            TreeObject treeObject = explorerView.getFirstSelectedTreeObject();
            Object databaseObject = treeObject.getObject();
            // XML Concat step
            if ((databaseObject != null) && (databaseObject instanceof XMLConcatStep)) {
                XMLConcatStep concatStep = (XMLConcatStep) databaseObject;
                TreeParent treeParent = treeObject.getParent();
                DatabaseObjectTreeObject parentTreeObject = null;
                if (treeParent instanceof DatabaseObjectTreeObject)
                    parentTreeObject = (DatabaseObjectTreeObject) treeParent;
                else
                    parentTreeObject = (DatabaseObjectTreeObject) treeParent.getParent();
                if (parentTreeObject != null) {
                    // New XMLElementStep step
                    XMLElementStep elementStep = new XMLElementStep();
                    if (DatabaseObjectsManager.acceptDatabaseObjects(concatStep.getParent(), elementStep)) {
                        if (concatStep.getSourcesDefinition().toString().equals("[[]]")) {
                            elementStep.setSourceDefinition(new XMLVector<String>());
                        } else {
                            // Set properties (Default value and Source)
                            XMLVector<XMLVector<Object>> sources = concatStep.getSourcesDefinition();
                            XMLVector<String> sourceDefinition = new XMLVector<String>();
                            String defaultValue = "";
                            for (XMLVector<Object> source : sources) {
                                if (sources.lastElement() == source) {
                                    defaultValue += source.get(2);
                                } else {
                                    defaultValue += source.get(2) + concatStep.getSeparator();
                                }
                                if (sourceDefinition.toString().equals("[]") && (!source.get(1).toString().equals("") && !source.get(1).toString().equals("[]"))) {
                                    sourceDefinition = (XMLVector<String>) source.get(1);
                                }
                            }
                            elementStep.setOutput(concatStep.isOutput());
                            elementStep.setEnabled(concatStep.isEnabled());
                            elementStep.setComment(concatStep.getComment());
                            elementStep.setNodeName(concatStep.getNodeName());
                            elementStep.setNodeText(defaultValue);
                            elementStep.setSourceDefinition(sourceDefinition);
                        }
                        elementStep.bNew = true;
                        elementStep.hasChanged = true;
                        // Add new XMLElementStep step to parent
                        DatabaseObject parentDbo = concatStep.getParent();
                        parentDbo.add(elementStep);
                        // Set correct order
                        if (parentDbo instanceof StepWithExpressions)
                            ((StepWithExpressions) parentDbo).insertAtOrder(elementStep, concatStep.priority);
                        else if (parentDbo instanceof Sequence)
                            ((Sequence) parentDbo).insertAtOrder(elementStep, concatStep.priority);
                        // Add new XMLElementStep step in Tree
                        StepTreeObject stepTreeObject = new StepTreeObject(explorerView.viewer, elementStep);
                        treeParent.addChild(stepTreeObject);
                        // Delete XMLConcatStep step
                        long oldPriority = concatStep.priority;
                        concatStep.delete();
                        elementStep.getSequence().fireStepMoved(new StepEvent(elementStep, String.valueOf(oldPriority)));
                        parentTreeObject.hasBeenModified(true);
                        explorerView.reloadTreeObject(parentTreeObject);
                        explorerView.setSelectedTreeObject(explorerView.findTreeObjectByUserObject(elementStep));
                    } else {
                        throw new EngineException("You cannot paste to a " + concatStep.getParent().getClass().getSimpleName() + " a database object of type " + elementStep.getClass().getSimpleName());
                    }
                }
            }
            // XML Attribute
            if ((databaseObject != null) && (databaseObject instanceof XMLAttributeStep)) {
                XMLAttributeStep attributeStep = (XMLAttributeStep) databaseObject;
                TreeParent treeParent = treeObject.getParent();
                DatabaseObjectTreeObject parentTreeObject = null;
                if (treeParent instanceof DatabaseObjectTreeObject)
                    parentTreeObject = (DatabaseObjectTreeObject) treeParent;
                else
                    parentTreeObject = (DatabaseObjectTreeObject) treeParent.getParent();
                if (parentTreeObject != null) {
                    // New XMLElement step
                    XMLElementStep elementStep = new XMLElementStep();
                    if (DatabaseObjectsManager.acceptDatabaseObjects(attributeStep.getParent(), elementStep)) {
                        // Set properties
                        elementStep.setOutput(attributeStep.isOutput());
                        elementStep.setEnabled(attributeStep.isEnabled());
                        elementStep.setComment(attributeStep.getComment());
                        elementStep.setSourceDefinition(attributeStep.getSourceDefinition());
                        elementStep.setNodeText(attributeStep.getNodeText());
                        elementStep.setNodeName(attributeStep.getNodeName());
                        elementStep.bNew = true;
                        elementStep.hasChanged = true;
                        // Add new XMLElement step to parent
                        DatabaseObject parentDbo = attributeStep.getParent();
                        parentDbo.add(elementStep);
                        // Set correct order
                        if (parentDbo instanceof StepWithExpressions)
                            ((StepWithExpressions) parentDbo).insertAtOrder(elementStep, attributeStep.priority);
                        else if (parentDbo instanceof Sequence)
                            ((Sequence) parentDbo).insertAtOrder(elementStep, attributeStep.priority);
                        // Add new XMLElement step in Tree
                        StepTreeObject stepTreeObject = new StepTreeObject(explorerView.viewer, attributeStep);
                        treeParent.addChild(stepTreeObject);
                        // Delete XMLAttribute step
                        long oldPriority = attributeStep.priority;
                        attributeStep.delete();
                        elementStep.getSequence().fireStepMoved(new StepEvent(elementStep, String.valueOf(oldPriority)));
                        parentTreeObject.hasBeenModified(true);
                        explorerView.reloadTreeObject(parentTreeObject);
                        explorerView.setSelectedTreeObject(explorerView.findTreeObjectByUserObject(elementStep));
                    } else {
                        throw new EngineException("You cannot paste to a " + attributeStep.getParent().getClass().getSimpleName() + " a database object of type " + elementStep.getClass().getSimpleName());
                    }
                }
            }
            // JElement
            if ((databaseObject != null) && (databaseObject instanceof ElementStep)) {
                ElementStep jelementStep = (ElementStep) databaseObject;
                TreeParent treeParent = treeObject.getParent();
                DatabaseObjectTreeObject parentTreeObject = null;
                if (treeParent instanceof DatabaseObjectTreeObject)
                    parentTreeObject = (DatabaseObjectTreeObject) treeParent;
                else
                    parentTreeObject = (DatabaseObjectTreeObject) treeParent.getParent();
                if (parentTreeObject != null) {
                    // New XMLElement step
                    XMLElementStep elementStep = new XMLElementStep();
                    if (DatabaseObjectsManager.acceptDatabaseObjects(jelementStep.getParent(), elementStep)) {
                        // Set properties
                        elementStep.setOutput(jelementStep.isOutput());
                        elementStep.setEnabled(jelementStep.isEnabled());
                        elementStep.setComment(jelementStep.getComment());
                        // elementStep.setSourceDefinition(jelementStep.getSourceDefinition());
                        elementStep.setNodeText(jelementStep.getNodeText());
                        elementStep.setNodeName(jelementStep.getNodeName());
                        elementStep.bNew = true;
                        elementStep.hasChanged = true;
                        // Add new XMLElement step to parent
                        DatabaseObject parentDbo = jelementStep.getParent();
                        parentDbo.add(elementStep);
                        for (Step step : jelementStep.getAllSteps()) {
                            try {
                                elementStep.addStep(step);
                            } catch (Throwable t) {
                            }
                        }
                        // Set correct order
                        if (parentDbo instanceof StepWithExpressions)
                            ((StepWithExpressions) parentDbo).insertAtOrder(elementStep, jelementStep.priority);
                        else if (parentDbo instanceof Sequence)
                            ((Sequence) parentDbo).insertAtOrder(elementStep, jelementStep.priority);
                        // Add new XMLElement step in Tree
                        StepTreeObject stepTreeObject = new StepTreeObject(explorerView.viewer, jelementStep);
                        treeParent.addChild(stepTreeObject);
                        // Delete XMLAttribute step
                        long oldPriority = jelementStep.priority;
                        jelementStep.delete();
                        elementStep.getSequence().fireStepMoved(new StepEvent(elementStep, String.valueOf(oldPriority)));
                        parentTreeObject.hasBeenModified(true);
                        explorerView.reloadTreeObject(parentTreeObject);
                        explorerView.setSelectedTreeObject(explorerView.findTreeObjectByUserObject(elementStep));
                    } else {
                        throw new EngineException("You cannot paste to a " + jelementStep.getParent().getClass().getSimpleName() + " a database object of type " + elementStep.getClass().getSimpleName());
                    }
                }
            }
        }
    } catch (Throwable e) {
        ConvertigoPlugin.logException(e, "Unable to change step to XMLElement step!");
    } finally {
        shell.setCursor(null);
        waitCursor.dispose();
    }
}
Also used : DatabaseObjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) TreeParent(com.twinsoft.convertigo.eclipse.views.projectexplorer.TreeParent) XMLElementStep(com.twinsoft.convertigo.beans.steps.XMLElementStep) StepTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.StepTreeObject) StepWithExpressions(com.twinsoft.convertigo.beans.core.StepWithExpressions) EngineException(com.twinsoft.convertigo.engine.EngineException) XMLConcatStep(com.twinsoft.convertigo.beans.steps.XMLConcatStep) XMLAttributeStep(com.twinsoft.convertigo.beans.steps.XMLAttributeStep) Step(com.twinsoft.convertigo.beans.core.Step) ElementStep(com.twinsoft.convertigo.beans.steps.ElementStep) XMLElementStep(com.twinsoft.convertigo.beans.steps.XMLElementStep) XMLConcatStep(com.twinsoft.convertigo.beans.steps.XMLConcatStep) Cursor(org.eclipse.swt.graphics.Cursor) Shell(org.eclipse.swt.widgets.Shell) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) ElementStep(com.twinsoft.convertigo.beans.steps.ElementStep) XMLElementStep(com.twinsoft.convertigo.beans.steps.XMLElementStep) StepEvent(com.twinsoft.convertigo.beans.core.StepEvent) ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) Sequence(com.twinsoft.convertigo.beans.core.Sequence) XMLAttributeStep(com.twinsoft.convertigo.beans.steps.XMLAttributeStep) StepTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.StepTreeObject) TreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject) DatabaseObjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject) StepTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.StepTreeObject) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) TreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject) DatabaseObjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject) Display(org.eclipse.swt.widgets.Display)

Example 35 with XMLVector

use of com.twinsoft.convertigo.beans.common.XMLVector in project convertigo by convertigo.

the class DatabaseObjectTreeObject method getPropertyValue.

public Object getPropertyValue(Object id) {
    if (id == null)
        return null;
    DatabaseObject databaseObject = getObject();
    String propertyName = (String) id;
    if (propertyName.equals(P_TYPE))
        return databaseObjectBeanDescriptor.getDisplayName();
    else if (propertyName.equals(P_JAVA_CLASS))
        return databaseObject.getClass().getName();
    else if (propertyName.equals(P_NAME))
        return databaseObject.getName();
    else if (propertyName.equals(P_QNAME))
        return databaseObject.getQName();
    else if (propertyName.equals(P_PRIORITY))
        return Long.toString(databaseObject.priority);
    else if (propertyName.equals(P_DEPTH)) {
        if (databaseObject instanceof ScreenClass)
            return Integer.toString(((ScreenClass) databaseObject).getDepth());
        else
            return org.apache.commons.lang3.StringUtils.countMatches(databaseObject.getQName(), '.');
    } else if (propertyName.equals(P_EXPORTED)) {
        return databaseObject.getProject().getInfoForProperty("exported");
    } else if (propertyName.equals(P_MIN_VERSION)) {
        return databaseObject.getProject().getMinVersion();
    } else {
        try {
            java.beans.PropertyDescriptor databaseObjectPropertyDescriptor = getPropertyDescriptor(propertyName);
            if (databaseObjectPropertyDescriptor == null) {
                return null;
            }
            Class<?> pec = databaseObjectPropertyDescriptor.getPropertyEditorClass();
            Method getter = databaseObjectPropertyDescriptor.getReadMethod();
            Object compilablePropertySourceValue = databaseObject.getCompilablePropertySourceValue(propertyName);
            Object value;
            if (compilablePropertySourceValue == null) {
                Object[] args = {};
                value = getter.invoke(databaseObject, args);
            } else {
                value = compilablePropertySourceValue;
            }
            boolean done = false;
            if (value instanceof String) {
                PropertyDescriptor propertyDescriptor = findPropertyDescriptor(id);
                if (propertyDescriptor instanceof DynamicComboBoxPropertyDescriptor) {
                    DynamicComboBoxPropertyDescriptor pd = (DynamicComboBoxPropertyDescriptor) propertyDescriptor;
                    value = pd.setValue((String) value);
                    done = true;
                }
            }
            if (done) {
            // do nothing
            } else if (value instanceof Boolean) {
                value = ((Boolean) value).booleanValue() ? Integer.valueOf(0) : Integer.valueOf(1);
            } else if ((pec != null) && (PropertyWithTagsEditor.class.isAssignableFrom(pec) || Enum.class.isAssignableFrom(pec))) {
                if (!(value instanceof Integer)) {
                    if (PropertyWithTagsEditorAdvance.class.isAssignableFrom(pec)) {
                        Method getTags = pec.getMethod("getTags", new Class[] { DatabaseObjectTreeObject.class, String.class });
                        String[] tags = (String[]) getTags.invoke(null, new Object[] { this, propertyName });
                        int i;
                        for (i = 0; i < tags.length; i++) {
                            if (tags[i].equals(value)) {
                                value = Integer.valueOf(i);
                                break;
                            }
                        }
                        // if we did not find our string in the tag list set value to index 0
                        if (i == tags.length) {
                            value = Integer.valueOf(0);
                            String message = "Incorrect property \"" + propertyName + "\" value for the object \"" + databaseObject.getName() + "\".";
                            ConvertigoPlugin.logWarning(message);
                        }
                    } else if (Enum.class.isAssignableFrom(pec)) {
                        value = Integer.valueOf(((Enum<?>) value).ordinal());
                    } else if (StringComboBoxPropertyDescriptor.class.isAssignableFrom(pec)) {
                    // nothing to do: value is a string
                    }
                }
                if ((EmulatorTechnologyEditor.class.equals(pec))) {
                    Method getEmulatorClassNames = pec.getDeclaredMethod("getEmulatorClassNames", new Class[] { DatabaseObjectTreeObject.class });
                    String[] emulatorClassNames = (String[]) getEmulatorClassNames.invoke(null, new Object[] { this });
                    for (int i = 0; i < emulatorClassNames.length; i++) {
                        if (emulatorClassNames[i].equals(value)) {
                            value = Integer.valueOf(i);
                            break;
                        }
                    }
                }
            // else simply return the combo index
            } else if (value instanceof Number) {
                value = ((Number) value).toString();
            }
            // Get property's nillable value
            if (Boolean.TRUE.equals(databaseObjectPropertyDescriptor.getValue("nillable"))) {
                try {
                    Boolean isNull = ((INillableProperty) databaseObject).isNullProperty(propertyName);
                    PropertyDescriptor pd = findPropertyDescriptor(propertyName);
                    if ((pd != null) && (pd instanceof DataOrNullPropertyDescriptor)) {
                        ((DataOrNullPropertyDescriptor) pd).setNullProperty(isNull);
                        if (isNull) {
                            // Overrides value by fake one used by property editor
                            if (value instanceof String)
                                value = "<value is null>";
                            if (value instanceof XMLVector) {
                                XMLVector<Object> xmlv = new XMLVector<Object>();
                                xmlv.add("null");
                                value = xmlv;
                            }
                        }
                    }
                } catch (Exception e) {
                    String message = "Error while trying to retrieve 'isNull' attribute of property \"" + propertyName + "\" for the object \"" + databaseObject.getName() + "\".";
                    ConvertigoPlugin.logException(e, message);
                }
            }
            // Check for property normalized value if needed
            if (Boolean.TRUE.equals(databaseObjectPropertyDescriptor.getValue(DatabaseObject.PROPERTY_XMLNAME))) {
                // Ignore compilable property source value
                if (compilablePropertySourceValue == null) {
                    if (value instanceof String) {
                        if (!XMLUtils.checkName(value.toString())) {
                            String message = "Property \"" + propertyName + "\" value for the object \"" + databaseObject.getName() + "\" isn't XML compliant.";
                            ConvertigoPlugin.logError(message, Boolean.TRUE);
                        }
                    }
                }
            }
            return value;
        } catch (Exception e) {
            String message = "Error while trying to retrieve property \"" + propertyName + "\" value for the object \"" + databaseObject.getName() + "\".";
            ConvertigoPlugin.logException(e, message);
            return null;
        }
    }
}
Also used : XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) InfoPropertyDescriptor(com.twinsoft.convertigo.eclipse.views.projectexplorer.InfoPropertyDescriptor) DynamicComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DynamicComboBoxPropertyDescriptor) PropertyDescriptor(org.eclipse.ui.views.properties.PropertyDescriptor) StringComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.StringComboBoxPropertyDescriptor) TextPropertyDescriptor(org.eclipse.ui.views.properties.TextPropertyDescriptor) ScriptablePropertyDescriptor(com.twinsoft.convertigo.eclipse.views.projectexplorer.ScriptablePropertyDescriptor) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor) DynamicInfoPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DynamicInfoPropertyDescriptor) DataOrNullPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DataOrNullPropertyDescriptor) ScreenClass(com.twinsoft.convertigo.beans.core.ScreenClass) Method(java.lang.reflect.Method) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConvertigoException(com.twinsoft.convertigo.engine.ConvertigoException) UndefinedSymbolsException(com.twinsoft.convertigo.engine.UndefinedSymbolsException) DynamicComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DynamicComboBoxPropertyDescriptor) StringComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.StringComboBoxPropertyDescriptor) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) PropertyWithTagsEditorAdvance(com.twinsoft.convertigo.eclipse.property_editors.PropertyWithTagsEditorAdvance) ScreenClass(com.twinsoft.convertigo.beans.core.ScreenClass) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) DataOrNullPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DataOrNullPropertyDescriptor)

Aggregations

XMLVector (com.twinsoft.convertigo.beans.common.XMLVector)51 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)19 EngineException (com.twinsoft.convertigo.engine.EngineException)15 Element (org.w3c.dom.Element)15 IOException (java.io.IOException)10 NodeList (org.w3c.dom.NodeList)8 RequestableHttpVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpVariable)7 RequestableVariable (com.twinsoft.convertigo.beans.variables.RequestableVariable)7 TreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject)7 DatabaseObjectTreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject)6 Shell (org.eclipse.swt.widgets.Shell)6 Step (com.twinsoft.convertigo.beans.core.Step)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 ArrayList (java.util.ArrayList)5 Document (org.w3c.dom.Document)5 Block (com.twinsoft.convertigo.beans.core.Block)4 Sequence (com.twinsoft.convertigo.beans.core.Sequence)4 StepWithExpressions (com.twinsoft.convertigo.beans.core.StepWithExpressions)4 TestCase (com.twinsoft.convertigo.beans.core.TestCase)4 Variable (com.twinsoft.convertigo.beans.core.Variable)4