Salome HOME
80d1dc71f151129264cd7f1cbae779a5197deceb
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / EditDocumentAction.java
1 package org.splat.simer;
2
3 import java.io.File;
4 import java.util.Calendar;
5
6 import org.hibernate.HibernateException;
7 import org.hibernate.Session;
8 import org.hibernate.Transaction;
9 import org.splat.kernel.InvalidPropertyException;
10 import org.splat.dal.bo.kernel.User;
11 import org.splat.dal.dao.som.Database;
12 import org.splat.dal.bo.som.Publication;
13 import org.splat.dal.bo.som.ConvertsRelation;
14 import org.splat.service.PublicationService;
15 import org.splat.som.Step;
16
17
18 public class EditDocumentAction extends DisplayStudyStepAction {
19
20     private String  index    = null;
21         private String  title    = null;
22     private String  filename = null;
23         private PublicationService _publicationService;
24
25         private static final long serialVersionUID = 4573036736137033679L;
26
27         private enum Execute { renameDocument, accept, promote, demote, review, invalidate, approve, disapprove };
28
29 //  ==============================================================================================================================
30 //  Action methods
31 //  ==============================================================================================================================
32
33     public String doInitialize () {
34 //  -----------------------------
35 //    Session      connex  = Database.getSession();
36 //        Transaction  transax = connex.beginTransaction();
37
38           mystudy = getOpenStudy();
39         
40 //    transax.commit();
41       return SUCCESS;
42     }
43
44     public String doSetDocument () {
45 //  ------------------------------
46       Session      connex  = Database.getSession();
47       Transaction  transax = connex.beginTransaction();
48       try {
49             mystudy = getOpenStudy();
50
51         Execute      todo = Execute.valueOf(action);
52         Step         step = mystudy.getSelectedStep();
53         Publication  doc  = step.getDocument(Integer.valueOf(index));
54         
55         if (todo == Execute.renameDocument) {
56           doc.rename(title);
57 //        Useless to update the document presentation
58         } else
59         if (todo == Execute.accept) {
60           doc.actualize();
61           mystudy.update(doc);
62         } else
63         if (todo == Execute.promote) {
64                 getPublicationService().promote(doc, Calendar.getInstance().getTime());
65           mystudy.update(doc);
66         } else
67         if (todo == Execute.demote) {
68                 getPublicationService().demote(doc);
69           mystudy.update(doc);
70         } else
71         if (todo == Execute.review) {
72                 getPublicationService().review(doc, Calendar.getInstance().getTime());
73           mystudy.update(doc);
74         } else
75         if (todo == Execute.invalidate) {
76                 getPublicationService().invalidate(doc);
77           mystudy.update(doc);
78         } else
79         if (todo == Execute.approve) {
80                 getPublicationService().approve(doc, Calendar.getInstance().getTime());
81           mystudy.update(doc);
82           mystudy.getMenu().refreshSelectedItem();   // Updates the menu icon, in case of other documents in approved state
83         }
84         transax.commit();
85         return SUCCESS;
86           }
87       catch (RuntimeException saverror) {
88         logger.error("Reason:", saverror);
89         if (transax != null && transax.isActive()) {
90 //        Second try-catch as the rollback could fail as well
91           try {
92                 transax.rollback();
93           } catch (HibernateException backerror) {
94             logger.debug("Error rolling back transaction", backerror);
95           }
96         }
97         return ERROR;
98           }
99       catch (InvalidPropertyException error) {
100         transax.commit();
101         return INPUT;
102       }
103     }
104
105     public String doAttach () {
106 //  -------------------------
107       Session      connex  = Database.getSession();
108           Transaction  transax = connex.beginTransaction();
109       try {
110 //      Getting user inputs
111                  mystudy = getOpenStudy();
112         User     user    = getConnectedUser();
113         Step     step    = mystudy.getSelectedStep();
114         File     updir   = Database.getDownloadDirectory(user);
115         File     upfile  = new File(updir.getPath() + "/" + filename);
116         String[] parse   = filename.split("\\x2E");
117
118         Publication       edited = step.getDocument(Integer.valueOf(index));
119         ConvertsRelation  export = edited.attach(parse[parse.length-1]);
120
121                 if (logger.isInfoEnabled()) logger.info("Moving \"" + upfile.getName() + "\" to \"" + updir.getPath() + "\".");
122         upfile.renameTo(export.getTo().asFile());
123
124         mystudy.update(edited);
125         transax.commit();
126         return  SUCCESS;
127       }
128       catch (Exception error) {
129         logger.error("Reason:", error);
130         return ERROR;
131       }
132     }
133
134     public String doDeleteDocument () {
135 //  ---------------------------------
136       Session     connex  = Database.getSession();
137           Transaction transax = connex.beginTransaction();
138           try {
139                 mystudy = getOpenStudy();
140            
141         Step         step   = mystudy.getSelectedStep();
142         Publication  doctag = step.getDocument(Integer.valueOf(index));
143
144         step.removeDocument(doctag);   // Updates the data structure
145         transax.commit();
146
147         mystudy.remove(doctag);        // Updates the presentation
148         return SUCCESS;
149           }
150       catch (RuntimeException saverror) {
151         logger.error("Reason:", saverror);
152         if (transax != null && transax.isActive()) {
153 //        Second try-catch as the rollback could fail as well
154           try {
155                 transax.rollback();
156           } catch (HibernateException backerror) {
157             logger.debug("Error rolling back transaction", backerror);
158           }
159         }
160         return ERROR;
161           }
162     }
163
164 //  ==============================================================================================================================
165 //  Getters and setters
166 //  ==============================================================================================================================
167
168     public void setDocumentTitle (String title) {
169 //  -------------------------------------------
170       this.title = title;
171     }
172     public void setFileName (String filename) {
173 //  -----------------------------------------
174       this.filename = filename;
175     }
176     public void setIndex (String index) {
177 //  -----------------------------------
178       this.index = index;
179     }
180         /**
181          * Get the publicationService.
182          * 
183          * @return the publicationService
184          */
185         public PublicationService getPublicationService() {
186                 return _publicationService;
187         }
188
189         /**
190          * Set the publicationService.
191          * 
192          * @param publicationService
193          *            the publicationService to set
194          */
195         public void setPublicationService(PublicationService publicationService) {
196                 _publicationService = publicationService;
197         }
198 }