Salome HOME
Fix: document removing is fixed. Unit tests for StudyDAO and StepService.removeDocume...
[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                                 LOG.error(e.getMessage(), e);
289                         }
290                 } else if (_tosave == Save.contributor) {
291
292                         // Edition of contributors
293                         if (_contributors == null) {
294                                 _contributors = "";
295                         }
296                         if (_candidates == null) {
297                                 _candidates = "";
298                         }
299
300                         String[] parsekept = _contributors.split(",");
301                         String[] parsenew = _candidates.split(",");
302                         List<User> toremove = new ArrayList<User>(getStudyService()
303                                         .getContributors(study));
304
305                         for (int i = 0; i < parsekept.length; i++) {
306                                 if (parsekept[i].length() == 0) {
307                                         continue; // Yet no contributor
308                                 }
309                                 int index = Integer.valueOf(parsekept[i].trim());
310                                 for (Iterator<User> j = toremove.iterator(); j.hasNext();) {
311                                         long present = j.next().getIndex();
312                                         if (present != index) {
313                                                 continue;
314                                         }
315                                         j.remove();
316                                         break;
317                                 }
318                         }
319                         int size = toremove.size();
320                         if (size > 0) {
321                                 getStudyService().removeContributor(study,
322                                                 toremove.toArray(new User[size]));
323                         }
324
325                         for (int i = 0; i < parsenew.length; i++) {
326                                 if (parsenew[i].length() == 0) {
327                                         continue; // No any new contributor
328                                 }
329                                 int index = Integer.valueOf(parsenew[i].trim());
330                                 User newser = getUserService().selectUser(index);
331
332                                 getStudyService().addContributor(study, newser);
333                         }
334                 } else if (_tosave == Save.cycle) {
335
336                         // Addition of a document validation cycle
337                         DocumentType apply = getDocumentTypeService().selectType(_type);
338                         ValidationCycle.Properties vprop = new ValidationCycle.Properties();
339                         if (_publisher > 0) {
340                                 User actor = getUserService().selectUser(_publisher);
341                                 vprop.setActor(ValidationStep.PROMOTION, actor);
342                         }
343                         if (_reviewer > 0) {
344                                 User actor = getUserService().selectUser(_reviewer);
345                                 vprop.setActor(ValidationStep.REVIEW, actor);
346                         }
347                         if (_approver > 0) {
348                                 User actor = getUserService().selectUser(_approver);
349                                 vprop.setActor(ValidationStep.APPROVAL, actor);
350                         }
351                         getStudyService().setValidationCycle(study, apply, vprop);
352                 }
353
354                 doInitialize(); // Re-initialization following the above edition
355
356                 initializationFullScreenContext(Constants.STUDY_MENU,
357                                 Constants.STUDY_MENU, Constants.TRUE, Constants.BACK,
358                                 Constants.OPEN);
359
360                 return SUCCESS;
361         }
362
363         // ==============================================================================================================================
364         // Getters
365         // ==============================================================================================================================
366
367         public User getAuthor() {
368                 return _openStudy.getStudyObject().getAuthor();
369         }
370
371         public List<User> getCandidates() {
372                 return _staff;
373         }
374
375         public String getCycle() {
376                 return _edicycle;
377         }
378
379         public List<User> getContributors() {
380                 return _member;
381         }
382
383         public ValidationFacade getDefaultValidation() {
384                 return _validefault;
385         }
386
387         public long getDocumentTypeIndex() {
388                 return _type;
389         }
390
391         public List<DocumentType> getOtherDocumentTypes() {
392                 return _other;
393         }
394
395         public String getStudyTitle() {
396                 return _openStudy.getTitle();
397         }
398
399         public List<ValidationFacade> getValidations() {
400                 return _validation;
401         }
402
403         public List<Name> getValidationActors() {
404                 return _validor;
405         }
406
407         // ==============================================================================================================================
408         // Setters
409         // ==============================================================================================================================
410
411         public void setCandidates(final String indices) {
412                 _candidates = indices;
413         }
414
415         public void setCycle(final String type) {
416                 _edicycle = type;
417         }
418
419         public void setMembers(final String indices) {
420                 _contributors = indices;
421         }
422
423         public void setDocumentType(final String index) {
424                 _type = Integer.valueOf(index);
425         }
426
427         public void setApprover(final String index) {
428                 _approver = Integer.valueOf(index);
429         }
430
431         public void setPublisher(final String index) {
432                 _publisher = Integer.valueOf(index);
433         }
434
435         public void setReviewer(final String index) {
436                 _reviewer = Integer.valueOf(index);
437         }
438
439         public void setTitle(final String title) {
440                 _stitle = title;
441         }
442
443         public void setSaveTitle(final String save) {
444                 _tosave = Save.title;
445         }
446
447         public void setSaveContributors(final String save) {
448                 _tosave = Save.contributor;
449         }
450
451         public void setSaveCycle(final String save) {
452                 _tosave = Save.cycle;
453         }
454
455         /**
456          * Get the documentTypeService.
457          * 
458          * @return the documentTypeService
459          */
460         public DocumentTypeService getDocumentTypeService() {
461                 return _documentTypeService;
462         }
463
464         /**
465          * Set the documentTypeService.
466          * 
467          * @param documentTypeService
468          *            the documentTypeService to set
469          */
470         public void setDocumentTypeService(
471                         final DocumentTypeService documentTypeService) {
472                 _documentTypeService = documentTypeService;
473         }
474
475         /**
476          * Get the userService.
477          * 
478          * @return the userService
479          */
480         public UserService getUserService() {
481                 return _userService;
482         }
483
484         /**
485          * Set the userService.
486          * 
487          * @param userService
488          *            the userService to set
489          */
490         public void setUserService(final UserService userService) {
491                 _userService = userService;
492         }
493 }