Search in sources :

Example 1 with START_TRANSFORM_BODY_TYPE_JSON

use of com.mulesoft.tools.migration.library.tools.SalesforceUtils.START_TRANSFORM_BODY_TYPE_JSON in project mule-migration-assistant by mulesoft.

the class InvokeApexRestMethodOperation method execute.

@Override
public void execute(Element mule3Operation, MigrationReport report) throws RuntimeException {
    super.execute(mule3Operation, report);
    resolveAttributes(mule3Operation, mule4Operation);
    StringBuilder requestContents = new StringBuilder();
    String body = mule3Operation.getAttributeValue("input-ref");
    if (body != null && !body.isEmpty()) {
        // ?
        String expression = expressionMigrator.migrateExpression(body, true, mule3Operation);
        requestContents.append("body: " + expressionMigrator.unwrap(expression));
    }
    String headers = mule3Operation.getAttributeValue("requestHeaders-ref");
    if (headers != null && !headers.isEmpty()) {
        String expression = expressionMigrator.migrateExpression(headers, true, mule3Operation);
        if (requestContents != null && !requestContents.equals("")) {
            requestContents.append(", \n");
        }
        requestContents.append("headers: " + expressionMigrator.unwrap(expression));
    }
    Optional<Element> mule3QueryParams = Optional.ofNullable(mule3Operation.getChild("query-parameters", SalesforceUtils.MULE3_SALESFORCE_NAMESPACE));
    mule3QueryParams.ifPresent(queryParams -> {
        String refHeaders = queryParams.getAttributeValue("ref");
        if (refHeaders != null) {
            String expression = expressionMigrator.migrateExpression(refHeaders, true, queryParams);
            if (requestContents != null && !requestContents.equals("")) {
                requestContents.append(", \n");
            }
            requestContents.append("queryParams: " + expressionMigrator.unwrap(expression));
        }
        List<Element> children = queryParams.getChildren();
        if (children.size() > 0) {
            String queryParam = children.stream().map(object -> object.getContent().stream().map(innerObject -> object.getAttributeValue("key") + ": " + "\"" + innerObject.getValue() + "\"").collect(Collectors.joining(""))).collect(Collectors.joining(", "));
            if (requestContents != null && !requestContents.equals("")) {
                requestContents.append(", \n");
            }
            requestContents.append("queryParams: { " + queryParam + " }");
        }
    });
    if (requestContents != null && requestContents.length() != 0) {
        Element request = new Element("request", SalesforceUtils.MULE4_SALESFORCE_NAMESPACE);
        request.setContent(new CDATA(START_TRANSFORM_BODY_TYPE_JSON + requestContents.toString() + SalesforceUtils.CLOSE_TRANSFORM_BODY_TYPE_JSON));
        mule4Operation.addContent(request);
    }
    XmlDslUtils.addElementAfter(mule4Operation, mule3Operation);
    mule3Operation.getParentElement().removeContent(mule3Operation);
}
Also used : CDATA(org.jdom2.CDATA) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) START_TRANSFORM_BODY_TYPE_JSON(com.mulesoft.tools.migration.library.tools.SalesforceUtils.START_TRANSFORM_BODY_TYPE_JSON) ExpressionMigratorAware(com.mulesoft.tools.migration.step.ExpressionMigratorAware) XmlDslUtils(com.mulesoft.tools.migration.step.util.XmlDslUtils) Optional(java.util.Optional) MigrationReport(com.mulesoft.tools.migration.step.category.MigrationReport) Collectors(java.util.stream.Collectors) SalesforceUtils(com.mulesoft.tools.migration.library.tools.SalesforceUtils) Element(org.jdom2.Element) Element(org.jdom2.Element) CDATA(org.jdom2.CDATA)

Aggregations

Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 SalesforceUtils (com.mulesoft.tools.migration.library.tools.SalesforceUtils)1 START_TRANSFORM_BODY_TYPE_JSON (com.mulesoft.tools.migration.library.tools.SalesforceUtils.START_TRANSFORM_BODY_TYPE_JSON)1 ExpressionMigratorAware (com.mulesoft.tools.migration.step.ExpressionMigratorAware)1 MigrationReport (com.mulesoft.tools.migration.step.category.MigrationReport)1 XmlDslUtils (com.mulesoft.tools.migration.step.util.XmlDslUtils)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 CDATA (org.jdom2.CDATA)1 Element (org.jdom2.Element)1