Skip to content

Commit a5e6792

Browse files
antoinebhsSlimane AMARthangqp
authored
gridsuite-dependencies 38 (#115)
Signed-off-by: BOUHOURS Antoine <[email protected]> Co-authored-by: Slimane AMAR <[email protected]> Co-authored-by: Thang PHAM <[email protected]>
1 parent 558b181 commit a5e6792

File tree

5 files changed

+91
-67
lines changed

5 files changed

+91
-67
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</scm>
3333

3434
<properties>
35-
<gridsuite-dependencies.version>37.0.0</gridsuite-dependencies.version>
35+
<gridsuite-dependencies.version>38.0.0</gridsuite-dependencies.version>
3636
<string-template.version>4.3.1</string-template.version>
3737
<liquibase-hibernate-package>org.gridsuite.mapping.server</liquibase-hibernate-package>
3838
</properties>

src/main/java/org/gridsuite/mapping/server/controller/MappingController.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
import org.gridsuite.mapping.server.dto.RenameObject;
1616
import org.gridsuite.mapping.server.dto.models.Model;
1717
import org.gridsuite.mapping.server.service.MappingService;
18+
import org.springframework.dao.DataIntegrityViolationException;
19+
import org.springframework.http.HttpStatus;
1820
import org.springframework.http.MediaType;
1921
import org.springframework.http.ResponseEntity;
2022
import org.springframework.web.bind.annotation.*;
23+
import org.springframework.web.server.ResponseStatusException;
2124

2225
import java.util.List;
2326

@@ -30,6 +33,8 @@
3033
@AllArgsConstructor
3134
public class MappingController {
3235

36+
private static final String CONFLICT_MAPPING_ERROR_MESSAGE = "A mapping already exists with name: ";
37+
3338
private final MappingService mappingService;
3439

3540
@GetMapping(value = "/")
@@ -77,8 +82,15 @@ public ResponseEntity<String> deleteMapping(@PathVariable("mappingName") String
7782
@ApiResponse(responseCode = "404", description = "Mapping not found"),
7883
@ApiResponse(responseCode = "500", description = "The storage is down or a mapping with the same name already exists")})
7984
public ResponseEntity<RenameObject> renameMapping(@PathVariable("oldName") String oldName, @PathVariable("newName") String newName) {
80-
RenameObject renamedMapping = mappingService.renameMapping(oldName, newName);
81-
return ResponseEntity.ok().body(renamedMapping);
85+
return ResponseEntity.ok().body(renameMappingWithUniquenessCheck(oldName, newName));
86+
}
87+
88+
private RenameObject renameMappingWithUniquenessCheck(String oldName, String newName) {
89+
try {
90+
return mappingService.renameMapping(oldName, newName);
91+
} catch (DataIntegrityViolationException ex) {
92+
throw new ResponseStatusException(HttpStatus.CONFLICT, CONFLICT_MAPPING_ERROR_MESSAGE + newName, ex);
93+
}
8294
}
8395

8496
@PostMapping(value = "/copy/{originalName}/to/{copyName}")
@@ -87,8 +99,15 @@ public ResponseEntity<RenameObject> renameMapping(@PathVariable("oldName") Strin
8799
@ApiResponse(responseCode = "404", description = "Mapping not found"),
88100
@ApiResponse(responseCode = "500", description = "The storage is down or a mapping with the same name already exists")})
89101
public ResponseEntity<InputMapping> copyMapping(@PathVariable("originalName") String originalName, @PathVariable("copyName") String copyName) {
90-
InputMapping copiedMapping = mappingService.copyMapping(originalName, copyName);
91-
return ResponseEntity.ok().body(copiedMapping);
102+
return ResponseEntity.ok().body(copyMappingWithUniquenessCheck(originalName, copyName));
103+
}
104+
105+
private InputMapping copyMappingWithUniquenessCheck(String originalName, String copyName) {
106+
try {
107+
return mappingService.copyMapping(originalName, copyName);
108+
} catch (DataIntegrityViolationException ex) {
109+
throw new ResponseStatusException(HttpStatus.CONFLICT, CONFLICT_MAPPING_ERROR_MESSAGE + copyName, ex);
110+
}
92111
}
93112

94113
}

src/main/java/org/gridsuite/mapping/server/service/implementation/MappingServiceImpl.java

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.gridsuite.mapping.server.service.client.filter.FilterClient;
2626
import org.gridsuite.mapping.server.utils.Methods;
2727
import org.springframework.beans.factory.annotation.Autowired;
28-
import org.springframework.dao.DataIntegrityViolationException;
2928
import org.springframework.http.HttpStatus;
3029
import org.springframework.stereotype.Service;
3130
import org.springframework.transaction.annotation.Transactional;
@@ -44,8 +43,7 @@
4443
@Service
4544
public class MappingServiceImpl implements MappingService {
4645

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: ";
4947

5048
private final ModelRepository modelRepository;
5149
private final MappingRepository mappingRepository;
@@ -114,6 +112,7 @@ public InputMapping getMapping(String mappingName) {
114112
}
115113

116114
@Override
115+
@Transactional
117116
public InputMapping saveMapping(String mappingName, InputMapping mapping) {
118117
if (!StringUtils.isBlank(mappingName)) {
119118
mapping.setName(mappingName);
@@ -210,66 +209,55 @@ public String deleteMapping(String mappingName) {
210209
}
211210

212211
@Override
212+
@Transactional
213213
public RenameObject renameMapping(String oldName, String newName) {
214214
Optional<MappingEntity> mappingToRename = mappingRepository.findById(oldName);
215215
if (mappingToRename.isPresent()) {
216216
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);
224220
} else if (oldName.equals(DEFAULT_MAPPING_NAME)) {
225221
// 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);
233224
} else {
234225
throw new ResponseStatusException(HttpStatus.NOT_FOUND, MAPPING_NOT_FOUND_ERROR_MESSAGE + oldName);
235226
}
236227
}
237228

238229
@Override
230+
@Transactional
239231
public InputMapping copyMapping(String originalName, String copyName) {
240232
Optional<MappingEntity> mappingToCopyOpt = mappingRepository.findById(originalName);
241233
MappingEntity mappingToCopy = mappingToCopyOpt.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, MAPPING_NOT_FOUND_ERROR_MESSAGE + originalName));
242234

243235
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();
251242

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);
255246

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+
}
261252

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);
264255

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));
268259

269-
return mapping;
270-
} catch (DataIntegrityViolationException ex) {
271-
throw new ResponseStatusException(HttpStatus.CONFLICT, CONFLICT_MAPPING_ERROR_MESSAGE + copyName, ex);
272-
}
260+
return mapping;
273261
}
274262

275263
@Transactional(readOnly = true)
@@ -291,11 +279,9 @@ public List<Model> getMappedModelsList(String mappingName) {
291279
.collect(Collectors.toSet());
292280

293281
// 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(),
295283
modelRepository.findAllByDefaultModelTrue().stream())
296284
.map(Model::new).toList();
297-
298-
return mappedModels;
299285
}
300286

301287
}

0 commit comments

Comments
 (0)