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