use of com.github.davidmoten.odata.client.generator.Generator 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);
}
}
}
}
Aggregations