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