Search in sources :

Example 6 with UrlMapping

use of com.twinsoft.convertigo.beans.core.UrlMapping in project convertigo by convertigo.

the class OpenApiUtils method writeOpenApiToFile.

private static void writeOpenApiToFile(final String requestUrl, final UrlMapper urlMapper, final File yamlFile, boolean useExternalRef) throws Exception {
    synchronized (lockObject) {
        if (yamlFile.exists() && Engine.isEngineMode())
            return;
        Long t0 = System.currentTimeMillis();
        Project project = urlMapper.getProject();
        String projectName = project.getName();
        String oasDirUrl = requestUrl.substring(0, requestUrl.indexOf("/" + servletMappingPath)) + "/projects/" + projectName + "/" + jsonSchemaDirectory + "/";
        OpenAPI openAPI = parseCommon(requestUrl, project);
        List<Tag> tags = new ArrayList<>();
        Tag tag = new Tag();
        tag.setName(project.getName());
        tag.setDescription(project.getComment());
        tags.add(tag);
        openAPI.setTags(tags);
        if (openAPI.getComponents() == null) {
            openAPI.components(new Components());
        }
        // Security
        Map<String, SecurityScheme> securitySchemes = openAPI.getComponents().getSecuritySchemes();
        for (UrlAuthentication authentication : urlMapper.getAuthenticationList()) {
            if (AuthenticationType.Basic.equals(authentication.getType())) {
                if (securitySchemes == null || !securitySchemes.containsKey("basicAuth")) {
                    SecurityScheme securitySchemesItem = new SecurityScheme();
                    securitySchemesItem.setType(SecurityScheme.Type.HTTP);
                    securitySchemesItem.setScheme("basic");
                    openAPI.getComponents().addSecuritySchemes("basicAuth", securitySchemesItem);
                    SecurityRequirement securityRequirement = new SecurityRequirement();
                    securityRequirement.addList("basicAuth", new ArrayList<String>());
                    openAPI.addSecurityItem(securityRequirement);
                }
            }
        }
        List<String> refList = new ArrayList<String>();
        List<String> opIdList = new ArrayList<String>();
        // Paths
        Paths paths = new Paths();
        try {
            for (UrlMapping urlMapping : urlMapper.getMappingList()) {
                PathItem item = new PathItem();
                for (UrlMappingOperation umo : urlMapping.getOperationList()) {
                    Operation operation = new Operation();
                    operation.setOperationId(getOperationId(opIdList, umo, false));
                    operation.setDescription(umo.getComment());
                    operation.setSummary(umo.getComment());
                    // Tags
                    List<String> list = Arrays.asList("" + project.getName());
                    operation.setTags(list);
                    // 1 - add path parameters
                    for (String pathVarName : urlMapping.getPathVariableNames()) {
                        PathParameter parameter = new PathParameter();
                        parameter.setName(pathVarName);
                        // retrieve parameter description from bean
                        UrlMappingParameter ump = null;
                        try {
                            ump = umo.getParameterByName(pathVarName);
                        } catch (Exception e) {
                        }
                        if (ump != null && ump.getType() == Type.Path) {
                            parameter.setDescription(ump.getComment());
                            Schema<?> schema = getSchema(ump);
                            if (schema != null) {
                                parameter.setSchema(schema);
                            }
                        }
                        operation.addParametersItem(parameter);
                    }
                    // 2 - add other parameters
                    for (UrlMappingParameter ump : umo.getParameterList()) {
                        Parameter parameter = null;
                        if (ump.getType() == Type.Query) {
                            parameter = new QueryParameter();
                        } else if (ump.getType() == Type.Form) {
                            addFormParameter(operation, ump);
                        } else if (ump.getType() == Type.Body) {
                            addBodyParameter(operation, ump, oasDirUrl, refList, useExternalRef);
                        } else if (ump.getType() == Type.Header) {
                            parameter = new HeaderParameter();
                        } else if (ump.getType() == Type.Path) {
                        // ignore : should have been treated before
                        }
                        if (parameter != null) {
                            // Query | Header
                            parameter.setName(ump.getName());
                            parameter.setDescription(ump.getComment());
                            parameter.setRequired(ump.isRequired());
                            // parameter.setAllowEmptyValue(allowEmptyValue);
                            Schema<?> schema = getSchema(ump);
                            if (schema != null) {
                                parameter.setSchema(schema);
                            }
                            // add parameter
                            if (ump.isExposed()) {
                                operation.addParametersItem(parameter);
                            }
                        }
                    }
                    // Responses
                    List<String> produces = new ArrayList<String>();
                    if (umo instanceof AbstractRestOperation) {
                        DataContent dataOutput = ((AbstractRestOperation) umo).getOutputContent();
                        if (dataOutput.equals(DataContent.toJson)) {
                            produces = Arrays.asList(MimeType.Json.value());
                        } else if (dataOutput.equals(DataContent.toXml)) {
                            produces = Arrays.asList(MimeType.Xml.value());
                        } else {
                            produces = Arrays.asList(MimeType.Json.value(), MimeType.Xml.value());
                        }
                    }
                    ApiResponses responses = new ApiResponses();
                    operation.setResponses(responses);
                    for (UrlMappingResponse umr : umo.getResponseList()) {
                        String statusCode = umr.getStatusCode();
                        if (!statusCode.isEmpty()) {
                            if (!responses.containsKey(statusCode)) {
                                ApiResponse response = new ApiResponse();
                                response.setDescription(umr.getStatusText());
                                responses.addApiResponse(statusCode, response);
                                String modelReference = ((IMappingRefModel) umr).getModelReference();
                                if (!modelReference.isEmpty() && !produces.isEmpty()) {
                                    if (modelReference.indexOf(".jsonschema") != -1) {
                                        modelReference = modelReference.replace(".jsonschema#/definitions/", ".json#/components/schemas/");
                                        modelReference = oasDirUrl + modelReference;
                                    }
                                    Content content = new Content();
                                    response.setContent(content);
                                    for (String mt : produces) {
                                        MediaType mediaType = new MediaType();
                                        content.addMediaType(mt, mediaType);
                                        ObjectSchema schema = new ObjectSchema();
                                        if (!refList.contains(modelReference)) {
                                            refList.add(modelReference);
                                        }
                                        if (!useExternalRef && modelReference.indexOf('#') != -1) {
                                            modelReference = modelReference.substring(modelReference.indexOf('#'));
                                        }
                                        schema.set$ref(modelReference);
                                        mediaType.setSchema(schema);
                                    }
                                }
                            }
                        }
                    }
                    if (umo.getMethod().equals(HttpMethodType.DELETE.name())) {
                        item.setDelete(operation);
                    } else if (umo.getMethod().equals(HttpMethodType.GET.name())) {
                        item.setGet(operation);
                    } else if (umo.getMethod().equals(HttpMethodType.HEAD.name())) {
                        item.setHead(operation);
                    } else if (umo.getMethod().equals(HttpMethodType.OPTIONS.name())) {
                        item.setOptions(operation);
                    } else if (umo.getMethod().equals(HttpMethodType.POST.name())) {
                        item.setPost(operation);
                    } else if (umo.getMethod().equals(HttpMethodType.PUT.name())) {
                        item.setPut(operation);
                    } else if (umo.getMethod().equals(HttpMethodType.TRACE.name())) {
                        item.setTrace(operation);
                    }
                }
                paths.addPathItem(urlMapping.getPathWithPrefix(), item);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Engine.logEngine.error("Unexpected exception while parsing UrlMapper to generate definition", e);
        }
        openAPI.setPaths(paths);
        // Models and Schemas
        try {
            Map<String, JSONObject> modelMap = new HashMap<String, JSONObject>(1000);
            String models = getModels(oasDirUrl, urlMapper, modelMap);
            /*System.out.println("refList");
				for (String keyRef: refList) {
					System.out.println(keyRef);
				}
				System.out.println("modelMap");
				for (String keyRef: modelMap.keySet()) {
					System.out.println(keyRef);
				}*/
            Set<String> done = new HashSet<String>();
            JSONObject jsonModels = new JSONObject(models);
            for (String keyRef : refList) {
                addModelsFromMap(done, modelMap, keyRef, jsonModels);
            }
            OpenAPI oa = new OpenAPI();
            String s = Json.pretty(oa.info(new Info()));
            JSONObject json = new JSONObject(s);
            json.put("components", new JSONObject());
            json.getJSONObject("components").put("schemas", jsonModels);
            JsonNode rootNode = Json.mapper().readTree(json.toString());
            OpenAPIDeserializer ds = new OpenAPIDeserializer();
            SwaggerParseResult result = ds.deserialize(rootNode);
            @SuppressWarnings("rawtypes") Map<String, Schema> map = result.getOpenAPI().getComponents().getSchemas();
            openAPI.getComponents().schemas(map);
            modelMap.clear();
        } catch (Throwable t) {
            t.printStackTrace();
            Engine.logEngine.error("Unexpected exception while parsing UrlMapper to generate models", t);
        }
        // write yaml
        try {
            FileUtils.write(yamlFile, prettyPrintYaml(openAPI), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            Engine.logEngine.error("Unexpected exception while writing project YAML file", e);
        } finally {
            Long t1 = System.currentTimeMillis();
            Engine.logEngine.info("YAML file for " + projectName + " project written in " + (t1 - t0) + " ms");
        }
    }
}
Also used : UrlMapping(com.twinsoft.convertigo.beans.core.UrlMapping) AbstractRestOperation(com.twinsoft.convertigo.beans.rest.AbstractRestOperation) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) OpenAPIDeserializer(io.swagger.v3.parser.util.OpenAPIDeserializer) UrlMappingParameter(com.twinsoft.convertigo.beans.core.UrlMappingParameter) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) FileSchema(io.swagger.v3.oas.models.media.FileSchema) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Schema(io.swagger.v3.oas.models.media.Schema) ArrayList(java.util.ArrayList) UrlMappingResponse(com.twinsoft.convertigo.beans.core.UrlMappingResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) UrlMappingOperation(com.twinsoft.convertigo.beans.core.UrlMappingOperation) Operation(io.swagger.v3.oas.models.Operation) AbstractRestOperation(com.twinsoft.convertigo.beans.rest.AbstractRestOperation) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) DataContent(com.twinsoft.convertigo.beans.core.UrlMappingParameter.DataContent) MediaType(io.swagger.v3.oas.models.media.MediaType) Paths(io.swagger.v3.oas.models.Paths) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) HashSet(java.util.HashSet) UrlMappingOperation(com.twinsoft.convertigo.beans.core.UrlMappingOperation) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Info(io.swagger.v3.oas.models.info.Info) JSONException(org.codehaus.jettison.json.JSONException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Project(com.twinsoft.convertigo.beans.core.Project) JSONObject(org.codehaus.jettison.json.JSONObject) Content(io.swagger.v3.oas.models.media.Content) DataContent(com.twinsoft.convertigo.beans.core.UrlMappingParameter.DataContent) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) LinkParameter(io.swagger.v3.oas.models.links.LinkParameter) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) UrlMappingParameter(com.twinsoft.convertigo.beans.core.UrlMappingParameter) Tag(io.swagger.v3.oas.models.tags.Tag) UrlAuthentication(com.twinsoft.convertigo.beans.core.UrlAuthentication) OpenAPI(io.swagger.v3.oas.models.OpenAPI) IMappingRefModel(com.twinsoft.convertigo.beans.core.IMappingRefModel) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Example 7 with UrlMapping

use of com.twinsoft.convertigo.beans.core.UrlMapping in project convertigo by convertigo.

the class ReferencesView method handleConnectorSelection.

private void handleConnectorSelection(Object firstElement) {
    ConnectorTreeObject connectorTreeObject = (ConnectorTreeObject) firstElement;
    Connector connectorSelected = connectorTreeObject.getObject();
    String connectorSelectedName = connectorSelected.getName();
    Project projectConnectorSelected = connectorSelected.getProject();
    String connectorProjectName = connectorSelected.getProject().getName();
    List<Transaction> transactions = connectorSelected.getTransactionsList();
    try {
        Project project = null;
        List<String> projectNames = Engine.theApp.databaseObjectsManager.getAllProjectNamesList();
        ProjectExplorerView projectExplorerView = ConvertigoPlugin.getDefault().getProjectExplorerView();
        treeViewer.setInput(null);
        RootNode root = new RootNode();
        ConnectorNode connectorNode = null;
        connectorNode = getConnectorNode(root, connectorSelected);
        root.addChild(connectorNode);
        IsUsedByNode isUsedByNode = new IsUsedByNode(connectorNode, "Is used by");
        RequiresNode requiresNode = new RequiresNode(connectorNode, "Requires");
        ProjectNode projectFolder = null;
        // Search handlers are that referenced by the selected connector for continue with site Clipper
        if (connectorSelected instanceof HtmlConnector) {
            ProjectNode projectNode = new ProjectNode(requiresNode, connectorProjectName, projectConnectorSelected);
            for (Transaction transaction : transactions) {
                List<Statement> statements = ((HtmlTransaction) transaction).getStatements();
                List<String> siteClipperConnectorNames = new ArrayList<String>();
                for (Statement statement : statements) {
                    List<Statement> statementList = ((FunctionStatement) statement).getStatements();
                    for (Statement st : statementList) {
                        if (st instanceof ContinueWithSiteClipperStatement) {
                            ContinueWithSiteClipperStatement continueWithSiteClipperStatement = (ContinueWithSiteClipperStatement) st;
                            String siteClipperconnectorName = continueWithSiteClipperStatement.getSiteClipperConnectorName();
                            if (!siteClipperConnectorNames.contains(siteClipperconnectorName)) {
                                siteClipperConnectorNames.add(siteClipperconnectorName);
                                Connector siteClipperConnector = projectConnectorSelected.getConnectorByName(siteClipperconnectorName);
                                ConnectorNode connectorSiteClipperNode = new SiteClipperConnectorNode(projectNode, siteClipperconnectorName, siteClipperConnector);
                                projectNode.addChild(connectorSiteClipperNode);
                            }
                        }
                    }
                }
            }
            if (projectNode.hasChildren()) {
                requiresNode.addChild(projectNode);
            }
        } else if (connectorSelected instanceof SiteClipperConnector) {
            List<Connector> connectors = projectConnectorSelected.getConnectorsList();
            ProjectNode projectNode = new ProjectNode(isUsedByNode, connectorProjectName, projectConnectorSelected);
            for (Connector connector : connectors) {
                if (connector instanceof HtmlConnector) {
                    List<Transaction> transactionList = ((HtmlConnector) connector).getTransactionsList();
                    for (Transaction transaction : transactionList) {
                        List<Statement> statements = ((HtmlTransaction) transaction).getStatements();
                        for (Statement statement : statements) {
                            List<Statement> statementList = ((FunctionStatement) statement).getStatements();
                            for (Statement st : statementList) {
                                if (st instanceof ContinueWithSiteClipperStatement) {
                                    String sourceSiteClipperConnectorName = ((ContinueWithSiteClipperStatement) st).getSiteClipperConnectorName();
                                    if (sourceSiteClipperConnectorName.equals(connectorSelectedName)) {
                                        ContinueWithSiteClipperStatement continueWithSiteClipperStatement = (ContinueWithSiteClipperStatement) st;
                                        HtmlConnectorNode htmlConnectorNode = new HtmlConnectorNode(projectNode, connector.getName(), connector);
                                        projectNode.addChild(htmlConnectorNode);
                                        TransactionNode transactionNode = new TransactionNode(htmlConnectorNode, transaction.getName(), continueWithSiteClipperStatement);
                                        htmlConnectorNode.addChild(transactionNode);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (projectNode.hasChildren()) {
                isUsedByNode.addChild(projectNode);
            }
        }
        // Searching all objects that are referenced by the selected connector
        for (String projectName : projectNames) {
            project = getProject(projectName, projectExplorerView);
            if (project != null) {
                projectFolder = new ProjectNode(isUsedByNode, projectName, project);
                UrlMapper urlMapper = project.getUrlMapper();
                if (urlMapper != null) {
                    MapperNode mapperNode = new MapperNode(projectFolder, urlMapper.getName(), urlMapper);
                    List<UrlMapping> mappings = urlMapper.getMappingList();
                    for (UrlMapping mapping : mappings) {
                        MappingPathNode pathNode = new MappingPathNode(mapperNode, mapping.getPath(), mapping);
                        List<UrlMappingOperation> operations = mapping.getOperationList();
                        for (UrlMappingOperation operation : operations) {
                            String targetRequestable = operation.getTargetRequestable();
                            if (targetRequestable.startsWith(projectConnectorSelected + "." + connectorSelectedName + ".")) {
                                MappingOperationNode operationNode = new MappingOperationNode(pathNode, operation.getName(), operation);
                                pathNode.addChild(operationNode);
                            }
                        }
                        if (pathNode.hasChildren()) {
                            mapperNode.addChild(pathNode);
                        }
                    }
                    if (mapperNode.hasChildren()) {
                        projectFolder.addChild(mapperNode);
                    }
                }
                List<Sequence> sequences = project.getSequencesList();
                for (Sequence sequence : sequences) {
                    List<Step> steps = sequence.getSteps();
                    SequenceNode sequenceNode = new SequenceNode(projectFolder, sequence.getName(), sequence);
                    for (Step step : steps) {
                        getConnectorReferencingIsUsedBy(step, projectExplorerView, sequenceNode, transactions, connectorProjectName, connectorSelectedName);
                    }
                    if (sequenceNode.hasChildren()) {
                        projectFolder.addChild(sequenceNode);
                    }
                }
                if (projectFolder.hasChildren()) {
                    isUsedByNode.addChild(projectFolder);
                }
            }
        }
        if (requiresNode.hasChildren()) {
            connectorNode.addChild(requiresNode);
        }
        if (isUsedByNode.hasChildren()) {
            connectorNode.addChild(isUsedByNode);
        }
        if (!connectorNode.hasChildren()) {
            connectorNode.addChild(new InformationNode(connectorNode, "This connector is not used by any other objects"));
        }
        treeViewer.setInput(root);
        treeViewer.expandAll();
    } catch (EngineException e) {
        ConvertigoPlugin.logException(e, "Error while analyzing the projects hierarchy", true);
    }
}
Also used : UrlMapping(com.twinsoft.convertigo.beans.core.UrlMapping) HtmlConnector(com.twinsoft.convertigo.beans.connectors.HtmlConnector) JavelinConnector(com.twinsoft.convertigo.beans.connectors.JavelinConnector) HttpConnector(com.twinsoft.convertigo.beans.connectors.HttpConnector) ProxyHttpConnector(com.twinsoft.convertigo.beans.connectors.ProxyHttpConnector) Connector(com.twinsoft.convertigo.beans.core.Connector) CicsConnector(com.twinsoft.convertigo.beans.connectors.CicsConnector) SiteClipperConnector(com.twinsoft.convertigo.beans.connectors.SiteClipperConnector) SqlConnector(com.twinsoft.convertigo.beans.connectors.SqlConnector) RootNode(com.twinsoft.convertigo.eclipse.views.references.model.RootNode) ContinueWithSiteClipperStatement(com.twinsoft.convertigo.beans.statements.ContinueWithSiteClipperStatement) MapperNode(com.twinsoft.convertigo.eclipse.views.references.model.MapperNode) ArrayList(java.util.ArrayList) EngineException(com.twinsoft.convertigo.engine.EngineException) Step(com.twinsoft.convertigo.beans.core.Step) ElseStep(com.twinsoft.convertigo.beans.steps.ElseStep) RequestableStep(com.twinsoft.convertigo.beans.core.RequestableStep) ThenStep(com.twinsoft.convertigo.beans.steps.ThenStep) SequenceStep(com.twinsoft.convertigo.beans.steps.SequenceStep) BranchStep(com.twinsoft.convertigo.beans.steps.BranchStep) XMLComplexStep(com.twinsoft.convertigo.beans.steps.XMLComplexStep) BlockStep(com.twinsoft.convertigo.beans.steps.BlockStep) TransactionStep(com.twinsoft.convertigo.beans.steps.TransactionStep) FunctionStatement(com.twinsoft.convertigo.beans.statements.FunctionStatement) SiteClipperConnector(com.twinsoft.convertigo.beans.connectors.SiteClipperConnector) HtmlConnectorNode(com.twinsoft.convertigo.eclipse.views.references.model.HtmlConnectorNode) InformationNode(com.twinsoft.convertigo.eclipse.views.references.model.InformationNode) List(java.util.List) ArrayList(java.util.ArrayList) SiteClipperConnectorNode(com.twinsoft.convertigo.eclipse.views.references.model.SiteClipperConnectorNode) UrlMapper(com.twinsoft.convertigo.beans.core.UrlMapper) HtmlConnector(com.twinsoft.convertigo.beans.connectors.HtmlConnector) ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) UrlMappingOperation(com.twinsoft.convertigo.beans.core.UrlMappingOperation) FunctionStatement(com.twinsoft.convertigo.beans.statements.FunctionStatement) Statement(com.twinsoft.convertigo.beans.core.Statement) ContinueWithSiteClipperStatement(com.twinsoft.convertigo.beans.statements.ContinueWithSiteClipperStatement) ScHandlerStatement(com.twinsoft.convertigo.beans.statements.ScHandlerStatement) HtmlTransaction(com.twinsoft.convertigo.beans.transactions.HtmlTransaction) MappingOperationNode(com.twinsoft.convertigo.eclipse.views.references.model.MappingOperationNode) Sequence(com.twinsoft.convertigo.beans.core.Sequence) ConnectorNode(com.twinsoft.convertigo.eclipse.views.references.model.ConnectorNode) SiteClipperConnectorNode(com.twinsoft.convertigo.eclipse.views.references.model.SiteClipperConnectorNode) HtmlConnectorNode(com.twinsoft.convertigo.eclipse.views.references.model.HtmlConnectorNode) JavelinConnectorNode(com.twinsoft.convertigo.eclipse.views.references.model.JavelinConnectorNode) CicsConnectorNode(com.twinsoft.convertigo.eclipse.views.references.model.CicsConnectorNode) SqlConnectorNode(com.twinsoft.convertigo.eclipse.views.references.model.SqlConnectorNode) ProxyHttpConnectorNode(com.twinsoft.convertigo.eclipse.views.references.model.ProxyHttpConnectorNode) HttpConnectorNode(com.twinsoft.convertigo.eclipse.views.references.model.HttpConnectorNode) SequenceNode(com.twinsoft.convertigo.eclipse.views.references.model.SequenceNode) ConnectorTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.ConnectorTreeObject) Project(com.twinsoft.convertigo.beans.core.Project) Transaction(com.twinsoft.convertigo.beans.core.Transaction) HtmlTransaction(com.twinsoft.convertigo.beans.transactions.HtmlTransaction) JavelinTransaction(com.twinsoft.convertigo.beans.transactions.JavelinTransaction) MappingPathNode(com.twinsoft.convertigo.eclipse.views.references.model.MappingPathNode) IsUsedByNode(com.twinsoft.convertigo.eclipse.views.references.model.IsUsedByNode) RequiresNode(com.twinsoft.convertigo.eclipse.views.references.model.RequiresNode) TransactionNode(com.twinsoft.convertigo.eclipse.views.references.model.TransactionNode) ProjectNode(com.twinsoft.convertigo.eclipse.views.references.model.ProjectNode)

Example 8 with UrlMapping

use of com.twinsoft.convertigo.beans.core.UrlMapping in project convertigo by convertigo.

the class ReferencesView method handleSequenceSelection.

private void handleSequenceSelection(Object firstElement) {
    SequenceTreeObject sequenceTreeObject = (SequenceTreeObject) firstElement;
    Sequence sequenceSelected = sequenceTreeObject.getObject();
    String sequenceSelectedName = sequenceSelected.getName();
    // String sequenceProjectName = sequenceSelected.getProject().getName();
    List<String> projectNames = Engine.theApp.databaseObjectsManager.getAllProjectNamesList();
    ProjectExplorerView projectExplorerView = ConvertigoPlugin.getDefault().getProjectExplorerView();
    treeViewer.setInput(null);
    // Get the referencing sequence steps
    List<String> referencingSequence = new ArrayList<String>();
    RootNode root = new RootNode();
    SequenceNode sequenceFolder = new SequenceNode(root, sequenceSelectedName, sequenceSelected);
    root.addChild(sequenceFolder);
    IsUsedByNode isUsedByNode = new IsUsedByNode(sequenceFolder, "Is used by");
    // Searching all objects that reference the selected sequence
    for (String projectName : projectNames) {
        Project project = getProject(projectName, projectExplorerView);
        if (project != null) {
            ProjectNode projectFolder = null;
            projectFolder = new ProjectNode(isUsedByNode, project.getName(), project);
            List<Sequence> sequences = project.getSequencesList();
            referencingSequence.clear();
            UrlMapper urlMapper = project.getUrlMapper();
            if (urlMapper != null) {
                MapperNode mapperNode = new MapperNode(projectFolder, urlMapper.getName(), urlMapper);
                List<UrlMapping> mappings = urlMapper.getMappingList();
                for (UrlMapping mapping : mappings) {
                    MappingPathNode pathNode = new MappingPathNode(mapperNode, mapping.getPath(), mapping);
                    List<UrlMappingOperation> operations = mapping.getOperationList();
                    for (UrlMappingOperation operation : operations) {
                        String targetRequestable = operation.getTargetRequestable();
                        if (targetRequestable.equals(projectName + "." + sequenceSelectedName)) {
                            MappingOperationNode operationNode = new MappingOperationNode(pathNode, operation.getName(), operation);
                            pathNode.addChild(operationNode);
                        }
                    }
                    if (pathNode.hasChildren()) {
                        mapperNode.addChild(pathNode);
                    }
                }
                if (mapperNode.hasChildren()) {
                    projectFolder.addChild(mapperNode);
                }
            }
            for (Sequence sequence : sequences) {
                List<Step> steps = sequence.getSteps();
                for (Step step : steps) {
                    SequenceNode sequenceNode = new SequenceNode(projectFolder, sequence.getName(), sequence);
                    getSequenceReferencingIsUsedBy(step, sequenceSelected, sequenceNode);
                    if (sequenceNode.hasChildren()) {
                        projectFolder.addChild(sequenceNode);
                    }
                }
            }
            if (projectFolder.hasChildren()) {
                isUsedByNode.addChild(projectFolder);
            }
        }
    }
    List<Step> steps = sequenceSelected.getSteps();
    RequiresNode requiresNode = new RequiresNode(root, "Requires");
    // Searching all objects that are referenced by the selected sequence
    List<String> transactionList = new ArrayList<String>();
    List<String> sequenceList = new ArrayList<String>();
    for (Step step : steps) {
        getSequenceReferencingRequires(step, sequenceSelected, projectExplorerView, requiresNode, transactionList, sequenceList);
    }
    if (requiresNode.hasChildren()) {
        sequenceFolder.addChild(requiresNode);
    }
    if (isUsedByNode.hasChildren()) {
        sequenceFolder.addChild(isUsedByNode);
    }
    if (!sequenceFolder.hasChildren()) {
        sequenceFolder.addChild(new InformationNode(sequenceFolder, "This sequence is not used in any sequence"));
    }
    treeViewer.setInput(root);
    treeViewer.expandAll();
}
Also used : UrlMapper(com.twinsoft.convertigo.beans.core.UrlMapper) UrlMapping(com.twinsoft.convertigo.beans.core.UrlMapping) RootNode(com.twinsoft.convertigo.eclipse.views.references.model.RootNode) ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) UrlMappingOperation(com.twinsoft.convertigo.beans.core.UrlMappingOperation) SequenceTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.SequenceTreeObject) MapperNode(com.twinsoft.convertigo.eclipse.views.references.model.MapperNode) ArrayList(java.util.ArrayList) Sequence(com.twinsoft.convertigo.beans.core.Sequence) MappingOperationNode(com.twinsoft.convertigo.eclipse.views.references.model.MappingOperationNode) Step(com.twinsoft.convertigo.beans.core.Step) ElseStep(com.twinsoft.convertigo.beans.steps.ElseStep) RequestableStep(com.twinsoft.convertigo.beans.core.RequestableStep) ThenStep(com.twinsoft.convertigo.beans.steps.ThenStep) SequenceStep(com.twinsoft.convertigo.beans.steps.SequenceStep) BranchStep(com.twinsoft.convertigo.beans.steps.BranchStep) XMLComplexStep(com.twinsoft.convertigo.beans.steps.XMLComplexStep) BlockStep(com.twinsoft.convertigo.beans.steps.BlockStep) TransactionStep(com.twinsoft.convertigo.beans.steps.TransactionStep) SequenceNode(com.twinsoft.convertigo.eclipse.views.references.model.SequenceNode) Project(com.twinsoft.convertigo.beans.core.Project) MappingPathNode(com.twinsoft.convertigo.eclipse.views.references.model.MappingPathNode) IsUsedByNode(com.twinsoft.convertigo.eclipse.views.references.model.IsUsedByNode) RequiresNode(com.twinsoft.convertigo.eclipse.views.references.model.RequiresNode) InformationNode(com.twinsoft.convertigo.eclipse.views.references.model.InformationNode) ProjectNode(com.twinsoft.convertigo.eclipse.views.references.model.ProjectNode)

Example 9 with UrlMapping

use of com.twinsoft.convertigo.beans.core.UrlMapping in project convertigo by convertigo.

the class UrlMappingTreeObject method treeObjectPropertyChanged.

@Override
public void treeObjectPropertyChanged(TreeObjectEvent treeObjectEvent) {
    super.treeObjectPropertyChanged(treeObjectEvent);
    String propertyName = (String) treeObjectEvent.propertyName;
    propertyName = ((propertyName == null) ? "" : propertyName);
    TreeObject treeObject = (TreeObject) treeObjectEvent.getSource();
    if (treeObject instanceof DatabaseObjectTreeObject) {
        DatabaseObject databaseObject = (DatabaseObject) treeObject.getObject();
        // Case Mapping path has changed
        if ("path".equals(propertyName)) {
            if (treeObject.equals(this)) {
                try {
                    UrlMapping urlMapping = (UrlMapping) databaseObject;
                    String oldPath = (String) treeObjectEvent.oldValue;
                    List<String> oldPathVariableNames = urlMapping.getPathVariableNames(oldPath);
                    List<String> newPathVariableNames = urlMapping.getPathVariableNames();
                    if (!oldPathVariableNames.equals(newPathVariableNames)) {
                        int oldLen = oldPathVariableNames.size();
                        int newLen = newPathVariableNames.size();
                        /*MessageBox messageBox = new MessageBox(viewer.getControl().getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO); 
							messageBox.setMessage("Do you really want to update mapping path parameters?");
							messageBox.setText("Update parameters\"?");
							int ret = messageBox.open();*/
                        int ret = SWT.YES;
                        if (ret == SWT.YES) {
                            // case of deletion
                            if (newLen < oldLen) {
                                for (String variableName : oldPathVariableNames) {
                                    if (!newPathVariableNames.contains(variableName)) {
                                        for (UrlMappingOperation operation : urlMapping.getOperationList()) {
                                            for (UrlMappingParameter parameter : operation.getParameterList()) {
                                                if (parameter.getName().equals(variableName)) {
                                                    if (parameter.getType().equals(Type.Path)) {
                                                        try {
                                                            parameter.delete();
                                                            operation.remove(parameter);
                                                            operation.hasChanged = true;
                                                        } catch (EngineException e) {
                                                            ConvertigoPlugin.logException(e, "Error when deleting the parameter \"" + variableName + "\"");
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            } else // case of rename
                            if (newLen == oldLen) {
                                for (int i = 0; i < oldLen; i++) {
                                    String variableName = oldPathVariableNames.get(i);
                                    for (UrlMappingOperation operation : urlMapping.getOperationList()) {
                                        for (UrlMappingParameter parameter : operation.getParameterList()) {
                                            if (parameter.getName().equals(variableName)) {
                                                if (parameter.getType().equals(Type.Path)) {
                                                    try {
                                                        parameter.setName(newPathVariableNames.get(i));
                                                        parameter.hasChanged = true;
                                                    } catch (EngineException e) {
                                                        ConvertigoPlugin.logException(e, "Error when renaming the parameter \"" + variableName + "\"");
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            } else // case of creation
                            {
                                for (String variableName : newPathVariableNames) {
                                    for (UrlMappingOperation operation : urlMapping.getOperationList()) {
                                        UrlMappingParameter parameter = null;
                                        try {
                                            parameter = operation.getParameterByName(variableName);
                                        } catch (EngineException e) {
                                            try {
                                                parameter = new PathParameter();
                                                parameter.setName(variableName);
                                                parameter.bNew = true;
                                                operation.add(parameter);
                                            } catch (EngineException ex) {
                                                ConvertigoPlugin.logException(ex, "Error when adding the parameter \"" + variableName + "\"");
                                            }
                                        }
                                    }
                                }
                            }
                            getProjectExplorerView().reloadTreeObject(this);
                        }
                    }
                } catch (Exception e) {
                    ConvertigoPlugin.logWarning(e, "Could not reload in tree Mapping \"" + databaseObject.getName() + "\" !");
                }
            }
        }
    }
}
Also used : UrlMapping(com.twinsoft.convertigo.beans.core.UrlMapping) UrlMappingOperation(com.twinsoft.convertigo.beans.core.UrlMappingOperation) UrlMappingParameter(com.twinsoft.convertigo.beans.core.UrlMappingParameter) EngineException(com.twinsoft.convertigo.engine.EngineException) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) PathParameter(com.twinsoft.convertigo.beans.rest.PathParameter) EngineException(com.twinsoft.convertigo.engine.EngineException)

Example 10 with UrlMapping

use of com.twinsoft.convertigo.beans.core.UrlMapping in project convertigo by convertigo.

the class UrlMappingTreeObject method treeObjectAdded.

@Override
public void treeObjectAdded(TreeObjectEvent treeObjectEvent) {
    super.treeObjectAdded(treeObjectEvent);
    TreeObject treeObject = (TreeObject) treeObjectEvent.getSource();
    if (treeObject instanceof DatabaseObjectTreeObject) {
        DatabaseObject databaseObject = (DatabaseObject) treeObject.getObject();
        try {
            boolean needReload = false;
            UrlMapping urlMapping = getObject();
            // An UrlMappingOperation has been added : add all path parameters
            if (databaseObject instanceof UrlMappingOperation) {
                if (urlMapping.equals(databaseObject.getParent())) {
                    UrlMappingOperation operation = (UrlMappingOperation) databaseObject;
                    if (operation.bNew) {
                        for (String variableName : urlMapping.getPathVariableNames()) {
                            UrlMappingParameter parameter = null;
                            try {
                                parameter = operation.getParameterByName(variableName);
                            } catch (EngineException e) {
                                try {
                                    parameter = new PathParameter();
                                    parameter.setName(variableName);
                                    parameter.bNew = true;
                                    operation.add(parameter);
                                    operation.hasChanged = true;
                                    needReload = true;
                                } catch (EngineException ex) {
                                    ConvertigoPlugin.logException(ex, "Error when adding the parameter \"" + variableName + "\"");
                                }
                            }
                        }
                    }
                    if (needReload) {
                        getProjectExplorerView().reloadTreeObject(this);
                    }
                }
            }
        } catch (Exception e) {
            ConvertigoPlugin.logWarning(e, "Could not reload in tree Mapping \"" + databaseObject.getName() + "\" !");
        }
    }
}
Also used : UrlMapping(com.twinsoft.convertigo.beans.core.UrlMapping) UrlMappingOperation(com.twinsoft.convertigo.beans.core.UrlMappingOperation) UrlMappingParameter(com.twinsoft.convertigo.beans.core.UrlMappingParameter) EngineException(com.twinsoft.convertigo.engine.EngineException) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) PathParameter(com.twinsoft.convertigo.beans.rest.PathParameter) EngineException(com.twinsoft.convertigo.engine.EngineException)

Aggregations

UrlMapping (com.twinsoft.convertigo.beans.core.UrlMapping)11 UrlMappingOperation (com.twinsoft.convertigo.beans.core.UrlMappingOperation)9 ArrayList (java.util.ArrayList)8 Project (com.twinsoft.convertigo.beans.core.Project)7 EngineException (com.twinsoft.convertigo.engine.EngineException)7 UrlMapper (com.twinsoft.convertigo.beans.core.UrlMapper)6 UrlMappingParameter (com.twinsoft.convertigo.beans.core.UrlMappingParameter)6 Sequence (com.twinsoft.convertigo.beans.core.Sequence)5 Step (com.twinsoft.convertigo.beans.core.Step)5 RequestableStep (com.twinsoft.convertigo.beans.core.RequestableStep)4 UrlMappingResponse (com.twinsoft.convertigo.beans.core.UrlMappingResponse)4 BlockStep (com.twinsoft.convertigo.beans.steps.BlockStep)4 BranchStep (com.twinsoft.convertigo.beans.steps.BranchStep)4 ElseStep (com.twinsoft.convertigo.beans.steps.ElseStep)4 SequenceStep (com.twinsoft.convertigo.beans.steps.SequenceStep)4 ThenStep (com.twinsoft.convertigo.beans.steps.ThenStep)4 TransactionStep (com.twinsoft.convertigo.beans.steps.TransactionStep)4 XMLComplexStep (com.twinsoft.convertigo.beans.steps.XMLComplexStep)4 ProjectExplorerView (com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView)4 InformationNode (com.twinsoft.convertigo.eclipse.views.references.model.InformationNode)4