Search in sources :

Example 1 with Options

use of com.github.davidmoten.odata.client.generator.Options in project odata-client by davidmoten.

the class GeneratorMojo method execute.

@Override
public void execute() throws MojoExecutionException {
    if (schemas == null) {
        schemas = Collections.emptyList();
    }
    List<SchemaOptions> schemaOptionsList = schemas.stream().map(s -> new SchemaOptions(s.namespace, s.packageName, s.packageSuffixEnum, s.packageSuffixEntity, s.packageSuffixComplexType, s.packageSuffixEntityRequest, s.packageSuffixCollectionRequest, s.packageSuffixContainer, s.packageSuffixSchema, s.simpleClassNameSchema, s.collectionRequestClassSuffix, s.entityRequestClassSuffix, // 
    s.actionRequestClassSuffix, // 
    s.pageComplexTypes, s.failOnMissingEntitySet)).collect(Collectors.toList());
    InputStream is = null;
    try {
        InputStream cis = GeneratorMojo.class.getResourceAsStream(metadata);
        if (cis == null) {
            File metadataFile = new File(metadata);
            System.out.println("metadataFile = " + metadataFile.getAbsolutePath());
            if (metadataFile.exists()) {
                is = new FileInputStream(metadataFile);
            } else {
                metadataFile = new File(project.getBasedir(), metadata);
                System.out.println("metadataFile = " + metadataFile.getAbsolutePath());
                if (metadataFile.exists()) {
                    is = new FileInputStream(metadataFile);
                } else {
                    throw new MojoExecutionException("could not find metadata on classpath or file system: " + metadata);
                }
            }
        } else {
            is = cis;
        }
        JAXBContext c = JAXBContext.newInstance(TDataServices.class);
        Unmarshaller unmarshaller = c.createUnmarshaller();
        TEdmx t = unmarshaller.unmarshal(new StreamSource(is), TEdmx.class).getValue();
        // log schemas
        List<Schema> schemas = t.getDataServices().getSchema();
        schemas.forEach(sch -> getLog().info("schema: " + sch.getNamespace()));
        // auto generate options when not configured
        List<SchemaOptions> schemaOptionsList2 = // 
        schemas.stream().flatMap(schema -> {
            Optional<SchemaOptions> o = // 
            schemaOptionsList.stream().filter(// 
            so -> schema.getNamespace().equals(so.namespace)).findFirst();
            if (o.isPresent()) {
                return Stream.of(o.get());
            } else if (!autoPackage) {
                return Stream.empty();
            } else {
                getLog().info("schema options not found so autogenerating for namespace=" + schema.getNamespace());
                return Stream.of(new SchemaOptions(schema.getNamespace(), blankIfNull(autoPackagePrefix) + toPackage(schema.getNamespace())));
            }
        }).collect(Collectors.toList());
        Options options = new Options(outputDirectory.getAbsolutePath(), schemaOptionsList2);
        Generator g = new Generator(options, schemas);
        g.generate();
    } catch (Throwable e) {
        if (e instanceof MojoExecutionException) {
            throw (MojoExecutionException) e;
        } else {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) StreamSource(javax.xml.transform.stream.StreamSource) TDataServices(org.oasisopen.odata.csdl.v4.TDataServices) Component(org.apache.maven.plugins.annotations.Component) Unmarshaller(jakarta.xml.bind.Unmarshaller) Parameter(org.apache.maven.plugins.annotations.Parameter) SchemaOptions(com.github.davidmoten.odata.client.generator.SchemaOptions) Mojo(org.apache.maven.plugins.annotations.Mojo) MavenProject(org.apache.maven.project.MavenProject) Options(com.github.davidmoten.odata.client.generator.Options) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) Schema(org.oasisopen.odata.csdl.v4.Schema) JAXBContext(jakarta.xml.bind.JAXBContext) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Collectors(java.util.stream.Collectors) File(java.io.File) Generator(com.github.davidmoten.odata.client.generator.Generator) VisibleForTesting(com.github.davidmoten.guavamini.annotations.VisibleForTesting) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) Collections(java.util.Collections) AbstractMojo(org.apache.maven.plugin.AbstractMojo) InputStream(java.io.InputStream) TEdmx(org.oasisopen.odata.csdl.v4.TEdmx) SchemaOptions(com.github.davidmoten.odata.client.generator.SchemaOptions) Options(com.github.davidmoten.odata.client.generator.Options) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Optional(java.util.Optional) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(org.oasisopen.odata.csdl.v4.Schema) JAXBContext(jakarta.xml.bind.JAXBContext) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SchemaOptions(com.github.davidmoten.odata.client.generator.SchemaOptions) Unmarshaller(jakarta.xml.bind.Unmarshaller) File(java.io.File) TEdmx(org.oasisopen.odata.csdl.v4.TEdmx) Generator(com.github.davidmoten.odata.client.generator.Generator)

Aggregations

VisibleForTesting (com.github.davidmoten.guavamini.annotations.VisibleForTesting)1 Generator (com.github.davidmoten.odata.client.generator.Generator)1 Options (com.github.davidmoten.odata.client.generator.Options)1 SchemaOptions (com.github.davidmoten.odata.client.generator.SchemaOptions)1 JAXBContext (jakarta.xml.bind.JAXBContext)1 Unmarshaller (jakarta.xml.bind.Unmarshaller)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 SourceVersion (javax.lang.model.SourceVersion)1 StreamSource (javax.xml.transform.stream.StreamSource)1 AbstractMojo (org.apache.maven.plugin.AbstractMojo)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1