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.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;
18 public class EditDocumentAction extends DisplayStudyStepAction {
20 private String index = null;
21 private String title = null;
22 private String filename = null;
23 private PublicationService _publicationService;
25 private static final long serialVersionUID = 4573036736137033679L;
27 private enum Execute { renameDocument, accept, promote, demote, review, invalidate, approve, disapprove };
29 // ==============================================================================================================================
31 // ==============================================================================================================================
33 public String doInitialize () {
34 // -----------------------------
35 // Session connex = Database.getSession();
36 // Transaction transax = connex.beginTransaction();
38 mystudy = getOpenStudy();
44 public String doSetDocument () {
45 // ------------------------------
46 Session connex = Database.getSession();
47 Transaction transax = connex.beginTransaction();
49 mystudy = getOpenStudy();
51 Execute todo = Execute.valueOf(action);
52 Step step = mystudy.getSelectedStep();
53 Publication doc = step.getDocument(Integer.valueOf(index));
55 if (todo == Execute.renameDocument) {
57 // Useless to update the document presentation
59 if (todo == Execute.accept) {
63 if (todo == Execute.promote) {
64 getPublicationService().promote(doc, Calendar.getInstance().getTime());
67 if (todo == Execute.demote) {
68 getPublicationService().demote(doc);
71 if (todo == Execute.review) {
72 getPublicationService().review(doc, Calendar.getInstance().getTime());
75 if (todo == Execute.invalidate) {
76 getPublicationService().invalidate(doc);
79 if (todo == Execute.approve) {
80 getPublicationService().approve(doc, Calendar.getInstance().getTime());
82 mystudy.getMenu().refreshSelectedItem(); // Updates the menu icon, in case of other documents in approved state
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
93 } catch (HibernateException backerror) {
94 logger.debug("Error rolling back transaction", backerror);
99 catch (InvalidPropertyException error) {
105 public String doAttach () {
106 // -------------------------
107 Session connex = Database.getSession();
108 Transaction transax = connex.beginTransaction();
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");
118 Publication edited = step.getDocument(Integer.valueOf(index));
119 ConvertsRelation export = edited.attach(parse[parse.length-1]);
121 if (logger.isInfoEnabled()) logger.info("Moving \"" + upfile.getName() + "\" to \"" + updir.getPath() + "\".");
122 upfile.renameTo(export.getTo().asFile());
124 mystudy.update(edited);
128 catch (Exception error) {
129 logger.error("Reason:", error);
134 public String doDeleteDocument () {
135 // ---------------------------------
136 Session connex = Database.getSession();
137 Transaction transax = connex.beginTransaction();
139 mystudy = getOpenStudy();
141 Step step = mystudy.getSelectedStep();
142 Publication doctag = step.getDocument(Integer.valueOf(index));
144 step.removeDocument(doctag); // Updates the data structure
147 mystudy.remove(doctag); // Updates the presentation
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
156 } catch (HibernateException backerror) {
157 logger.debug("Error rolling back transaction", backerror);
164 // ==============================================================================================================================
165 // Getters and setters
166 // ==============================================================================================================================
168 public void setDocumentTitle (String title) {
169 // -------------------------------------------
172 public void setFileName (String filename) {
173 // -----------------------------------------
174 this.filename = filename;
176 public void setIndex (String index) {
177 // -----------------------------------
181 * Get the publicationService.
183 * @return the publicationService
185 public PublicationService getPublicationService() {
186 return _publicationService;
190 * Set the publicationService.
192 * @param publicationService
193 * the publicationService to set
195 public void setPublicationService(PublicationService publicationService) {
196 _publicationService = publicationService;