Salome HOME
be785bcef57662c0654c90282a4dc83f93c0eb1f
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / StudyPropertiesAction.java
1 package org.splat.simer;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import org.splat.dal.bo.kernel.User;
8 import org.splat.dal.bo.som.DocumentType;
9 import org.splat.dal.bo.som.Study;
10 import org.splat.dal.bo.som.ValidationCycle;
11 import org.splat.dal.bo.som.ValidationStep;
12 import org.splat.kernel.InvalidPropertyException;
13 import org.splat.kernel.Name;
14 import org.splat.service.DocumentTypeService;
15 import org.splat.service.UserService;
16 import org.splat.som.ApplicationRights;
17 import org.splat.som.StudyRights;
18 import org.splat.wapp.Constants;
19
20 /**
21  * Edit/display study properties (study configuration) screen action.
22  */
23 public class StudyPropertiesAction extends DisplayStudyStepAction {
24
25         /**
26          * Serial version ID.
27          */
28         private static final long serialVersionUID = 4210696018741092900L;
29
30         // Presentation fields
31         private transient List<User> _staff;
32         private transient List<User> _member;
33         private transient List<Name> _validor;
34         private transient List<ValidationFacade> _validation;
35         private transient ValidationFacade _validefault;
36         private transient List<DocumentType> _other;
37
38         // User input fields
39         /**
40          * Edition action (title, contributors or cycle).
41          */
42         private transient Save _tosave;
43         private transient String _edicycle;
44         /**
45          * Title of the study.
46          */
47         private transient String _stitle;
48         /**
49          * List of existing contributors, some of them may have been removed.
50          */
51         private transient String _contributors;
52         /**
53          * List of added contributors.
54          */
55         private transient String _candidates;
56         /**
57          * Type of document to be included in the validation process.
58          */
59         private transient long _type;
60         private transient int _publisher;
61         private transient int _reviewer;
62         private transient int _approver;
63
64         /**
65          * Injected document type service.
66          */
67         private DocumentTypeService _documentTypeService;
68
69         /**
70          * Injected user service.
71          */
72         private UserService _userService;
73
74         /**
75          * Save operation type enumeration pointing which section of properties has been edited.
76          */
77         private enum Save {
78                 /**
79                  * Save study title.
80                  */
81                 title,
82                 /**
83                  * Save contributors.
84                  */
85                 contributor,
86                 /**
87                  * Save validation cycle.
88                  */
89                 cycle
90         }
91
92         // ==============================================================================================================================
93         // Action methods
94         // ==============================================================================================================================
95
96         /**
97          * Initialize study properties action.
98          * 
99          * @return "edit" if user can edit study properties, otherwise return "display"
100          */
101         public String doInitialize() {
102
103                 String res;
104                 _openStudy = getOpenStudy();
105                 _validation = new ArrayList<ValidationFacade>();
106                 _validefault = null;
107                 _other = getDocumentTypeService().selectResultTypes();
108
109                 Study study = _openStudy.getStudyObject();
110                 StudyRights user = _openStudy.getStudyRights();
111                 for (Iterator<DocumentType> i = _other.iterator(); i.hasNext();) {
112                         DocumentType type = i.next();
113                         ValidationCycle cycle = getStudyService().getValidationCycleOf(
114                                         study, type);
115                         if (cycle.isDefault()) {
116                                 _validefault = new ValidationFacade(cycle,
117                                                 getApplicationSettings().getCurrentLocale());
118                                 continue;
119                         }
120                         _validation.add(new ValidationFacade(cycle,
121                                         getApplicationSettings().getCurrentLocale()));
122                         i.remove();
123                 }
124                 if (_validefault != null) {
125                         _validation.add(_validefault); // In order to be at the end
126                 }
127                 _member = getStudyService().getContributors(study);
128                 _staff = null;
129                 _validor = null;
130
131                 initializationFullScreenContext(Constants.STUDY_MENU,
132                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
133                                 Constants.OPEN);
134
135                 if (_openStudy.isOpenForWriting() && user.canEditProperties()) {
136                         res = "edit";
137                 } else {
138                         res = "display";
139                 }
140                 return res;
141         }
142
143         public String doEditTitle() {
144
145                 _openStudy = getOpenStudy();
146                 _validation = new ArrayList<ValidationFacade>();
147                 _validefault = null;
148                 _other = getDocumentTypeService().selectResultTypes();
149
150                 Study study = _openStudy.getStudyObject();
151                 for (Iterator<DocumentType> i = _other.iterator(); i.hasNext();) {
152                         DocumentType type = i.next();
153                         ValidationCycle cycle = getStudyService().getValidationCycleOf(
154                                         study, type);
155                         if (cycle.isDefault()) {
156                                 _validefault = new ValidationFacade(cycle,
157                                                 getApplicationSettings().getCurrentLocale());
158                                 continue;
159                         }
160                         _validation.add(new ValidationFacade(cycle,
161                                         getApplicationSettings().getCurrentLocale()));
162                 }
163                 if (_validefault != null) {
164                         _validation.add(_validefault); // In order to be at the end
165                 }
166                 _member = getStudyService().getContributors(study);
167                 _staff = null;
168                 _validor = null;
169                 _other = null;
170
171                 initializationFullScreenContext(Constants.STUDY_MENU,
172                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
173                                 Constants.OPEN);
174                 setActionType("edititle");
175
176                 return SUCCESS;
177         }
178
179         public String doEditContributors() {
180
181                 _openStudy = getOpenStudy();
182                 _validation = new ArrayList<ValidationFacade>();
183                 _validefault = null;
184                 _other = getDocumentTypeService().selectAllTypes();
185
186                 Study study = _openStudy.getStudyObject();
187                 for (Iterator<DocumentType> i = _other.iterator(); i.hasNext();) {
188                         DocumentType type = i.next();
189                         ValidationCycle cycle = getStudyService().getValidationCycleOf(
190                                         study, type);
191                         if (cycle.isDefault()) {
192                                 _validefault = new ValidationFacade(cycle,
193                                                 getApplicationSettings().getCurrentLocale());
194                                 continue;
195                         }
196                         _validation.add(new ValidationFacade(cycle,
197                                         getApplicationSettings().getCurrentLocale()));
198                 }
199                 if (_validefault != null) {
200                         _validation.add(_validefault); // In order to be at the end
201                 }
202                 _member = getStudyService().getContributors(study);
203                 _staff = getUserService().selectAllUsers();
204                 _validor = null;
205                 _other = null;
206                 User me = this.getConnectedUser();
207                 for (Iterator<User> i = _staff.iterator(); i.hasNext();) {
208                         User next = i.next();
209                         ApplicationRights he = new ApplicationRights(next);
210                         if (next.equals(me) || _member.contains(next)
211                                         || !he.canContributeToStudy()) {
212                                 i.remove();
213                         }
214                 }
215
216                 initializationFullScreenContext(Constants.STUDY_MENU,
217                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
218                                 Constants.OPEN);
219
220                 setActionType("edibutor");
221                 return SUCCESS;
222         }
223
224         public String doEditCycle() {
225
226                 _openStudy = getOpenStudy();
227                 _validation = new ArrayList<ValidationFacade>();
228                 _validefault = null;
229                 _other = getDocumentTypeService().selectResultTypes();
230
231                 Study study = _openStudy.getStudyObject();
232                 for (Iterator<DocumentType> i = _other.iterator(); i.hasNext();) {
233                         DocumentType type = i.next();
234                         ValidationCycle cycle = getStudyService().getValidationCycleOf(
235                                         study, type);
236                         if (cycle.isDefault()) {
237                                 _validefault = new ValidationFacade(cycle,
238                                                 getApplicationSettings().getCurrentLocale());
239                                 continue;
240                         }
241                         if (type.getName().equals(_edicycle)) {
242                                 this._type = type.getIndex();
243                         }
244                         _validation.add(new ValidationFacade(cycle,
245                                         getApplicationSettings().getCurrentLocale()));
246                         i.remove();
247                 }
248                 if (_validefault != null) {
249                         _validation.add(_validefault); // In order to be at the end
250                 }
251                 _member = getStudyService().getContributors(study);
252                 _validor = new ArrayList<Name>();
253                 _staff = null;
254                 List<User> user = getUserService().selectAllUsers();
255                 for (Iterator<User> i = user.iterator(); i.hasNext();) {
256                         User next = i.next();
257                         ApplicationRights he = new ApplicationRights(next);
258                         if (he.canValidate()) {
259                                 if (next.equals(study.getAuthor())) {
260                                         _validor.add(new ValidationFacade.ByManager(next,
261                                                         getApplicationSettings().getCurrentLocale()));
262                                 } else {
263                                         _validor.add(next);
264                                 }
265                         }
266                 }
267
268                 initializationFullScreenContext(Constants.STUDY_MENU,
269                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
270                                 Constants.OPEN);
271
272                 setActionType("edicycle");
273
274                 return SUCCESS;
275         }
276
277         public String doEdition() {
278                 Study study = getOpenStudy().getStudyObject();
279
280                 if (_tosave == Save.title) {
281
282                         // Edition of the title
283                         Study.Properties sprop = new Study.Properties();
284                         try {
285                                 getStudyService().update(study, sprop.setTitle(_stitle));
286                         } catch (InvalidPropertyException e) {
287                                 // TODO
288                         }
289                 } else if (_tosave == Save.contributor) {
290
291                         // Edition of contributors
292                         if (_contributors == null) {
293                                 _contributors = "";
294                         }
295                         if (_candidates == null) {
296                                 _candidates = "";
297                         }
298
299                         String[] parsekept = _contributors.split(",");
300                         String[] parsenew = _candidates.split(",");
301                         List<User> toremove = new ArrayList<User>(getStudyService()
302                                         .getContributors(study));
303
304                         for (int i = 0; i < parsekept.length; i++) {
305                                 if (parsekept[i].length() == 0) {
306                                         continue; // Yet no contributor
307                                 }
308                                 int index = Integer.valueOf(parsekept[i].trim());
309                                 for (Iterator<User> j = toremove.iterator(); j.hasNext();) {
310                                         long present = j.next().getIndex();
311                                         if (present != index) {
312                                                 continue;
313                                         }
314                                         j.remove();
315                                         break;
316                                 }
317                         }
318                         int size = toremove.size();
319                         if (size > 0) {
320                                 getStudyService().removeContributor(study,
321                                                 toremove.toArray(new User[size]));
322                         }
323
324                         for (int i = 0; i < parsenew.length; i++) {
325                                 if (parsenew[i].length() == 0) {
326                                         continue; // No any new contributor
327                                 }
328                                 int index = Integer.valueOf(parsenew[i].trim());
329                                 User newser = getUserService().selectUser(index);
330
331                                 getStudyService().addContributor(study, newser);
332                         }
333                 } else if (_tosave == Save.cycle) {
334
335                         // Addition of a document validation cycle
336                         DocumentType apply = getDocumentTypeService().selectType(_type);
337                         ValidationCycle.Properties vprop = new ValidationCycle.Properties();
338                         if (_publisher > 0) {
339                                 User actor = getUserService().selectUser(_publisher);
340                                 vprop.setActor(ValidationStep.PROMOTION, actor);
341                         }
342                         if (_reviewer > 0) {
343                                 User actor = getUserService().selectUser(_reviewer);
344                                 vprop.setActor(ValidationStep.REVIEW, actor);
345                         }
346                         if (_approver > 0) {
347                                 User actor = getUserService().selectUser(_approver);
348                                 vprop.setActor(ValidationStep.APPROVAL, actor);
349                         }
350                         getStudyService().setValidationCycle(study, apply, vprop);
351                 }
352
353                 doInitialize(); // Re-initialization following the above edition
354
355                 initializationFullScreenContext(Constants.STUDY_MENU,
356                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
357                                 Constants.OPEN);
358
359                 return SUCCESS;
360         }
361
362         // ==============================================================================================================================
363         // Getters
364         // ==============================================================================================================================
365
366         public User getAuthor() {
367                 return _openStudy.getStudyObject().getAuthor();
368         }
369
370         public List<User> getCandidates() {
371                 return _staff;
372         }
373
374         public String getCycle() {
375                 return _edicycle;
376         }
377
378         public List<User> getContributors() {
379                 return _member;
380         }
381
382         public ValidationFacade getDefaultValidation() {
383                 return _validefault;
384         }
385
386         public long getDocumentTypeIndex() {
387                 return _type;
388         }
389
390         public List<DocumentType> getOtherDocumentTypes() {
391                 return _other;
392         }
393
394         public String getStudyTitle() {
395                 return _openStudy.getTitle();
396         }
397
398         public List<ValidationFacade> getValidations() {
399                 return _validation;
400         }
401
402         public List<Name> getValidationActors() {
403                 return _validor;
404         }
405
406         // ==============================================================================================================================
407         // Setters
408         // ==============================================================================================================================
409
410         public void setCandidates(final String indices) {
411                 _candidates = indices;
412         }
413
414         public void setCycle(final String type) {
415                 _edicycle = type;
416         }
417
418         public void setMembers(final String indices) {
419                 _contributors = indices;
420         }
421
422         public void setDocumentType(final String index) {
423                 _type = Integer.valueOf(index);
424         }
425
426         public void setApprover(final String index) {
427                 _approver = Integer.valueOf(index);
428         }
429
430         public void setPublisher(final String index) {
431                 _publisher = Integer.valueOf(index);
432         }
433
434         public void setReviewer(final String index) {
435                 _reviewer = Integer.valueOf(index);
436         }
437
438         public void setTitle(final String title) {
439                 _stitle = title;
440         }
441
442         public void setSaveTitle(final String save) {
443                 _tosave = Save.title;
444         }
445
446         public void setSaveContributors(final String save) {
447                 _tosave = Save.contributor;
448         }
449
450         public void setSaveCycle(final String save) {
451                 _tosave = Save.cycle;
452         }
453
454         /**
455          * Get the documentTypeService.
456          * 
457          * @return the documentTypeService
458          */
459         public DocumentTypeService getDocumentTypeService() {
460                 return _documentTypeService;
461         }
462
463         /**
464          * Set the documentTypeService.
465          * 
466          * @param documentTypeService
467          *            the documentTypeService to set
468          */
469         public void setDocumentTypeService(
470                         final DocumentTypeService documentTypeService) {
471                 _documentTypeService = documentTypeService;
472         }
473
474         /**
475          * Get the userService.
476          * 
477          * @return the userService
478          */
479         public UserService getUserService() {
480                 return _userService;
481         }
482
483         /**
484          * Set the userService.
485          * 
486          * @param userService
487          *            the userService to set
488          */
489         public void setUserService(final UserService userService) {
490                 _userService = userService;
491         }
492 }