18
18
import org .gridsuite .studyconfig .server .mapper .SpreadsheetConfigMapper ;
19
19
import org .gridsuite .studyconfig .server .repositories .SpreadsheetConfigCollectionRepository ;
20
20
import org .gridsuite .studyconfig .server .repositories .SpreadsheetConfigRepository ;
21
+ import org .springframework .beans .factory .annotation .Value ;
22
+ import org .springframework .core .io .Resource ;
21
23
import org .springframework .stereotype .Service ;
22
24
import org .springframework .transaction .annotation .Transactional ;
23
25
26
+ import com .fasterxml .jackson .databind .ObjectMapper ;
27
+
28
+ import java .io .IOException ;
29
+ import java .io .InputStream ;
24
30
import java .util .List ;
25
31
import java .util .Objects ;
26
32
import java .util .UUID ;
@@ -34,6 +40,10 @@ public class SpreadsheetConfigService {
34
40
35
41
private final SpreadsheetConfigRepository spreadsheetConfigRepository ;
36
42
private final SpreadsheetConfigCollectionRepository spreadsheetConfigCollectionRepository ;
43
+ private final ObjectMapper objectMapper ;
44
+
45
+ @ Value ("classpath:default-spreadsheet-config-collection.json" )
46
+ private Resource defaultSpreadsheetConfigCollectionResource ;
37
47
38
48
private static final String SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND = "SpreadsheetConfigCollection not found with id: " ;
39
49
private static final String COLUMN_NOT_FOUND = "Column not found with id: " ;
@@ -53,6 +63,7 @@ public UUID duplicateSpreadsheetConfig(UUID id) {
53
63
private SpreadsheetConfigEntity duplicateSpreadsheetConfigEntity (UUID id ) {
54
64
SpreadsheetConfigEntity entity = findEntityById (id );
55
65
SpreadsheetConfigEntity duplicate = SpreadsheetConfigEntity .builder ()
66
+ .name (entity .getName ())
56
67
.sheetType (entity .getSheetType ())
57
68
.build ();
58
69
List <ColumnEntity > columns = entity .getColumns ().stream ()
@@ -96,6 +107,7 @@ public void updateSpreadsheetConfig(UUID id, SpreadsheetConfigInfos dto) {
96
107
SpreadsheetConfigEntity entity = findEntityById (id );
97
108
98
109
entity .setSheetType (dto .sheetType ());
110
+ entity .setName (dto .name ());
99
111
entity .getColumns ().clear ();
100
112
if (dto .columns () != null ) {
101
113
entity .getColumns ().addAll (dto .columns ().stream ()
@@ -121,7 +133,6 @@ private EntityNotFoundException entityNotFoundException(UUID id) {
121
133
return new EntityNotFoundException ("SpreadsheetConfig not found with id: " + id );
122
134
}
123
135
124
- @ Transactional
125
136
public UUID createSpreadsheetConfigCollection (SpreadsheetConfigCollectionInfos dto ) {
126
137
SpreadsheetConfigCollectionEntity entity = new SpreadsheetConfigCollectionEntity ();
127
138
entity .setSpreadsheetConfigs (dto .spreadsheetConfigs ().stream ()
@@ -176,6 +187,7 @@ public UUID duplicateSpreadsheetConfigCollection(UUID id) {
176
187
duplicate .setSpreadsheetConfigs (entity .getSpreadsheetConfigs ().stream ()
177
188
.map (config -> {
178
189
SpreadsheetConfigEntity configDuplicate = SpreadsheetConfigEntity .builder ()
190
+ .name (config .getName ())
179
191
.sheetType (config .getSheetType ())
180
192
.build ();
181
193
configDuplicate .setColumns (config .getColumns ().stream ()
@@ -241,4 +253,42 @@ public void deleteColumn(UUID id, UUID columnId) {
241
253
spreadsheetConfigRepository .save (entity );
242
254
}
243
255
256
+ private SpreadsheetConfigCollectionInfos readDefaultSpreadsheetConfigCollection () throws IOException {
257
+ try (InputStream inputStream = defaultSpreadsheetConfigCollectionResource .getInputStream ()) {
258
+ return objectMapper .readValue (inputStream , SpreadsheetConfigCollectionInfos .class );
259
+ }
260
+ }
261
+
262
+ public UUID createDefaultSpreadsheetConfigCollection () {
263
+ try {
264
+ SpreadsheetConfigCollectionInfos defaultCollection = readDefaultSpreadsheetConfigCollection ();
265
+ return createSpreadsheetConfigCollection (defaultCollection );
266
+ } catch (IOException e ) {
267
+ throw new RuntimeException ("Failed to read default spreadsheet config collection" , e );
268
+ }
269
+ }
270
+
271
+ @ Transactional
272
+ public UUID addSpreadsheetConfigToCollection (UUID collectionId , SpreadsheetConfigInfos dto ) {
273
+ SpreadsheetConfigCollectionEntity collection = spreadsheetConfigCollectionRepository .findById (collectionId )
274
+ .orElseThrow (() -> new EntityNotFoundException (SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + collectionId ));
275
+
276
+ SpreadsheetConfigEntity newConfig = SpreadsheetConfigMapper .toEntity (dto );
277
+ collection .getSpreadsheetConfigs ().add (newConfig );
278
+ spreadsheetConfigCollectionRepository .flush ();
279
+ return newConfig .getId ();
280
+ }
281
+
282
+ @ Transactional
283
+ public void removeSpreadsheetConfigFromCollection (UUID collectionId , UUID configId ) {
284
+ SpreadsheetConfigCollectionEntity collection = spreadsheetConfigCollectionRepository .findById (collectionId )
285
+ .orElseThrow (() -> new EntityNotFoundException (SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + collectionId ));
286
+
287
+ boolean removed = collection .getSpreadsheetConfigs ().removeIf (config -> config .getId ().equals (configId ));
288
+ if (!removed ) {
289
+ throw new EntityNotFoundException ("Spreadsheet configuration not found in collection" );
290
+ }
291
+ spreadsheetConfigCollectionRepository .save (collection );
292
+ }
293
+
244
294
}
0 commit comments