|
23 | 23 | import java.net.URI; |
24 | 24 | import java.net.URL; |
25 | 25 | import java.nio.file.Path; |
26 | | -import java.nio.file.Paths; |
27 | 26 | import java.util.Arrays; |
28 | 27 | import java.util.List; |
29 | 28 | import java.util.Optional; |
30 | 29 | import java.util.function.Predicate; |
31 | 30 |
|
32 | | -import org.apache.commons.io.FileUtils; |
33 | 31 | import org.apache.commons.lang3.StringUtils; |
34 | 32 | import org.apache.jena.rdf.model.Model; |
35 | 33 | import org.apache.jena.riot.RiotException; |
36 | 34 | import org.eclipse.esmf.ame.config.ApplicationSettings; |
37 | | -import org.eclipse.esmf.ame.exceptions.FileNotFoundException; |
| 35 | +import org.eclipse.esmf.ame.exceptions.FileReadException; |
38 | 36 | 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; |
41 | 37 | import org.eclipse.esmf.ame.model.validation.ViolationReport; |
42 | | -import org.eclipse.esmf.ame.repository.strategy.utils.LocalFolderResolverUtils; |
43 | 38 | import org.eclipse.esmf.ame.resolver.strategy.FileSystemStrategy; |
44 | 39 | import org.eclipse.esmf.ame.resolver.strategy.InMemoryStrategy; |
45 | 40 | import org.eclipse.esmf.ame.validation.ViolationFormatter; |
|
55 | 50 | import org.eclipse.esmf.aspectmodel.validation.services.AspectModelValidator; |
56 | 51 | import org.eclipse.esmf.aspectmodel.versionupdate.MigratorService; |
57 | 52 | import org.eclipse.esmf.metamodel.Aspect; |
| 53 | +import org.eclipse.esmf.metamodel.AspectContext; |
58 | 54 | import org.eclipse.esmf.metamodel.loader.AspectModelLoader; |
59 | 55 |
|
60 | 56 | import io.vavr.control.Try; |
@@ -127,11 +123,48 @@ public static String migrateModel( final String aspectModel ) throws InvalidAspe |
127 | 123 | * @param aspectModel as a string. |
128 | 124 | * @return the Aspect as an object. |
129 | 125 | */ |
130 | | - public static Aspect resolveAspectFromModel( final String aspectModel ) |
131 | | - throws InvalidAspectModelException { |
| 126 | + public static Aspect resolveAspectFromModel( final String aspectModel ) throws InvalidAspectModelException { |
132 | 127 | 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(); |
135 | 168 | } |
136 | 169 |
|
137 | 170 | /** |
@@ -227,25 +260,6 @@ public static Predicate<Violation> isProcessingViolation() { |
227 | 260 | .equals( ProcessingViolation.ERROR_CODE ); |
228 | 261 | } |
229 | 262 |
|
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 | | - |
249 | 263 | /** |
250 | 264 | * Returns the {@link Model} that corresponds to the given model URN |
251 | 265 | * |
|
0 commit comments