1 package org.splat.simer;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
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;
25 * Edit/display study properties (study configuration) screen action.
27 public class StudyPropertiesAction extends DisplayStudyStepAction {
32 private static final long serialVersionUID = 4210696018741092900L;
34 // Presentation fields
36 * List of available users which are not yet included into contributors.
38 private transient List<User> _staff;
40 * Study contributors list.
42 private transient List<User> _member;
46 private transient List<Name> _validor;
48 * List of specific study validation cycles presentations.
50 private transient List<ValidationFacade> _validation;
52 * Default validation cycle presentation.
54 private transient ValidationFacade _validefault;
56 * List of document types which have no specific validation cycles for this study.
58 private transient List<DocumentType> _other;
62 * Edition action (title, contributors or cycle).
64 private transient Save _tosave;
66 * Document type name for the modified validation cycle.
68 private transient String _edicycle;
72 private transient String _stitle;
74 * List of existing contributors, some of them may have been removed.
76 private transient String _contributors;
78 * List of added contributors.
80 private transient String _candidates;
82 * Type of document to be included in the validation process.
84 private transient long _type;
86 * Validation cycle publisher's id.
88 private transient long _publisher;
90 * Validation cycle reviewer's id.
92 private transient long _reviewer;
94 * Validation cycle approver's id.
96 private transient long _approver;
99 * Injected document type service.
101 private DocumentTypeService _documentTypeService;
104 * Injected user service.
106 private UserService _userService;
109 * Study readers list.
111 private List<UserDTO> _readers;
114 * Study reader id list.
116 private List<Long> _readerIds;
119 * Save operation type enumeration pointing which section of properties has been edited.
131 * Save validation cycle.
135 * Save study readers.
140 // ==============================================================================================================================
142 // ==============================================================================================================================
145 * Initialize study properties action.
147 * @return "edit" if user can edit study properties, otherwise return "display"
149 public String doInitialize() {
151 _openStudy = getOpenStudy();
152 Study study = _openStudy.getStudyObject();
160 StudyRights user = _openStudy.getStudyRights();
161 if (_openStudy.isOpenForWriting() && user.canEditProperties()) {
168 _readers = getStudyService().getReaders(
169 getOpenStudy().getIndex().longValue());
170 } catch (InvalidParameterException e) {
171 LOG.error(e.getMessage(), e);
177 * Initialize common study data presented on the screen.
182 private void initData(final Study study) {
183 _validation = new ArrayList<ValidationFacade>();
185 // RKV: _other = getDocumentTypeService().selectResultTypes();
186 _other = getDocumentTypeService().selectAllTypes(); // RKV
188 for (Iterator<DocumentType> i = _other.iterator(); i.hasNext();) {
189 DocumentType type = i.next();
190 ValidationCycle cycle = getStudyService().getValidationCycleOf(
192 if (cycle.isDefault()) {
193 if (_validefault == null) {
194 _validefault = new ValidationFacade(cycle,
195 getApplicationSettings().getCurrentLocale());
198 if (type.getName().equals(_edicycle)) {
199 this._type = type.getIndex();
201 _validation.add(new ValidationFacade(cycle,
202 getApplicationSettings().getCurrentLocale()));
206 if (_validefault != null) {
207 _validation.add(_validefault); // In order to be at the end
210 _member = getStudyService().getContributors(study);
212 initializationFullScreenContext(Constants.STUDY_MENU,
213 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
218 * Prepare study title editing.
222 public String doEditTitle() {
224 _openStudy = getOpenStudy();
226 initData(_openStudy.getStudyObject());
233 _readers = getStudyService().getReaders(
234 getOpenStudy().getIndex().longValue());
235 } catch (InvalidParameterException e) {
236 LOG.error(e.getMessage(), e);
238 setActionType("edititle");
243 * Prepare contributors list editing for the selected study.
247 public String doEditContributors() {
249 _openStudy = getOpenStudy();
250 Study study = _openStudy.getStudyObject();
254 _staff = getUserService().selectAllUsers();
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()) {
268 _readers = getStudyService().getReaders(
269 getOpenStudy().getIndex().longValue());
270 } catch (InvalidParameterException e) {
271 LOG.error(e.getMessage(), e);
274 removeValidCycleFromStaff();
275 setActionType("edibutor");
280 * Initialize edit readers screen.
284 public String doInitEditReaders() {
287 _staff = getUserService().selectAllUsers();
289 // remove user from potential readers
290 _staff.remove(getConnectedUser());
292 // remove contributors from potential readers
293 if (_member != null) {
294 _staff.removeAll(_member);
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));
302 _staff.removeAll(readers);
304 removeValidCycleFromStaff();
305 setActionType("editReaders");
311 * Remove validation cycle members from potential readers or contributors.
313 private void removeValidCycleFromStaff() {
315 for(DocumentType type : _documentTypeService.selectAllTypes()) {
316 User[] users = getStudyService()
317 .getValidationCycleOf(getOpenStudy().getStudyObject(), type).getAllActors();
318 _staff.removeAll(Arrays.asList(users));
324 * Add or edit a document validation cycle for the selected study.
328 public String doEditCycle() {
330 _openStudy = getOpenStudy();
331 Study study = _openStudy.getStudyObject();
335 _validor = new ArrayList<Name>();
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()));
352 _readers = getStudyService().getReaders(
353 getOpenStudy().getIndex().longValue());
354 } catch (InvalidParameterException e) {
355 LOG.error(e.getMessage(), e);
357 setActionType("edicycle");
362 * Save modifications.
365 * @throws InvalidParameterException
366 * if can't modify readers list
368 public String doEdition() throws InvalidParameterException {
369 Study study = getOpenStudy().getStudyObject();
371 if (_tosave == Save.title) {
373 // Edition of the title
374 Study.Properties sprop = new Study.Properties();
376 getStudyService().update(study, sprop.setTitle(_stitle));
377 } catch (InvalidPropertyException e) {
378 LOG.error(e.getMessage(), e);
380 } else if (_tosave == Save.contributor) {
381 saveContributor(study);
383 } else if (_tosave == Save.cycle) {
384 saveValidationCycle(study);
386 } else if (_tosave == Save.readers) {
390 doInitialize(); // Re-initialization following the above edition
396 * Save the study readers list.
400 * @throws InvalidParameterException
401 * if the study readers list modification is failed
403 private void saveReaders(final Study study)
404 throws InvalidParameterException {
405 long studyId = getOpenStudy().getIndex().longValue();
406 _readers = getStudyService().getReaders(studyId);
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());
418 // Add newly checked users
419 if (_readerIds != null) {
420 for (Long userId : _readerIds) {
421 if (_readers == null) {
422 getStudyService().addReader(studyId, userId);
424 boolean contains = false;
425 for (UserDTO userDTO : _readers) {
426 if (userId.longValue() == userDTO.getIndex()) {
432 getStudyService().addReader(studyId, userId);
439 _openStudy.open(getConnectedUser(), getStudyService().selectStudy(
440 _openStudy.getIndex()));
444 * Save the study document validation cycle.
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);
458 User actor = getUserService().selectUser(_reviewer);
459 vprop.setActor(ValidationStep.REVIEW, actor);
462 User actor = getUserService().selectUser(_approver);
463 vprop.setActor(ValidationStep.APPROVAL, actor);
465 getStudyService().setValidationCycle(study, apply, vprop);
469 * Save the study contributors list.
474 private void saveContributor(final Study study) {
475 // Edition of contributors
476 if (_contributors == null) {
479 if (_candidates == null) {
483 String[] parsekept = _contributors.split(",");
484 String[] parsenew = _candidates.split(",");
485 List<User> toremove = new ArrayList<User>(getStudyService()
486 .getContributors(study));
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) {
500 int size = toremove.size();
502 getStudyService().removeContributor(study,
503 toremove.toArray(new User[size]));
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);
511 getStudyService().addContributor(study, newser);
517 * Get the study author.
519 * @return the study author
521 public User getAuthor() {
522 return _openStudy.getStudyObject().getAuthor();
526 * Get list of available candidates.
528 * @return list of candidates
530 public List<User> getCandidates() {
535 * Get the document type name of the modified validation cycle.
537 * @return the type name of the modified cycle
539 public String getCycle() {
544 * Get study contributors list.
546 * @return list of contributors
548 public List<User> getContributors() {
553 * Get default validation cycle presentation.
555 * @return default validation cycle presentation
557 public ValidationFacade getDefaultValidation() {
562 * Get selected document type id.
564 * @return the selected document type id
566 public long getDocumentTypeIndex() {
571 * Get list of document types which have no specific validation cycles for this study.
573 * @return list of document types
575 public List<DocumentType> getOtherDocumentTypes() {
582 * @return the study title
584 public String getStudyTitle() {
585 return _openStudy.getTitle();
589 * Get the list of specific study validation cycles presentations.
591 * @return list of validation cycles facades
593 public List<ValidationFacade> getValidations() {
598 * Get the list of validators.
600 * @return list of user names
602 public List<Name> getValidationActors() {
607 * Set the list of candidates ids.
610 * the list of users ids
612 public void setCandidates(final String indices) {
613 _candidates = indices;
617 * Set the document type name for the modified validation cycle.
620 * the document type name
622 public void setCycle(final String type) {
627 * Set the list of contributors ids.
630 * the list of users ids
632 public void setMembers(final String indices) {
633 _contributors = indices;
637 * Set the id of the selected document type.
642 public void setDocumentType(final String index) {
643 _type = Long.valueOf(index);
647 * Set the selected approver id.
652 public void setApprover(final String index) {
653 _approver = Long.valueOf(index);
657 * Set the selected publisher id.
662 public void setPublisher(final String index) {
663 _publisher = Long.valueOf(index);
667 * Set the selected reviewer id.
672 public void setReviewer(final String index) {
673 _reviewer = Long.valueOf(index);
677 * Set the study title.
682 public void setTitle(final String title) {
687 * Set the current operation to title saving.
692 public void setSaveTitle(final String save) {
693 _tosave = Save.title;
697 * Set the current operation to contributors saving.
702 public void setSaveContributors(final String save) {
703 _tosave = Save.contributor;
707 * Set the current operation to validation cycle saving.
712 public void setSaveCycle(final String save) {
713 _tosave = Save.cycle;
717 * Set the current operation to readers saving.
722 public void setSaveReaders(final String save) {
723 _tosave = Save.readers;
727 * Get the documentTypeService.
729 * @return the documentTypeService
731 public DocumentTypeService getDocumentTypeService() {
732 return _documentTypeService;
736 * Set the documentTypeService.
738 * @param documentTypeService
739 * the documentTypeService to set
741 public void setDocumentTypeService(
742 final DocumentTypeService documentTypeService) {
743 _documentTypeService = documentTypeService;
747 * Get the userService.
749 * @return the userService
751 public UserService getUserService() {
756 * Set the userService.
759 * the userService to set
761 public void setUserService(final UserService userService) {
762 _userService = userService;
768 * @return the readers
770 public List<UserDTO> getReaders() {
780 public void setReaders(final List<UserDTO> readers) {
785 * Get the list of readers ids.
787 * @return the list of users ids
789 public List<Long> getReaderIds() {
794 * Set the list of readers ids.
797 * the list of users ids to set
799 public void setReaderIds(final List<Long> readerIds) {
800 this._readerIds = readerIds;