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.Date;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.ResourceBundle;
11 import java.util.Vector;
12
13 import org.splat.kernel.InvalidPropertyException;
14 import org.splat.dal.bo.kernel.Relation;
15 import org.splat.dal.bo.kernel.User;
16 import org.splat.manox.Reader;
17 import org.splat.manox.Toolbox;
18 import org.splat.dal.bo.som.Document;
19 import org.splat.dal.bo.som.ProgressState;
20 import org.splat.service.PublicationService;
21 import org.splat.service.StepService;
22 import org.splat.service.technical.ProjectSettingsService;
23 import org.splat.service.technical.RepositoryService;
24 import org.splat.dal.bo.som.Publication;
25 import org.splat.som.Revision;
26 import org.splat.som.Step;
27 import org.splat.dal.bo.som.UsedByRelation;
28 import org.splat.dal.bo.som.UsesRelation;
29
30 /**
31  * Action for creating a new version of a document.
32  */
33 public class VersionDocumentAction extends UploadBaseNextAction {
34
35         /**
36          * Serial version ID.
37          */
38         private static final long serialVersionUID = -5702264003232132168L;
39
40         /**
41          * Versioned document index.
42          */
43         private String index = null;
44         /**
45          * List of publications which use the selected document.
46          */
47         private List<Publication> usedby = null;
48         /**
49          * List of selected impacted documents ids.
50          */
51         private long[] docusedby = null;
52         /**
53          * Summary of changes in the new version.
54          */
55         private String summary = null;
56         /**
57          * Version number extracted from the imported file, if exist.
58          */
59         private String docver = "";
60         /**
61          * Date extracted from the imported file, if exist.
62          */
63         private String date = "";
64         /**
65          * Injected project settings service.
66          */
67         private ProjectSettingsService _projectSettingsService;
68         /**
69          * Injected publication service.
70          */
71         private PublicationService _publicationService;
72         /**
73          * Injected step service.
74          */
75         private StepService _stepService;
76         /**
77          * Injected repository service.
78          */
79         private RepositoryService _repositoryService;
80         /**
81          * Value of the menu property. 
82          * It can be: none, create, open, study, knowledge, sysadmin, help.
83          */
84         private String _menuProperty;
85
86         /**
87          * Value of the title bar property. 
88          * It can be: study, knowledge.
89          */
90         private String _titleProperty;
91
92         /**
93          * Value of the tool bar property. 
94          * It can be: none, standard, study, back.
95          */
96         private String _toolProperty;
97         
98         /**
99          * Value of the left menu property. 
100          * It can be: open, study, knowledge, scenario.
101          */
102         private String _leftMenuProperty;
103
104         /**
105          * Property that indicates whether the current open study is editable or not.
106          * On the screen it looks like pen on the status icon, pop-up menu also can be called.
107          * It is necessary for correct building the title bar.
108          */
109         private String _editDisabledProperty = "false";
110
111         // ==============================================================================================================================
112         // Action methods
113         // ==============================================================================================================================
114
115         /**
116          * Initialize the action form.
117          * 
118          * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
119          */
120         public String doInitialize() {
121
122                 setMenuProperty("study");
123                 setTitleProperty("study");
124                 setEditDisabledProperty("true");
125                 if ("true".equals(getWriteAccess())) {
126                         setToolProperty("study");
127                 } else {
128                         setToolProperty("none");
129                 }
130                 setLeftMenuProperty("study");
131         initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
132             
133                 User user = getConnectedUser();
134                 File updir = getRepositoryService().getDownloadDirectory(user);
135                 File upfile = new File(updir.getPath() + "/" + filename);
136
137                 mystudy = getOpenStudy();
138
139                 Publication tag = mystudy.getSelectedStep().getDocument(
140                                 Integer.valueOf(index));
141                 Document doc = tag.value();
142                 deftype = doc.getType();
143                 docname = doc.getTitle();
144                 defuses = new Vector<Document>();
145                 usedby = new Vector<Publication>();
146
147                 if (!extractProperties(upfile, doc)) {
148                         if (!("none".equals(_toolProperty))) {
149                                 setToolProperty("none");
150                                 setLeftMenuProperty("study");
151                                 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
152                         }
153                         return ERROR;
154                 }
155                 setupDefaultUses(deftype);
156                 // Add additional documents used by the current version
157                 List<Relation> uses = doc.getRelations(UsesRelation.class);
158                 for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
159                         Document used = (Document) i.next().getTo();
160                         if (!defuses.contains(used))
161                                 defuses.add(used);
162                 }
163                 // Setup dependencies
164                 List<Publication> relist = tag.getRelations(UsedByRelation.class);
165                 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
166                         usedby.add(i.next());
167                 }
168                 return SUCCESS;
169         }
170
171         /**
172          * Try to extract properties from the uploaded file if it is XML.
173          * 
174          * @param upfile
175          *            the file to parse
176          * @param doc
177          *            the document to version
178          * @return true if succeeded or if the file is not XML, otherwise return false
179          */
180         private boolean extractProperties(File upfile, Document doc) {
181                 boolean res = true;
182                 Reader tool = Toolbox.getReader(upfile);
183                 if (tool != null) {
184                         String fileref = tool.extractProperty("reference");
185                         String filever = tool.extractProperty("version");
186                         if (fileref != null && !doc.getReference().equals(fileref)) {
187                                 setErrorCode("reference.mismatch");
188                                 res = false;
189                         } else {
190                                 if (filever != null) {
191                                         try {
192                                                 Revision.Format get = new Revision.Format(
193                                                                 getProjectSettings().getRevisionPattern());
194                                                 Revision newver = get.parse(filever);
195                                                 Revision oldver = new Revision(doc.getVersion());
196                                                 if (!newver.isGraterThan(oldver)) {
197                                                         throw new InvalidPropertyException("version");
198                                                 }
199                                                 if (newver.isMinor()) {
200                                                         state = ProgressState.inWORK;
201                                                 } else {
202                                                         state = ProgressState.inDRAFT;
203                                                 }
204                                                 docver = newver.toString();
205                                         } catch (Exception e) {
206                                                 setErrorCode("version.mismatch");
207                                                 res = false;
208                                         }
209                                 }
210                                 if (res) {
211                                         summary = tool.extractProperty("history");
212                                         date = tool.extractProperty("date");
213                                         if (date != null) {
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                                         } else {
225                                                 date = "";
226                                         }
227                                 }
228                         }
229                 }
230                 return res;
231         }
232
233         /**
234          * Create a new version of the selected document.
235          * 
236          * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
237          */
238         public String doVersion() {
239                 setMenuProperty("study");
240                 setTitleProperty("study");
241                 setEditDisabledProperty("true");
242         initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
243             
244                 if (action == ToDo.cancel)
245                         return "cancel";
246
247                 try {
248                         // Getting user inputs
249                         mystudy = getOpenStudy();
250                         User user = getConnectedUser();
251                         Step step = mystudy.getSelectedStep();
252                         Date aDate = null;
253                         if (date.length() > 0) {
254                                 ResourceBundle locale = ResourceBundle.getBundle("som",
255                                                 getApplicationSettings().getCurrentLocale());
256                                 SimpleDateFormat get = new SimpleDateFormat(locale
257                                                 .getString("date.format"));
258                                 aDate = get.parse(date);
259                         }
260
261                         String[] listDocuses = null;
262                         if (docuses != null) {
263                                 listDocuses = docuses.split(",");
264                         }
265                         getPublicationService().versionDocument(step, user, filename,
266                                         Integer.valueOf(index), docver, summary, state, aDate,
267                                         listDocuses, docusedby);
268
269                         // Update of the open study
270                         mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
271                         // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
272
273                         return SUCCESS;
274                 } catch (FileNotFoundException error) {
275                         LOG.error("Reason:", error);
276                         setErrorCode("import.file");
277                 } catch (Exception error) {
278                         LOG.error("Reason:", error);
279                         setErrorCode("internal");
280                 }
281                 
282                 setToolProperty("none");
283                 
284                 setLeftMenuProperty("study");
285                 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
286                 
287                 return ERROR;
288         }
289
290         // ==============================================================================================================================
291         // Getters and setters
292         // ==============================================================================================================================
293
294         public String getDate() {
295                 return date;
296         }
297
298         public List<Publication> getDependencies() {
299                 return usedby;
300         }
301
302         public String getDescription() {
303                 return summary;
304         }
305
306         public String getIndex() {
307                 return index;
308         }
309
310         public String getVersion() {
311                 return docver;
312         }
313
314         public void setDate(String date) {
315                 this.date = date;
316         }
317
318         public void setDefaultDescription(String summary) {
319                 if (this.summary == null)
320                         this.summary = summary;
321         }
322
323         public void setDescription(String summary) {
324                 this.summary = summary;
325         }
326
327         public void setIndex(String index) {
328                 this.index = index;
329         }
330
331         public void setUsedBy(long[] list) {
332                 this.docusedby = list;
333         }
334
335         public void setVersion(String value) {
336                 this.docver = value;
337         }
338
339         /**
340          * Get project settings.
341          * 
342          * @return Project settings service
343          */
344         private ProjectSettingsService getProjectSettings() {
345                 return _projectSettingsService;
346         }
347
348         /**
349          * Set project settings service.
350          * 
351          * @param projectSettingsService
352          *            project settings service
353          */
354         public void setProjectSettings(ProjectSettingsService projectSettingsService) {
355                 _projectSettingsService = projectSettingsService;
356         }
357
358         /**
359          * Get the publicationService.
360          * 
361          * @return the publicationService
362          */
363         public PublicationService getPublicationService() {
364                 return _publicationService;
365         }
366
367         /**
368          * Set the publicationService.
369          * 
370          * @param publicationService
371          *            the publicationService to set
372          */
373         public void setPublicationService(PublicationService publicationService) {
374                 _publicationService = publicationService;
375         }
376
377         /**
378          * Get the stepService.
379          * 
380          * @return the stepService
381          */
382         public StepService getStepService() {
383                 return _stepService;
384         }
385
386         /**
387          * Set the stepService.
388          * 
389          * @param stepService
390          *            the stepService to set
391          */
392         public void setStepService(StepService stepService) {
393                 _stepService = stepService;
394         }
395
396         /**
397          * Get the repositoryService.
398          * 
399          * @return the repositoryService
400          */
401         public RepositoryService getRepositoryService() {
402                 return _repositoryService;
403         }
404
405         /**
406          * Set the repositoryService.
407          * 
408          * @param repositoryService
409          *            the repositoryService to set
410          */
411         public void setRepositoryService(RepositoryService repositoryService) {
412                 _repositoryService = repositoryService;
413         }
414
415         /**
416          * Get the menuProperty.
417          * 
418          * @return the menuProperty
419          */
420         public String getMenuProperty() {
421                 return _menuProperty;
422         }
423
424         /**
425          * Set the menuProperty.
426          * 
427          * @param menuProperty
428          *            the menuProperty to set
429          */
430         public void setMenuProperty(String menuProperty) {
431                 this._menuProperty = menuProperty;
432         }
433
434         /**
435          * Get the _titleProperty.
436          * 
437          * @return the _titleProperty
438          */
439         public String getTitleProperty() {
440                 return _titleProperty;
441         }
442
443         /**
444          * Set the _titleProperty.
445          * 
446          * @param titleProperty
447          *            the titleProperty to set
448          */
449         public void setTitleProperty(String titleProperty) {
450                 _titleProperty = titleProperty;
451         }
452
453         /**
454          * Get the _editDisabledProperty.
455          * 
456          * @return the _editDisabledProperty
457          */
458         public String getEditDisabledProperty() {
459                 return _editDisabledProperty;
460         }
461
462         /**
463          * Set the _editDisabledProperty.
464          * 
465          * @param _editDisabledProperty
466          *            the _editDisabledProperty to set
467          */
468         public void setEditDisabledProperty(String _editDisabledProperty) {
469                 this._editDisabledProperty = _editDisabledProperty;
470         }
471         
472         /**
473          * Get the toolProperty.
474          * @return the toolProperty
475          */
476         public String getToolProperty() {
477                 return _toolProperty;
478         }
479
480         /**
481          * Set the toolProperty.
482          * @param toolProperty the toolProperty to set
483          */
484         public void setToolProperty(final String toolProperty) {
485                 _toolProperty = toolProperty;
486         }
487         
488         /**
489          * Get the leftMenuProperty.
490          * @return the leftMenuProperty
491          */
492         public String getLeftMenuProperty() {
493                 return _leftMenuProperty;
494         }
495
496         /**
497          * Set the leftMenuProperty.
498          * @param leftMenuProperty the leftMenuProperty to set
499          */
500         public void setLeftMenuProperty(final String leftMenuProperty) {
501                 _leftMenuProperty = leftMenuProperty;
502         }
503 }