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.exception.InvalidParameterException;
12 import org.splat.exception.UserRightsException;
13 import org.splat.kernel.InvalidPropertyException;
14 import org.splat.service.PublicationService;
15 import org.splat.service.StepService;
16 import org.splat.service.technical.RepositoryService;
17 import org.splat.som.Step;
20 * Document modification action.
22 public class EditDocumentAction extends DisplayStudyStepAction {
27 private static final long serialVersionUID = 4573036736137033679L;
30 * The selected document id.
32 private transient String _index = null;
36 private transient String _title = null;
40 private transient String _filename = null;
42 * Injected publication service.
44 private PublicationService _publicationService;
46 * Injected step service.
48 private StepService _stepService;
50 * Injected repository service.
52 private RepositoryService _repositoryService;
55 * Operations enumeration.
57 private enum Execute {
59 * Possible document operations.
61 renameDocument, accept, promote, demote, review, invalidate, approve, disapprove
64 // =========================================================================
66 // =========================================================================
73 public String doInitialize() {
74 _openStudy = getOpenStudy();
80 * Perform the selected operation on the document.
82 * @return SUCCESS if succeeded, INPUT if input data are invalid, ERROR if failed
84 public String doSetDocument() throws InvalidParameterException, UserRightsException {
88 _openStudy = getOpenStudy();
90 Execute todo = Execute.valueOf(_action);
91 Step step = _openStudy.getSelectedStep();
92 Publication doc = step.getDocument(Long.valueOf(_index));
94 if (todo == Execute.renameDocument) {
95 getPublicationService().rename(doc, _title);
96 // Useless to update the document presentation
98 } else if (todo == Execute.accept) {
99 getPublicationService().actualize(doc);
100 doOpen(); //so pop-up menus of documents depending on this one will be updated
101 } else if (todo == Execute.promote) {
102 getPublicationService().promote(doc,
103 Calendar.getInstance().getTime(), getConnectedUser());
104 _openStudy.update(doc);
105 } else if (todo == Execute.demote) {
106 getPublicationService().demote(doc);
107 _openStudy.update(doc);
108 } else if (todo == Execute.review) {
109 getPublicationService().review(doc,
110 Calendar.getInstance().getTime(), getConnectedUser());
111 _openStudy.update(doc);
112 } else if (todo == Execute.invalidate) {
113 getPublicationService().invalidate(doc);
114 _openStudy.update(doc);
115 } else if (todo == Execute.approve) {
116 getPublicationService().approve(doc,
117 Calendar.getInstance().getTime(), getConnectedUser());
118 _openStudy.update(doc);
119 // Update the menu icon, in case of other documents in approved state
120 _openStudy.getMenu().refreshSelectedItem();
123 } catch (RuntimeException saverror) {
124 LOG.error("Reason:", saverror);
125 } catch (InvalidPropertyException error) {
132 * Attach an uploaded result file to the selected document.
134 * @return SUCCESS if succeeded, otherwise return ERROR
136 public String doAttach() {
140 // Getting user inputs
141 _openStudy = getOpenStudy();
142 User user = getConnectedUser();
143 Step step = _openStudy.getSelectedStep();
144 File updir = getRepositoryService().getDownloadDirectory(user);
145 File upfile = new File(updir.getPath() + "/" + _filename);
146 String[] parse = _filename.split("\\x2E");
148 Publication edited = step.getDocument(Long.valueOf(_index));
149 ConvertsRelation export = getPublicationService().attach(edited,
150 parse[parse.length - 1]);
152 if (LOG.isInfoEnabled()) {
153 LOG.info("Moving \"" + upfile.getName() + "\" to \""
154 + export.getTo().asFile().getAbsolutePath() + "\".");
156 upfile.renameTo(export.getTo().asFile());
158 _openStudy.update(edited);
160 } catch (Exception error) {
161 LOG.error("Reason:", error);
167 * Replace the selected document source file by the uploaded file.
169 * @return SUCCESS if succeeded, otherwise return ERROR
171 public String doReplace() {
175 // Getting user inputs
176 User user = getConnectedUser();
177 File updir = getRepositoryService().getDownloadDirectory(user);
178 File upfile = new File(updir.getPath() + "/" + _filename);
180 _openStudy = getOpenStudy();
181 Step step = _openStudy.getSelectedStep();
182 Publication edited = step.getDocument(Long.valueOf(_index));
183 Date modifTime = Calendar.getInstance().getTime();
185 // Replace the document source file
186 getPublicationService().replace(edited, upfile, modifTime);
188 _openStudy.update(edited);
190 } catch (Exception error) {
191 LOG.error("Reason:", error);
197 * Remove the selected document from the current activity.
200 * @throws DocumentIsUsedException
201 * if the document is used by other document(s)
202 * @throws NumberFormatException
203 * if the document id can not be transformed to number
205 public String doDeleteDocument() throws DocumentIsUsedException,
206 NumberFormatException {
209 _openStudy = getOpenStudy();
210 // updating current step and doc relations
211 _openStudy.updateCurrentStep();
212 Step step = _openStudy.getSelectedStep();
214 if (getStepService().removeDocument(step, Long.valueOf(_index))) { // Updates the data structure
215 // Re-opening (refreshing) the currently open study
216 if (_selection == null) { // Opening a study just newed
217 _selection = _openStudy.getSelection(); // Default selection
219 _openStudy = open(getStudyService().selectStudy(
220 _openStudy.getIndex())); // Closes the previously open study
221 //updating relations and pop-up menu
222 _openStudy.updateCurrentStep();
223 _openStudy.setSelection(_selection);
228 // =========================================================================
229 // Getters and setters
230 // =========================================================================
233 * Set the document title.
236 * the document title to set
238 public void setDocumentTitle(final String title) {
243 * Set the uploaded file name.
246 * the file name to set
248 public void setFileName(final String filename) {
249 this._filename = filename;
255 * @see org.splat.simer.AbstractDisplayAction#setIndex(java.lang.String)
258 public void setIndex(final String index) {
268 public String getIndex() {
273 * Get the publicationService.
275 * @return the publicationService
277 public PublicationService getPublicationService() {
278 return _publicationService;
282 * Set the publicationService.
284 * @param publicationService
285 * the publicationService to set
287 public void setPublicationService(
288 final PublicationService publicationService) {
289 _publicationService = publicationService;
293 * Get the repositoryService.
295 * @return the repositoryService
297 public RepositoryService getRepositoryService() {
298 return _repositoryService;
302 * Set the repositoryService.
304 * @param repositoryService
305 * the repositoryService to set
307 public void setRepositoryService(final RepositoryService repositoryService) {
308 _repositoryService = repositoryService;
312 * Get the stepService.
314 * @return the stepService
316 public StepService getStepService() {
321 * Set the stepService.
324 * the stepService to set
326 public void setStepService(final StepService stepService) {
327 _stepService = stepService;