Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</scm>

<properties>
<gridsuite-dependencies.version>37.0.0</gridsuite-dependencies.version>
<gridsuite-dependencies.version>38.0.0</gridsuite-dependencies.version>
<string-template.version>4.3.1</string-template.version>
<liquibase-hibernate-package>org.gridsuite.mapping.server</liquibase-hibernate-package>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import org.gridsuite.mapping.server.dto.RenameObject;
import org.gridsuite.mapping.server.dto.models.Model;
import org.gridsuite.mapping.server.service.MappingService;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

Expand All @@ -30,6 +33,8 @@
@AllArgsConstructor
public class MappingController {

private static final String CONFLICT_MAPPING_ERROR_MESSAGE = "A mapping already exists with name: ";

private final MappingService mappingService;

@GetMapping(value = "/")
Expand Down Expand Up @@ -77,8 +82,15 @@ public ResponseEntity<String> deleteMapping(@PathVariable("mappingName") String
@ApiResponse(responseCode = "404", description = "Mapping not found"),
@ApiResponse(responseCode = "500", description = "The storage is down or a mapping with the same name already exists")})
public ResponseEntity<RenameObject> renameMapping(@PathVariable("oldName") String oldName, @PathVariable("newName") String newName) {
RenameObject renamedMapping = mappingService.renameMapping(oldName, newName);
return ResponseEntity.ok().body(renamedMapping);
return ResponseEntity.ok().body(renameMappingWithUniquenessCheck(oldName, newName));
}

private RenameObject renameMappingWithUniquenessCheck(String oldName, String newName) {
try {
return mappingService.renameMapping(oldName, newName);
} catch (DataIntegrityViolationException ex) {
throw new ResponseStatusException(HttpStatus.CONFLICT, CONFLICT_MAPPING_ERROR_MESSAGE + newName, ex);
}
}

@PostMapping(value = "/copy/{originalName}/to/{copyName}")
Expand All @@ -87,8 +99,15 @@ public ResponseEntity<RenameObject> renameMapping(@PathVariable("oldName") Strin
@ApiResponse(responseCode = "404", description = "Mapping not found"),
@ApiResponse(responseCode = "500", description = "The storage is down or a mapping with the same name already exists")})
public ResponseEntity<InputMapping> copyMapping(@PathVariable("originalName") String originalName, @PathVariable("copyName") String copyName) {
InputMapping copiedMapping = mappingService.copyMapping(originalName, copyName);
return ResponseEntity.ok().body(copiedMapping);
return ResponseEntity.ok().body(copyMappingWithUniquenessCheck(originalName, copyName));
}

private InputMapping copyMappingWithUniquenessCheck(String originalName, String copyName) {
try {
return mappingService.copyMapping(originalName, copyName);
} catch (DataIntegrityViolationException ex) {
throw new ResponseStatusException(HttpStatus.CONFLICT, CONFLICT_MAPPING_ERROR_MESSAGE + copyName, ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.gridsuite.mapping.server.service.client.filter.FilterClient;
import org.gridsuite.mapping.server.utils.Methods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -44,8 +43,7 @@
@Service
public class MappingServiceImpl implements MappingService {

public static final String CONFLICT_MAPPING_ERROR_MESSAGE = "A mapping already exists with name: ";
public static final String MAPPING_NOT_FOUND_ERROR_MESSAGE = "Mapping not found with name: ";
private static final String MAPPING_NOT_FOUND_ERROR_MESSAGE = "Mapping not found with name: ";

private final ModelRepository modelRepository;
private final MappingRepository mappingRepository;
Expand Down Expand Up @@ -114,6 +112,7 @@ public InputMapping getMapping(String mappingName) {
}

@Override
@Transactional
public InputMapping saveMapping(String mappingName, InputMapping mapping) {
if (!StringUtils.isBlank(mappingName)) {
mapping.setName(mappingName);
Expand Down Expand Up @@ -210,66 +209,55 @@ public String deleteMapping(String mappingName) {
}

@Override
@Transactional
public RenameObject renameMapping(String oldName, String newName) {
Optional<MappingEntity> mappingToRename = mappingRepository.findById(oldName);
if (mappingToRename.isPresent()) {
MappingEntity mappingToSave = new MappingEntity(newName, mappingToRename.get());
try {
mappingRepository.deleteById(oldName);
mappingRepository.save(mappingToSave);
return new RenameObject(oldName, newName);
} catch (DataIntegrityViolationException ex) {
throw new ResponseStatusException(HttpStatus.CONFLICT, CONFLICT_MAPPING_ERROR_MESSAGE + newName, ex);
}
mappingRepository.deleteById(oldName);
mappingRepository.save(mappingToSave);
return new RenameObject(oldName, newName);
} else if (oldName.equals(DEFAULT_MAPPING_NAME)) {
// In case of naming of new mapping, save it to db.
try {
mappingRepository.save(new MappingEntity(newName, new ArrayList<>(), new ArrayList<>(), false));
return new RenameObject(DEFAULT_MAPPING_NAME, newName);

} catch (DataIntegrityViolationException ex) {
throw new ResponseStatusException(HttpStatus.CONFLICT, CONFLICT_MAPPING_ERROR_MESSAGE + newName, ex);
}
mappingRepository.save(new MappingEntity(newName, new ArrayList<>(), new ArrayList<>(), false));
return new RenameObject(DEFAULT_MAPPING_NAME, newName);
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, MAPPING_NOT_FOUND_ERROR_MESSAGE + oldName);
}
}

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

MappingEntity copiedMapping = new MappingEntity(copyName, mappingToCopy);
try {
// --- duplicate filters in filter-server--- //
// get all filter uuids that needs to duplicate its corresponding filter
List<UUID> filterUuids = copiedMapping.getRules().stream()
.map(RuleEntity::getFilterUuid)
.filter(Objects::nonNull)
.toList();
// --- duplicate filters in filter-server--- //
// get all filter uuids that needs to duplicate its corresponding filter
List<UUID> filterUuids = copiedMapping.getRules().stream()
.map(RuleEntity::getFilterUuid)
.filter(Objects::nonNull)
.toList();

if (CollectionUtils.isNotEmpty(filterUuids)) {
// call filter-server API to duplicate filter
Map<UUID, UUID> uuidsMap = filterClient.duplicateFilters(filterUuids);
if (CollectionUtils.isNotEmpty(filterUuids)) {
// call filter-server API to duplicate filter
Map<UUID, UUID> uuidsMap = filterClient.duplicateFilters(filterUuids);

// replace the old by the new uuid for rule entities
copiedMapping.getRules().stream()
.filter(rule -> rule.getFilterUuid() != null)
.forEach(rule -> rule.setFilterUuid(uuidsMap.get(rule.getFilterUuid())));
}
// replace the old by the new uuid for rule entities
copiedMapping.getRules().stream()
.filter(rule -> rule.getFilterUuid() != null)
.forEach(rule -> rule.setFilterUuid(uuidsMap.get(rule.getFilterUuid())));
}

// --- persist in cascade the mapping in local database --- //
MappingEntity savedMappingEntity = mappingRepository.save(copiedMapping);
// --- persist in cascade the mapping in local database --- //
MappingEntity savedMappingEntity = mappingRepository.save(copiedMapping);

// --- build mapping dto to return --- //
InputMapping mapping = new InputMapping(savedMappingEntity);
enrichFiltersForMappings(List.of(mapping));
// --- build mapping dto to return --- //
InputMapping mapping = new InputMapping(savedMappingEntity);
enrichFiltersForMappings(List.of(mapping));

return mapping;
} catch (DataIntegrityViolationException ex) {
throw new ResponseStatusException(HttpStatus.CONFLICT, CONFLICT_MAPPING_ERROR_MESSAGE + copyName, ex);
}
return mapping;
}

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

// get model by name from db, concat to default models and convert to dtos
List<Model> mappedModels = Stream.concat(modelRepository.findAllById(mappedModelNames).stream(),
return Stream.concat(modelRepository.findAllById(mappedModelNames).stream(),
modelRepository.findAllByDefaultModelTrue().stream())
.map(Model::new).toList();

return mappedModels;
}

}
Expand Down
Loading