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. It can be: none, create, open, study, knowledge, sysadmin, help.
84 private String _menuProperty;
87 * Value of the title bar property. It can be: study, knowledge.
89 private String _titleProperty;
92 * Value of the tool bar property. It can be: none, standard, study, back.
94 private String _toolProperty;
97 * Value of the left menu property. It can be: open, study, knowledge, scenario.
99 private String _leftMenuProperty;
102 * Property that indicates whether the current open study is editable or not. On the screen it looks like pen on the status icon, pop-up
103 * menu also can be called. It is necessary for correct building the title bar.
105 private String _editDisabledProperty = "false";
107 // ==============================================================================================================================
109 // ==============================================================================================================================
112 * Initialize the action form.
114 * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
116 public String doInitialize() {
118 setMenuProperty(Constants.STUDY_MENU);
119 setTitleProperty(Constants.STUDY_MENU);
120 setEditDisabledProperty("true");
121 if ("true".equals(getWriteAccess())) {
122 setToolProperty(Constants.STUDY_MENU);
124 setToolProperty(Constants.NONE);
126 setLeftMenuProperty(Constants.STUDY_MENU);
127 initializationFullScreenContext(_menuProperty, _titleProperty,
128 _editDisabledProperty, _toolProperty, _leftMenuProperty);
130 User user = getConnectedUser();
131 File updir = getRepositoryService().getDownloadDirectory(user);
132 File upfile = new File(updir.getPath() + "/" + filename);
134 mystudy = getOpenStudy();
136 Publication tag = mystudy.getSelectedStep().getDocument(
137 Integer.valueOf(_index));
138 Document doc = tag.value();
139 deftype = doc.getType();
140 docname = doc.getTitle();
141 defuses = new ArrayList<Document>();
142 _usedby = new ArrayList<Publication>();
144 String res = SUCCESS;
145 if (extractProperties(upfile, doc)) {
146 setupDefaultUses(deftype);
147 // Add additional documents used by the current version
148 List<Relation> uses = doc.getRelations(UsesRelation.class);
149 for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
150 Document used = (Document) i.next().getTo();
151 if (!defuses.contains(used)) {
155 // Setup dependencies
156 _usedby.addAll(tag.getRelations(UsedByRelation.class));
158 if (!(Constants.NONE.equals(_toolProperty))) {
159 setToolProperty(Constants.NONE);
160 setLeftMenuProperty(Constants.STUDY_MENU);
161 initializationFullScreenContext(_menuProperty, _titleProperty,
162 _editDisabledProperty, _toolProperty, _leftMenuProperty);
170 * Try to extract properties from the uploaded file if it is XML.
175 * the document to version
176 * @return true if succeeded or if the file is not XML, otherwise return false
178 private boolean extractProperties(final File upfile, final Document doc) {
180 Reader tool = Toolbox.getReader(upfile);
182 String fileref = tool.extractProperty("reference");
183 String filever = tool.extractProperty("version");
184 if (fileref == null || doc.getReference().equals(fileref)) {
185 if (filever != null) {
187 Revision.Format get = new Revision.Format(
188 getProjectSettings().getRevisionPattern());
189 Revision newver = get.parse(filever);
190 Revision oldver = new Revision(doc.getVersion());
191 if (!newver.isGraterThan(oldver)) {
192 throw new InvalidPropertyException("version");
194 if (newver.isMinor()) {
195 state = ProgressState.inWORK;
197 state = ProgressState.inDRAFT;
199 _version = newver.toString();
200 } catch (Exception e) {
201 setErrorCode("message.error.version.mismatch");
206 _description = tool.extractProperty("history");
207 _date = tool.extractProperty("date");
211 ResourceBundle locale = ResourceBundle.getBundle("som",
212 getApplicationSettings().getCurrentLocale());
213 SimpleDateFormat check = new SimpleDateFormat(locale
214 .getString("date.format"));
217 } catch (ParseException e) {
218 setErrorCode("message.error.format.date");
224 setErrorCode("message.error.reference.mismatch");
232 * Create a new version of the selected document.
234 * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
236 public String doVersion() {
238 setMenuProperty(Constants.STUDY_MENU);
239 setTitleProperty(Constants.STUDY_MENU);
240 setEditDisabledProperty("true");
241 initializationScreenContext(_menuProperty, _titleProperty,
242 _editDisabledProperty);
244 if (action == ToDo.cancel) {
249 // Getting user inputs
250 mystudy = getOpenStudy();
251 User user = getConnectedUser();
252 Step step = mystudy.getSelectedStep();
254 if (_date.length() > 0) {
255 ResourceBundle locale = ResourceBundle.getBundle("som",
256 getApplicationSettings().getCurrentLocale());
257 SimpleDateFormat get = new SimpleDateFormat(locale
258 .getString("date.format"));
259 aDate = get.parse(_date);
262 String[] listDocuses = null;
263 if (docuses != null) {
264 listDocuses = docuses.split(",");
266 getPublicationService().versionDocument(step, user, filename,
267 Integer.valueOf(_index), _version, _description, state,
268 aDate, listDocuses, _docusedby);
270 // Update of the open study
271 mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
272 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
275 } catch (FileNotFoundException error) {
276 LOG.error("Reason:", error);
277 setErrorCode("message.error.import.file");
278 } catch (Exception error) {
279 LOG.error("Reason:", error);
280 setErrorCode("message.error.internal");
282 if (!SUCCESS.equals(res)) {
283 setToolProperty(Constants.NONE);
285 setLeftMenuProperty(Constants.STUDY_MENU);
286 initializationFullScreenContext(_menuProperty, _titleProperty,
287 _editDisabledProperty, _toolProperty, _leftMenuProperty);
293 // ==============================================================================================================================
294 // Getters and setters
295 // ==============================================================================================================================
297 public String getDate() {
301 public List<Publication> getDependencies() {
305 public String getDescription() {
309 public String getIndex() {
313 public String getVersion() {
317 public void setDate(final String date) {
321 public void setDefaultDescription(final String summary) {
322 if (this._description == null) {
323 this._description = summary;
327 public void setDescription(final String summary) {
328 this._description = summary;
331 public void setIndex(final String index) {
335 public void setUsedBy(final long[] list) {
336 this._docusedby = list;
339 public void setVersion(final String value) {
340 this._version = value;
344 * Get project settings.
346 * @return Project settings service
348 private ProjectSettingsService getProjectSettings() {
349 return _projectSettings;
353 * Set project settings service.
355 * @param projectSettingsService
356 * project settings service
358 public void setProjectSettings(
359 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(
379 final PublicationService publicationService) {
380 _publicationService = publicationService;
384 * Get the stepService.
386 * @return the stepService
388 public StepService getStepService() {
393 * Set the stepService.
396 * the stepService to set
398 public void setStepService(final StepService stepService) {
399 _stepService = stepService;
403 * Get the repositoryService.
405 * @return the repositoryService
407 public RepositoryService getRepositoryService() {
408 return _repositoryService;
412 * Set the repositoryService.
414 * @param repositoryService
415 * the repositoryService to set
417 public void setRepositoryService(final RepositoryService repositoryService) {
418 _repositoryService = repositoryService;
422 * Get the menuProperty.
424 * @return the menuProperty
426 public String getMenuProperty() {
427 return _menuProperty;
431 * Set the menuProperty.
433 * @param menuProperty
434 * the menuProperty to set
436 public void setMenuProperty(final String menuProperty) {
437 this._menuProperty = menuProperty;
441 * Get the _titleProperty.
443 * @return the _titleProperty
445 public String getTitleProperty() {
446 return _titleProperty;
450 * Set the _titleProperty.
452 * @param titleProperty
453 * the titleProperty to set
455 public void setTitleProperty(final String titleProperty) {
456 _titleProperty = titleProperty;
460 * Get the _editDisabledProperty.
462 * @return the _editDisabledProperty
464 public String getEditDisabledProperty() {
465 return _editDisabledProperty;
469 * Set the editDisabledProperty.
471 * @param editDisabledProperty
472 * the editDisabledProperty to set
474 public void setEditDisabledProperty(final String editDisabledProperty) {
475 this._editDisabledProperty = editDisabledProperty;
479 * Get the toolProperty.
481 * @return the toolProperty
483 public String getToolProperty() {
484 return _toolProperty;
488 * Set the toolProperty.
490 * @param toolProperty
491 * the toolProperty to set
493 public void setToolProperty(final String toolProperty) {
494 _toolProperty = toolProperty;
498 * Get the leftMenuProperty.
500 * @return the leftMenuProperty
502 public String getLeftMenuProperty() {
503 return _leftMenuProperty;
507 * Set the leftMenuProperty.
509 * @param leftMenuProperty
510 * the leftMenuProperty to set
512 public void setLeftMenuProperty(final String leftMenuProperty) {
513 _leftMenuProperty = leftMenuProperty;