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