1 package org.splat.simer;
4 import java.util.Calendar;
7 import org.splat.dal.bo.kernel.User;
8 import org.splat.dal.bo.som.ConvertsRelation;
9 import org.splat.dal.bo.som.Publication;
10 import org.splat.exception.DocumentIsUsedException;
11 import org.splat.kernel.InvalidPropertyException;
12 import org.splat.service.PublicationService;
13 import org.splat.service.StepService;
14 import org.splat.service.technical.RepositoryService;
15 import org.splat.som.Step;
18 * Document modification action.
20 public class EditDocumentAction extends DisplayStudyStepAction {
25 private static final long serialVersionUID = 4573036736137033679L;
28 * The selected document id.
30 private transient String _index = null;
34 private transient String _title = null;
38 private transient String _filename = null;
40 * Injected publication service.
42 private PublicationService _publicationService;
44 * Injected step service.
46 private StepService _stepService;
48 * Injected repository service.
50 private RepositoryService _repositoryService;
53 * Operations enumeration.
55 private enum Execute {
56 renameDocument, accept, promote, demote, review, invalidate, approve, disapprove
59 // ==============================================================================================================================
61 // ==============================================================================================================================
68 public String doInitialize() {
69 _openStudy = getOpenStudy();
75 * Perform the selected operation on the document.
77 * @return SUCCESS if succeeded, INPUT if input data are invalid, ERROR if failed
79 public String doSetDocument() {
83 _openStudy = getOpenStudy();
85 Execute todo = Execute.valueOf(_action);
86 Step step = _openStudy.getSelectedStep();
87 Publication doc = step.getDocument(Long.valueOf(_index));
89 if (todo == Execute.renameDocument) {
90 getPublicationService().rename(doc, _title);
91 // Useless to update the document presentation
93 } else if (todo == Execute.accept) {
94 getPublicationService().actualize(doc);
95 _openStudy.update(doc);
96 } else if (todo == Execute.promote) {
97 getPublicationService().promote(doc,
98 Calendar.getInstance().getTime());
99 _openStudy.update(doc);
100 } else if (todo == Execute.demote) {
101 getPublicationService().demote(doc);
102 _openStudy.update(doc);
103 } else if (todo == Execute.review) {
104 getPublicationService().review(doc,
105 Calendar.getInstance().getTime());
106 _openStudy.update(doc);
107 } else if (todo == Execute.invalidate) {
108 getPublicationService().invalidate(doc);
109 _openStudy.update(doc);
110 } else if (todo == Execute.approve) {
111 getPublicationService().approve(doc,
112 Calendar.getInstance().getTime());
113 _openStudy.update(doc);
114 _openStudy.getMenu().refreshSelectedItem(); // Updates the menu icon, in case of other documents in approved state
117 } catch (RuntimeException saverror) {
118 LOG.error("Reason:", saverror);
119 } catch (InvalidPropertyException error) {
126 * Attach an uploaded result file to the selected document.
128 * @return SUCCESS if succeeded, otherwise return ERROR
130 public String doAttach() {
134 // Getting user inputs
135 _openStudy = getOpenStudy();
136 User user = getConnectedUser();
137 Step step = _openStudy.getSelectedStep();
138 File updir = getRepositoryService().getDownloadDirectory(user);
139 File upfile = new File(updir.getPath() + "/" + _filename);
140 String[] parse = _filename.split("\\x2E");
142 Publication edited = step.getDocument(Long.valueOf(_index));
143 ConvertsRelation export = getPublicationService().attach(edited,
144 parse[parse.length - 1]);
146 if (LOG.isInfoEnabled()) {
147 LOG.info("Moving \"" + upfile.getName() + "\" to \""
148 + export.getTo().asFile().getAbsolutePath() + "\".");
150 upfile.renameTo(export.getTo().asFile());
152 _openStudy.update(edited);
154 } catch (Exception error) {
155 LOG.error("Reason:", error);
161 * Replace the selected document source file by the uploaded file.
163 * @return SUCCESS if succeeded, otherwise return ERROR
165 public String doReplace() {
169 // Getting user inputs
170 User user = getConnectedUser();
171 File updir = getRepositoryService().getDownloadDirectory(user);
172 File upfile = new File(updir.getPath() + "/" + _filename);
174 _openStudy = getOpenStudy();
175 Step step = _openStudy.getSelectedStep();
176 Publication edited = step.getDocument(Long.valueOf(_index));
177 Date modifTime = Calendar.getInstance().getTime();
179 // Replace the document source file
180 getPublicationService().replace(edited, upfile, modifTime);
182 _openStudy.update(edited);
184 } catch (Exception error) {
185 LOG.error("Reason:", error);
191 * Remove the selected document from the current activity.
194 * @throws DocumentIsUsedException
195 * if the document is used by other document(s)
196 * @throws NumberFormatException
197 * if the document id can not be transformed to number
199 public String doDeleteDocument() throws DocumentIsUsedException,
200 NumberFormatException {
203 _openStudy = getOpenStudy();
204 Step step = _openStudy.getSelectedStep();
205 Publication doctag = step.getDocument(Long.valueOf(_index));
206 Long prevVersion = 0L;
207 if (doctag.value().getPreviousVersion() != null) {
208 prevVersion = doctag.value().getPreviousVersion().getIndex();
211 if (getStepService().removeDocument(step, Long.valueOf(_index))) { // Updates the data structure
212 _openStudy.remove(doctag); // Updates the presentation
213 // The previous version must be republished if any
214 if (prevVersion > 0) {
215 _openStudy.add(step.getDocument(prevVersion));
221 // ==============================================================================================================================
222 // Getters and setters
223 // ==============================================================================================================================
226 * Set the document title.
229 * the document title to set
231 public void setDocumentTitle(final String title) {
236 * Set the uploaded file name.
239 * the file name to set
241 public void setFileName(final String filename) {
242 this._filename = filename;
248 * @see org.splat.simer.AbstractDisplayAction#setIndex(java.lang.String)
251 public void setIndex(final String index) {
261 public String getIndex() {
266 * Get the publicationService.
268 * @return the publicationService
270 public PublicationService getPublicationService() {
271 return _publicationService;
275 * Set the publicationService.
277 * @param publicationService
278 * the publicationService to set
280 public void setPublicationService(
281 final PublicationService publicationService) {
282 _publicationService = publicationService;
286 * Get the repositoryService.
288 * @return the repositoryService
290 public RepositoryService getRepositoryService() {
291 return _repositoryService;
295 * Set the repositoryService.
297 * @param repositoryService
298 * the repositoryService to set
300 public void setRepositoryService(final RepositoryService repositoryService) {
301 _repositoryService = repositoryService;
305 * Get the stepService.
307 * @return the stepService
309 public StepService getStepService() {
314 * Set the stepService.
317 * the stepService to set
319 public void setStepService(final StepService stepService) {
320 _stepService = stepService;