Skip to content

Commit 8051a10

Browse files
authored
Add the case name to the study server endpoint. (#141)
Signed-off-by: AAJELLAL <[email protected]>
1 parent 0fde900 commit 8051a10

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

src/main/java/org/gridsuite/explore/server/services/DirectoryService.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.gridsuite.explore.server.dto.PermissionDTO;
1313
import org.gridsuite.explore.server.dto.PermissionType;
1414
import org.gridsuite.explore.server.utils.ParametersType;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
1517
import org.springframework.core.ParameterizedTypeReference;
1618
import org.springframework.http.*;
1719
import org.springframework.stereotype.Service;
@@ -59,6 +61,7 @@ public class DirectoryService implements IDirectoryElementsService {
5961
private static final String PARAM_USER_INPUT = "userInput";
6062

6163
private static final String HEADER_PERMISION_ERROR = "X-Permission-Error";
64+
private static final Logger LOGGER = LoggerFactory.getLogger(DirectoryService.class);
6265

6366
private final Map<String, IDirectoryElementsService> genericServices;
6467
private final RestTemplate restTemplate;
@@ -266,13 +269,18 @@ public void deleteElementsFromDirectory(List<UUID> elementUuids, UUID parentDire
266269
restTemplate.exchange(directoryServerBaseUri + path, HttpMethod.DELETE, new HttpEntity<>(headers), Void.class);
267270
}
268271

269-
public ElementAttributes getElementInfos(UUID elementUuid) {
272+
public Optional<ElementAttributes> getElementInfos(UUID elementUuid) {
270273
String path = UriComponentsBuilder
271274
.fromPath(ELEMENTS_SERVER_ELEMENT_PATH)
272275
.buildAndExpand(elementUuid)
273276
.toUriString();
274-
return restTemplate.exchange(directoryServerBaseUri + path, HttpMethod.GET, null, ElementAttributes.class)
275-
.getBody();
277+
try {
278+
return Optional.ofNullable(restTemplate.exchange(directoryServerBaseUri + path, HttpMethod.GET, null, ElementAttributes.class).getBody());
279+
} catch (HttpStatusCodeException e) {
280+
LOGGER.error(e.toString(), e);
281+
}
282+
return Optional.empty();
283+
276284
}
277285

278286
public List<ElementAttributes> getElementsInfos(List<UUID> elementsUuids, List<String> elementTypes, String userId) {
@@ -331,7 +339,7 @@ private List<ElementAttributes> getDirectoryElements(UUID directoryUuid, String
331339
}
332340

333341
public void deleteElement(UUID id, String userId) {
334-
ElementAttributes elementAttribute = getElementInfos(id);
342+
ElementAttributes elementAttribute = getElementInfos(id).orElseThrow(() -> new ExploreException(NOT_FOUND));
335343
IDirectoryElementsService service = getGenericService(elementAttribute.getType());
336344
service.delete(elementAttribute.getElementUuid(), userId);
337345
}

src/main/java/org/gridsuite/explore/server/services/ExploreService.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
import org.springframework.stereotype.Service;
1717
import org.springframework.web.multipart.MultipartFile;
1818

19-
import java.util.List;
20-
import java.util.Map;
21-
import java.util.Objects;
22-
import java.util.UUID;
19+
import java.util.*;
2320
import java.util.stream.Stream;
2421

2522
import static org.gridsuite.explore.server.ExploreException.Type.*;
@@ -90,7 +87,12 @@ public ExploreService(
9087

9188
public void createStudy(String studyName, CaseInfo caseInfo, String description, String userId, UUID parentDirectoryUuid, Map<String, Object> importParams, Boolean duplicateCase) {
9289
ElementAttributes elementAttributes = new ElementAttributes(UUID.randomUUID(), studyName, STUDY, userId, 0L, description);
93-
studyService.insertStudyWithExistingCaseFile(elementAttributes.getElementUuid(), userId, caseInfo.caseUuid(), caseInfo.caseFormat(), importParams, duplicateCase);
90+
// Two scenarios to handle.
91+
// Scenario 1: the study is created from an existing case, so the case is available in the directory server.
92+
// Scenario 2: the study is not created from an existing case, in which case the directory throws exception because no element with the given uuid.
93+
Optional<ElementAttributes> caseAttributes = directoryService.getElementInfos(caseInfo.caseUuid());
94+
String elementName = caseAttributes.map(ElementAttributes::getElementName).orElse(null);
95+
studyService.insertStudyWithExistingCaseFile(elementAttributes.getElementUuid(), userId, caseInfo.caseUuid(), caseInfo.caseFormat(), importParams, duplicateCase, elementName);
9496
directoryService.createElement(elementAttributes, parentDirectoryUuid, userId);
9597
}
9698

@@ -132,7 +134,7 @@ public void createFormContingencyList(String listName, String content, String de
132134
}
133135

134136
public void newScriptFromFormContingencyList(UUID id, String scriptName, String userId, UUID parentDirectoryUuid) {
135-
ElementAttributes elementAttribute = directoryService.getElementInfos(id);
137+
ElementAttributes elementAttribute = directoryService.getElementInfos(id).orElseThrow(() -> new ExploreException(NOT_FOUND));
136138
if (!elementAttribute.getType().equals(CONTINGENCY_LIST)) {
137139
throw new ExploreException(NOT_ALLOWED);
138140
}
@@ -143,7 +145,7 @@ public void newScriptFromFormContingencyList(UUID id, String scriptName, String
143145
}
144146

145147
public void replaceFormContingencyListWithScript(UUID id, String userId) {
146-
ElementAttributes elementAttribute = directoryService.getElementInfos(id);
148+
ElementAttributes elementAttribute = directoryService.getElementInfos(id).orElseThrow(() -> new ExploreException(NOT_FOUND));
147149
if (!elementAttribute.getType().equals(CONTINGENCY_LIST)) {
148150
throw new ExploreException(NOT_ALLOWED);
149151
}
@@ -169,7 +171,7 @@ public void duplicateFilter(UUID sourceFilterId, UUID targetDirectoryId, String
169171
}
170172

171173
public void newScriptFromFilter(UUID filterId, String scriptName, String userId, UUID parentDirectoryUuid) {
172-
ElementAttributes elementAttribute = directoryService.getElementInfos(filterId);
174+
ElementAttributes elementAttribute = directoryService.getElementInfos(filterId).orElseThrow(() -> new ExploreException(NOT_FOUND));
173175
if (!elementAttribute.getType().equals(FILTER)) {
174176
throw new ExploreException(NOT_ALLOWED);
175177
}
@@ -180,7 +182,7 @@ public void newScriptFromFilter(UUID filterId, String scriptName, String userId,
180182
}
181183

182184
public void replaceFilterWithScript(UUID id, String userId) {
183-
ElementAttributes elementAttribute = directoryService.getElementInfos(id);
185+
ElementAttributes elementAttribute = directoryService.getElementInfos(id).orElseThrow(() -> new ExploreException(NOT_FOUND));
184186
if (!userId.equals(elementAttribute.getOwner())) {
185187
throw new ExploreException(NOT_ALLOWED);
186188
}
@@ -385,7 +387,7 @@ public void notifyCasesThresholdReached(int userCasesCount, int userMaxAllowedSt
385387
public void updateElement(UUID id, ElementAttributes elementAttributes, String userId) {
386388
// The check to know if the user have the right to update the element is done in the directory-server
387389
directoryService.updateElement(id, elementAttributes, userId);
388-
ElementAttributes elementsInfos = directoryService.getElementInfos(id);
390+
ElementAttributes elementsInfos = directoryService.getElementInfos(id).orElseThrow(() -> new ExploreException(NOT_FOUND));
389391
// send notification if the study name was updated
390392
notifyStudyUpdate(elementsInfos, userId);
391393
}

src/main/java/org/gridsuite/explore/server/services/ParametersService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import lombok.AllArgsConstructor;
1010
import lombok.Getter;
11+
import org.gridsuite.explore.server.ExploreException;
1112
import org.gridsuite.explore.server.dto.ElementAttributes;
1213
import org.gridsuite.explore.server.utils.ParametersType;
1314
import org.springframework.context.annotation.Lazy;
@@ -20,6 +21,8 @@
2021
import java.util.Objects;
2122
import java.util.UUID;
2223

24+
import static org.gridsuite.explore.server.ExploreException.Type.NOT_FOUND;
25+
2326
/**
2427
* @author Ayoub LABIDI <ayoub.labidi at rte-france.com>
2528
*/
@@ -113,7 +116,7 @@ public UUID duplicateParameters(UUID sourceParametersUuid, ParametersType parame
113116

114117
@Override
115118
public void delete(UUID parametersUuid, String userId) {
116-
ElementAttributes elementAttributes = directoryService.getElementInfos(parametersUuid);
119+
ElementAttributes elementAttributes = directoryService.getElementInfos(parametersUuid).orElseThrow(() -> new ExploreException(NOT_FOUND));
117120
ParametersType parametersType = ParametersType.valueOf(elementAttributes.getType());
118121
String parametersServerBaseUri = remoteServicesProperties.getServiceUri(genericParametersServices.get(parametersType).getServerName());
119122
String path = UriComponentsBuilder.fromPath(DELIMITER + SERVER_API_VERSION + genericParametersServices.get(parametersType).getParametersBaseUrl() + "/{parametersUuid}")

src/main/java/org/gridsuite/explore/server/services/StudyService.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.gridsuite.explore.server.services;
88

9+
import org.apache.commons.lang3.StringUtils;
910
import org.springframework.core.ParameterizedTypeReference;
1011
import org.springframework.http.*;
1112
import org.springframework.stereotype.Service;
@@ -37,14 +38,17 @@ public void setStudyServerBaseUri(String studyServerBaseUri) {
3738
}
3839

3940
public void insertStudyWithExistingCaseFile(UUID studyUuid, String userId, UUID caseUuid, String caseFormat,
40-
Map<String, Object> importParams, Boolean duplicateCase) {
41-
String path = UriComponentsBuilder.fromPath(DELIMITER + STUDY_SERVER_API_VERSION +
41+
Map<String, Object> importParams, Boolean duplicateCase, String firstRootNetworkName) {
42+
var uriComponentsBuilder = UriComponentsBuilder.fromPath(DELIMITER + STUDY_SERVER_API_VERSION +
4243
"/studies/cases/{caseUuid}")
4344
.queryParam("studyUuid", studyUuid)
4445
.queryParam("duplicateCase", duplicateCase)
45-
.queryParam("caseFormat", caseFormat)
46-
.buildAndExpand(caseUuid)
47-
.toUriString();
46+
.queryParam("caseFormat", caseFormat);
47+
48+
if (!StringUtils.isBlank(firstRootNetworkName)) {
49+
uriComponentsBuilder.queryParam("firstRootNetworkName", firstRootNetworkName);
50+
}
51+
String path = uriComponentsBuilder.buildAndExpand(caseUuid).toUriString();
4852
HttpHeaders headers = new HttpHeaders();
4953
headers.setContentType(MediaType.APPLICATION_JSON);
5054
headers.add(HEADER_USER_ID, userId);

0 commit comments

Comments
 (0)