Salome HOME
New study creation is fixed. Search page is also accessible now.
[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.StudyService;
14 import org.splat.som.ApplicationRights;
15 import org.splat.dal.dao.som.Database;
16 import org.splat.dal.bo.som.Document;
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 public class StudyPropertiesAction extends DisplayStudyStepAction {
25
26 //  Presentation fields
27     private List<User>             staff;
28     private List<User>             member;
29     private List<Name>             validor;
30     private List<ValidationFacade> validation;
31     private ValidationFacade       validefault;
32     private List<DocumentType>     other;
33
34 //  User input fields
35     private Save                   tosave;         // Edition action (title, contributors or cycle)
36     private String                 edicycle;
37     private String                 stitle;         // Title of the study
38     private String                 contributors;   // List of existing contributors, some of them may have been removed
39     private String                 candidates;     // List of added contributors
40     private int                    type;           // Type of document to be included in the validation process
41     private int                    publisher;
42     private int                    reviewer;
43     private int                    approver;
44         private StudyService _studyService;
45
46         private static final long serialVersionUID = 4210696018741092900L;
47
48     private enum Save { title, contributor, cycle }
49
50 //  ==============================================================================================================================
51 //  Action methods
52 //  ==============================================================================================================================
53
54         public String doInitialize () {
55 //  -----------------------------
56       Session      connex  = Database.getSession();
57       Transaction  transax = connex.beginTransaction();
58
59       mystudy     = getOpenStudy();
60       validation  = new Vector<ValidationFacade>();
61       validefault = null;
62           other       = Document.selectResultTypes();
63
64           Study        study = mystudy.getStudyObject();
65           StudyRights  user  = mystudy.getStudyRights();
66       for (Iterator<DocumentType> i=other.iterator(); i.hasNext(); ) {
67         DocumentType    type  = i.next();
68         ValidationCycle cycle = study.getValidationCycleOf(type);
69         if (cycle.isDefault()) {
70           validefault = new ValidationFacade(cycle);
71           continue;
72         }
73         validation.add( new ValidationFacade(cycle) );
74         i.remove();
75       }
76           if (validefault != null) validation.add(validefault);   // In order to be at the end
77       member  = study.getContributors();
78       staff   = null;
79       validor = null;
80
81       transax.commit();
82       if (mystudy.isOpenForWriting() && user.canEditProperties()) return "edit";
83       else return "display";
84     }
85
86         public String doEditTitle () {
87 //  ----------------------------
88       Session      connex  = Database.getSession();
89       Transaction  transax = connex.beginTransaction();
90
91       mystudy     = getOpenStudy();
92       validation  = new Vector<ValidationFacade>();
93       validefault = null;
94           other       = Document.selectResultTypes();
95
96           Study study = mystudy.getStudyObject();
97       for (Iterator<DocumentType> i=other.iterator(); i.hasNext(); ) {
98         DocumentType    type  = i.next();
99         ValidationCycle cycle = study.getValidationCycleOf(type);
100         if (cycle.isDefault()) {
101           validefault = new ValidationFacade(cycle);
102           continue;
103         }
104         validation.add( new ValidationFacade(cycle) );
105       }
106           if (validefault != null) validation.add(validefault);   // In order to be at the end
107       member  = study.getContributors();
108       staff   = null;
109       validor = null;
110       other   = null;
111
112       transax.commit();
113       return SUCCESS;
114     }
115
116         public String doEditContributors () {
117 //  -----------------------------------
118       Session      connex  = Database.getSession();
119       Transaction  transax = connex.beginTransaction();
120
121       mystudy     = getOpenStudy();
122       validation  = new Vector<ValidationFacade>();
123       validefault = null;
124           other       = Document.selectAllTypes();
125
126           Study study = mystudy.getStudyObject();
127       for (Iterator<DocumentType> i=other.iterator(); i.hasNext(); ) {
128         DocumentType    type  = i.next();
129         ValidationCycle cycle = study.getValidationCycleOf(type);
130         if (cycle.isDefault()) {
131           validefault = new ValidationFacade(cycle);
132           continue;
133         }
134         validation.add( new ValidationFacade(cycle) );
135       }
136           if (validefault != null) validation.add(validefault);   // In order to be at the end
137       member  = study.getContributors();
138       staff   = UserDirectory.selectAllUsers();
139       validor = null;
140       other   = null;
141       User me = this.getConnectedUser();
142       for (Iterator<User> i=staff.iterator(); i.hasNext(); ) {
143         User              next = i.next();
144         ApplicationRights he   = new ApplicationRights(next);
145         if (next.equals(me) || member.contains(next) || !he.canContributeToStudy()) i.remove();
146       }
147       transax.commit();
148       return SUCCESS;
149     }
150
151         public String doEditCycle () {
152 //  ----------------------------
153       Session      connex  = Database.getSession();
154       Transaction  transax = connex.beginTransaction();
155
156       mystudy     = getOpenStudy();
157       validation  = new Vector<ValidationFacade>();
158       validefault = null;
159           other       = Document.selectResultTypes();
160
161           Study study = mystudy.getStudyObject();
162       for (Iterator<DocumentType> i=other.iterator(); i.hasNext(); ) {
163         DocumentType    type  = i.next();
164         ValidationCycle cycle = study.getValidationCycleOf(type);
165         if (cycle.isDefault()) {
166           validefault = new ValidationFacade(cycle);
167           continue;
168         }
169         if (type.getName().equals(edicycle)) {
170           this.type = type.getIndex();
171         }
172         validation.add( new ValidationFacade(cycle) );
173         i.remove();
174       }
175           if (validefault != null) validation.add(validefault);   // In order to be at the end
176       member  = study.getContributors();
177       validor = new Vector<Name>();
178       staff   = null;
179       List<User>            user = UserDirectory.selectAllUsers();
180       for (Iterator<User> i=user.iterator(); i.hasNext(); ) {
181         User              next = i.next();
182         ApplicationRights he   = new ApplicationRights(next);
183         if (he.canValidate()) {
184           if (next.equals(study.getAuthor())) validor.add( new ValidationFacade.ByManager(next) );
185           else                                validor.add(next);
186         }
187       }
188       transax.commit();
189       return SUCCESS;
190     }
191
192     public String doEdition () {
193 //  --------------------------
194       Session      connex  = Database.getSession();
195       Transaction  transax = connex.beginTransaction();
196       Study        study   = getOpenStudy().getStudyObject();
197
198       if (tosave == Save.title) {
199
200 //      Edition of the title
201         Study.Properties sprop = new Study.Properties();
202         try {
203                 getStudyService().update(study, sprop.setTitle(stitle));
204         }
205         catch (InvalidPropertyException e) {
206 //TODO
207         }
208       } else
209       if (tosave == Save.contributor) {
210
211 //      Edition of contributors
212         if (contributors == null) contributors = "";
213         if (candidates   == null) candidates   = "";
214
215         String[]     parsekept = contributors.split(",");
216         String[]     parsenew  = candidates.split(",");
217         Vector<User> toremove  = new Vector<User>(study.getContributors());
218
219         for (int i=0; i<parsekept.length; i++) {
220           if (parsekept[i].length() == 0) continue;   // Yet no contributor
221           int  index = Integer.valueOf(parsekept[i].trim());
222           for (Iterator<User> j=toremove.iterator(); j.hasNext(); ){
223             int present = j.next().getIndex();
224             if (present != index) continue;
225             j.remove();
226             break;
227           }
228         }
229         int size = toremove.size();
230         if (size > 0) getStudyService().removeContributor(study, toremove.toArray(new User[size]));
231
232         for (int i=0; i<parsenew.length; i++) {
233           if (parsenew[i].length() == 0) continue;    // No any new contributor
234           int  index  = Integer.valueOf(parsenew[i].trim());
235           User newser = UserDirectory.selectUser(index);
236
237           getStudyService().addContributor(study, newser);
238         }
239       } else
240       if (tosave == Save.cycle) {
241
242 //      Addition of a document validation cycle
243         DocumentType               apply = Document.selectType(type);
244         ValidationCycle.Properties vprop = new ValidationCycle.Properties();
245         if (publisher > 0) {
246           User actor = UserDirectory.selectUser(publisher);
247           vprop.setActor(ValidationStep.PROMOTION, actor);
248         }
249         if (reviewer > 0) {
250           User actor = UserDirectory.selectUser(reviewer);
251           vprop.setActor(ValidationStep.REVIEW, actor);
252         }
253         if (approver > 0) {
254           User actor = UserDirectory.selectUser(approver);
255           vprop.setActor(ValidationStep.APPROVAL, actor);
256         }
257         getStudyService().setValidationCycle(study, apply, vprop);
258       }
259       transax.commit();
260
261       doInitialize();     // Re-initialization following the above edition
262       return SUCCESS;
263         }
264
265 //  ==============================================================================================================================
266 //  Getters
267 //  ==============================================================================================================================
268
269     public User getAuthor () {
270 //  ------------------------
271       return mystudy.getStudyObject().getAuthor();
272     }
273     public List<User> getCandidates () {
274 //  ----------------------------------
275       return staff;
276     }
277     public String getCycle () {
278 //  -------------------------
279       return edicycle;
280     }
281     public List<User> getContributors () {
282 //  ------------------------------------
283       return member;
284     }
285     public ValidationFacade getDefaultValidation () {
286 //  -----------------------------------------------
287       return validefault;
288     }
289     public int getDocumentTypeIndex () {
290 //  ----------------------------------
291       return type;
292     }
293     public List<DocumentType> getOtherDocumentTypes () {
294 //  --------------------------------------------------
295       return other;
296     }
297     public String getStudyTitle () {
298 //  ------------------------------
299       return mystudy.getTitle();
300     }
301     public List<ValidationFacade> getValidations () {
302 //  -----------------------------------------------
303       return validation;
304     }
305     public List<Name> getValidationActors () {
306 //  ----------------------------------------
307       return validor;
308     }
309
310 //  ==============================================================================================================================
311 //  Setters
312 //  ==============================================================================================================================
313
314     public void setCandidates (String indices) {
315 //  ------------------------------------------
316       candidates = indices;
317     }
318     public void setCycle (String type) {
319 //  ----------------------------------
320       edicycle = type;
321     }
322     public void setMembers (String indices) {
323 //  ---------------------------------------
324       contributors = indices;
325     }
326     public void setDocumentType (String index) {
327 //  ------------------------------------------
328       type = Integer.valueOf(index);
329     }
330     public void setApprover (String index) {
331 //  --------------------------------------
332       approver = Integer.valueOf(index);
333     }
334     public void setPublisher (String index) {
335 //  ---------------------------------------
336       publisher = Integer.valueOf(index);
337     }
338     public void setReviewer (String index) {
339 //  --------------------------------------
340       reviewer = Integer.valueOf(index);
341     }
342     public void setTitle (String title) {
343 //  -----------------------------------
344       stitle = title;
345     }
346     public void setSaveTitle (String save) {
347 //  --------------------------------------
348       tosave = Save.title;
349     }
350     public void setSaveContributors (String save) {
351 //  ---------------------------------------------
352       tosave = Save.contributor;
353     }
354     public void setSaveCycle (String save) {
355 //  --------------------------------------
356       tosave = Save.cycle;
357     }
358         /**
359          * Get the studyService.
360          * 
361          * @return the studyService
362          */
363         public StudyService getStudyService() {
364                 return _studyService;
365         }
366
367         /**
368          * Set the studyService.
369          * 
370          * @param studyService
371          *            the studyService to set
372          */
373         public void setStudyService(StudyService studyService) {
374                 _studyService = studyService;
375         }
376 }