1 package org.splat.simer;
4 import java.io.FileNotFoundException;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.ResourceBundle;
13 import org.splat.dal.bo.kernel.Relation;
14 import org.splat.dal.bo.kernel.User;
15 import org.splat.dal.bo.som.Document;
16 import org.splat.dal.bo.som.ProgressState;
17 import org.splat.dal.bo.som.Publication;
18 import org.splat.dal.bo.som.UsedByRelation;
19 import org.splat.dal.bo.som.UsesRelation;
20 import org.splat.kernel.InvalidPropertyException;
21 import org.splat.manox.Reader;
22 import org.splat.manox.Toolbox;
23 import org.splat.service.PublicationService;
24 import org.splat.service.StepService;
25 import org.splat.service.technical.ProjectSettingsService;
26 import org.splat.service.technical.RepositoryService;
27 import org.splat.som.Revision;
28 import org.splat.som.Step;
29 import org.splat.wapp.Constants;
32 * Action for creating a new version of a document.
34 public class VersionDocumentAction extends UploadBaseNextAction {
39 private static final long serialVersionUID = -5702264003232132168L;
42 * Versioned document index.
44 private String _index = null;
46 * List of publications which use the selected document.
48 private transient List<Publication> _usedby = null;
50 * List of selected impacted documents ids.
52 private transient long[] _docusedby = null;
54 * Summary of changes in the new version.
56 private String _description = null;
58 * Version number extracted from the imported file, if exist.
60 private String _version = "";
62 * Date extracted from the imported file, if exist.
64 private String _date = "";
66 * Injected project settings service.
68 private ProjectSettingsService _projectSettings;
70 * Injected publication service.
72 private PublicationService _publicationService;
74 * Injected step service.
76 private StepService _stepService;
78 * Injected repository service.
80 private RepositoryService _repositoryService;
82 * Value of the menu property.
83 * It can be: none, create, open, study, knowledge, sysadmin, help.
85 private String _menuProperty;
88 * Value of the title bar property.
89 * It can be: study, knowledge.
91 private String _titleProperty;
94 * Value of the tool bar property.
95 * It can be: none, standard, study, back.
97 private String _toolProperty;
100 * Value of the left menu property.
101 * It can be: open, study, knowledge, scenario.
103 private String _leftMenuProperty;
106 * Property that indicates whether the current open study is editable or not.
107 * On the screen it looks like pen on the status icon, pop-up menu also can be called.
108 * It is necessary for correct building the title bar.
110 private String _editDisabledProperty = "false";
112 // ==============================================================================================================================
114 // ==============================================================================================================================
117 * Initialize the action form.
119 * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
121 public String doInitialize() {
123 setMenuProperty(Constants.STUDY_MENU);
124 setTitleProperty(Constants.STUDY_MENU);
125 setEditDisabledProperty("true");
126 if ("true".equals(getWriteAccess())) {
127 setToolProperty(Constants.STUDY_MENU);
129 setToolProperty(Constants.NONE);
131 setLeftMenuProperty(Constants.STUDY_MENU);
132 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
134 User user = getConnectedUser();
135 File updir = getRepositoryService().getDownloadDirectory(user);
136 File upfile = new File(updir.getPath() + "/" + filename);
138 mystudy = getOpenStudy();
140 Publication tag = mystudy.getSelectedStep().getDocument(
141 Integer.valueOf(_index));
142 Document doc = tag.value();
143 deftype = doc.getType();
144 docname = doc.getTitle();
145 defuses = new ArrayList<Document>();
146 _usedby = new ArrayList<Publication>();
148 String res = SUCCESS;
149 if (extractProperties(upfile, doc)) {
150 setupDefaultUses(deftype);
151 // Add additional documents used by the current version
152 List<Relation> uses = doc.getRelations(UsesRelation.class);
153 for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
154 Document used = (Document) i.next().getTo();
155 if (!defuses.contains(used)) {
159 // Setup dependencies
160 _usedby.addAll(tag.getRelations(UsedByRelation.class));
162 if (!(Constants.NONE.equals(_toolProperty))) {
163 setToolProperty(Constants.NONE);
164 setLeftMenuProperty(Constants.STUDY_MENU);
165 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
173 * Try to extract properties from the uploaded file if it is XML.
178 * the document to version
179 * @return true if succeeded or if the file is not XML, otherwise return false
181 private boolean extractProperties(final File upfile, final Document doc) {
183 Reader tool = Toolbox.getReader(upfile);
185 String fileref = tool.extractProperty("reference");
186 String filever = tool.extractProperty("version");
187 if (fileref == null || doc.getReference().equals(fileref)) {
188 if (filever != null) {
190 Revision.Format get = new Revision.Format(
191 getProjectSettings().getRevisionPattern());
192 Revision newver = get.parse(filever);
193 Revision oldver = new Revision(doc.getVersion());
194 if (!newver.isGraterThan(oldver)) {
195 throw new InvalidPropertyException("version");
197 if (newver.isMinor()) {
198 state = ProgressState.inWORK;
200 state = ProgressState.inDRAFT;
202 _version = newver.toString();
203 } catch (Exception e) {
204 setErrorCode("message.error.version.mismatch");
209 _description = tool.extractProperty("history");
210 _date = tool.extractProperty("date");
214 ResourceBundle locale = ResourceBundle.getBundle("som",
215 getApplicationSettings().getCurrentLocale());
216 SimpleDateFormat check = new SimpleDateFormat(locale
217 .getString("date.format"));
220 } catch (ParseException e) {
221 setErrorCode("message.error.format.date");
227 setErrorCode("message.error.reference.mismatch");
235 * Create a new version of the selected document.
237 * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
239 public String doVersion() {
241 setMenuProperty(Constants.STUDY_MENU);
242 setTitleProperty(Constants.STUDY_MENU);
243 setEditDisabledProperty("true");
244 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
246 if (action == ToDo.cancel) {
251 // Getting user inputs
252 mystudy = getOpenStudy();
253 User user = getConnectedUser();
254 Step step = mystudy.getSelectedStep();
256 if (_date.length() > 0) {
257 ResourceBundle locale = ResourceBundle.getBundle("som",
258 getApplicationSettings().getCurrentLocale());
259 SimpleDateFormat get = new SimpleDateFormat(locale
260 .getString("date.format"));
261 aDate = get.parse(_date);
264 String[] listDocuses = null;
265 if (docuses != null) {
266 listDocuses = docuses.split(",");
268 getPublicationService().versionDocument(step, user, filename,
269 Integer.valueOf(_index), _version, _description, state, aDate,
270 listDocuses, _docusedby);
272 // Update of the open study
273 mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
274 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
277 } catch (FileNotFoundException error) {
278 LOG.error("Reason:", error);
279 setErrorCode("message.error.import.file");
280 } catch (Exception error) {
281 LOG.error("Reason:", error);
282 setErrorCode("message.error.internal");
284 if (!SUCCESS.equals(res)) {
285 setToolProperty(Constants.NONE);
287 setLeftMenuProperty(Constants.STUDY_MENU);
288 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
294 // ==============================================================================================================================
295 // Getters and setters
296 // ==============================================================================================================================
298 public String getDate() {
302 public List<Publication> getDependencies() {
306 public String getDescription() {
310 public String getIndex() {
314 public String getVersion() {
318 public void setDate(final String date) {
322 public void setDefaultDescription(final String summary) {
323 if (this._description == null) {
324 this._description = summary;
328 public void setDescription(final String summary) {
329 this._description = summary;
332 public void setIndex(final String index) {
336 public void setUsedBy(final long[] list) {
337 this._docusedby = list;
340 public void setVersion(final String value) {
341 this._version = value;
345 * Get project settings.
347 * @return Project settings service
349 private ProjectSettingsService getProjectSettings() {
350 return _projectSettings;
354 * Set project settings service.
356 * @param projectSettingsService
357 * project settings service
359 public void setProjectSettings(final ProjectSettingsService projectSettingsService) {
360 _projectSettings = projectSettingsService;
364 * Get the publicationService.
366 * @return the publicationService
368 public PublicationService getPublicationService() {
369 return _publicationService;
373 * Set the publicationService.
375 * @param publicationService
376 * the publicationService to set
378 public void setPublicationService(final PublicationService publicationService) {
379 _publicationService = publicationService;
383 * Get the stepService.
385 * @return the stepService
387 public StepService getStepService() {
392 * Set the stepService.
395 * the stepService to set
397 public void setStepService(final StepService stepService) {
398 _stepService = stepService;
402 * Get the repositoryService.
404 * @return the repositoryService
406 public RepositoryService getRepositoryService() {
407 return _repositoryService;
411 * Set the repositoryService.
413 * @param repositoryService
414 * the repositoryService to set
416 public void setRepositoryService(final RepositoryService repositoryService) {
417 _repositoryService = repositoryService;
421 * Get the menuProperty.
423 * @return the menuProperty
425 public String getMenuProperty() {
426 return _menuProperty;
430 * Set the menuProperty.
432 * @param menuProperty
433 * the menuProperty to set
435 public void setMenuProperty(final String menuProperty) {
436 this._menuProperty = menuProperty;
440 * Get the _titleProperty.
442 * @return the _titleProperty
444 public String getTitleProperty() {
445 return _titleProperty;
449 * Set the _titleProperty.
451 * @param titleProperty
452 * the titleProperty to set
454 public void setTitleProperty(final String titleProperty) {
455 _titleProperty = titleProperty;
459 * Get the _editDisabledProperty.
461 * @return the _editDisabledProperty
463 public String getEditDisabledProperty() {
464 return _editDisabledProperty;
468 * Set the editDisabledProperty.
470 * @param editDisabledProperty
471 * the editDisabledProperty to set
473 public void setEditDisabledProperty(final String editDisabledProperty) {
474 this._editDisabledProperty = editDisabledProperty;
478 * Get the toolProperty.
479 * @return the toolProperty
481 public String getToolProperty() {
482 return _toolProperty;
486 * Set the toolProperty.
487 * @param toolProperty the toolProperty to set
489 public void setToolProperty(final String toolProperty) {
490 _toolProperty = toolProperty;
494 * Get the leftMenuProperty.
495 * @return the leftMenuProperty
497 public String getLeftMenuProperty() {
498 return _leftMenuProperty;
502 * Set the leftMenuProperty.
503 * @param leftMenuProperty the leftMenuProperty to set
505 public void setLeftMenuProperty(final String leftMenuProperty) {
506 _leftMenuProperty = leftMenuProperty;