Skip to content

Commit cdb09e3

Browse files
Dennis LabordusRob Tjalma
authored andcommitted
Improved setting private element and changed SclFilename to SclName.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent 1538011 commit cdb09e3

File tree

7 files changed

+68
-44
lines changed

7 files changed

+68
-44
lines changed

app/src/test/java/org/lfenergy/compas/scl/data/rest/v1/CompasSclDataResourceTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class CompasSclDataResourceTest {
3737
void list_WhenCalled_ThenItemResponseRetrieved() {
3838
var type = SclType.SCD;
3939
var uuid = UUID.randomUUID();
40-
var filename = "Filename";
40+
var name = "name";
4141
var version = "1.0.0";
4242

4343
when(compasSclDataService.list(type))
44-
.thenReturn(Collections.singletonList(new Item(uuid.toString(), filename, version)));
44+
.thenReturn(Collections.singletonList(new Item(uuid.toString(), name, version)));
4545

4646
var response = given()
4747
.pathParam(TYPE_PATH_PARAM, type)
@@ -53,7 +53,7 @@ void list_WhenCalled_ThenItemResponseRetrieved() {
5353

5454
var xmlPath = response.xmlPath();
5555
assertEquals(uuid.toString(), xmlPath.get("ListResponse.Item[0].Id"));
56-
assertEquals(filename, xmlPath.get("ListResponse.Item[0].Filename"));
56+
assertEquals(name, xmlPath.get("ListResponse.Item[0].Name"));
5757
assertEquals(version, xmlPath.get("ListResponse.Item[0].Version"));
5858
verify(compasSclDataService, times(1)).list(type);
5959
}
@@ -62,11 +62,11 @@ void list_WhenCalled_ThenItemResponseRetrieved() {
6262
void listVersionsByUUID_WhenCalled_ThenItemResponseRetrieved() {
6363
var type = SclType.SCD;
6464
var uuid = UUID.randomUUID();
65-
var filename = "Filename";
65+
var name = "Name";
6666
var version = "1.0.0";
6767

6868
when(compasSclDataService.listVersionsByUUID(type, uuid))
69-
.thenReturn(Collections.singletonList(new Item(uuid.toString(), filename, version)));
69+
.thenReturn(Collections.singletonList(new Item(uuid.toString(), name, version)));
7070

7171
var response = given()
7272
.pathParam(TYPE_PATH_PARAM, type)
@@ -79,7 +79,7 @@ void listVersionsByUUID_WhenCalled_ThenItemResponseRetrieved() {
7979

8080
var xmlPath = response.xmlPath();
8181
assertEquals(uuid.toString(), xmlPath.get("ListResponse.Item[0].Id"));
82-
assertEquals(filename, xmlPath.get("ListResponse.Item[0].Filename"));
82+
assertEquals(name, xmlPath.get("ListResponse.Item[0].Name"));
8383
assertEquals(version, xmlPath.get("ListResponse.Item[0].Version"));
8484
verify(compasSclDataService, times(1)).listVersionsByUUID(type, uuid);
8585
}

repository-basex/src/main/java/org/lfenergy/compas/scl/data/repository/CompasSclDataBaseXRepository.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@
2626
import java.util.UUID;
2727

2828
import static java.lang.String.format;
29+
import static org.lfenergy.compas.commons.CommonConstants.XML_DEFAULT_NS_URI;
30+
import static org.lfenergy.compas.scl.extensions.common.CompasExtensionsConstants.COMPAS_EXTENSION_NS;
31+
import static org.lfenergy.compas.scl.extensions.common.CompasExtensionsConstants.COMPAS_SCL_EXTENSION_TYPE;
32+
import static org.lfenergy.compas.scl.extensions.common.CompasExtensionsField.SCL_NAME_EXTENSION;
2933

3034
@ApplicationScoped
3135
public class CompasSclDataBaseXRepository implements CompasSclDataRepository {
3236
private static final Logger LOGGER = LoggerFactory.getLogger(CompasSclDataBaseXRepository.class);
3337

34-
private static final String DECLARE_NAMESPACE = "declare namespace scl=\"http://www.iec.ch/61850/2003/SCL\";\n";
38+
private static final String DECLARE_SCL_NAMESPACE = "declare namespace scl=\"" + XML_DEFAULT_NS_URI + "\";\n";
39+
private static final String DECLARE_COMPAS_NAMESPACE = "declare namespace compas=\"" + COMPAS_EXTENSION_NS + "\";\n";
3540
private static final String DECLARE_LATEST_VERSION_FUNC =
3641
"declare function local:latest-version($db as xs:string, $id as xs:string)\n" +
3742
" as document-node() { \n" +
@@ -72,33 +77,35 @@ public CompasSclDataBaseXRepository(BaseXClientFactory baseXClientFactory) throw
7277

7378
@Override
7479
public List<Item> list(SclType type) {
75-
return executeQuery(type, DECLARE_NAMESPACE +
80+
return executeQuery(type, DECLARE_SCL_NAMESPACE +
81+
DECLARE_COMPAS_NAMESPACE +
7682
DECLARE_LATEST_VERSION_FUNC +
7783
format(DECLARE_DB_VARIABLE, type) +
7884
"for $resource in db:open($db)\n" +
7985
" let $id := $resource/scl:SCL/scl:Header/@id\n" +
80-
" let $filename := $resource/scl:SCL/scl:Private[@type='compas_scl']/*:SclFilename\n" +
86+
" let $name := $resource/scl:SCL/scl:Private[@type='" + COMPAS_SCL_EXTENSION_TYPE + "']/compas:" + SCL_NAME_EXTENSION.getFieldName() + "\n" +
8187
" group by $id\n" +
82-
" return '<Item><Id>' || $id || '</Id><Filename>' || $filename || '</Filename><Version>' || local:latest-version($db, $id)//scl:SCL/scl:Header/@version || '</Version></Item>'",
88+
" return '<Item><Id>' || $id || '</Id><Name>' || $name || '</Name><Version>' || local:latest-version($db, $id)//scl:SCL/scl:Header/@version || '</Version></Item>'",
8389
sclDataMarshaller::unmarshal
8490
);
8591
}
8692

8793
@Override
8894
public List<Item> listVersionsByUUID(SclType type, UUID id) {
89-
return executeQuery(type, DECLARE_NAMESPACE +
95+
return executeQuery(type, DECLARE_SCL_NAMESPACE +
96+
DECLARE_COMPAS_NAMESPACE +
9097
format(DECLARE_DB_VARIABLE, type) +
9198
format(DECLARE_ID_VARIABLE, id) +
9299
"for $resource in db:open($db, $id)\n" +
93100
" let $id := $resource/scl:SCL/scl:Header/@id\n" +
94-
" let $filename := $resource/scl:SCL/scl:Private[@type='compas_scl']/*:SclFilename\n" +
101+
" let $name := $resource/scl:SCL/scl:Private[@type='" + COMPAS_SCL_EXTENSION_TYPE + "']/compas:" + SCL_NAME_EXTENSION.getFieldName() + "\n" +
95102
" let $version := $resource/scl:SCL/scl:Header/@version\n" +
96103
" let $parts := tokenize($version, '\\.')\n" +
97104
" let $majorVersion := xs:int($parts[1])\n" +
98105
" let $minorVersion := xs:int($parts[2])\n" +
99106
" let $patchVersion := xs:int($parts[3])\n" +
100107
" order by $majorVersion, $minorVersion, $patchVersion\n" +
101-
" return '<Item><Id>' || $id || '</Id><Filename>' || $filename || '</Filename><Version>' || $version || '</Version></Item>' ",
108+
" return '<Item><Id>' || $id || '</Id><Name>' || $name || '</Name><Version>' || $version || '</Version></Item>' ",
102109
sclDataMarshaller::unmarshal
103110
);
104111
}
@@ -109,7 +116,7 @@ public SCL findByUUID(SclType type, UUID id) {
109116
// Retrieve all versions using db:list-details function.
110117
// Sort the result descending, this way the last version is the first.
111118
// Use this path to retrieve the document with the doc function.
112-
var result = executeQuery(type, DECLARE_NAMESPACE +
119+
var result = executeQuery(type, DECLARE_SCL_NAMESPACE +
113120
DECLARE_LATEST_VERSION_FUNC +
114121
format(DECLARE_DB_VARIABLE, type) +
115122
format(DECLARE_ID_VARIABLE, id) +

repository/src/main/java/org/lfenergy/compas/scl/data/model/Item.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public class Item {
1717
)
1818
private String id;
1919
@XmlElement(
20-
name = "Filename",
20+
name = "Name",
2121
required = true
2222
)
23-
private String filename;
23+
private String name;
2424
@XmlElement(
2525
name = "Version",
2626
required = true
@@ -30,9 +30,9 @@ public class Item {
3030
public Item() {
3131
}
3232

33-
public Item(String id, String filename, String version) {
33+
public Item(String id, String name, String version) {
3434
this.id = id;
35-
this.filename = filename;
35+
this.name = name;
3636
this.version = version;
3737
}
3838

@@ -44,12 +44,12 @@ public void setId(String id) {
4444
this.id = id;
4545
}
4646

47-
public String getFilename() {
48-
return filename;
47+
public String getName() {
48+
return name;
4949
}
5050

51-
public void setFilename(String filename) {
52-
this.filename = filename;
51+
public void setName(String name) {
52+
this.name = name;
5353
}
5454

5555
public String getVersion() {

repository/src/test/java/org/lfenergy/compas/scl/data/model/ItemTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ protected Class<?> getClassToBeTested() {
1818
@Test
1919
void constructor_WhenCalledWithParameters_ThenValuesAreFilled() {
2020
var id = UUID.randomUUID().toString();
21-
var filename = "Filename";
21+
var name = "Name";
2222
var version = "1.0.0";
2323

24-
var item = new Item(id, filename, version);
24+
var item = new Item(id, name, version);
2525

2626
assertEquals(id, item.getId());
27-
assertEquals(filename, item.getFilename());
27+
assertEquals(name, item.getName());
2828
assertEquals(version, item.getVersion());
2929
}
3030
}

repository/src/test/java/org/lfenergy/compas/scl/data/util/JaxbMarshallerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ class JaxbMarshallerTest {
1515
@Test
1616
void unmarshal_WhenItemXMLPassed_ThenItemObjectReturned() throws JAXBException {
1717
var id = "ID";
18-
var filename = "FILENAME";
18+
var name = "NAME";
1919
var version = "VERSION";
20-
var xml = "<Item><Id>" + id + "</Id><Filename>" + filename + "</Filename><Version>" + version + "</Version></Item>";
20+
var xml = "<Item><Id>" + id + "</Id><Name>" + name + "</Name><Version>" + version + "</Version></Item>";
2121

2222
JaxbMarshaller marshaller = new JaxbMarshaller();
2323
Item item = marshaller.unmarshal(xml);
2424

2525
assertNotNull(item);
2626
assertEquals(id, item.getId());
27-
assertEquals(filename, item.getFilename());
27+
assertEquals(name, item.getName());
2828
assertEquals(version, item.getVersion());
2929
}
3030
}

service/src/main/java/org/lfenergy/compas/scl/data/service/CompasSclDataService.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.lfenergy.compas.commons.CompasExtensionsManager;
88
import org.lfenergy.compas.scl.SCL;
9+
import org.lfenergy.compas.scl.TPrivate;
910
import org.lfenergy.compas.scl.data.model.ChangeSetType;
1011
import org.lfenergy.compas.scl.data.model.Item;
1112
import org.lfenergy.compas.scl.data.model.SclType;
@@ -58,22 +59,22 @@ public UUID create(SclType type, String name, SCL scl) {
5859
scl.getHeader().setVersion(version.toString());
5960

6061
// Set name and type to SCL before storing the SCL.
61-
setSclCompasPrivateElement(scl, Optional.of(name), type);
62+
setSclCompasPrivateElement(scl, Optional.empty(), Optional.of(name), type);
6263

6364
repository.create(type, id, scl, version);
6465
return id;
6566
}
6667

6768
public void update(SclType type, UUID id, ChangeSetType changeSetType, SCL scl) {
68-
var currentSCL = repository.findByUUID(type, id);
69+
var currentScl = repository.findByUUID(type, id);
6970
// We always add a new version to the database, so add version record to the SCL and create a new record.
70-
var version = new Version(currentSCL.getHeader().getVersion());
71+
var version = new Version(currentScl.getHeader().getVersion());
7172
version = version.getNextVersion(changeSetType);
7273
scl.getHeader().setId(id.toString());
7374
scl.getHeader().setVersion(version.toString());
7475

7576
// Add name and type to SCL before storing the SCL.
76-
setSclCompasPrivateElement(scl, Optional.empty(), type);
77+
setSclCompasPrivateElement(scl, Optional.of(currentScl), Optional.empty(), type);
7778

7879
repository.create(type, id, scl, version);
7980
}
@@ -90,11 +91,10 @@ public void delete(SclType type, UUID id, Version version) {
9091
* Set the full CoMPAS private element for the given SCL file.
9192
*
9293
* @param scl the SCL file to edit.
93-
* @param filename the filename to add
94+
* @param name the name to add
9495
* @param fileType the file type to add.
9596
*/
96-
private void setSclCompasPrivateElement(SCL scl, Optional<String> filename, SclType fileType) {
97-
var compasPrivateElementFactory = new ObjectFactory();
97+
private void setSclCompasPrivateElement(SCL scl, Optional<SCL> currentScl, Optional<String> name, SclType fileType) {
9898
var compasPrivate = compasExtensionsManager.getCompasPrivate(scl)
9999
.orElseGet(() -> {
100100
// Creating a private
@@ -104,20 +104,37 @@ private void setSclCompasPrivateElement(SCL scl, Optional<String> filename, SclT
104104
return newPrivate;
105105
});
106106

107-
// If a filename is passed, this name will be set as private element.
108-
filename.ifPresent(value ->
109-
compasExtensionsManager.getCompasElement(compasPrivate, SCL_NAME_EXTENSION)
110-
.ifPresentOrElse(
111-
element -> element.setValue(value),
112-
() -> compasPrivate.getContent().add(compasPrivateElementFactory.createSclName(value)))
113-
);
107+
// If the new SCL contains the Name Element we will use that value (or set the new name if passed to this method)
108+
// Otherwise if there is no Name Element there are 2 options, if the new name is passed that will be used to create a Name Element
109+
// If no name is passed, but a previous version of the SCL exists, we will copy the Name Element from there.
110+
compasExtensionsManager.getCompasElement(compasPrivate, SCL_NAME_EXTENSION)
111+
.ifPresentOrElse(
112+
element -> name.ifPresent(element::setValue),
113+
() -> name.ifPresentOrElse(
114+
value -> addCompasName(compasPrivate, value),
115+
() -> currentScl
116+
.flatMap(previousScl -> compasExtensionsManager.getCompasPrivate(previousScl))
117+
.flatMap(previousCompasPrivate -> compasExtensionsManager.getCompasName(previousCompasPrivate))
118+
.ifPresent(previousSclName -> addCompasName(compasPrivate, previousSclName))
119+
)
120+
);
114121

115122
// Always set the file type as private element.
116123
TSclFileType sclFileType = TSclFileType.valueOf(fileType.toString());
117124
compasExtensionsManager.getCompasElement(compasPrivate, SCL_FILETYPE_EXTENSION)
118125
.ifPresentOrElse(
119126
element -> element.setValue(sclFileType),
120-
() -> compasPrivate.getContent().add(compasPrivateElementFactory.createSclFileType(sclFileType))
127+
() -> addCompasType(compasPrivate, sclFileType)
121128
);
122129
}
130+
131+
private void addCompasName(TPrivate compasPrivate, String name) {
132+
var compasPrivateElementFactory = new ObjectFactory();
133+
compasPrivate.getContent().add(compasPrivateElementFactory.createSclName(name));
134+
}
135+
136+
private void addCompasType(TPrivate compasPrivate, TSclFileType sclFileType) {
137+
var compasPrivateElementFactory = new ObjectFactory();
138+
compasPrivate.getContent().add(compasPrivateElementFactory.createSclFileType(sclFileType));
139+
}
123140
}

service/src/test/java/org/lfenergy/compas/scl/data/service/CompasSclDataServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void create_WhenCalledWithCompasExtension_ThenRepositoryIsCalledAndUUIDIsReturne
108108
var type = SclType.SCD;
109109
var name = "JUSTSOMENAME";
110110
var scl = createBasicSCL();
111-
createCompasPrivate(scl, "JUSTANOTHERNAME", SclType.ISD);
111+
createCompasPrivate(scl, "JUSTANOTHERNAME", SclType.IID);
112112

113113
doNothing().when(compasSclDataRepository).create(eq(type), any(UUID.class), eq(scl), any(Version.class));
114114

0 commit comments

Comments
 (0)