1 package org.splat.simer;
4 import java.util.Calendar;
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;
17 public class EditDocumentAction extends DisplayStudyStepAction {
19 private String index = null;
20 private String title = null;
21 private String filename = null;
23 private static final long serialVersionUID = 4573036736137033679L;
25 private enum Execute { renameDocument, accept, promote, demote, review, invalidate, approve, disapprove };
27 // ==============================================================================================================================
29 // ==============================================================================================================================
31 public String doInitialize () {
32 // -----------------------------
33 // Session connex = Database.getSession();
34 // Transaction transax = connex.beginTransaction();
36 mystudy = getOpenStudy();
42 public String doSetDocument () {
43 // ------------------------------
44 Session connex = Database.getSession();
45 Transaction transax = connex.beginTransaction();
47 mystudy = getOpenStudy();
49 Execute todo = Execute.valueOf(action);
50 Step step = mystudy.getSelectedStep();
51 Publication doc = step.getDocument(Integer.valueOf(index));
53 if (todo == Execute.renameDocument) {
55 // Useless to update the document presentation
57 if (todo == Execute.accept) {
61 if (todo == Execute.promote) {
62 doc.promote(Calendar.getInstance().getTime());
65 if (todo == Execute.demote) {
69 if (todo == Execute.review) {
70 doc.review(Calendar.getInstance().getTime());
73 if (todo == Execute.invalidate) {
77 if (todo == Execute.approve) {
78 doc.approve(Calendar.getInstance().getTime());
80 mystudy.getMenu().refreshSelectedItem(); // Updates the menu icon, in case of other documents in approved state
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
91 } catch (HibernateException backerror) {
92 logger.debug("Error rolling back transaction", backerror);
97 catch (InvalidPropertyException error) {
103 public String doAttach () {
104 // -------------------------
105 Session connex = Database.getSession();
106 Transaction transax = connex.beginTransaction();
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");
116 Publication edited = step.getDocument(Integer.valueOf(index));
117 ConvertsRelation export = edited.attach(parse[parse.length-1]);
119 if (logger.isInfoEnabled()) logger.info("Moving \"" + upfile.getName() + "\" to \"" + updir.getPath() + "\".");
120 upfile.renameTo(export.getTo().asFile());
122 mystudy.update(edited);
126 catch (Exception error) {
127 logger.error("Reason:", error);
132 public String doDeleteDocument () {
133 // ---------------------------------
134 Session connex = Database.getSession();
135 Transaction transax = connex.beginTransaction();
137 mystudy = getOpenStudy();
139 Step step = mystudy.getSelectedStep();
140 Publication doctag = step.getDocument(Integer.valueOf(index));
142 step.removeDocument(doctag); // Updates the data structure
145 mystudy.remove(doctag); // Updates the presentation
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
154 } catch (HibernateException backerror) {
155 logger.debug("Error rolling back transaction", backerror);
162 // ==============================================================================================================================
163 // Getters and setters
164 // ==============================================================================================================================
166 public void setDocumentTitle (String title) {
167 // -------------------------------------------
170 public void setFileName (String filename) {
171 // -----------------------------------------
172 this.filename = filename;
174 public void setIndex (String index) {
175 // -----------------------------------