]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/VersionDocumentAction.java
Salome HOME
Default document types mappings are moved from application settings (my.xml) to proje...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / VersionDocumentAction.java
1 package org.splat.simer;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.ArrayList;
8 import java.util.Date;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.ResourceBundle;
12
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;
30
31 /**
32  * Action for creating a new version of a document.
33  */
34 public class VersionDocumentAction extends UploadBaseNextAction {
35
36         /**
37          * Serial version ID.
38          */
39         private static final long serialVersionUID = -5702264003232132168L;
40
41         /**
42          * Versioned document index.
43          */
44         private String _index = null;
45         /**
46          * List of publications which use the selected document.
47          */
48         private transient List<Publication> _usedby = null;
49         /**
50          * List of selected impacted documents ids.
51          */
52         private transient long[] _docusedby = null;
53         /**
54          * Summary of changes in the new version.
55          */
56         private String _description = null;
57         /**
58          * Version number extracted from the imported file, if exist.
59          */
60         private String _version = "";
61         /**
62          * Date extracted from the imported file, if exist.
63          */
64         private String _date = "";
65         /**
66          * Injected project settings service.
67          */
68         private ProjectSettingsService _projectSettings;
69         /**
70          * Injected publication service.
71          */
72         private PublicationService _publicationService;
73         /**
74          * Injected step service.
75          */
76         private StepService _stepService;
77         /**
78          * Injected repository service.
79          */
80         private RepositoryService _repositoryService;
81         /**
82          * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
83          */
84         private String _menuProperty;
85
86         /**
87          * Value of the title bar property. It can be: study, knowledge.
88          */
89         private String _titleProperty;
90
91         /**
92          * Value of the tool bar property. It can be: none, standard, study, back.
93          */
94         private String _toolProperty;
95
96         /**
97          * Value of the left menu property. It can be: open, study, knowledge, scenario.
98          */
99         private String _leftMenuProperty;
100
101         /**
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.
104          */
105         private String _editDisabledProperty = "false";
106
107         // ==============================================================================================================================
108         // Action methods
109         // ==============================================================================================================================
110
111         /**
112          * Initialize the action form.
113          * 
114          * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
115          */
116         public String doInitialize() {
117
118                 setMenuProperty(Constants.STUDY_MENU);
119                 setTitleProperty(Constants.STUDY_MENU);
120                 setEditDisabledProperty("true");
121                 if ("true".equals(getWriteAccess())) {
122                         setToolProperty(Constants.STUDY_MENU);
123                 } else {
124                         setToolProperty(Constants.NONE);
125                 }
126                 setLeftMenuProperty(Constants.STUDY_MENU);
127                 initializationFullScreenContext(_menuProperty, _titleProperty,
128                                 _editDisabledProperty, _toolProperty, _leftMenuProperty);
129
130                 User user = getConnectedUser();
131                 File updir = getRepositoryService().getDownloadDirectory(user);
132                 File upfile = new File(updir.getPath() + "/" + filename);
133
134                 mystudy = getOpenStudy();
135
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>();
143
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)) {
152                                         defuses.add(used);
153                                 }
154                         }
155                         // Setup dependencies
156                         _usedby.addAll(tag.getRelations(UsedByRelation.class));
157                 } else {
158                         if (!(Constants.NONE.equals(_toolProperty))) {
159                                 setToolProperty(Constants.NONE);
160                                 setLeftMenuProperty(Constants.STUDY_MENU);
161                                 initializationFullScreenContext(_menuProperty, _titleProperty,
162                                                 _editDisabledProperty, _toolProperty, _leftMenuProperty);
163                         }
164                         res = ERROR;
165                 }
166                 return res;
167         }
168
169         /**
170          * Try to extract properties from the uploaded file if it is XML.
171          * 
172          * @param upfile
173          *            the file to parse
174          * @param doc
175          *            the document to version
176          * @return true if succeeded or if the file is not XML, otherwise return false
177          */
178         private boolean extractProperties(final File upfile, final Document doc) {
179                 boolean res = true;
180                 Reader tool = Toolbox.getReader(upfile);
181                 if (tool != null) {
182                         String fileref = tool.extractProperty("reference");
183                         String filever = tool.extractProperty("version");
184                         if (fileref == null || doc.getReference().equals(fileref)) {
185                                 if (filever != null) {
186                                         try {
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");
193                                                 }
194                                                 if (newver.isMinor()) {
195                                                         state = ProgressState.inWORK;
196                                                 } else {
197                                                         state = ProgressState.inDRAFT;
198                                                 }
199                                                 _version = newver.toString();
200                                         } catch (Exception e) {
201                                                 setErrorCode("message.error.version.mismatch");
202                                                 res = false;
203                                         }
204                                 }
205                                 if (res) {
206                                         _description = tool.extractProperty("history");
207                                         _date = tool.extractProperty("date");
208                                         if (_date == null) {
209                                                 _date = "";
210                                         } else {
211                                                 ResourceBundle locale = ResourceBundle.getBundle("som",
212                                                                 getApplicationSettings().getCurrentLocale());
213                                                 SimpleDateFormat check = new SimpleDateFormat(locale
214                                                                 .getString("date.format"));
215                                                 try {
216                                                         check.parse(_date);
217                                                 } catch (ParseException e) {
218                                                         setErrorCode("message.error.format.date");
219                                                         res = false;
220                                                 }
221                                         }
222                                 }
223                         } else {
224                                 setErrorCode("message.error.reference.mismatch");
225                                 res = false;
226                         }
227                 }
228                 return res;
229         }
230
231         /**
232          * Create a new version of the selected document.
233          * 
234          * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
235          */
236         public String doVersion() {
237                 String res = ERROR;
238                 setMenuProperty(Constants.STUDY_MENU);
239                 setTitleProperty(Constants.STUDY_MENU);
240                 setEditDisabledProperty("true");
241                 initializationScreenContext(_menuProperty, _titleProperty,
242                                 _editDisabledProperty);
243
244                 if (action == ToDo.cancel) {
245                         res = "cancel";
246                 } else {
247
248                         try {
249                                 // Getting user inputs
250                                 mystudy = getOpenStudy();
251                                 User user = getConnectedUser();
252                                 Step step = mystudy.getSelectedStep();
253                                 Date aDate = null;
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);
260                                 }
261
262                                 String[] listDocuses = null;
263                                 if (docuses != null) {
264                                         listDocuses = docuses.split(",");
265                                 }
266                                 getPublicationService().versionDocument(step, user, filename,
267                                                 Integer.valueOf(_index), _version, _description, state,
268                                                 aDate, listDocuses, _docusedby);
269
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)
273
274                                 res = SUCCESS;
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");
281                         }
282                         if (!SUCCESS.equals(res)) {
283                                 setToolProperty(Constants.NONE);
284
285                                 setLeftMenuProperty(Constants.STUDY_MENU);
286                                 initializationFullScreenContext(_menuProperty, _titleProperty,
287                                                 _editDisabledProperty, _toolProperty, _leftMenuProperty);
288                         }
289                 }
290                 return res;
291         }
292
293         // ==============================================================================================================================
294         // Getters and setters
295         // ==============================================================================================================================
296
297         public String getDate() {
298                 return _date;
299         }
300
301         public List<Publication> getDependencies() {
302                 return _usedby;
303         }
304
305         public String getDescription() {
306                 return _description;
307         }
308
309         public String getIndex() {
310                 return _index;
311         }
312
313         public String getVersion() {
314                 return _version;
315         }
316
317         public void setDate(final String date) {
318                 this._date = date;
319         }
320
321         public void setDefaultDescription(final String summary) {
322                 if (this._description == null) {
323                         this._description = summary;
324                 }
325         }
326
327         public void setDescription(final String summary) {
328                 this._description = summary;
329         }
330
331         public void setIndex(final String index) {
332                 this._index = index;
333         }
334
335         public void setUsedBy(final long[] list) {
336                 this._docusedby = list;
337         }
338
339         public void setVersion(final String value) {
340                 this._version = value;
341         }
342
343         /**
344          * Get project settings.
345          * 
346          * @return Project settings service
347          */
348         private ProjectSettingsService getProjectSettings() {
349                 return _projectSettings;
350         }
351
352         /**
353          * Set project settings service.
354          * 
355          * @param projectSettingsService
356          *            project settings service
357          */
358         public void setProjectSettings(
359                         final ProjectSettingsService projectSettingsService) {
360                 _projectSettings = projectSettingsService;
361         }
362
363         /**
364          * Get the publicationService.
365          * 
366          * @return the publicationService
367          */
368         public PublicationService getPublicationService() {
369                 return _publicationService;
370         }
371
372         /**
373          * Set the publicationService.
374          * 
375          * @param publicationService
376          *            the publicationService to set
377          */
378         public void setPublicationService(
379                         final PublicationService publicationService) {
380                 _publicationService = publicationService;
381         }
382
383         /**
384          * Get the stepService.
385          * 
386          * @return the stepService
387          */
388         public StepService getStepService() {
389                 return _stepService;
390         }
391
392         /**
393          * Set the stepService.
394          * 
395          * @param stepService
396          *            the stepService to set
397          */
398         public void setStepService(final StepService stepService) {
399                 _stepService = stepService;
400         }
401
402         /**
403          * Get the repositoryService.
404          * 
405          * @return the repositoryService
406          */
407         public RepositoryService getRepositoryService() {
408                 return _repositoryService;
409         }
410
411         /**
412          * Set the repositoryService.
413          * 
414          * @param repositoryService
415          *            the repositoryService to set
416          */
417         public void setRepositoryService(final RepositoryService repositoryService) {
418                 _repositoryService = repositoryService;
419         }
420
421         /**
422          * Get the menuProperty.
423          * 
424          * @return the menuProperty
425          */
426         public String getMenuProperty() {
427                 return _menuProperty;
428         }
429
430         /**
431          * Set the menuProperty.
432          * 
433          * @param menuProperty
434          *            the menuProperty to set
435          */
436         public void setMenuProperty(final String menuProperty) {
437                 this._menuProperty = menuProperty;
438         }
439
440         /**
441          * Get the _titleProperty.
442          * 
443          * @return the _titleProperty
444          */
445         public String getTitleProperty() {
446                 return _titleProperty;
447         }
448
449         /**
450          * Set the _titleProperty.
451          * 
452          * @param titleProperty
453          *            the titleProperty to set
454          */
455         public void setTitleProperty(final String titleProperty) {
456                 _titleProperty = titleProperty;
457         }
458
459         /**
460          * Get the _editDisabledProperty.
461          * 
462          * @return the _editDisabledProperty
463          */
464         public String getEditDisabledProperty() {
465                 return _editDisabledProperty;
466         }
467
468         /**
469          * Set the editDisabledProperty.
470          * 
471          * @param editDisabledProperty
472          *            the editDisabledProperty to set
473          */
474         public void setEditDisabledProperty(final String editDisabledProperty) {
475                 this._editDisabledProperty = editDisabledProperty;
476         }
477
478         /**
479          * Get the toolProperty.
480          * 
481          * @return the toolProperty
482          */
483         public String getToolProperty() {
484                 return _toolProperty;
485         }
486
487         /**
488          * Set the toolProperty.
489          * 
490          * @param toolProperty
491          *            the toolProperty to set
492          */
493         public void setToolProperty(final String toolProperty) {
494                 _toolProperty = toolProperty;
495         }
496
497         /**
498          * Get the leftMenuProperty.
499          * 
500          * @return the leftMenuProperty
501          */
502         public String getLeftMenuProperty() {
503                 return _leftMenuProperty;
504         }
505
506         /**
507          * Set the leftMenuProperty.
508          * 
509          * @param leftMenuProperty
510          *            the leftMenuProperty to set
511          */
512         public void setLeftMenuProperty(final String leftMenuProperty) {
513                 _leftMenuProperty = leftMenuProperty;
514         }
515 }