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