Search in sources :

Example 1 with GlobalSchema

use of org.raml.v2.api.model.v08.api.GlobalSchema in project raml-module-builder by folio-org.

the class GenerateRunner method generate.

/**
 * Generate the .java files from the .raml files.
 * @param inputDirectory  where to search for .raml files
 * @throws Exception  on read, write or validate error
 */
public void generate(String inputDirectory) throws Exception {
    log.info("Input directory " + inputDirectory);
    File inputDir = new File(inputDirectory);
    if (!inputDir.isDirectory()) {
        throw new IOException("Input path is not a valid directory: " + inputDirectory);
    }
    configuration.setOutputDirectory(new File(outputDirectory));
    configuration.setSourceDirectory(inputDir);
    int numMatches = 0;
    File[] ramls = new File(inputDirectory).listFiles((dir, name) -> name.endsWith(".raml"));
    Set<String> processedPojos = new HashSet<>();
    List<List<GlobalSchema>> globalUnprocessedSchemas = new ArrayList<>();
    for (int j = 0; j < ramls.length; j++) {
        String line;
        try (BufferedReader reader = new BufferedReader(new FileReader(ramls[j]))) {
            line = reader.readLine();
        }
        if (line.startsWith("#%RAML")) {
            log.info("processing " + ramls[j]);
            // generate java interfaces and pojos from raml
            GENERATOR.run(new FileReader(ramls[j]), configuration, ramls[j].getAbsolutePath());
            numMatches++;
            // get list of schema to injectedFieldList
            // have a map of top level schemas (schemas used in the raml schema:, etc...) to pojos
            // scan fields in top level pojo list to get referenced pojos
            // the name of their schema will be the jsonproperty annotation name
            // check if for the top level pojo , the embedded objects need annotating
            List<GlobalSchema> schemaList = JsonSchemaPojoUtil.getSchemasFromRaml(ramls[j]);
            log.info("Schemas found in " + ramls[j]);
            schemaList.forEach(entry -> log.info("* " + entry.key()));
            List<GlobalSchema> unprocessedSchemas = new ArrayList<>();
            unprocessedSchemas.addAll(schemaList);
            for (int k = 0; k < schemaCustomFields.length; k++) {
                String message = "";
                if (usingDefaultCustomField) {
                    message = "Using default: ";
                }
                log.info(message + " custom field " + schemaCustomFields[k]);
                JsonObject jo = new JsonObject(schemaCustomFields[k]);
                String fieldName = jo.getString("fieldname");
                Object fieldValue = jo.getValue("fieldvalue");
                String annotation = jo.getString("annotation");
                log.info("processing referenced schemas. looking for " + fieldName + " with value " + fieldValue);
                processReferencedSchemas(schemaList, unprocessedSchemas, processedPojos, fieldName, fieldValue, annotation, k);
            }
            globalUnprocessedSchemas.add(unprocessedSchemas);
        } else {
            log.info(ramls[j] + " has a .raml suffix but does not start with #%RAML");
        }
    }
    for (int j = 0; j < schemaCustomFields.length; j++) {
        JsonObject jo = new JsonObject(schemaCustomFields[j]);
        String fieldName = jo.getString("fieldname");
        Object fieldValue = jo.getValue("fieldvalue");
        String annotation = jo.getString("annotation");
        log.info("processing unreferenced schemas. looking for " + fieldName + " with value " + fieldValue);
        processRemainingSchemas(globalUnprocessedSchemas, processedPojos, fieldName, fieldValue, annotation);
    }
    log.info("processed: " + numMatches + " raml files");
}
Also used : GlobalSchema(org.raml.v2.api.model.v08.api.GlobalSchema) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List) FileReader(java.io.FileReader) JsonObject(io.vertx.core.json.JsonObject) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with GlobalSchema

use of org.raml.v2.api.model.v08.api.GlobalSchema in project raml-module-builder by folio-org.

the class GenerateRunner method processRemainingSchemas.

private void processRemainingSchemas(List<List<GlobalSchema>> globalUnprocessedSchemas, Set<String> processedPojos, String fieldName, Object fieldValue, String annotation) throws Exception {
    File modelDir = new File(outputDirectoryWithPackage + "/model/");
    if (!modelDir.exists()) {
        return;
    }
    File[] pojos = modelDir.listFiles((dir, name) -> name.endsWith(".java"));
    // meaning mapped to a schema and annotated, if not, then process
    for (int k = 0; k < pojos.length; k++) {
        if (!processedPojos.contains(MODEL_PACKAGE_DEFAULT + "." + pojos[k].getName().replace(".java", ""))) {
            // get all fields in the pojo to compare them to all fields in each schema so that we
            // can map a pojo to a schema and then annotate the pojo's field in accordance with the schema
            Map<Object, Object> fieldsInPojo = JsonSchemaPojoUtil.jsonFields2Pojo(pojos[k].getAbsolutePath());
            int size1 = globalUnprocessedSchemas.size();
            // loop over all unprocessed schema across all ramls
            for (int l = 0; l < size1; l++) {
                List<GlobalSchema> gsList = globalUnprocessedSchemas.get(l);
                int size2 = gsList.size();
                // loop over all schemas for a specific raml
                for (int m = 0; m < size2; m++) {
                    int counter = 0;
                    GlobalSchema gs = gsList.get(m);
                    List<String> schemaFields = JsonSchemaPojoUtil.getAllFieldsInSchema(new JsonObject(gs.value().value()));
                    int size3 = schemaFields.size();
                    // if so, then we have mapped the pojo to the schema
                    for (int n = 0; n < size3; n++) {
                        if ((fieldsInPojo.size() != schemaFields.size()) || (!fieldsInPojo.containsKey(schemaFields.get(n)))) {
                            break;
                        }
                        counter++;
                    }
                    if (counter == fieldsInPojo.size()) {
                        List<String> injectFieldList = JsonSchemaPojoUtil.getFieldsInSchemaWithType(new JsonObject(gs.value().value()), fieldName, fieldValue);
                        try {
                            inject(pojos[k].getAbsolutePath(), annotation, injectFieldList);
                        } catch (Exception e) {
                            log.error(e.getMessage(), e);
                        }
                    }
                }
            }
        }
    }
}
Also used : GlobalSchema(org.raml.v2.api.model.v08.api.GlobalSchema) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) File(java.io.File) IOException(java.io.IOException)

Example 3 with GlobalSchema

use of org.raml.v2.api.model.v08.api.GlobalSchema in project raml-module-builder by folio-org.

the class JsonSchemaPojoUtil method getSchemasFromRaml.

public static List<GlobalSchema> getSchemasFromRaml(File path2Raml) {
    RamlModelResult ramlModelResult = new RamlModelBuilder().buildApi(path2Raml.getAbsolutePath());
    // get a list of schemas from the raml
    List<GlobalSchema> schemaListInRAML = ramlModelResult.getApiV08().schemas();
    return schemaListInRAML;
}
Also used : RamlModelBuilder(org.raml.v2.api.RamlModelBuilder) GlobalSchema(org.raml.v2.api.model.v08.api.GlobalSchema) RamlModelResult(org.raml.v2.api.RamlModelResult)

Aggregations

GlobalSchema (org.raml.v2.api.model.v08.api.GlobalSchema)3 JsonObject (io.vertx.core.json.JsonObject)2 File (java.io.File)2 IOException (java.io.IOException)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 RamlModelBuilder (org.raml.v2.api.RamlModelBuilder)1 RamlModelResult (org.raml.v2.api.RamlModelResult)1