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