Salome HOME
Fix of wrong search results after logout.
[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.Arrays;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import org.splat.dal.bo.kernel.User;
9 import org.splat.dal.bo.som.DocumentType;
10 import org.splat.dal.bo.som.Study;
11 import org.splat.dal.bo.som.ValidationCycle;
12 import org.splat.dal.bo.som.ValidationStep;
13 import org.splat.exception.InvalidParameterException;
14 import org.splat.kernel.InvalidPropertyException;
15 import org.splat.kernel.Name;
16 import org.splat.service.DocumentTypeService;
17 import org.splat.service.UserService;
18 import org.splat.service.dto.UserDTO;
19 import org.splat.som.ApplicationRights;
20 import org.splat.som.StudyRights;
21 import org.splat.util.BeanHelper;
22 import org.splat.wapp.Constants;
23
24 /**
25  * Edit/display study properties (study configuration) screen action.
26  */
27 public class StudyPropertiesAction extends DisplayStudyStepAction {
28
29         /**
30          * Serial version ID.
31          */
32         private static final long serialVersionUID = 4210696018741092900L;
33
34         // Presentation fields
35         /**
36          * List of available users which are not yet included into contributors.
37          */
38         private transient List<User> _staff;
39         /**
40          * Study contributors list.
41          */
42         private transient List<User> _member;
43         /**
44          * List of validators.
45          */
46         private transient List<Name> _validor;
47         /**
48          * List of specific study validation cycles presentations.
49          */
50         private transient List<ValidationFacade> _validation;
51         /**
52          * Default validation cycle presentation.
53          */
54         private transient ValidationFacade _validefault;
55         /**
56          * List of document types which have no specific validation cycles for this study.
57          */
58         private transient List<DocumentType> _other;
59
60         // User input fields
61         /**
62          * Edition action (title, contributors or cycle).
63          */
64         private transient Save _tosave;
65         /**
66          * Document type name for the modified validation cycle.
67          */
68         private transient String _edicycle;
69         /**
70          * Title of the study.
71          */
72         private transient String _stitle;
73         /**
74          * List of existing contributors, some of them may have been removed.
75          */
76         private transient String _contributors;
77         /**
78          * List of added contributors.
79          */
80         private transient String _candidates;
81         /**
82          * Type of document to be included in the validation process.
83          */
84         private transient long _type;
85         /**
86          * Validation cycle publisher's id.
87          */
88         private transient long _publisher;
89         /**
90          * Validation cycle reviewer's id.
91          */
92         private transient long _reviewer;
93         /**
94          * Validation cycle approver's id.
95          */
96         private transient long _approver;
97
98         /**
99          * Injected document type service.
100          */
101         private DocumentTypeService _documentTypeService;
102
103         /**
104          * Injected user service.
105          */
106         private UserService _userService;
107
108         /**
109          * Study readers list.
110          */
111         private List<UserDTO> _readers;
112
113         /**
114          * Study reader id list.
115          */
116         private List<Long> _readerIds;
117
118         /**
119          * Save operation type enumeration pointing which section of properties has been edited.
120          */
121         private enum Save {
122                 /**
123                  * Save study title.
124                  */
125                 title,
126                 /**
127                  * Save contributors.
128                  */
129                 contributor,
130                 /**
131                  * Save validation cycle.
132                  */
133                 cycle,
134                 /**
135                  * Save study readers.
136                  */
137                 readers
138         }
139
140         // ==============================================================================================================================
141         // Action methods
142         // ==============================================================================================================================
143
144         /**
145          * Initialize study properties action.
146          * 
147          * @return "edit" if user can edit study properties, otherwise return "display"
148          */
149         public String doInitialize() {
150
151                 _openStudy = getOpenStudy();
152                 Study study = _openStudy.getStudyObject();
153
154                 initData(study);
155
156                 _staff = null;
157                 _validor = null;
158
159                 String res;
160                 StudyRights user = _openStudy.getStudyRights();
161                 if (_openStudy.isOpenForWriting() && user.canEditProperties()) {
162                         res = "edit";
163                 } else {
164                         res = "display";
165                 }
166
167                 try {
168                         _readers = getStudyService().getReaders(
169                                         getOpenStudy().getIndex().longValue());
170                 } catch (InvalidParameterException e) {
171                         LOG.error(e.getMessage(), e);
172                 }
173                 return res;
174         }
175
176         /**
177          * Initialize common study data presented on the screen.
178          * 
179          * @param study
180          *            the selected study
181          */
182         private void initData(final Study study) {
183                 _validation = new ArrayList<ValidationFacade>();
184                 _validefault = null;
185                 // RKV: _other = getDocumentTypeService().selectResultTypes();
186                 _other = getDocumentTypeService().selectAllTypes(); // RKV
187
188                 for (Iterator<DocumentType> i = _other.iterator(); i.hasNext();) {
189                         DocumentType type = i.next();
190                         ValidationCycle cycle = getStudyService().getValidationCycleOf(
191                                         study, type);
192                         if (cycle.isDefault()) {
193                                 if (_validefault == null) {
194                                         _validefault = new ValidationFacade(cycle,
195                                                         getApplicationSettings().getCurrentLocale());
196                                 }
197                         } else {
198                                 if (type.getName().equals(_edicycle)) {
199                                         this._type = type.getIndex();
200                                 }
201                                 _validation.add(new ValidationFacade(cycle,
202                                                 getApplicationSettings().getCurrentLocale()));
203                                 i.remove();
204                         }
205                 }
206                 if (_validefault != null) {
207                         _validation.add(_validefault); // In order to be at the end
208                 }
209
210                 _member = getStudyService().getContributors(study);
211
212                 initializationFullScreenContext(Constants.STUDY_MENU,
213                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
214                                 Constants.OPEN);
215         }
216
217         /**
218          * Prepare study title editing.
219          * 
220          * @return SUCCESS
221          */
222         public String doEditTitle() {
223
224                 _openStudy = getOpenStudy();
225
226                 initData(_openStudy.getStudyObject());
227
228                 _staff = null;
229                 _validor = null;
230                 _other = null;
231
232                 try {
233                         _readers = getStudyService().getReaders(
234                                         getOpenStudy().getIndex().longValue());
235                 } catch (InvalidParameterException e) {
236                         LOG.error(e.getMessage(), e);
237                 }
238                 setActionType("edititle");
239                 return SUCCESS;
240         }
241
242         /**
243          * Prepare contributors list editing for the selected study.
244          * 
245          * @return SUCCESS
246          */
247         public String doEditContributors() {
248
249                 _openStudy = getOpenStudy();
250                 Study study = _openStudy.getStudyObject();
251
252                 initData(study);
253
254                 _staff = getUserService().selectAllUsers();
255                 _validor = null;
256                 _other = null;
257                 User me = this.getConnectedUser();
258                 for (Iterator<User> i = _staff.iterator(); i.hasNext();) {
259                         User next = i.next();
260                         ApplicationRights he = new ApplicationRights(next);
261                         if (next.equals(me) || _member.contains(next)
262                                         || !he.canContributeToStudy()) {
263                                 i.remove();
264                         }
265                 }
266
267                 try {
268                         _readers = getStudyService().getReaders(
269                                         getOpenStudy().getIndex().longValue());
270                 } catch (InvalidParameterException e) {
271                         LOG.error(e.getMessage(), e);
272                 }
273                 
274                 removeValidCycleFromStaff();
275                 setActionType("edibutor");
276                 return SUCCESS;
277         }
278
279         /**
280          * Initialize edit readers screen.
281          * 
282          * @return SUCCES
283          */
284         public String doInitEditReaders() {
285                 doInitialize();
286
287                 _staff = getUserService().selectAllUsers();
288
289                 // remove user from potential readers
290                 _staff.remove(getConnectedUser());
291
292                 // remove contributors from potential readers
293                 if (_member != null) {
294                         _staff.removeAll(_member);
295                 }
296
297                 // remove readers from potential readers
298                 List<User> readers = new ArrayList<User>();
299                 for (UserDTO userDTO : _readers) {
300                         readers.add(BeanHelper.copyBean(userDTO, User.class));
301                 }
302                 _staff.removeAll(readers);
303
304                 removeValidCycleFromStaff();
305                 setActionType("editReaders");
306
307                 return SUCCESS;
308         }
309         
310         /**
311          * Remove validation cycle members from potential readers or contributors.
312          */
313         private void removeValidCycleFromStaff() {
314                 if(_staff != null) {
315                         for(DocumentType type : _documentTypeService.selectAllTypes()) {
316                                 User[] users = getStudyService()
317                                                 .getValidationCycleOf(getOpenStudy().getStudyObject(), type).getAllActors();
318                                 _staff.removeAll(Arrays.asList(users));
319                         }
320                 }
321         }
322
323         /**
324          * Add or edit a document validation cycle for the selected study.
325          * 
326          * @return SUCCESS
327          */
328         public String doEditCycle() {
329
330                 _openStudy = getOpenStudy();
331                 Study study = _openStudy.getStudyObject();
332
333                 initData(study);
334
335                 _validor = new ArrayList<Name>();
336                 _staff = null;
337                 List<User> user = getUserService().selectAllUsers();
338                 for (Iterator<User> i = user.iterator(); i.hasNext();) {
339                         User next = i.next();
340                         ApplicationRights he = new ApplicationRights(next);
341                         if (he.canValidate()) {
342                                 if (next.equals(study.getAuthor())) {
343                                         _validor.add(new ValidationFacade.ByManager(next,
344                                                         getApplicationSettings().getCurrentLocale()));
345                                 } else {
346                                         _validor.add(next);
347                                 }
348                         }
349                 }
350
351                 try {
352                         _readers = getStudyService().getReaders(
353                                         getOpenStudy().getIndex().longValue());
354                 } catch (InvalidParameterException e) {
355                         LOG.error(e.getMessage(), e);
356                 }
357                 setActionType("edicycle");
358                 return SUCCESS;
359         }
360
361         /**
362          * Save modifications.
363          * 
364          * @return SUCCESS
365          * @throws InvalidParameterException
366          *             if can't modify readers list
367          */
368         public String doEdition() throws InvalidParameterException {
369                 Study study = getOpenStudy().getStudyObject();
370
371                 if (_tosave == Save.title) {
372
373                         // Edition of the title
374                         Study.Properties sprop = new Study.Properties();
375                         try {
376                                 getStudyService().update(study, sprop.setTitle(_stitle));
377                         } catch (InvalidPropertyException e) {
378                                 LOG.error(e.getMessage(), e);
379                         }
380                 } else if (_tosave == Save.contributor) {
381                         saveContributor(study);
382
383                 } else if (_tosave == Save.cycle) {
384                         saveValidationCycle(study);
385
386                 } else if (_tosave == Save.readers) {
387                         saveReaders(study);
388                 }
389
390                 doInitialize(); // Re-initialization following the above edition
391
392                 return SUCCESS;
393         }
394
395         /**
396          * Save the study readers list.
397          * 
398          * @param study
399          *            the selected study
400          * @throws InvalidParameterException
401          *             if the study readers list modification is failed
402          */
403         private void saveReaders(final Study study)
404                         throws InvalidParameterException {
405                 long studyId = getOpenStudy().getIndex().longValue();
406                 _readers = getStudyService().getReaders(studyId);
407
408                 // Remove newly unchecked users
409                 if (_readers != null) {
410                         for (UserDTO userDTO : _readers) {
411                                 if (_readerIds == null
412                                                 || !_readerIds.contains(userDTO.getIndex())) {
413                                         getStudyService().removeReader(studyId, userDTO.getIndex());
414                                 }
415                         }
416                 }
417
418                 // Add newly checked users
419                 if (_readerIds != null) {
420                         for (Long userId : _readerIds) {
421                                 if (_readers == null) {
422                                         getStudyService().addReader(studyId, userId);
423                                 } else {
424                                         boolean contains = false;
425                                         for (UserDTO userDTO : _readers) {
426                                                 if (userId.longValue() == userDTO.getIndex()) {
427                                                         contains = true;
428                                                         break;
429                                                 }
430                                         }
431                                         if (!contains) {
432                                                 getStudyService().addReader(studyId, userId);
433                                         }
434                                 }
435                         }
436                 }
437
438                 // Update OpenStudy
439                 _openStudy.open(getConnectedUser(), getStudyService().selectStudy(
440                                 _openStudy.getIndex()));
441         }
442
443         /**
444          * Save the study document validation cycle.
445          * 
446          * @param study
447          *            the selected study
448          */
449         private void saveValidationCycle(final Study study) {
450                 // Addition of a document validation cycle
451                 DocumentType apply = getDocumentTypeService().selectType(_type);
452                 ValidationCycle.Properties vprop = new ValidationCycle.Properties();
453                 if (_publisher > 0) {
454                         User actor = getUserService().selectUser(_publisher);
455                         vprop.setActor(ValidationStep.PROMOTION, actor);
456                 }
457                 if (_reviewer > 0) {
458                         User actor = getUserService().selectUser(_reviewer);
459                         vprop.setActor(ValidationStep.REVIEW, actor);
460                 }
461                 if (_approver > 0) {
462                         User actor = getUserService().selectUser(_approver);
463                         vprop.setActor(ValidationStep.APPROVAL, actor);
464                 }
465                 getStudyService().setValidationCycle(study, apply, vprop);
466         }
467
468         /**
469          * Save the study contributors list.
470          * 
471          * @param study
472          *            the selected study
473          */
474         private void saveContributor(final Study study) {
475                 // Edition of contributors
476                 if (_contributors == null) {
477                         _contributors = "";
478                 }
479                 if (_candidates == null) {
480                         _candidates = "";
481                 }
482
483                 String[] parsekept = _contributors.split(",");
484                 String[] parsenew = _candidates.split(",");
485                 List<User> toremove = new ArrayList<User>(getStudyService()
486                                 .getContributors(study));
487
488                 for (int i = 0; i < parsekept.length; i++) {
489                         if (parsekept[i].length() != 0) {
490                                 int index = Integer.valueOf(parsekept[i].trim());
491                                 for (Iterator<User> j = toremove.iterator(); j.hasNext();) {
492                                         long present = j.next().getIndex();
493                                         if (present == index) {
494                                                 j.remove();
495                                                 break;
496                                         }
497                                 }
498                         }
499                 }
500                 int size = toremove.size();
501                 if (size > 0) {
502                         getStudyService().removeContributor(study,
503                                         toremove.toArray(new User[size]));
504                 }
505
506                 for (int i = 0; i < parsenew.length; i++) {
507                         if (parsenew[i].length() != 0) {
508                                 int index = Integer.valueOf(parsenew[i].trim());
509                                 User newser = getUserService().selectUser(index);
510
511                                 getStudyService().addContributor(study, newser);
512                         }
513                 }
514         }
515
516         /**
517          * Get the study author.
518          * 
519          * @return the study author
520          */
521         public User getAuthor() {
522                 return _openStudy.getStudyObject().getAuthor();
523         }
524
525         /**
526          * Get list of available candidates.
527          * 
528          * @return list of candidates
529          */
530         public List<User> getCandidates() {
531                 return _staff;
532         }
533
534         /**
535          * Get the document type name of the modified validation cycle.
536          * 
537          * @return the type name of the modified cycle
538          */
539         public String getCycle() {
540                 return _edicycle;
541         }
542
543         /**
544          * Get study contributors list.
545          * 
546          * @return list of contributors
547          */
548         public List<User> getContributors() {
549                 return _member;
550         }
551
552         /**
553          * Get default validation cycle presentation.
554          * 
555          * @return default validation cycle presentation
556          */
557         public ValidationFacade getDefaultValidation() {
558                 return _validefault;
559         }
560
561         /**
562          * Get selected document type id.
563          * 
564          * @return the selected document type id
565          */
566         public long getDocumentTypeIndex() {
567                 return _type;
568         }
569
570         /**
571          * Get list of document types which have no specific validation cycles for this study.
572          * 
573          * @return list of document types
574          */
575         public List<DocumentType> getOtherDocumentTypes() {
576                 return _other;
577         }
578
579         /**
580          * Get study title.
581          * 
582          * @return the study title
583          */
584         public String getStudyTitle() {
585                 return _openStudy.getTitle();
586         }
587
588         /**
589          * Get the list of specific study validation cycles presentations.
590          * 
591          * @return list of validation cycles facades
592          */
593         public List<ValidationFacade> getValidations() {
594                 return _validation;
595         }
596
597         /**
598          * Get the list of validators.
599          * 
600          * @return list of user names
601          */
602         public List<Name> getValidationActors() {
603                 return _validor;
604         }
605
606         /**
607          * Set the list of candidates ids.
608          * 
609          * @param indices
610          *            the list of users ids
611          */
612         public void setCandidates(final String indices) {
613                 _candidates = indices;
614         }
615
616         /**
617          * Set the document type name for the modified validation cycle.
618          * 
619          * @param type
620          *            the document type name
621          */
622         public void setCycle(final String type) {
623                 _edicycle = type;
624         }
625
626         /**
627          * Set the list of contributors ids.
628          * 
629          * @param indices
630          *            the list of users ids
631          */
632         public void setMembers(final String indices) {
633                 _contributors = indices;
634         }
635
636         /**
637          * Set the id of the selected document type.
638          * 
639          * @param index
640          *            document type id
641          */
642         public void setDocumentType(final String index) {
643                 _type = Long.valueOf(index);
644         }
645
646         /**
647          * Set the selected approver id.
648          * 
649          * @param index
650          *            the user id
651          */
652         public void setApprover(final String index) {
653                 _approver = Long.valueOf(index);
654         }
655
656         /**
657          * Set the selected publisher id.
658          * 
659          * @param index
660          *            the user id
661          */
662         public void setPublisher(final String index) {
663                 _publisher = Long.valueOf(index);
664         }
665
666         /**
667          * Set the selected reviewer id.
668          * 
669          * @param index
670          *            the user id
671          */
672         public void setReviewer(final String index) {
673                 _reviewer = Long.valueOf(index);
674         }
675
676         /**
677          * Set the study title.
678          * 
679          * @param title
680          *            the study title
681          */
682         public void setTitle(final String title) {
683                 _stitle = title;
684         }
685
686         /**
687          * Set the current operation to title saving.
688          * 
689          * @param save
690          *            unused parameter
691          */
692         public void setSaveTitle(final String save) {
693                 _tosave = Save.title;
694         }
695
696         /**
697          * Set the current operation to contributors saving.
698          * 
699          * @param save
700          *            unused parameter
701          */
702         public void setSaveContributors(final String save) {
703                 _tosave = Save.contributor;
704         }
705
706         /**
707          * Set the current operation to validation cycle saving.
708          * 
709          * @param save
710          *            unused parameter
711          */
712         public void setSaveCycle(final String save) {
713                 _tosave = Save.cycle;
714         }
715
716         /**
717          * Set the current operation to readers saving.
718          * 
719          * @param save
720          *            unused parameter
721          */
722         public void setSaveReaders(final String save) {
723                 _tosave = Save.readers;
724         }
725
726         /**
727          * Get the documentTypeService.
728          * 
729          * @return the documentTypeService
730          */
731         public DocumentTypeService getDocumentTypeService() {
732                 return _documentTypeService;
733         }
734
735         /**
736          * Set the documentTypeService.
737          * 
738          * @param documentTypeService
739          *            the documentTypeService to set
740          */
741         public void setDocumentTypeService(
742                         final DocumentTypeService documentTypeService) {
743                 _documentTypeService = documentTypeService;
744         }
745
746         /**
747          * Get the userService.
748          * 
749          * @return the userService
750          */
751         public UserService getUserService() {
752                 return _userService;
753         }
754
755         /**
756          * Set the userService.
757          * 
758          * @param userService
759          *            the userService to set
760          */
761         public void setUserService(final UserService userService) {
762                 _userService = userService;
763         }
764
765         /**
766          * Get the readers.
767          * 
768          * @return the readers
769          */
770         public List<UserDTO> getReaders() {
771                 return _readers;
772         }
773
774         /**
775          * Set the readers.
776          * 
777          * @param readers
778          *            the readers to set
779          */
780         public void setReaders(final List<UserDTO> readers) {
781                 _readers = readers;
782         }
783
784         /**
785          * Get the list of readers ids.
786          * 
787          * @return the list of users ids
788          */
789         public List<Long> getReaderIds() {
790                 return _readerIds;
791         }
792
793         /**
794          * Set the list of readers ids.
795          * 
796          * @param readerIds
797          *            the list of users ids to set
798          */
799         public void setReaderIds(final List<Long> readerIds) {
800                 this._readerIds = readerIds;
801         }
802 }