Skip to content

Commit 284d510

Browse files
committed
Refactor aspect model versioning and file handling
Simplifies the process of saving aspect model files by removing the saveAspectModelFiles method and updating logic to use AspectSerializer directly. The applyNamespaceVersionChange method now accepts an errors list and throws a FileHandlingException for missing filenames, improving error reporting and exception handling.
1 parent f402a34 commit 284d510

File tree

1 file changed

+10
-35
lines changed
  • aspect-model-editor-service/src/main/java/org/eclipse/esmf/ame/services

1 file changed

+10
-35
lines changed

aspect-model-editor-service/src/main/java/org/eclipse/esmf/ame/services/ModelService.java

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.eclipse.esmf.ame.config.ApplicationSettings;
2626
import org.eclipse.esmf.ame.exceptions.CreateFileException;
27+
import org.eclipse.esmf.ame.exceptions.FileHandlingException;
2728
import org.eclipse.esmf.ame.exceptions.FileNotFoundException;
2829
import org.eclipse.esmf.ame.exceptions.FileReadException;
2930
import org.eclipse.esmf.ame.exceptions.InvalidAspectModelException;
@@ -188,26 +189,25 @@ private void processVersion( final String namespace, final Version version, fina
188189
final AspectModel aspectModel = aspectModelLoader.load( aspectModelPath.toFile() );
189190

190191
if ( setNewVersion ) {
191-
applyNamespaceVersionChange( aspectModel );
192+
applyNamespaceVersionChange( aspectModel, errors );
192193
return;
193194
}
194195

195-
saveAspectModelFiles( aspectModel, setNewVersion );
196+
AspectSerializer.INSTANCE.write( aspectModel );
196197
} catch ( final Exception e ) {
197198
errors.add( String.format( "Error processing model: %s", model.getModel() ) );
198199
}
199200
} );
200201
}
201202

202-
private void applyNamespaceVersionChange( final AspectModel aspectModel ) {
203+
private void applyNamespaceVersionChange( final AspectModel aspectModel, final List<String> errors ) {
203204
try {
204205
final AspectModelFile originalFile = aspectModel.files().getFirst();
205206
final AspectChangeManager changeManager = new AspectChangeManager( aspectModel );
206207
changeManager.applyChange( new CopyFileWithIncreasedNamespaceVersion( originalFile, IncreaseVersion.MAJOR ) );
207208

208209
final List<AspectModelFile> newFiles = aspectModel.files().stream()
209-
.filter( file -> !file.namespaceUrn().getVersion().equals( originalFile.namespaceUrn().getVersion() ) )
210-
.toList();
210+
.filter( file -> !file.namespaceUrn().getVersion().equals( originalFile.namespaceUrn().getVersion() ) ).toList();
211211

212212
if ( newFiles.size() != 1 ) {
213213
return;
@@ -218,43 +218,18 @@ private void applyNamespaceVersionChange( final AspectModel aspectModel ) {
218218
.orElseThrow( () -> new IllegalStateException( "Source location missing" ) );
219219

220220
if ( new File( sourceLocation ).exists() ) {
221+
errors.add( String.format( "A new version of the Aspect Model: %s with Version: %s already exists",
222+
updatedFile.filename().orElse( "unknown" ), originalFile.namespaceUrn().getVersion() ) );
221223
return;
222224
}
223225

224-
ModelUtils.createFile(
225-
updatedFile.namespaceUrn(),
226-
updatedFile.filename().orElseThrow( () -> new IllegalStateException( "Filename missing" ) ),
227-
ApplicationSettings.getMetaModelStoragePath()
228-
);
226+
ModelUtils.createFile( updatedFile.namespaceUrn(),
227+
updatedFile.filename().orElseThrow( () -> new FileHandlingException( "Filename missing" ) ),
228+
ApplicationSettings.getMetaModelStoragePath() );
229229

230230
AspectSerializer.INSTANCE.write( updatedFile );
231231
} catch ( final IOException e ) {
232232
throw new CreateFileException( "Cannot create file %s on workspace", e );
233233
}
234234
}
235-
236-
private void saveAspectModelFiles( final AspectModel aspectModel, final boolean setNewVersion ) {
237-
aspectModel.files().forEach( aspectModelFile -> aspectModelFile.sourceLocation().ifPresent( sourceLocation -> {
238-
final File file = new File( sourceLocation );
239-
try {
240-
if ( !setNewVersion ) {
241-
AspectSerializer.INSTANCE.write( aspectModelFile );
242-
return;
243-
}
244-
245-
if ( file.exists() ) {
246-
return;
247-
}
248-
249-
final File parent = file.getParentFile();
250-
if ( !parent.exists() && !parent.mkdirs() ) {
251-
throw new IOException( "Failed to create directories for: " + parent );
252-
}
253-
254-
AspectSerializer.INSTANCE.write( aspectModelFile );
255-
} catch ( final IOException e ) {
256-
throw new RuntimeException( "Error saving aspect model file: " + sourceLocation, e );
257-
}
258-
} ) );
259-
}
260235
}

0 commit comments

Comments
 (0)