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 Step step = _openStudy.getSelectedStep();
212 if (getStepService().removeDocument(step, Long.valueOf(_index))) { // Updates the data structure
213 // Re-opening (refreshing) the currently open study
214 if (_selection == null) { // Opening a study just newed
215 _selection = _openStudy.getSelection(); // Default selection
217 _openStudy = open(getStudyService().selectStudy(
218 _openStudy.getIndex())); // Closes the previously open study
219 _openStudy.setSelection(_selection);
224 // =========================================================================
225 // Getters and setters
226 // =========================================================================
229 * Set the document title.
232 * the document title to set
234 public void setDocumentTitle(final String title) {
239 * Set the uploaded file name.
242 * the file name to set
244 public void setFileName(final String filename) {
245 this._filename = filename;
251 * @see org.splat.simer.AbstractDisplayAction#setIndex(java.lang.String)
254 public void setIndex(final String index) {
264 public String getIndex() {
269 * Get the publicationService.
271 * @return the publicationService
273 public PublicationService getPublicationService() {
274 return _publicationService;
278 * Set the publicationService.
280 * @param publicationService
281 * the publicationService to set
283 public void setPublicationService(
284 final PublicationService publicationService) {
285 _publicationService = publicationService;
289 * Get the repositoryService.
291 * @return the repositoryService
293 public RepositoryService getRepositoryService() {
294 return _repositoryService;
298 * Set the repositoryService.
300 * @param repositoryService
301 * the repositoryService to set
303 public void setRepositoryService(final RepositoryService repositoryService) {
304 _repositoryService = repositoryService;
308 * Get the stepService.
310 * @return the stepService
312 public StepService getStepService() {
317 * Set the stepService.
320 * the stepService to set
322 public void setStepService(final StepService stepService) {
323 _stepService = stepService;