Skip to content

Commit 7bcad76

Browse files
authored
Fix open api generation to add also here an single aspect method (#41)
* Fix open api generation to add also here an single aspect method * Update ModelUtils.java
1 parent ed51546 commit 7bcad76

File tree

2 files changed

+45
-51
lines changed

2 files changed

+45
-51
lines changed

src/main/java/org/eclipse/esmf/ame/services/GenerateService.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,21 @@
1515

1616
import java.io.ByteArrayOutputStream;
1717
import java.io.IOException;
18-
import java.util.List;
1918
import java.util.Locale;
2019
import java.util.Optional;
2120

2221
import org.apache.commons.lang3.LocaleUtils;
23-
import org.eclipse.esmf.ame.exceptions.FileReadException;
2422
import org.eclipse.esmf.ame.exceptions.InvalidAspectModelException;
2523
import org.eclipse.esmf.ame.resolver.strategy.FileSystemStrategy;
2624
import org.eclipse.esmf.ame.services.utils.ModelUtils;
27-
import org.eclipse.esmf.ame.validation.ViolationFormatter;
2825
import org.eclipse.esmf.aspectmodel.generator.docu.AspectModelDocumentationGenerator;
2926
import org.eclipse.esmf.aspectmodel.generator.json.AspectModelJsonPayloadGenerator;
3027
import org.eclipse.esmf.aspectmodel.generator.jsonschema.AspectModelJsonSchemaGenerator;
3128
import org.eclipse.esmf.aspectmodel.generator.openapi.AspectModelOpenApiGenerator;
3229
import org.eclipse.esmf.aspectmodel.generator.openapi.PagingOption;
3330
import org.eclipse.esmf.aspectmodel.resolver.services.DataType;
3431
import org.eclipse.esmf.aspectmodel.resolver.services.VersionedModel;
35-
import org.eclipse.esmf.aspectmodel.shacl.violation.Violation;
36-
import org.eclipse.esmf.aspectmodel.validation.services.AspectModelValidator;
3732
import org.eclipse.esmf.metamodel.AspectContext;
38-
import org.eclipse.esmf.metamodel.loader.AspectModelLoader;
3933
import org.slf4j.Logger;
4034
import org.slf4j.LoggerFactory;
4135
import org.springframework.stereotype.Service;
@@ -103,23 +97,9 @@ private AspectContext generateAspectContext( final String aspectModel ) {
10397
final Try<VersionedModel> versionedModels = ModelUtils.fetchVersionModel( fileSystemStrategy );
10498

10599
final Try<AspectContext> context = versionedModels.flatMap(
106-
model -> getSingleAspect( fileSystemStrategy, model ) );
100+
model -> ModelUtils.getSingleAspect( fileSystemStrategy, model ) );
107101

108-
return context.recover( throwable -> {
109-
// Another exception, e.g. syntax error. Let the validator handle this
110-
final List<Violation> violations = new AspectModelValidator().validateModel(
111-
context.map( AspectContext::rdfModel ) );
112-
113-
throw new FileReadException(
114-
String.format( "The generation process encountered failures due to the following violations: %s",
115-
new ViolationFormatter().apply( violations ) ) );
116-
} ).get();
117-
}
118-
119-
private static Try<AspectContext> getSingleAspect( FileSystemStrategy fileSystemStrategy, VersionedModel model ) {
120-
return AspectModelLoader.getSingleAspect( model,
121-
aspect -> aspect.getName().equals( fileSystemStrategy.getAspectModelUrn().getName() ) )
122-
.map( aspect -> new AspectContext( model, aspect ) );
102+
return ModelUtils.getAspectContext( context );
123103
}
124104

125105
public String generateYamlOpenApiSpec( final String language, final String aspectModel, final String baseUrl,

src/main/java/org/eclipse/esmf/ame/services/utils/ModelUtils.java

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,18 @@
2323
import java.net.URI;
2424
import java.net.URL;
2525
import java.nio.file.Path;
26-
import java.nio.file.Paths;
2726
import java.util.Arrays;
2827
import java.util.List;
2928
import java.util.Optional;
3029
import java.util.function.Predicate;
3130

32-
import org.apache.commons.io.FileUtils;
3331
import org.apache.commons.lang3.StringUtils;
3432
import org.apache.jena.rdf.model.Model;
3533
import org.apache.jena.riot.RiotException;
3634
import org.eclipse.esmf.ame.config.ApplicationSettings;
37-
import org.eclipse.esmf.ame.exceptions.FileNotFoundException;
35+
import org.eclipse.esmf.ame.exceptions.FileReadException;
3836
import org.eclipse.esmf.ame.exceptions.InvalidAspectModelException;
39-
import org.eclipse.esmf.ame.model.packaging.AspectModelFiles;
40-
import org.eclipse.esmf.ame.model.resolver.FolderStructure;
4137
import org.eclipse.esmf.ame.model.validation.ViolationReport;
42-
import org.eclipse.esmf.ame.repository.strategy.utils.LocalFolderResolverUtils;
4338
import org.eclipse.esmf.ame.resolver.strategy.FileSystemStrategy;
4439
import org.eclipse.esmf.ame.resolver.strategy.InMemoryStrategy;
4540
import org.eclipse.esmf.ame.validation.ViolationFormatter;
@@ -55,6 +50,7 @@
5550
import org.eclipse.esmf.aspectmodel.validation.services.AspectModelValidator;
5651
import org.eclipse.esmf.aspectmodel.versionupdate.MigratorService;
5752
import org.eclipse.esmf.metamodel.Aspect;
53+
import org.eclipse.esmf.metamodel.AspectContext;
5854
import org.eclipse.esmf.metamodel.loader.AspectModelLoader;
5955

6056
import io.vavr.control.Try;
@@ -127,11 +123,48 @@ public static String migrateModel( final String aspectModel ) throws InvalidAspe
127123
* @param aspectModel as a string.
128124
* @return the Aspect as an object.
129125
*/
130-
public static Aspect resolveAspectFromModel( final String aspectModel )
131-
throws InvalidAspectModelException {
126+
public static Aspect resolveAspectFromModel( final String aspectModel ) throws InvalidAspectModelException {
132127
final FileSystemStrategy fileSystemStrategy = new FileSystemStrategy( aspectModel );
133-
final VersionedModel versionedModel = ModelUtils.loadModelFromStoragePath( fileSystemStrategy );
134-
return AspectModelLoader.getSingleAspectUnchecked( versionedModel );
128+
final Try<VersionedModel> versionedModels = ModelUtils.fetchVersionModel( fileSystemStrategy );
129+
130+
final Try<AspectContext> context = versionedModels.flatMap(
131+
model -> getSingleAspect( fileSystemStrategy, model ) );
132+
133+
return getAspectContext( context ).aspect();
134+
}
135+
136+
/**
137+
* Retrieves a single AspectContext based on the given FileSystemStrategy and VersionedModel.
138+
*
139+
* @param fileSystemStrategy The file system strategy to retrieve the AspectModel URN.
140+
* @param model The versioned model to search for the aspect.
141+
* @return A Try containing the AspectContext if found, otherwise a failure.
142+
*/
143+
public static Try<AspectContext> getSingleAspect( final FileSystemStrategy fileSystemStrategy,
144+
final VersionedModel model ) {
145+
return AspectModelLoader.getSingleAspect( model,
146+
aspect -> aspect.getName().equals( fileSystemStrategy.getAspectModelUrn().getName() ) )
147+
.map( aspect -> new AspectContext( model, aspect ) );
148+
}
149+
150+
/**
151+
* Retrieves the AspectContext from the provided Try<AspectContext>, handling exceptions if necessary.
152+
*
153+
* @param context The Try<AspectContext> representing the context to retrieve the AspectContext from.
154+
* @return The retrieved AspectContext.
155+
*
156+
* @throws FileReadException If there are failures in the generation process due to violations in the model.
157+
*/
158+
public static AspectContext getAspectContext( Try<AspectContext> context ) {
159+
return context.recover( throwable -> {
160+
// Another exception, e.g. syntax error. Let the validator handle this
161+
final List<Violation> violations = new AspectModelValidator().validateModel(
162+
context.map( AspectContext::rdfModel ) );
163+
164+
throw new FileReadException(
165+
String.format( "The generation process encountered failures due to the following violations: %s",
166+
new ViolationFormatter().apply( violations ) ) );
167+
} ).get();
135168
}
136169

137170
/**
@@ -227,25 +260,6 @@ public static Predicate<Violation> isProcessingViolation() {
227260
.equals( ProcessingViolation.ERROR_CODE );
228261
}
229262

230-
public static List<String> copyAspectModelToDirectory( final List<AspectModelFiles> aspectModelFiles,
231-
final String destStorage ) {
232-
233-
return aspectModelFiles.stream().flatMap( data -> data.getFiles().stream().map( fileName -> {
234-
final FolderStructure folderStructure = LocalFolderResolverUtils.extractFilePath( data.getNamespace() );
235-
folderStructure.setFileName( fileName );
236-
final String absoluteAspectModelPath = File.separator + folderStructure;
237-
final File aspectModelStoragePath = Paths.get( destStorage, folderStructure.getFileRootPath(),
238-
folderStructure.getVersion() ).toFile();
239-
try {
240-
FileUtils.copyFileToDirectory( new File( absoluteAspectModelPath ), aspectModelStoragePath );
241-
return folderStructure.toString();
242-
} catch ( final IOException e ) {
243-
throw new FileNotFoundException(
244-
String.format( "Cannot copy file %s to %s", folderStructure.getFileName(), aspectModelStoragePath ) );
245-
}
246-
} ) ).toList();
247-
}
248-
249263
/**
250264
* Returns the {@link Model} that corresponds to the given model URN
251265
*

0 commit comments

Comments
 (0)