]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/VersionDocumentAction.java
Salome HOME
Modifications to respect PMD rules.
[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. 
83          * It can be: none, create, open, study, knowledge, sysadmin, help.
84          */
85         private String _menuProperty;
86
87         /**
88          * Value of the title bar property. 
89          * It can be: study, knowledge.
90          */
91         private String _titleProperty;
92
93         /**
94          * Value of the tool bar property. 
95          * It can be: none, standard, study, back.
96          */
97         private String _toolProperty;
98         
99         /**
100          * Value of the left menu property. 
101          * It can be: open, study, knowledge, scenario.
102          */
103         private String _leftMenuProperty;
104
105         /**
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.
109          */
110         private String _editDisabledProperty = "false";
111
112         // ==============================================================================================================================
113         // Action methods
114         // ==============================================================================================================================
115
116         /**
117          * Initialize the action form.
118          * 
119          * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
120          */
121         public String doInitialize() {
122
123                 setMenuProperty(Constants.STUDY_MENU);
124                 setTitleProperty(Constants.STUDY_MENU);
125                 setEditDisabledProperty("true");
126                 if ("true".equals(getWriteAccess())) {
127                         setToolProperty(Constants.STUDY_MENU);
128                 } else {
129                         setToolProperty(Constants.NONE);
130                 }
131                 setLeftMenuProperty(Constants.STUDY_MENU);
132         initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
133             
134                 User user = getConnectedUser();
135                 File updir = getRepositoryService().getDownloadDirectory(user);
136                 File upfile = new File(updir.getPath() + "/" + filename);
137
138                 mystudy = getOpenStudy();
139
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>();
147
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)) {
156                                         defuses.add(used);
157                                 }
158                         }
159                         // Setup dependencies
160                         _usedby.addAll(tag.getRelations(UsedByRelation.class));
161                 } else {
162                         if (!(Constants.NONE.equals(_toolProperty))) {
163                                 setToolProperty(Constants.NONE);
164                                 setLeftMenuProperty(Constants.STUDY_MENU);
165                                 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
166                         }
167                         res = ERROR;
168                 }
169                 return res;
170         }
171
172         /**
173          * Try to extract properties from the uploaded file if it is XML.
174          * 
175          * @param upfile
176          *            the file to parse
177          * @param doc
178          *            the document to version
179          * @return true if succeeded or if the file is not XML, otherwise return false
180          */
181         private boolean extractProperties(final File upfile, final Document doc) {
182                 boolean res = true;
183                 Reader tool = Toolbox.getReader(upfile);
184                 if (tool != null) {
185                         String fileref = tool.extractProperty("reference");
186                         String filever = tool.extractProperty("version");
187                         if (fileref == null || doc.getReference().equals(fileref)) {
188                                 if (filever != null) {
189                                         try {
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");
196                                                 }
197                                                 if (newver.isMinor()) {
198                                                         state = ProgressState.inWORK;
199                                                 } else {
200                                                         state = ProgressState.inDRAFT;
201                                                 }
202                                                 _version = newver.toString();
203                                         } catch (Exception e) {
204                                                 setErrorCode("version.mismatch");
205                                                 res = false;
206                                         }
207                                 }
208                                 if (res) {
209                                         _description = tool.extractProperty("history");
210                                         _date = tool.extractProperty("date");
211                                         if (_date == null) {
212                                                 _date = "";
213                                         } else {
214                                                 ResourceBundle locale = ResourceBundle.getBundle("som",
215                                                                 getApplicationSettings().getCurrentLocale());
216                                                 SimpleDateFormat check = new SimpleDateFormat(locale
217                                                                 .getString("date.format"));
218                                                 try {
219                                                         check.parse(_date);
220                                                 } catch (ParseException e) {
221                                                         setErrorCode("format.date");
222                                                         res = false;
223                                                 }
224                                         }
225                                 }
226                         } else {
227                                 setErrorCode("reference.mismatch");
228                                 res = false;
229                         }
230                 }
231                 return res;
232         }
233
234         /**
235          * Create a new version of the selected document.
236          * 
237          * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
238          */
239         public String doVersion() {
240                 String res = ERROR;
241                 setMenuProperty(Constants.STUDY_MENU);
242                 setTitleProperty(Constants.STUDY_MENU);
243                 setEditDisabledProperty("true");
244         initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
245             
246                 if (action == ToDo.cancel) {
247                         res = "cancel";
248                 } else {
249         
250                         try {
251                                 // Getting user inputs
252                                 mystudy = getOpenStudy();
253                                 User user = getConnectedUser();
254                                 Step step = mystudy.getSelectedStep();
255                                 Date aDate = null;
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);
262                                 }
263         
264                                 String[] listDocuses = null;
265                                 if (docuses != null) {
266                                         listDocuses = docuses.split(",");
267                                 }
268                                 getPublicationService().versionDocument(step, user, filename,
269                                                 Integer.valueOf(_index), _version, _description, state, aDate,
270                                                 listDocuses, _docusedby);
271         
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)
275         
276                                 res = SUCCESS;
277                         } catch (FileNotFoundException error) {
278                                 LOG.error("Reason:", error);
279                                 setErrorCode("import.file");
280                         } catch (Exception error) {
281                                 LOG.error("Reason:", error);
282                                 setErrorCode("internal");
283                         }
284                         if (!SUCCESS.equals(res)) {
285                                 setToolProperty(Constants.NONE);
286                                 
287                                 setLeftMenuProperty(Constants.STUDY_MENU);
288                                 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
289                         }
290                 }               
291                 return res;
292         }
293
294         // ==============================================================================================================================
295         // Getters and setters
296         // ==============================================================================================================================
297
298         public String getDate() {
299                 return _date;
300         }
301
302         public List<Publication> getDependencies() {
303                 return _usedby;
304         }
305
306         public String getDescription() {
307                 return _description;
308         }
309
310         public String getIndex() {
311                 return _index;
312         }
313
314         public String getVersion() {
315                 return _version;
316         }
317
318         public void setDate(final String date) {
319                 this._date = date;
320         }
321
322         public void setDefaultDescription(final String summary) {
323                 if (this._description == null) {
324                         this._description = summary;
325                 }
326         }
327
328         public void setDescription(final String summary) {
329                 this._description = summary;
330         }
331
332         public void setIndex(final String index) {
333                 this._index = index;
334         }
335
336         public void setUsedBy(final long[] list) {
337                 this._docusedby = list;
338         }
339
340         public void setVersion(final String value) {
341                 this._version = value;
342         }
343
344         /**
345          * Get project settings.
346          * 
347          * @return Project settings service
348          */
349         private ProjectSettingsService getProjectSettings() {
350                 return _projectSettings;
351         }
352
353         /**
354          * Set project settings service.
355          * 
356          * @param projectSettingsService
357          *            project settings service
358          */
359         public void setProjectSettings(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(final PublicationService publicationService) {
379                 _publicationService = publicationService;
380         }
381
382         /**
383          * Get the stepService.
384          * 
385          * @return the stepService
386          */
387         public StepService getStepService() {
388                 return _stepService;
389         }
390
391         /**
392          * Set the stepService.
393          * 
394          * @param stepService
395          *            the stepService to set
396          */
397         public void setStepService(final StepService stepService) {
398                 _stepService = stepService;
399         }
400
401         /**
402          * Get the repositoryService.
403          * 
404          * @return the repositoryService
405          */
406         public RepositoryService getRepositoryService() {
407                 return _repositoryService;
408         }
409
410         /**
411          * Set the repositoryService.
412          * 
413          * @param repositoryService
414          *            the repositoryService to set
415          */
416         public void setRepositoryService(final RepositoryService repositoryService) {
417                 _repositoryService = repositoryService;
418         }
419
420         /**
421          * Get the menuProperty.
422          * 
423          * @return the menuProperty
424          */
425         public String getMenuProperty() {
426                 return _menuProperty;
427         }
428
429         /**
430          * Set the menuProperty.
431          * 
432          * @param menuProperty
433          *            the menuProperty to set
434          */
435         public void setMenuProperty(final String menuProperty) {
436                 this._menuProperty = menuProperty;
437         }
438
439         /**
440          * Get the _titleProperty.
441          * 
442          * @return the _titleProperty
443          */
444         public String getTitleProperty() {
445                 return _titleProperty;
446         }
447
448         /**
449          * Set the _titleProperty.
450          * 
451          * @param titleProperty
452          *            the titleProperty to set
453          */
454         public void setTitleProperty(final String titleProperty) {
455                 _titleProperty = titleProperty;
456         }
457
458         /**
459          * Get the _editDisabledProperty.
460          * 
461          * @return the _editDisabledProperty
462          */
463         public String getEditDisabledProperty() {
464                 return _editDisabledProperty;
465         }
466
467         /**
468          * Set the editDisabledProperty.
469          * 
470          * @param editDisabledProperty
471          *            the editDisabledProperty to set
472          */
473         public void setEditDisabledProperty(final String editDisabledProperty) {
474                 this._editDisabledProperty = editDisabledProperty;
475         }
476         
477         /**
478          * Get the toolProperty.
479          * @return the toolProperty
480          */
481         public String getToolProperty() {
482                 return _toolProperty;
483         }
484
485         /**
486          * Set the toolProperty.
487          * @param toolProperty the toolProperty to set
488          */
489         public void setToolProperty(final String toolProperty) {
490                 _toolProperty = toolProperty;
491         }
492         
493         /**
494          * Get the leftMenuProperty.
495          * @return the leftMenuProperty
496          */
497         public String getLeftMenuProperty() {
498                 return _leftMenuProperty;
499         }
500
501         /**
502          * Set the leftMenuProperty.
503          * @param leftMenuProperty the leftMenuProperty to set
504          */
505         public void setLeftMenuProperty(final String leftMenuProperty) {
506                 _leftMenuProperty = leftMenuProperty;
507         }
508 }