Skip to content

Commit

Permalink
Merge pull request #233 from com-pas/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
Dennis Labordus authored Jun 30, 2022
2 parents 1f6979a + 1d5f867 commit 0a3a6f2
Show file tree
Hide file tree
Showing 19 changed files with 256 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package org.lfenergy.compas.scl.data.rest.v1.model;

import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.lfenergy.compas.scl.data.model.Item;
import org.lfenergy.compas.scl.data.model.HistoryItem;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
Expand All @@ -19,14 +19,14 @@
@XmlAccessorType(XmlAccessType.FIELD)
public class VersionsResponse {
@Schema(description = "List of found Versions of a specific SCL in the database.")
@XmlElement(name = "Item", namespace = SCL_DATA_SERVICE_V1_NS_URI)
private List<Item> items;
@XmlElement(name = "HistoryItem", namespace = SCL_DATA_SERVICE_V1_NS_URI)
private List<HistoryItem> items;

public List<Item> getItems() {
public List<HistoryItem> getItems() {
return items;
}

public void setItems(List<Item> items) {
public void setItems(List<HistoryItem> items) {
this.items = items;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package org.lfenergy.compas.scl.data.rest.mock;

import io.quarkus.test.Mock;
import org.lfenergy.compas.scl.data.model.HistoryItem;
import org.lfenergy.compas.scl.data.model.Item;
import org.lfenergy.compas.scl.data.model.SclMetaInfo;
import org.lfenergy.compas.scl.data.model.Version;
Expand All @@ -23,7 +24,7 @@ public List<Item> list(SclFileType type) {
}

@Override
public List<Item> listVersionsByUUID(SclFileType type, UUID id) {
public List<HistoryItem> listVersionsByUUID(SclFileType type, UUID id) {
throw new IllegalStateException("Mock method using Mockito. Only needed to startup.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
import org.lfenergy.compas.scl.data.model.ChangeSetType;
import org.lfenergy.compas.scl.data.model.HistoryItem;
import org.lfenergy.compas.scl.data.model.Item;
import org.lfenergy.compas.scl.data.model.Version;
import org.lfenergy.compas.scl.data.rest.v1.model.CreateRequest;
Expand Down Expand Up @@ -76,7 +77,7 @@ void listVersionsByUUID_WhenCalled_ThenItemResponseRetrieved() {
var version = "1.0.0";

when(compasSclDataService.listVersionsByUUID(type, uuid))
.thenReturn(Collections.singletonList(new Item(uuid.toString(), name, version)));
.thenReturn(Collections.singletonList(new HistoryItem(uuid.toString(), name, version, null, null, null)));

var response = given()
.pathParam(TYPE_PATH_PARAM, type)
Expand All @@ -88,9 +89,9 @@ void listVersionsByUUID_WhenCalled_ThenItemResponseRetrieved() {
.response();

var xmlPath = response.xmlPath();
assertEquals(uuid.toString(), xmlPath.get("ListResponse.Item[0].Id"));
assertEquals(name, xmlPath.get("ListResponse.Item[0].Name"));
assertEquals(version, xmlPath.get("ListResponse.Item[0].Version"));
assertEquals(uuid.toString(), xmlPath.get("VersionsResponse.HistoryItem[0].Id"));
assertEquals(name, xmlPath.get("VersionsResponse.HistoryItem[0].Name"));
assertEquals(version, xmlPath.get("VersionsResponse.HistoryItem[0].Version"));
verify(compasSclDataService, times(1)).listVersionsByUUID(type, uuid);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
import org.lfenergy.compas.scl.data.model.ChangeSetType;
import org.lfenergy.compas.scl.data.model.HistoryItem;
import org.lfenergy.compas.scl.data.model.Item;
import org.lfenergy.compas.scl.data.model.Version;
import org.lfenergy.compas.scl.data.rest.v1.model.CreateRequest;
Expand Down Expand Up @@ -68,7 +69,7 @@ void listVersionsByUUID_WhenCalled_ThenItemResponseRetrieved() {
var version = "1.0.0";

when(compasSclDataService.listVersionsByUUID(type, uuid))
.thenReturn(Collections.singletonList(new Item(uuid.toString(), name, version)));
.thenReturn(Collections.singletonList(new HistoryItem(uuid.toString(), name, version, null, null, null)));

var response = given()
.pathParam(TYPE_PATH_PARAM, type)
Expand All @@ -80,9 +81,9 @@ void listVersionsByUUID_WhenCalled_ThenItemResponseRetrieved() {
.response();

var xmlPath = response.xmlPath();
assertEquals(uuid.toString(), xmlPath.get("ListResponse.Item[0].Id"));
assertEquals(name, xmlPath.get("ListResponse.Item[0].Name"));
assertEquals(version, xmlPath.get("ListResponse.Item[0].Version"));
assertEquals(uuid.toString(), xmlPath.get("VersionsResponse.HistoryItem[0].Id"));
assertEquals(name, xmlPath.get("VersionsResponse.HistoryItem[0].Name"));
assertEquals(version, xmlPath.get("VersionsResponse.HistoryItem[0].Version"));
verify(compasSclDataService, times(1)).listVersionsByUUID(type, uuid);
}

Expand Down
18 changes: 9 additions & 9 deletions doc/postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ For the SCL Data Service the following table is automatically created using FlyW

Table: scl_file

| Column | Type | Required | Description |
| ----------------- | -------------------- | ----------| ------------------------------------------------------------------ |
| id | uuid | True | Unique ID generated according to standards |
| major_version | smallint | True | Versioning according to [Semantic Versioning](https://semver.org/) |
| minor_version | smallint | True | Versioning according to [Semantic Versioning](https://semver.org/) |
| patch_version | smallint | True | Versioning according to [Semantic Versioning](https://semver.org/) |
| type | varchar(3) | True | The type of SCL stored |
| name | varchar(255) | True | The name of the SCL File |
| scl_data | text | True | The SCL XML Content |
| Column | Type | Required | Description |
|---------------|--------------|----------|--------------------------------------------------------------------|
| id | uuid | True | Unique ID generated according to standards |
| major_version | smallint | True | Versioning according to [Semantic Versioning](https://semver.org/) |
| minor_version | smallint | True | Versioning according to [Semantic Versioning](https://semver.org/) |
| patch_version | smallint | True | Versioning according to [Semantic Versioning](https://semver.org/) |
| type | varchar(3) | True | The type of SCL stored |
| name | varchar(255) | True | The name of the SCL File |
| scl_data | text | True | The SCL XML Content |

## Development

Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ SPDX-License-Identifier: Apache-2.0
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<compiler-plugin.version>3.10.1</compiler-plugin.version>
<surefire-plugin.version>3.0.0-M6</surefire-plugin.version>
<surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
<sonarqube-plugin.version>3.2.0</sonarqube-plugin.version>

<compas.core.version>0.9.0</compas.core.version>
<compas.core.version>0.9.1</compas.core.version>

<quarkus.platform.version>2.9.2.Final</quarkus.platform.version>
<quarkus.platform.version>2.10.1.Final</quarkus.platform.version>
<jaxb-impl.version>2.3.6</jaxb-impl.version>
<microprofile-openapi-api.version>3.0</microprofile-openapi-api.version>
<slf4j.version>1.7.36</slf4j.version>
Expand Down Expand Up @@ -165,7 +165,7 @@ SPDX-License-Identifier: Apache-2.0
<plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<version>1.2.2</version>
<version>1.2.3</version>
<executions>
<execution>
<id>make-index</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.lfenergy.compas.scl.data.basex.client.BaseXClientFactory;
import org.lfenergy.compas.scl.data.exception.CompasNoDataFoundException;
import org.lfenergy.compas.scl.data.exception.CompasSclDataServiceException;
import org.lfenergy.compas.scl.data.model.HistoryItem;
import org.lfenergy.compas.scl.data.model.Item;
import org.lfenergy.compas.scl.data.model.SclMetaInfo;
import org.lfenergy.compas.scl.data.model.Version;
Expand Down Expand Up @@ -105,7 +106,7 @@ public List<Item> list(SclFileType type) {
}

@Override
public List<Item> listVersionsByUUID(SclFileType type, UUID id) {
public List<HistoryItem> listVersionsByUUID(SclFileType type, UUID id) {
return executeQuery(type, DECLARE_SCL_NS_URI +
DECLARE_COMPAS_NAMESPACE +
format(DECLARE_DB_VARIABLE, type) +
Expand All @@ -114,13 +115,21 @@ public List<Item> listVersionsByUUID(SclFileType type, UUID id) {
" let $id := $resource" + SCL_HEADER_ID_XPATH + "\n" +
" let $version := $resource" + SCL_HEADER_VERSION_XPATH + "\n" +
" let $name := $resource" + COMPAS_NAME_EXTENSION_XPATH + "\n" +
" let $header := $resource/scl:SCL/scl:Header/scl:History/scl:Hitem[(not(@revision) or @revision=\"\") and @version=$version]\n" +
" let $parts := tokenize($version, '\\.')\n" +
" let $majorVersion := xs:int($parts[1])\n" +
" let $minorVersion := xs:int($parts[2])\n" +
" let $patchVersion := xs:int($parts[3])\n" +
" order by $majorVersion, $minorVersion, $patchVersion\n" +
" return '<Item xmlns=\"" + SCL_DATA_SERVICE_V1_NS_URI + "\"><Id>' || $id || '</Id><Name>' || $name || '</Name><Version>' || $version || '</Version></Item>' ",
sclDataMarshaller::unmarshalItem);
" return '<HistoryItem xmlns=\"" + SCL_DATA_SERVICE_V1_NS_URI + "\">'" +
" || ' <Id>' || $id || '</Id>'" +
" || ' <Name>' || $name || '</Name>'" +
" || ' <Version>' || $version || '</Version>' " +
" || ' <Who>' || $header/@who || '</Who>' " +
" || ' <When>' || $header/@when || '</When>' " +
" || ' <What>' || $header/@what || '</What>' " +
" || '</HistoryItem>' ",
sclDataMarshaller::unmarshalHistoryItem);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.lfenergy.compas.scl.data.exception.CompasNoDataFoundException;
import org.lfenergy.compas.scl.data.exception.CompasSclDataServiceException;
import org.lfenergy.compas.scl.data.model.HistoryItem;
import org.lfenergy.compas.scl.data.model.Item;
import org.lfenergy.compas.scl.data.model.SclMetaInfo;
import org.lfenergy.compas.scl.data.model.Version;
Expand All @@ -22,23 +23,41 @@
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.*;

public class CompasSclDataPostgreSQLRepository implements CompasSclDataRepository {
private static final String SELECT_METADATA_CLAUSE = "select id, name, major_version, minor_version, patch_version ";
private static final String SELECT_DATA_CLAUSE = "select scl_data ";
private static final String SELECT_METADATA_CLAUSE = "select scl_file.id, scl_file.name, scl_file.major_version, scl_file.minor_version, scl_file.patch_version ";
private static final String SELECT_DATA_CLAUSE = "select scl_file.scl_data ";
private static final String SELECT_HEADER_INFO_CLAUSE =
", (xpath('/scl:Hitem/@who', scl_data.header, ARRAY[ARRAY['scl', 'http://www.iec.ch/61850/2003/SCL']]))[1] hitem_who " +
", (xpath('/scl:Hitem/@when', scl_data.header, ARRAY[ARRAY['scl', 'http://www.iec.ch/61850/2003/SCL']]))[1] hitem_when " +
", (xpath('/scl:Hitem/@what', scl_data.header, ARRAY[ARRAY['scl', 'http://www.iec.ch/61850/2003/SCL']]))[1] hitem_what ";
private static final String FROM_CLAUSE = " from scl_file ";
private static final String JOIN_HEADER_CLAUSE = " left outer join (" +
"SELECT id, major_version, minor_version, patch_version," +
" unnest(" +
" xpath('/scl:SCL/scl:Header//scl:Hitem[(not(@revision) or @revision=\"\") and @version=\"' || major_version || '.' || minor_version || '.' || patch_version || '\"]' " +
" , scl_data::xml " +
" , ARRAY[ARRAY['scl', 'http://www.iec.ch/61850/2003/SCL']])) as header " +
" from scl_file) scl_data " +
" on scl_data.id = scl_file.id " +
" and scl_data.major_version = scl_file.major_version " +
" and scl_data.minor_version = scl_file.minor_version " +
" and scl_data.patch_version = scl_file.patch_version ";
private static final String DELETE_FROM_CLAUSE = "delete " + FROM_CLAUSE;
private static final String WHERE_CLAUSE = " where ";
private static final String AND_CLAUSE = " and ";

private static final String FILTER_ON_TYPE = "type = ?";
private static final String FILTER_ON_ID = "id = ?";
private static final String FILTER_ON_VERSION = "major_version = ? and minor_version = ? and patch_version = ? ";
private static final String FILTER_ON_TYPE = "scl_file.type = ?";
private static final String FILTER_ON_ID = "scl_file.id = ?";
private static final String FILTER_ON_VERSION = "scl_file.major_version = ? and scl_file.minor_version = ? and scl_file.patch_version = ? ";

private static final String ID_FIELD = "id";
private static final String MAJOR_VERSION_FIELD = "major_version";
private static final String MINOR_VERSION_FIELD = "minor_version";
private static final String PATCH_VERSION_FIELD = "patch_version";
private static final String NAME_FIELD = "name";
private static final String SCL_DATA_FIELD = "scl_data";
private static final String HITEM_WHO_FIELD = "hitem_who";
private static final String HITEM_WHEN_FIELD = "hitem_when";
private static final String HITEM_WHAT_FIELD = "hitem_what";

private final DataSource dataSource;

Expand All @@ -51,7 +70,7 @@ public List<Item> list(SclFileType type) {
var sql = SELECT_METADATA_CLAUSE
+ FROM_CLAUSE
+ WHERE_CLAUSE + FILTER_ON_TYPE
+ " and (id, major_version, minor_version, patch_version) in ("
+ " and (scl_file.id, scl_file.major_version, scl_file.minor_version, scl_file.patch_version) in ("
// Last select the maximum patch version with the major/minor version per id.
+ " select id, major_version, minor_version, max(patch_version)"
+ " from scl_file patch_scl"
Expand All @@ -72,7 +91,7 @@ public List<Item> list(SclFileType type) {
+ " )"
+ " group by id, major_version, minor_version"
+ " )"
+ " order by name, major_version, minor_version, patch_version";
+ " order by scl_file.name, scl_file.major_version, scl_file.minor_version, scl_file.patch_version";

var items = new ArrayList<Item>();
try (var connection = dataSource.getConnection();
Expand All @@ -93,24 +112,29 @@ public List<Item> list(SclFileType type) {
}

@Override
public List<Item> listVersionsByUUID(SclFileType type, UUID id) {
public List<HistoryItem> listVersionsByUUID(SclFileType type, UUID id) {
var sql = SELECT_METADATA_CLAUSE
+ SELECT_HEADER_INFO_CLAUSE
+ FROM_CLAUSE
+ JOIN_HEADER_CLAUSE
+ WHERE_CLAUSE + FILTER_ON_ID
+ AND_CLAUSE + FILTER_ON_TYPE
+ " order by major_version, minor_version, patch_version";
+ " order by scl_file.major_version, scl_file.minor_version, scl_file.patch_version";

var items = new ArrayList<Item>();
var items = new ArrayList<HistoryItem>();
try (var connection = dataSource.getConnection();
var stmt = connection.prepareStatement(sql)) {
stmt.setObject(1, id);
stmt.setString(2, type.name());

try (var resultSet = stmt.executeQuery()) {
while (resultSet.next()) {
items.add(new Item(id.toString(),
items.add(new HistoryItem(id.toString(),
resultSet.getString(NAME_FIELD),
createVersion(resultSet)));
createVersion(resultSet),
resultSet.getString(HITEM_WHO_FIELD),
resultSet.getString(HITEM_WHEN_FIELD),
resultSet.getString(HITEM_WHAT_FIELD)));
}
}
} catch (SQLException exp) {
Expand Down Expand Up @@ -157,10 +181,10 @@ public String findByUUID(SclFileType type, UUID id, Version version) {

@Override
public boolean hasDuplicateSclName(SclFileType type, String name) {
var sql = "SELECT DISTINCT ON (id) * "
var sql = "SELECT DISTINCT ON (scl_file.id) * "
+ FROM_CLAUSE
+ "WHERE type=? "
+ "ORDER BY id, major_version desc, minor_version desc, patch_version desc";
+ "WHERE scl_file.type = ? "
+ "ORDER BY scl_file.id, scl_file.major_version desc, scl_file.minor_version desc, scl_file.patch_version desc";

try (var connection = dataSource.getConnection();
var stmt = connection.prepareStatement(sql)) {
Expand All @@ -184,7 +208,7 @@ public SclMetaInfo findMetaInfoByUUID(SclFileType type, UUID id) {
+ FROM_CLAUSE
+ WHERE_CLAUSE + FILTER_ON_ID
+ AND_CLAUSE + FILTER_ON_TYPE
+ " order by major_version desc, minor_version desc, patch_version desc";
+ " order by scl_file.major_version desc, scl_file.minor_version desc, scl_file.patch_version desc";

try (var connection = dataSource.getConnection();
var stmt = connection.prepareStatement(sql)) {
Expand Down
Loading

0 comments on commit 0a3a6f2

Please sign in to comment.