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 {
57 * Possible document operations.
59 renameDocument, accept, promote, demote, review, invalidate, approve, disapprove
62 // =========================================================================
64 // =========================================================================
71 public String doInitialize() {
72 _openStudy = getOpenStudy();
78 * Perform the selected operation on the document.
80 * @return SUCCESS if succeeded, INPUT if input data are invalid, ERROR if failed
82 public String doSetDocument() {
86 _openStudy = getOpenStudy();
88 Execute todo = Execute.valueOf(_action);
89 Step step = _openStudy.getSelectedStep();
90 Publication doc = step.getDocument(Long.valueOf(_index));
92 if (todo == Execute.renameDocument) {
93 getPublicationService().rename(doc, _title);
94 // Useless to update the document presentation
96 } else if (todo == Execute.accept) {
97 getPublicationService().actualize(doc);
98 _openStudy.update(doc);
99 } else if (todo == Execute.promote) {
100 getPublicationService().promote(doc,
101 Calendar.getInstance().getTime());
102 _openStudy.update(doc);
103 } else if (todo == Execute.demote) {
104 getPublicationService().demote(doc);
105 _openStudy.update(doc);
106 } else if (todo == Execute.review) {
107 getPublicationService().review(doc,
108 Calendar.getInstance().getTime());
109 _openStudy.update(doc);
110 } else if (todo == Execute.invalidate) {
111 getPublicationService().invalidate(doc);
112 _openStudy.update(doc);
113 } else if (todo == Execute.approve) {
114 getPublicationService().approve(doc,
115 Calendar.getInstance().getTime());
116 _openStudy.update(doc);
117 // Update the menu icon, in case of other documents in approved state
118 _openStudy.getMenu().refreshSelectedItem();
121 } catch (RuntimeException saverror) {
122 LOG.error("Reason:", saverror);
123 } catch (InvalidPropertyException error) {
130 * Attach an uploaded result file to the selected document.
132 * @return SUCCESS if succeeded, otherwise return ERROR
134 public String doAttach() {
138 // Getting user inputs
139 _openStudy = getOpenStudy();
140 User user = getConnectedUser();
141 Step step = _openStudy.getSelectedStep();
142 File updir = getRepositoryService().getDownloadDirectory(user);
143 File upfile = new File(updir.getPath() + "/" + _filename);
144 String[] parse = _filename.split("\\x2E");
146 Publication edited = step.getDocument(Long.valueOf(_index));
147 ConvertsRelation export = getPublicationService().attach(edited,
148 parse[parse.length - 1]);
150 if (LOG.isInfoEnabled()) {
151 LOG.info("Moving \"" + upfile.getName() + "\" to \""
152 + export.getTo().asFile().getAbsolutePath() + "\".");
154 upfile.renameTo(export.getTo().asFile());
156 _openStudy.update(edited);
158 } catch (Exception error) {
159 LOG.error("Reason:", error);
165 * Replace the selected document source file by the uploaded file.
167 * @return SUCCESS if succeeded, otherwise return ERROR
169 public String doReplace() {
173 // Getting user inputs
174 User user = getConnectedUser();
175 File updir = getRepositoryService().getDownloadDirectory(user);
176 File upfile = new File(updir.getPath() + "/" + _filename);
178 _openStudy = getOpenStudy();
179 Step step = _openStudy.getSelectedStep();
180 Publication edited = step.getDocument(Long.valueOf(_index));
181 Date modifTime = Calendar.getInstance().getTime();
183 // Replace the document source file
184 getPublicationService().replace(edited, upfile, modifTime);
186 _openStudy.update(edited);
188 } catch (Exception error) {
189 LOG.error("Reason:", error);
195 * Remove the selected document from the current activity.
198 * @throws DocumentIsUsedException
199 * if the document is used by other document(s)
200 * @throws NumberFormatException
201 * if the document id can not be transformed to number
203 public String doDeleteDocument() throws DocumentIsUsedException,
204 NumberFormatException {
207 _openStudy = getOpenStudy();
208 Step step = _openStudy.getSelectedStep();
210 if (getStepService().removeDocument(step, Long.valueOf(_index))) { // Updates the data structure
211 // Re-opening (refreshing) the currently open study
212 if (_selection == null) { // Opening a study just newed
213 _selection = _openStudy.getSelection(); // Default selection
215 _openStudy = open(getStudyService().selectStudy(
216 _openStudy.getIndex())); // Closes the previously open study
217 _openStudy.setSelection(_selection);
222 // =========================================================================
223 // Getters and setters
224 // =========================================================================
227 * Set the document title.
230 * the document title to set
232 public void setDocumentTitle(final String title) {
237 * Set the uploaded file name.
240 * the file name to set
242 public void setFileName(final String filename) {
243 this._filename = filename;
249 * @see org.splat.simer.AbstractDisplayAction#setIndex(java.lang.String)
252 public void setIndex(final String index) {
262 public String getIndex() {
267 * Get the publicationService.
269 * @return the publicationService
271 public PublicationService getPublicationService() {
272 return _publicationService;
276 * Set the publicationService.
278 * @param publicationService
279 * the publicationService to set
281 public void setPublicationService(
282 final PublicationService publicationService) {
283 _publicationService = publicationService;
287 * Get the repositoryService.
289 * @return the repositoryService
291 public RepositoryService getRepositoryService() {
292 return _repositoryService;
296 * Set the repositoryService.
298 * @param repositoryService
299 * the repositoryService to set
301 public void setRepositoryService(final RepositoryService repositoryService) {
302 _repositoryService = repositoryService;
306 * Get the stepService.
308 * @return the stepService
310 public StepService getStepService() {
315 * Set the stepService.
318 * the stepService to set
320 public void setStepService(final StepService stepService) {
321 _stepService = stepService;