16
16
import org .springframework .stereotype .Service ;
17
17
import org .springframework .web .multipart .MultipartFile ;
18
18
19
- import java .util .List ;
20
- import java .util .Map ;
21
- import java .util .Objects ;
22
- import java .util .UUID ;
19
+ import java .util .*;
23
20
import java .util .stream .Stream ;
24
21
25
22
import static org .gridsuite .explore .server .ExploreException .Type .*;
@@ -90,7 +87,12 @@ public ExploreService(
90
87
91
88
public void createStudy (String studyName , CaseInfo caseInfo , String description , String userId , UUID parentDirectoryUuid , Map <String , Object > importParams , Boolean duplicateCase ) {
92
89
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 );
94
96
directoryService .createElement (elementAttributes , parentDirectoryUuid , userId );
95
97
}
96
98
@@ -132,7 +134,7 @@ public void createFormContingencyList(String listName, String content, String de
132
134
}
133
135
134
136
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 )) ;
136
138
if (!elementAttribute .getType ().equals (CONTINGENCY_LIST )) {
137
139
throw new ExploreException (NOT_ALLOWED );
138
140
}
@@ -143,7 +145,7 @@ public void newScriptFromFormContingencyList(UUID id, String scriptName, String
143
145
}
144
146
145
147
public void replaceFormContingencyListWithScript (UUID id , String userId ) {
146
- ElementAttributes elementAttribute = directoryService .getElementInfos (id );
148
+ ElementAttributes elementAttribute = directoryService .getElementInfos (id ). orElseThrow (() -> new ExploreException ( NOT_FOUND )) ;
147
149
if (!elementAttribute .getType ().equals (CONTINGENCY_LIST )) {
148
150
throw new ExploreException (NOT_ALLOWED );
149
151
}
@@ -169,7 +171,7 @@ public void duplicateFilter(UUID sourceFilterId, UUID targetDirectoryId, String
169
171
}
170
172
171
173
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 )) ;
173
175
if (!elementAttribute .getType ().equals (FILTER )) {
174
176
throw new ExploreException (NOT_ALLOWED );
175
177
}
@@ -180,7 +182,7 @@ public void newScriptFromFilter(UUID filterId, String scriptName, String userId,
180
182
}
181
183
182
184
public void replaceFilterWithScript (UUID id , String userId ) {
183
- ElementAttributes elementAttribute = directoryService .getElementInfos (id );
185
+ ElementAttributes elementAttribute = directoryService .getElementInfos (id ). orElseThrow (() -> new ExploreException ( NOT_FOUND )) ;
184
186
if (!userId .equals (elementAttribute .getOwner ())) {
185
187
throw new ExploreException (NOT_ALLOWED );
186
188
}
@@ -385,7 +387,7 @@ public void notifyCasesThresholdReached(int userCasesCount, int userMaxAllowedSt
385
387
public void updateElement (UUID id , ElementAttributes elementAttributes , String userId ) {
386
388
// The check to know if the user have the right to update the element is done in the directory-server
387
389
directoryService .updateElement (id , elementAttributes , userId );
388
- ElementAttributes elementsInfos = directoryService .getElementInfos (id );
390
+ ElementAttributes elementsInfos = directoryService .getElementInfos (id ). orElseThrow (() -> new ExploreException ( NOT_FOUND )) ;
389
391
// send notification if the study name was updated
390
392
notifyStudyUpdate (elementsInfos , userId );
391
393
}
0 commit comments