6
6
7
7
import org .lfenergy .compas .scl .data .exception .CompasNoDataFoundException ;
8
8
import org .lfenergy .compas .scl .data .exception .CompasSclDataServiceException ;
9
+ import org .lfenergy .compas .scl .data .model .HistoryItem ;
9
10
import org .lfenergy .compas .scl .data .model .Item ;
10
11
import org .lfenergy .compas .scl .data .model .SclMetaInfo ;
11
12
import org .lfenergy .compas .scl .data .model .Version ;
22
23
import static org .lfenergy .compas .scl .data .exception .CompasSclDataServiceErrorCode .*;
23
24
24
25
public class CompasSclDataPostgreSQLRepository implements CompasSclDataRepository {
25
- private static final String SELECT_METADATA_CLAUSE = "select id, name, major_version, minor_version, patch_version " ;
26
- private static final String SELECT_DATA_CLAUSE = "select scl_data " ;
26
+ 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 " ;
27
+ private static final String SELECT_DATA_CLAUSE = "select scl_file.scl_data " ;
28
+ private static final String SELECT_HEADER_INFO_CLAUSE =
29
+ ", (xpath('/scl:Hitem/@who', scl_data.header, ARRAY[ARRAY['scl', 'http://www.iec.ch/61850/2003/SCL']]))[1] hitem_who " +
30
+ ", (xpath('/scl:Hitem/@when', scl_data.header, ARRAY[ARRAY['scl', 'http://www.iec.ch/61850/2003/SCL']]))[1] hitem_when " +
31
+ ", (xpath('/scl:Hitem/@what', scl_data.header, ARRAY[ARRAY['scl', 'http://www.iec.ch/61850/2003/SCL']]))[1] hitem_what " ;
27
32
private static final String FROM_CLAUSE = " from scl_file " ;
33
+ private static final String JOIN_HEADER_CLAUSE = " left outer join (" +
34
+ "SELECT id, major_version, minor_version, patch_version," +
35
+ " unnest(" +
36
+ " xpath('/scl:SCL/scl:Header//scl:Hitem[(not(@revision) or @revision=\" \" ) and @version=\" ' || major_version || '.' || minor_version || '.' || patch_version || '\" ]' " +
37
+ " , scl_data::xml " +
38
+ " , ARRAY[ARRAY['scl', 'http://www.iec.ch/61850/2003/SCL']])) as header " +
39
+ " from scl_file) scl_data " +
40
+ " on scl_data.id = scl_file.id " +
41
+ " and scl_data.major_version = scl_file.major_version " +
42
+ " and scl_data.minor_version = scl_file.minor_version " +
43
+ " and scl_data.patch_version = scl_file.patch_version " ;
28
44
private static final String DELETE_FROM_CLAUSE = "delete " + FROM_CLAUSE ;
29
45
private static final String WHERE_CLAUSE = " where " ;
30
46
private static final String AND_CLAUSE = " and " ;
31
47
32
- private static final String FILTER_ON_TYPE = "type = ?" ;
33
- private static final String FILTER_ON_ID = "id = ?" ;
34
- private static final String FILTER_ON_VERSION = "major_version = ? and minor_version = ? and patch_version = ? " ;
48
+ private static final String FILTER_ON_TYPE = "scl_file. type = ?" ;
49
+ private static final String FILTER_ON_ID = "scl_file. id = ?" ;
50
+ private static final String FILTER_ON_VERSION = "scl_file. major_version = ? and scl_file. minor_version = ? and scl_file. patch_version = ? " ;
35
51
36
52
private static final String ID_FIELD = "id" ;
37
53
private static final String MAJOR_VERSION_FIELD = "major_version" ;
38
54
private static final String MINOR_VERSION_FIELD = "minor_version" ;
39
55
private static final String PATCH_VERSION_FIELD = "patch_version" ;
40
56
private static final String NAME_FIELD = "name" ;
41
57
private static final String SCL_DATA_FIELD = "scl_data" ;
58
+ private static final String HITEM_WHO_FIELD = "hitem_who" ;
59
+ private static final String HITEM_WHEN_FIELD = "hitem_when" ;
60
+ private static final String HITEM_WHAT_FIELD = "hitem_what" ;
42
61
43
62
private final DataSource dataSource ;
44
63
@@ -51,7 +70,7 @@ public List<Item> list(SclFileType type) {
51
70
var sql = SELECT_METADATA_CLAUSE
52
71
+ FROM_CLAUSE
53
72
+ WHERE_CLAUSE + FILTER_ON_TYPE
54
- + " and (id, major_version, minor_version, patch_version) in ("
73
+ + " and (scl_file. id, scl_file. major_version, scl_file. minor_version, scl_file. patch_version) in ("
55
74
// Last select the maximum patch version with the major/minor version per id.
56
75
+ " select id, major_version, minor_version, max(patch_version)"
57
76
+ " from scl_file patch_scl"
@@ -72,7 +91,7 @@ public List<Item> list(SclFileType type) {
72
91
+ " )"
73
92
+ " group by id, major_version, minor_version"
74
93
+ " )"
75
- + " order by name, major_version, minor_version, patch_version" ;
94
+ + " order by scl_file. name, scl_file. major_version, scl_file. minor_version, scl_file. patch_version" ;
76
95
77
96
var items = new ArrayList <Item >();
78
97
try (var connection = dataSource .getConnection ();
@@ -93,24 +112,29 @@ public List<Item> list(SclFileType type) {
93
112
}
94
113
95
114
@ Override
96
- public List <Item > listVersionsByUUID (SclFileType type , UUID id ) {
115
+ public List <HistoryItem > listVersionsByUUID (SclFileType type , UUID id ) {
97
116
var sql = SELECT_METADATA_CLAUSE
117
+ + SELECT_HEADER_INFO_CLAUSE
98
118
+ FROM_CLAUSE
119
+ + JOIN_HEADER_CLAUSE
99
120
+ WHERE_CLAUSE + FILTER_ON_ID
100
121
+ AND_CLAUSE + FILTER_ON_TYPE
101
- + " order by major_version, minor_version, patch_version" ;
122
+ + " order by scl_file. major_version, scl_file. minor_version, scl_file. patch_version" ;
102
123
103
- var items = new ArrayList <Item >();
124
+ var items = new ArrayList <HistoryItem >();
104
125
try (var connection = dataSource .getConnection ();
105
126
var stmt = connection .prepareStatement (sql )) {
106
127
stmt .setObject (1 , id );
107
128
stmt .setString (2 , type .name ());
108
129
109
130
try (var resultSet = stmt .executeQuery ()) {
110
131
while (resultSet .next ()) {
111
- items .add (new Item (id .toString (),
132
+ items .add (new HistoryItem (id .toString (),
112
133
resultSet .getString (NAME_FIELD ),
113
- createVersion (resultSet )));
134
+ createVersion (resultSet ),
135
+ resultSet .getString (HITEM_WHO_FIELD ),
136
+ resultSet .getString (HITEM_WHEN_FIELD ),
137
+ resultSet .getString (HITEM_WHAT_FIELD )));
114
138
}
115
139
}
116
140
} catch (SQLException exp ) {
@@ -157,10 +181,10 @@ public String findByUUID(SclFileType type, UUID id, Version version) {
157
181
158
182
@ Override
159
183
public boolean hasDuplicateSclName (SclFileType type , String name ) {
160
- var sql = "SELECT DISTINCT ON (id) * "
184
+ var sql = "SELECT DISTINCT ON (scl_file. id) * "
161
185
+ FROM_CLAUSE
162
- + "WHERE type= ? "
163
- + "ORDER BY id, major_version desc, minor_version desc, patch_version desc" ;
186
+ + "WHERE scl_file. type = ? "
187
+ + "ORDER BY scl_file. id, scl_file. major_version desc, scl_file. minor_version desc, scl_file. patch_version desc" ;
164
188
165
189
try (var connection = dataSource .getConnection ();
166
190
var stmt = connection .prepareStatement (sql )) {
@@ -184,7 +208,7 @@ public SclMetaInfo findMetaInfoByUUID(SclFileType type, UUID id) {
184
208
+ FROM_CLAUSE
185
209
+ WHERE_CLAUSE + FILTER_ON_ID
186
210
+ AND_CLAUSE + FILTER_ON_TYPE
187
- + " order by major_version desc, minor_version desc, patch_version desc" ;
211
+ + " order by scl_file. major_version desc, scl_file. minor_version desc, scl_file. patch_version desc" ;
188
212
189
213
try (var connection = dataSource .getConnection ();
190
214
var stmt = connection .prepareStatement (sql )) {
0 commit comments