Skip to content

Commit 0d024a2

Browse files
committed
Refactor model classes to use Java records
Converted model classes (FileEntry, FileInformation, MigrationResult, Model, Version) to Java records for improved immutability and conciseness. Added AspectModelResult record. Updated ModelService and ModelController to use new record types and adjusted method signatures and usages accordingly. Minor dependency update in pom.xml for @usebruno/cli.
1 parent 1031774 commit 0d024a2

File tree

10 files changed

+118
-250
lines changed

10 files changed

+118
-250
lines changed

aspect-model-editor-runtime/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@
446446
</goals>
447447
<configuration>
448448
<workingDirectory>${project.build.directory}</workingDirectory>
449-
<arguments>install . @usebruno/cli</arguments>
449+
<arguments>install --ignore-scripts . @usebruno/cli@2.15.0</arguments>
450450
</configuration>
451451
</execution>
452452
</executions>

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

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222
import java.util.Map;
23-
import java.util.Optional;
2423
import java.util.function.Supplier;
2524

2625
import org.eclipse.esmf.ame.exceptions.CreateFileException;
2726
import org.eclipse.esmf.ame.exceptions.FileHandlingException;
2827
import org.eclipse.esmf.ame.exceptions.FileNotFoundException;
2928
import org.eclipse.esmf.ame.exceptions.FileReadException;
3029
import org.eclipse.esmf.ame.exceptions.InvalidAspectModelException;
30+
import org.eclipse.esmf.ame.services.models.AspectModelResult;
3131
import org.eclipse.esmf.ame.services.models.MigrationResult;
3232
import org.eclipse.esmf.ame.services.models.Version;
3333
import org.eclipse.esmf.ame.services.utils.ModelGroupingUtils;
@@ -47,12 +47,9 @@
4747
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
4848
import org.eclipse.esmf.aspectmodel.validation.services.AspectModelValidator;
4949
import org.eclipse.esmf.metamodel.AspectModel;
50-
import org.eclipse.esmf.metamodel.Namespace;
5150
import org.eclipse.esmf.samm.KnownVersion;
5251

5352
import io.micronaut.http.multipart.CompletedFileUpload;
54-
import io.vavr.Tuple;
55-
import io.vavr.Tuple2;
5653
import jakarta.inject.Singleton;
5754
import org.slf4j.Logger;
5855
import org.slf4j.LoggerFactory;
@@ -78,39 +75,25 @@ public ModelService( final AspectModelValidator aspectModelValidator, final Aspe
7875
this.modelPath = modelPath;
7976
}
8077

81-
public Tuple2<Optional<String>, String> getModel( final AspectModelUrn aspectModelUrn, final String filePath ) {
78+
public AspectModelResult getModel( final AspectModelUrn aspectModelUrn, final String filePath ) {
8279
try {
8380
final AspectModel aspectModel = ( filePath != null ) ?
8481
ModelUtils.loadModelFromFile( modelPath, filePath, aspectModelLoader ) :
8582
loadModelFromUrn( aspectModelUrn );
8683
validateModel( aspectModel );
8784

88-
return aspectModel.files().stream().filter( a -> a.elements().stream().anyMatch( e -> e.urn().equals( aspectModelUrn ) ) )
89-
.findFirst().map( aspectModelFile -> Tuple.of( aspectModelFile.filename(),
85+
return aspectModel.files().stream()
86+
.filter( a -> a.elements().stream().anyMatch( e -> e.urn().equals( aspectModelUrn ) ) )
87+
.findFirst()
88+
.map( aspectModelFile -> new AspectModelResult(
89+
aspectModelFile.filename(),
9090
AspectSerializer.INSTANCE.aspectModelFileToString( aspectModelFile ) ) )
9191
.orElseThrow( () -> new FileNotFoundException( "Aspect Model not found" ) );
9292
} catch ( final ModelResolutionException e ) {
9393
throw new FileNotFoundException( e.getMessage(), e );
9494
}
9595
}
9696

97-
public String getModelNamespace( final AspectModelUrn aspectModelUrn, final String filePath ) {
98-
try {
99-
final AspectModel aspectModel = ( filePath != null ) ?
100-
ModelUtils.loadModelFromFile( modelPath, filePath, aspectModelLoader ) :
101-
loadModelFromUrn( aspectModelUrn );
102-
validateModel( aspectModel );
103-
104-
final Namespace first = aspectModel.namespaces().getFirst();
105-
106-
return aspectModel.files().stream().filter( a -> a.elements().stream().anyMatch( e -> e.urn().equals( aspectModelUrn ) ) )
107-
.findFirst().map( AspectSerializer.INSTANCE::aspectModelFileToString )
108-
.orElseThrow( () -> new FileNotFoundException( "Aspect Model not found" ) );
109-
} catch ( final ModelResolutionException e ) {
110-
throw new FileNotFoundException( e.getMessage(), e );
111-
}
112-
}
113-
11497
private AspectModel loadModelFromUrn( final AspectModelUrn aspectModelUrn ) {
11598
final Supplier<AspectModel> aspectModelSupplier = ModelUtils.getAspectModelSupplier( aspectModelUrn, aspectModelLoader );
11699
return aspectModelSupplier.get();
@@ -213,16 +196,16 @@ public MigrationResult migrateWorkspace( final boolean setNewVersion, final Path
213196

214197
private void processVersion( final String namespace, final Version version, final boolean setNewVersion, final List<String> errors,
215198
final Path metaModelStoragePath ) {
216-
version.getModels().forEach( model -> {
199+
version.models().forEach( model -> {
217200
try {
218-
final boolean isNotLatestKnownVersion = KnownVersion.fromVersionString( model.getVersion() )
201+
final boolean isNotLatestKnownVersion = KnownVersion.fromVersionString( model.version() )
219202
.filter( v -> KnownVersion.getLatest().equals( v ) ).isPresent();
220203

221204
if ( isNotLatestKnownVersion ) {
222205
return;
223206
}
224207

225-
final Path aspectModelPath = ModelUtils.constructModelPath( modelPath, namespace, version.getVersion(), model.getModel() );
208+
final Path aspectModelPath = ModelUtils.constructModelPath( modelPath, namespace, version.version(), model.model() );
226209
final AspectModel aspectModel = aspectModelLoader.load( aspectModelPath.toFile() );
227210

228211
if ( setNewVersion ) {
@@ -232,7 +215,7 @@ private void processVersion( final String namespace, final Version version, fina
232215

233216
AspectSerializer.INSTANCE.write( aspectModel );
234217
} catch ( final Exception e ) {
235-
errors.add( String.format( "Error processing model: %s", model.getModel() ) );
218+
errors.add( String.format( "Error processing model: %s", model.model() ) );
236219
}
237220
} );
238221
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2025 Robert Bosch Manufacturing Solutions GmbH
3+
*
4+
* See the AUTHORS file(s) distributed with this work for
5+
* additional information regarding authorship.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
10+
*
11+
* SPDX-License-Identifier: MPL-2.0
12+
*/
13+
14+
package org.eclipse.esmf.ame.services.models;
15+
16+
import java.util.Optional;
17+
18+
import com.fasterxml.jackson.annotation.JsonInclude;
19+
import io.micronaut.core.annotation.Introspected;
20+
import io.micronaut.serde.annotation.Serdeable;
21+
22+
/**
23+
* Represents the result of an aspect model operation.
24+
*
25+
* @param filename the optional name of the file associated with the model result
26+
* @param content the content of the model result
27+
*/
28+
@Serdeable
29+
@Introspected
30+
@JsonInclude( JsonInclude.Include.ALWAYS )
31+
public record AspectModelResult( Optional<String> filename, String content ) {}

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

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,60 +13,28 @@
1313

1414
package org.eclipse.esmf.ame.services.models;
1515

16+
import com.fasterxml.jackson.annotation.JsonInclude;
1617
import io.micronaut.core.annotation.Introspected;
1718
import io.micronaut.serde.annotation.Serdeable;
1819

1920
/**
20-
* Represents a single migration result with success and erros.
21+
* Represents a file entry with aspect model information.
22+
*
23+
* @param absoluteName the absolute path of the file
24+
* @param fileName the name of the file
25+
* @param aspectModelUrn the URN of the aspect model
26+
* @param modelVersion the version of the model
2127
*/
2228
@Serdeable
2329
@Introspected
24-
public class FileEntry {
25-
private String absoluteName;
26-
private String fileName;
27-
private String aspectModelUrn;
28-
private String modelVersion;
29-
30+
@JsonInclude( JsonInclude.Include.ALWAYS )
31+
public record FileEntry( String absoluteName, String fileName, String aspectModelUrn, String modelVersion ) {
32+
/**
33+
* Creates a FileEntry with only the aspect model URN.
34+
*
35+
* @param aspectModelUrn the URN of the aspect model
36+
*/
3037
public FileEntry( final String aspectModelUrn ) {
31-
this.aspectModelUrn = aspectModelUrn;
32-
}
33-
34-
public FileEntry( final String absoluteName, final String fileName, final String aspectModelUrn, final String modelVersion ) {
35-
this.absoluteName = absoluteName;
36-
this.fileName = fileName;
37-
this.aspectModelUrn = aspectModelUrn;
38-
this.modelVersion = modelVersion;
39-
}
40-
41-
public String getAbsoluteName() {
42-
return absoluteName;
43-
}
44-
45-
public void setAbsoluteName( final String absoluteName ) {
46-
this.absoluteName = absoluteName;
47-
}
48-
49-
public String getFileName() {
50-
return fileName;
51-
}
52-
53-
public void setFileName( final String fileName ) {
54-
this.fileName = fileName;
55-
}
56-
57-
public String getAspectModelUrn() {
58-
return aspectModelUrn;
59-
}
60-
61-
public void setAspectModelUrn( final String aspectModelUrn ) {
62-
this.aspectModelUrn = aspectModelUrn;
63-
}
64-
65-
public String getModelVersion() {
66-
return modelVersion;
67-
}
68-
69-
public void setModelVersion( final String modelVersion ) {
70-
this.modelVersion = modelVersion;
38+
this( null, null, aspectModelUrn, null );
7139
}
72-
}
40+
}

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

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,67 +13,26 @@
1313

1414
package org.eclipse.esmf.ame.services.models;
1515

16+
import com.fasterxml.jackson.annotation.JsonInclude;
1617
import io.micronaut.core.annotation.Introspected;
1718
import io.micronaut.serde.annotation.Serdeable;
1819

1920
/**
20-
* Represents a single migration result with success and erros.
21+
* Represents information about an Aspect Model file.
22+
*
23+
* @param absoluteName the absolute path of the file
24+
* @param aspectModelUrn the URN of the aspect model
25+
* @param modelVersion the version of the model
26+
* @param aspectModel the aspect model content
27+
* @param fileName the name of the file
2128
*/
2229
@Serdeable
2330
@Introspected
24-
public class FileInformation {
25-
private String absoluteName;
26-
private String aspectModelUrn;
27-
private String modelVersion;
28-
private String aspectModel;
29-
private String fileName;
30-
31-
public FileInformation( final String absoluteName, final String aspectModelUrn, final String modelVersion, final String aspectModel,
32-
final String fileName ) {
33-
this.absoluteName = absoluteName;
34-
this.aspectModelUrn = aspectModelUrn;
35-
this.modelVersion = modelVersion;
36-
this.aspectModel = aspectModel;
37-
this.fileName = fileName;
38-
}
39-
40-
public String getAbsoluteName() {
41-
return absoluteName;
42-
}
43-
44-
public void setAbsoluteName( final String absoluteName ) {
45-
this.absoluteName = absoluteName;
46-
}
47-
48-
public String getAspectModelUrn() {
49-
return aspectModelUrn;
50-
}
51-
52-
public void setAspectModelUrn( final String aspectModelUrn ) {
53-
this.aspectModelUrn = aspectModelUrn;
54-
}
55-
56-
public String getModelVersion() {
57-
return modelVersion;
58-
}
59-
60-
public void setModelVersion( final String modelVersion ) {
61-
this.modelVersion = modelVersion;
62-
}
63-
64-
public String getAspectModel() {
65-
return aspectModel;
66-
}
67-
68-
public void setAspectModel( final String aspectModel ) {
69-
this.aspectModel = aspectModel;
70-
}
71-
72-
public String getFileName() {
73-
return fileName;
74-
}
75-
76-
public void setFileName( final String fileName ) {
77-
this.fileName = fileName;
78-
}
79-
}
31+
@JsonInclude( JsonInclude.Include.ALWAYS )
32+
public record FileInformation(
33+
String absoluteName,
34+
String aspectModelUrn,
35+
String modelVersion,
36+
String aspectModel,
37+
String fileName
38+
) {}

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

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,20 @@
1515

1616
import java.util.List;
1717

18+
import com.fasterxml.jackson.annotation.JsonInclude;
1819
import io.micronaut.core.annotation.Introspected;
1920
import io.micronaut.serde.annotation.Serdeable;
2021

2122
/**
22-
* Represents a single migration result with success and erros.
23+
* Represents the result of a migration operation.
24+
*
25+
* @param success indicates whether the migration was successful
26+
* @param errors list of error messages encountered during migration, empty if successful
2327
*/
2428
@Serdeable
2529
@Introspected
26-
public class MigrationResult {
27-
private boolean success;
28-
private List<String> errors;
29-
30-
public MigrationResult() {
31-
}
32-
33-
public MigrationResult( final boolean success, final List<String> errors ) {
34-
this.success = success;
35-
this.errors = errors;
36-
}
37-
38-
public boolean isSuccess() {
39-
return success;
40-
}
41-
42-
public void setSuccess( final boolean success ) {
43-
this.success = success;
44-
}
45-
46-
public List<String> getErrors() {
47-
return errors;
48-
}
49-
50-
public void setErrors( final List<String> errors ) {
51-
this.errors = errors;
52-
}
53-
}
30+
@JsonInclude( JsonInclude.Include.ALWAYS )
31+
public record MigrationResult(
32+
boolean success,
33+
List<String> errors
34+
) {}

0 commit comments

Comments
 (0)