25
25
import org .gridsuite .mapping .server .service .client .filter .FilterClient ;
26
26
import org .gridsuite .mapping .server .utils .Methods ;
27
27
import org .springframework .beans .factory .annotation .Autowired ;
28
- import org .springframework .dao .DataIntegrityViolationException ;
29
28
import org .springframework .http .HttpStatus ;
30
29
import org .springframework .stereotype .Service ;
31
30
import org .springframework .transaction .annotation .Transactional ;
44
43
@ Service
45
44
public class MappingServiceImpl implements MappingService {
46
45
47
- public static final String CONFLICT_MAPPING_ERROR_MESSAGE = "A mapping already exists with name: " ;
48
- public static final String MAPPING_NOT_FOUND_ERROR_MESSAGE = "Mapping not found with name: " ;
46
+ private static final String MAPPING_NOT_FOUND_ERROR_MESSAGE = "Mapping not found with name: " ;
49
47
50
48
private final ModelRepository modelRepository ;
51
49
private final MappingRepository mappingRepository ;
@@ -114,6 +112,7 @@ public InputMapping getMapping(String mappingName) {
114
112
}
115
113
116
114
@ Override
115
+ @ Transactional
117
116
public InputMapping saveMapping (String mappingName , InputMapping mapping ) {
118
117
if (!StringUtils .isBlank (mappingName )) {
119
118
mapping .setName (mappingName );
@@ -210,66 +209,55 @@ public String deleteMapping(String mappingName) {
210
209
}
211
210
212
211
@ Override
212
+ @ Transactional
213
213
public RenameObject renameMapping (String oldName , String newName ) {
214
214
Optional <MappingEntity > mappingToRename = mappingRepository .findById (oldName );
215
215
if (mappingToRename .isPresent ()) {
216
216
MappingEntity mappingToSave = new MappingEntity (newName , mappingToRename .get ());
217
- try {
218
- mappingRepository .deleteById (oldName );
219
- mappingRepository .save (mappingToSave );
220
- return new RenameObject (oldName , newName );
221
- } catch (DataIntegrityViolationException ex ) {
222
- throw new ResponseStatusException (HttpStatus .CONFLICT , CONFLICT_MAPPING_ERROR_MESSAGE + newName , ex );
223
- }
217
+ mappingRepository .deleteById (oldName );
218
+ mappingRepository .save (mappingToSave );
219
+ return new RenameObject (oldName , newName );
224
220
} else if (oldName .equals (DEFAULT_MAPPING_NAME )) {
225
221
// In case of naming of new mapping, save it to db.
226
- try {
227
- mappingRepository .save (new MappingEntity (newName , new ArrayList <>(), new ArrayList <>(), false ));
228
- return new RenameObject (DEFAULT_MAPPING_NAME , newName );
229
-
230
- } catch (DataIntegrityViolationException ex ) {
231
- throw new ResponseStatusException (HttpStatus .CONFLICT , CONFLICT_MAPPING_ERROR_MESSAGE + newName , ex );
232
- }
222
+ mappingRepository .save (new MappingEntity (newName , new ArrayList <>(), new ArrayList <>(), false ));
223
+ return new RenameObject (DEFAULT_MAPPING_NAME , newName );
233
224
} else {
234
225
throw new ResponseStatusException (HttpStatus .NOT_FOUND , MAPPING_NOT_FOUND_ERROR_MESSAGE + oldName );
235
226
}
236
227
}
237
228
238
229
@ Override
230
+ @ Transactional
239
231
public InputMapping copyMapping (String originalName , String copyName ) {
240
232
Optional <MappingEntity > mappingToCopyOpt = mappingRepository .findById (originalName );
241
233
MappingEntity mappingToCopy = mappingToCopyOpt .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , MAPPING_NOT_FOUND_ERROR_MESSAGE + originalName ));
242
234
243
235
MappingEntity copiedMapping = new MappingEntity (copyName , mappingToCopy );
244
- try {
245
- // --- duplicate filters in filter-server--- //
246
- // get all filter uuids that needs to duplicate its corresponding filter
247
- List <UUID > filterUuids = copiedMapping .getRules ().stream ()
248
- .map (RuleEntity ::getFilterUuid )
249
- .filter (Objects ::nonNull )
250
- .toList ();
236
+ // --- duplicate filters in filter-server--- //
237
+ // get all filter uuids that needs to duplicate its corresponding filter
238
+ List <UUID > filterUuids = copiedMapping .getRules ().stream ()
239
+ .map (RuleEntity ::getFilterUuid )
240
+ .filter (Objects ::nonNull )
241
+ .toList ();
251
242
252
- if (CollectionUtils .isNotEmpty (filterUuids )) {
253
- // call filter-server API to duplicate filter
254
- Map <UUID , UUID > uuidsMap = filterClient .duplicateFilters (filterUuids );
243
+ if (CollectionUtils .isNotEmpty (filterUuids )) {
244
+ // call filter-server API to duplicate filter
245
+ Map <UUID , UUID > uuidsMap = filterClient .duplicateFilters (filterUuids );
255
246
256
- // replace the old by the new uuid for rule entities
257
- copiedMapping .getRules ().stream ()
258
- .filter (rule -> rule .getFilterUuid () != null )
259
- .forEach (rule -> rule .setFilterUuid (uuidsMap .get (rule .getFilterUuid ())));
260
- }
247
+ // replace the old by the new uuid for rule entities
248
+ copiedMapping .getRules ().stream ()
249
+ .filter (rule -> rule .getFilterUuid () != null )
250
+ .forEach (rule -> rule .setFilterUuid (uuidsMap .get (rule .getFilterUuid ())));
251
+ }
261
252
262
- // --- persist in cascade the mapping in local database --- //
263
- MappingEntity savedMappingEntity = mappingRepository .save (copiedMapping );
253
+ // --- persist in cascade the mapping in local database --- //
254
+ MappingEntity savedMappingEntity = mappingRepository .save (copiedMapping );
264
255
265
- // --- build mapping dto to return --- //
266
- InputMapping mapping = new InputMapping (savedMappingEntity );
267
- enrichFiltersForMappings (List .of (mapping ));
256
+ // --- build mapping dto to return --- //
257
+ InputMapping mapping = new InputMapping (savedMappingEntity );
258
+ enrichFiltersForMappings (List .of (mapping ));
268
259
269
- return mapping ;
270
- } catch (DataIntegrityViolationException ex ) {
271
- throw new ResponseStatusException (HttpStatus .CONFLICT , CONFLICT_MAPPING_ERROR_MESSAGE + copyName , ex );
272
- }
260
+ return mapping ;
273
261
}
274
262
275
263
@ Transactional (readOnly = true )
@@ -291,11 +279,9 @@ public List<Model> getMappedModelsList(String mappingName) {
291
279
.collect (Collectors .toSet ());
292
280
293
281
// get model by name from db, concat to default models and convert to dtos
294
- List < Model > mappedModels = Stream .concat (modelRepository .findAllById (mappedModelNames ).stream (),
282
+ return Stream .concat (modelRepository .findAllById (mappedModelNames ).stream (),
295
283
modelRepository .findAllByDefaultModelTrue ().stream ())
296
284
.map (Model ::new ).toList ();
297
-
298
- return mappedModels ;
299
285
}
300
286
301
287
}
0 commit comments