Salome HOME
Readers functionality is implemented.
[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.exception.InvalidParameterException;
13 import org.splat.kernel.InvalidPropertyException;
14 import org.splat.kernel.Name;
15 import org.splat.service.DocumentTypeService;
16 import org.splat.service.UserService;
17 import org.splat.service.dto.UserDTO;
18 import org.splat.som.ApplicationRights;
19 import org.splat.som.StudyRights;
20 import org.splat.util.BeanHelper;
21 import org.splat.wapp.Constants;
22
23 /**
24  * Edit/display study properties (study configuration) screen action.
25  */
26 public class StudyPropertiesAction extends DisplayStudyStepAction {
27
28         /**
29          * Serial version ID.
30          */
31         private static final long serialVersionUID = 4210696018741092900L;
32
33         // Presentation fields
34         private transient List<User> _staff;
35         private transient List<User> _member;
36         private transient List<Name> _validor;
37         private transient List<ValidationFacade> _validation;
38         private transient ValidationFacade _validefault;
39         private transient List<DocumentType> _other;
40
41         // User input fields
42         /**
43          * Edition action (title, contributors or cycle).
44          */
45         private transient Save _tosave;
46         private transient String _edicycle;
47         /**
48          * Title of the study.
49          */
50         private transient String _stitle;
51         /**
52          * List of existing contributors, some of them may have been removed.
53          */
54         private transient String _contributors;
55         /**
56          * List of added contributors.
57          */
58         private transient String _candidates;
59         /**
60          * Type of document to be included in the validation process.
61          */
62         private transient long _type;
63         private transient int _publisher;
64         private transient int _reviewer;
65         private transient int _approver;
66
67         /**
68          * Injected document type service.
69          */
70         private DocumentTypeService _documentTypeService;
71
72         /**
73          * Injected user service.
74          */
75         private UserService _userService;
76         
77         /**
78          * Study readers list.
79          */
80         List<UserDTO> _readers;
81
82         /**
83          * Study reader id list.
84          */
85         List<Long > _readerIds;
86
87         /**
88          * Save operation type enumeration pointing which section of properties has been edited.
89          */
90         private enum Save {
91                 /**
92                  * Save study title.
93                  */
94                 title,
95                 /**
96                  * Save contributors.
97                  */
98                 contributor,
99                 /**
100                  * Save validation cycle.
101                  */
102                 cycle,
103                 /**
104                  * Save study readers.
105                  */
106                 readers
107         }
108
109         // ==============================================================================================================================
110         // Action methods
111         // ==============================================================================================================================
112
113         /**
114          * Initialize study properties action.
115          * 
116          * @return "edit" if user can edit study properties, otherwise return "display"
117          */
118         public String doInitialize() {
119
120                 String res;
121                 _openStudy = getOpenStudy();
122                 _validation = new ArrayList<ValidationFacade>();
123                 _validefault = null;
124                 _other = getDocumentTypeService().selectResultTypes();
125
126                 Study study = _openStudy.getStudyObject();
127                 StudyRights user = _openStudy.getStudyRights();
128                 for (Iterator<DocumentType> i = _other.iterator(); i.hasNext();) {
129                         DocumentType type = i.next();
130                         ValidationCycle cycle = getStudyService().getValidationCycleOf(
131                                         study, type);
132                         if (cycle.isDefault()) {
133                                 _validefault = new ValidationFacade(cycle,
134                                                 getApplicationSettings().getCurrentLocale());
135                                 continue;
136                         }
137                         _validation.add(new ValidationFacade(cycle,
138                                         getApplicationSettings().getCurrentLocale()));
139                         i.remove();
140                 }
141                 if (_validefault != null) {
142                         _validation.add(_validefault); // In order to be at the end
143                 }
144                 _member = getStudyService().getContributors(study);
145                 _staff = null;
146                 _validor = null;
147
148                 initializationFullScreenContext(Constants.STUDY_MENU,
149                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
150                                 Constants.OPEN);
151
152                 if (_openStudy.isOpenForWriting() && user.canEditProperties()) {
153                         res = "edit";
154                 } else {
155                         res = "display";
156                 }
157                 
158                 try {
159                         _readers = getStudyService().getReaders(getOpenStudy().getIndex().longValue());
160                 } catch(InvalidParameterException e) {
161                         LOG.error(e.getMessage(), e);                   
162                 }
163                 return res;
164         }
165
166         public String doEditTitle() {
167
168                 _openStudy = getOpenStudy();
169                 _validation = new ArrayList<ValidationFacade>();
170                 _validefault = null;
171                 _other = getDocumentTypeService().selectResultTypes();
172
173                 Study study = _openStudy.getStudyObject();
174                 for (Iterator<DocumentType> i = _other.iterator(); i.hasNext();) {
175                         DocumentType type = i.next();
176                         ValidationCycle cycle = getStudyService().getValidationCycleOf(
177                                         study, type);
178                         if (cycle.isDefault()) {
179                                 _validefault = new ValidationFacade(cycle,
180                                                 getApplicationSettings().getCurrentLocale());
181                                 continue;
182                         }
183                         _validation.add(new ValidationFacade(cycle,
184                                         getApplicationSettings().getCurrentLocale()));
185                 }
186                 if (_validefault != null) {
187                         _validation.add(_validefault); // In order to be at the end
188                 }
189                 _member = getStudyService().getContributors(study);
190                 _staff = null;
191                 _validor = null;
192                 _other = null;
193
194                 initializationFullScreenContext(Constants.STUDY_MENU,
195                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
196                                 Constants.OPEN);
197                 
198                 try {
199                         _readers = getStudyService().getReaders(getOpenStudy().getIndex().longValue());
200                 } catch(InvalidParameterException e) {
201                         LOG.error(e.getMessage(), e);                   
202                 }
203                 setActionType("edititle");
204                 return SUCCESS;
205         }
206
207         public String doEditContributors() {
208
209                 _openStudy = getOpenStudy();
210                 _validation = new ArrayList<ValidationFacade>();
211                 _validefault = null;
212                 _other = getDocumentTypeService().selectAllTypes();
213
214                 Study study = _openStudy.getStudyObject();
215                 for (Iterator<DocumentType> i = _other.iterator(); i.hasNext();) {
216                         DocumentType type = i.next();
217                         ValidationCycle cycle = getStudyService().getValidationCycleOf(
218                                         study, type);
219                         if (cycle.isDefault()) {
220                                 _validefault = new ValidationFacade(cycle,
221                                                 getApplicationSettings().getCurrentLocale());
222                                 continue;
223                         }
224                         _validation.add(new ValidationFacade(cycle,
225                                         getApplicationSettings().getCurrentLocale()));
226                 }
227                 if (_validefault != null) {
228                         _validation.add(_validefault); // In order to be at the end
229                 }
230                 _member = getStudyService().getContributors(study);
231                 _staff = getUserService().selectAllUsers();
232                 _validor = null;
233                 _other = null;
234                 User me = this.getConnectedUser();
235                 for (Iterator<User> i = _staff.iterator(); i.hasNext();) {
236                         User next = i.next();
237                         ApplicationRights he = new ApplicationRights(next);
238                         if (next.equals(me) || _member.contains(next)
239                                         || !he.canContributeToStudy()) {
240                                 i.remove();
241                         }
242                 }
243
244                 initializationFullScreenContext(Constants.STUDY_MENU,
245                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
246                                 Constants.OPEN);
247
248                 try {
249                         _readers = getStudyService().getReaders(getOpenStudy().getIndex().longValue());
250                 } catch(InvalidParameterException e) {
251                         LOG.error(e.getMessage(), e);
252                 }
253                 setActionType("edibutor");
254                 return SUCCESS;
255         }
256         
257         /**
258          * Initialize edit readers screen.
259          * @return SUCCES
260          */
261         public String doInitEditReaders() {
262                 doInitialize();
263                 
264                 _staff = getUserService().selectAllUsers();
265                 
266                 //remove user from potential readers
267                 _staff.remove(getConnectedUser());
268                 
269                 //remove contributors from potential readers
270                 if(_member != null) {
271                         _staff.removeAll(_member);
272                 }
273                 
274                 //remove readers from potential readers
275                 List<User> readers = new ArrayList<User>();
276                 for(UserDTO userDTO : _readers) {
277                         readers.add(BeanHelper.copyBean(userDTO, User.class));
278                 }
279                 _staff.removeAll(readers);
280                 
281                 setActionType("editReaders");
282                 
283             return SUCCESS;
284         }
285
286         public String doEditCycle() {
287
288                 _openStudy = getOpenStudy();
289                 _validation = new ArrayList<ValidationFacade>();
290                 _validefault = null;
291                 _other = getDocumentTypeService().selectResultTypes();
292
293                 Study study = _openStudy.getStudyObject();
294                 for (Iterator<DocumentType> i = _other.iterator(); i.hasNext();) {
295                         DocumentType type = i.next();
296                         ValidationCycle cycle = getStudyService().getValidationCycleOf(
297                                         study, type);
298                         if (cycle.isDefault()) {
299                                 _validefault = new ValidationFacade(cycle,
300                                                 getApplicationSettings().getCurrentLocale());
301                                 continue;
302                         }
303                         if (type.getName().equals(_edicycle)) {
304                                 this._type = type.getIndex();
305                         }
306                         _validation.add(new ValidationFacade(cycle,
307                                         getApplicationSettings().getCurrentLocale()));
308                         i.remove();
309                 }
310                 if (_validefault != null) {
311                         _validation.add(_validefault); // In order to be at the end
312                 }
313                 _member = getStudyService().getContributors(study);
314                 _validor = new ArrayList<Name>();
315                 _staff = null;
316                 List<User> user = getUserService().selectAllUsers();
317                 for (Iterator<User> i = user.iterator(); i.hasNext();) {
318                         User next = i.next();
319                         ApplicationRights he = new ApplicationRights(next);
320                         if (he.canValidate()) {
321                                 if (next.equals(study.getAuthor())) {
322                                         _validor.add(new ValidationFacade.ByManager(next,
323                                                         getApplicationSettings().getCurrentLocale()));
324                                 } else {
325                                         _validor.add(next);
326                                 }
327                         }
328                 }
329
330                 initializationFullScreenContext(Constants.STUDY_MENU,
331                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
332                                 Constants.OPEN);
333
334                 try {
335                         _readers = getStudyService().getReaders(getOpenStudy().getIndex().longValue());
336                 } catch(InvalidParameterException e) {
337                         LOG.error(e.getMessage(), e);                   
338                 }
339                 setActionType("edicycle");
340                 return SUCCESS;
341         }
342
343         public String doEdition() throws InvalidParameterException {
344                 Study study = getOpenStudy().getStudyObject();
345
346                 if (_tosave == Save.title) {
347
348                         // Edition of the title
349                         Study.Properties sprop = new Study.Properties();
350                         try {
351                                 getStudyService().update(study, sprop.setTitle(_stitle));
352                         } catch (InvalidPropertyException e) {
353                                 // TODO:
354                                 LOG.error(e.getMessage(), e);
355                         }
356                 } else if (_tosave == Save.contributor) {
357
358                         // Edition of contributors
359                         if (_contributors == null) {
360                                 _contributors = "";
361                         }
362                         if (_candidates == null) {
363                                 _candidates = "";
364                         }
365
366                         String[] parsekept = _contributors.split(",");
367                         String[] parsenew = _candidates.split(",");
368                         List<User> toremove = new ArrayList<User>(getStudyService()
369                                         .getContributors(study));
370
371                         for (int i = 0; i < parsekept.length; i++) {
372                                 if (parsekept[i].length() == 0) {
373                                         continue; // Yet no contributor
374                                 }
375                                 int index = Integer.valueOf(parsekept[i].trim());
376                                 for (Iterator<User> j = toremove.iterator(); j.hasNext();) {
377                                         long present = j.next().getIndex();
378                                         if (present != index) {
379                                                 continue;
380                                         }
381                                         j.remove();
382                                         break;
383                                 }
384                         }
385                         int size = toremove.size();
386                         if (size > 0) {
387                                 getStudyService().removeContributor(study,
388                                                 toremove.toArray(new User[size]));
389                         }
390
391                         for (int i = 0; i < parsenew.length; i++) {
392                                 if (parsenew[i].length() == 0) {
393                                         continue; // No any new contributor
394                                 }
395                                 int index = Integer.valueOf(parsenew[i].trim());
396                                 User newser = getUserService().selectUser(index);
397
398                                 getStudyService().addContributor(study, newser);
399                         }
400                 } else if (_tosave == Save.cycle) {
401
402                         // Addition of a document validation cycle
403                         DocumentType apply = getDocumentTypeService().selectType(_type);
404                         ValidationCycle.Properties vprop = new ValidationCycle.Properties();
405                         if (_publisher > 0) {
406                                 User actor = getUserService().selectUser(_publisher);
407                                 vprop.setActor(ValidationStep.PROMOTION, actor);
408                         }
409                         if (_reviewer > 0) {
410                                 User actor = getUserService().selectUser(_reviewer);
411                                 vprop.setActor(ValidationStep.REVIEW, actor);
412                         }
413                         if (_approver > 0) {
414                                 User actor = getUserService().selectUser(_approver);
415                                 vprop.setActor(ValidationStep.APPROVAL, actor);
416                         }
417                         getStudyService().setValidationCycle(study, apply, vprop);
418                 } else if(_tosave == Save.readers) {
419                         long studyId = getOpenStudy().getIndex().longValue();
420                         _readers = getStudyService().getReaders(studyId);
421
422                         //Remove newly unchecked users
423                         if(_readers != null) {
424                                 for(UserDTO userDTO : _readers) {
425                                         if(_readerIds == null || !_readerIds.contains(userDTO.getIndex())) {
426                                                 getStudyService().removeReader(studyId, userDTO.getIndex());
427                                         }
428                                 }
429                         }
430                         
431                         //Add newly checked users
432                         if(_readerIds != null) {
433                                 for(Long userId : _readerIds) {
434                                         if(_readers == null) {
435                                                 getStudyService().addReader(studyId, userId);
436                                         } else {
437                                                 boolean contains = false;
438                                                 for(UserDTO userDTO : _readers) {
439                                                         if(userId.longValue() == userDTO.getIndex()) {
440                                                                 contains = true;
441                                                                 break;
442                                                         }
443                                                 }
444                                                 if(!contains) {
445                                                         getStudyService().addReader(studyId, userId);
446                                                 }
447                                         }
448                                 }
449                         }
450                         
451                         //Update OpenStudy
452                         _openStudy.open(getConnectedUser(), getStudyService().selectStudy(_openStudy.getIndex()));
453                 }
454
455                 doInitialize(); // Re-initialization following the above edition
456
457                 initializationFullScreenContext(Constants.STUDY_MENU,
458                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
459                                 Constants.OPEN);
460
461                 return SUCCESS;
462         }
463
464         // ==============================================================================================================================
465         // Getters
466         // ==============================================================================================================================
467
468         public User getAuthor() {
469                 return _openStudy.getStudyObject().getAuthor();
470         }
471
472         public List<User> getCandidates() {
473                 return _staff;
474         }
475
476         public String getCycle() {
477                 return _edicycle;
478         }
479
480         public List<User> getContributors() {
481                 return _member;
482         }
483
484         public ValidationFacade getDefaultValidation() {
485                 return _validefault;
486         }
487
488         public long getDocumentTypeIndex() {
489                 return _type;
490         }
491
492         public List<DocumentType> getOtherDocumentTypes() {
493                 return _other;
494         }
495
496         public String getStudyTitle() {
497                 return _openStudy.getTitle();
498         }
499
500         public List<ValidationFacade> getValidations() {
501                 return _validation;
502         }
503
504         public List<Name> getValidationActors() {
505                 return _validor;
506         }
507
508         // ==============================================================================================================================
509         // Setters
510         // ==============================================================================================================================
511
512         public void setCandidates(final String indices) {
513                 _candidates = indices;
514         }
515
516         public void setCycle(final String type) {
517                 _edicycle = type;
518         }
519
520         public void setMembers(final String indices) {
521                 _contributors = indices;
522         }
523
524         public void setDocumentType(final String index) {
525                 _type = Integer.valueOf(index);
526         }
527
528         public void setApprover(final String index) {
529                 _approver = Integer.valueOf(index);
530         }
531
532         public void setPublisher(final String index) {
533                 _publisher = Integer.valueOf(index);
534         }
535
536         public void setReviewer(final String index) {
537                 _reviewer = Integer.valueOf(index);
538         }
539
540         public void setTitle(final String title) {
541                 _stitle = title;
542         }
543
544         public void setSaveTitle(final String save) {
545                 _tosave = Save.title;
546         }
547
548         public void setSaveContributors(final String save) {
549                 _tosave = Save.contributor;
550         }
551
552         public void setSaveCycle(final String save) {
553                 _tosave = Save.cycle;
554         }
555
556         /**
557          * Set tosave to readers.
558          * @param save the save
559          */
560         public void setSaveReaders(final String save) {
561                 _tosave = Save.readers;
562         }
563         
564         /**
565          * Get the documentTypeService.
566          * 
567          * @return the documentTypeService
568          */
569         public DocumentTypeService getDocumentTypeService() {
570                 return _documentTypeService;
571         }
572
573         /**
574          * Set the documentTypeService.
575          * 
576          * @param documentTypeService
577          *            the documentTypeService to set
578          */
579         public void setDocumentTypeService(
580                         final DocumentTypeService documentTypeService) {
581                 _documentTypeService = documentTypeService;
582         }
583
584         /**
585          * Get the userService.
586          * 
587          * @return the userService
588          */
589         public UserService getUserService() {
590                 return _userService;
591         }
592
593         /**
594          * Set the userService.
595          * 
596          * @param userService
597          *            the userService to set
598          */
599         public void setUserService(final UserService userService) {
600                 _userService = userService;
601         }
602
603         /**
604          * Get the readers.
605          * @return the readers
606          */
607         public List<UserDTO> getReaders() {
608                 return _readers;
609         }
610
611         /**
612          * Set the readers.
613          * @param readers the readers to set
614          */
615         public void setReaders(final List<UserDTO> readers) {
616                 _readers = readers;
617         }
618
619         /**
620          * Get the readerIds.
621          * @return the readerIds
622          */
623         public List<Long> getReaderIds() {
624                 return _readerIds;
625         }
626
627         /**
628          * Set the readerIds.
629          * @param readerIds the readerIds to set
630          */
631         public void setReaderIds(final List<Long> readerIds) {
632                 this._readerIds = readerIds;
633         }
634 }