]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/VersionDocumentAction.java
Salome HOME
app.root property is removed.
[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. It can be: none, create, open, study, knowledge, sysadmin, help.
82          */
83         private String _menuProperty;
84
85         /**
86          * Value of the title bar property. It can be: study, knowledge.
87          */
88         private String _titleProperty;
89
90         /**
91          * 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
92          * menu also can be called. It is necessary for correct building the title bar.
93          */
94         private String _editDisabledProperty = "false";
95
96         // ==============================================================================================================================
97         // Action methods
98         // ==============================================================================================================================
99
100         /**
101          * Initialize the action form.
102          * 
103          * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
104          */
105         public String doInitialize() {
106
107                 setMenuProperty("study");
108                 setTitleProperty("study");
109                 setEditDisabledProperty("true");
110                 initializationScreenContext(_menuProperty, _titleProperty,
111                                 _editDisabledProperty);
112
113                 User user = getConnectedUser();
114                 File updir = getRepositoryService().getDownloadDirectory(user);
115                 File upfile = new File(updir.getPath() + "/" + filename);
116
117                 mystudy = getOpenStudy();
118
119                 Publication tag = mystudy.getSelectedStep().getDocument(
120                                 Integer.valueOf(index));
121                 Document doc = tag.value();
122                 deftype = doc.getType();
123                 docname = doc.getTitle();
124                 defuses = new Vector<Document>();
125                 usedby = new Vector<Publication>();
126
127                 if (!extractProperties(upfile, doc)) {
128                         return ERROR;
129                 }
130                 setupDefaultUses(deftype);
131                 // Add additional documents used by the current version
132                 List<Relation> uses = doc.getRelations(UsesRelation.class);
133                 for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
134                         Document used = (Document) i.next().getTo();
135                         if (!defuses.contains(used))
136                                 defuses.add(used);
137                 }
138                 // Setup dependencies
139                 List<Publication> relist = tag.getRelations(UsedByRelation.class);
140                 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
141                         usedby.add(i.next());
142                 }
143                 return SUCCESS;
144         }
145
146         /**
147          * Try to extract properties from the uploaded file if it is XML.
148          * 
149          * @param upfile
150          *            the file to parse
151          * @param doc
152          *            the document to version
153          * @return true if succeeded or if the file is not XML, otherwise return false
154          */
155         private boolean extractProperties(File upfile, Document doc) {
156                 boolean res = true;
157                 Reader tool = Toolbox.getReader(upfile);
158                 if (tool != null) {
159                         String fileref = tool.extractProperty("reference");
160                         String filever = tool.extractProperty("version");
161                         if (fileref != null && !doc.getReference().equals(fileref)) {
162                                 setErrorCode("reference.mismatch");
163                                 res = false;
164                         } else {
165                                 if (filever != null) {
166                                         try {
167                                                 Revision.Format get = new Revision.Format(
168                                                                 getProjectSettings().getRevisionPattern());
169                                                 Revision newver = get.parse(filever);
170                                                 Revision oldver = new Revision(doc.getVersion());
171                                                 if (!newver.isGraterThan(oldver)) {
172                                                         throw new InvalidPropertyException("version");
173                                                 }
174                                                 if (newver.isMinor()) {
175                                                         state = ProgressState.inWORK;
176                                                 } else {
177                                                         state = ProgressState.inDRAFT;
178                                                 }
179                                                 docver = newver.toString();
180                                         } catch (Exception e) {
181                                                 setErrorCode("version.mismatch");
182                                                 res = false;
183                                         }
184                                 }
185                                 if (res) {
186                                         summary = tool.extractProperty("history");
187                                         date = tool.extractProperty("date");
188                                         if (date != null) {
189                                                 ResourceBundle locale = ResourceBundle.getBundle("som",
190                                                                 getApplicationSettings().getCurrentLocale());
191                                                 SimpleDateFormat check = new SimpleDateFormat(locale
192                                                                 .getString("date.format"));
193                                                 try {
194                                                         check.parse(date);
195                                                 } catch (ParseException e) {
196                                                         setErrorCode("format.date");
197                                                         res = false;
198                                                 }
199                                         } else {
200                                                 date = "";
201                                         }
202                                 }
203                         }
204                 }
205                 return res;
206         }
207
208         /**
209          * Create a new version of the selected document.
210          * 
211          * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
212          */
213         public String doVersion() {
214                 setMenuProperty("study");
215                 setTitleProperty("study");
216                 setEditDisabledProperty("true");
217                 initializationScreenContext(_menuProperty, _titleProperty,
218                                 _editDisabledProperty);
219
220                 if (action == ToDo.cancel)
221                         return "cancel";
222
223                 try {
224                         // Getting user inputs
225                         mystudy = getOpenStudy();
226                         User user = getConnectedUser();
227                         Step step = mystudy.getSelectedStep();
228                         Date aDate = null;
229                         if (date.length() > 0) {
230                                 ResourceBundle locale = ResourceBundle.getBundle("som",
231                                                 getApplicationSettings().getCurrentLocale());
232                                 SimpleDateFormat get = new SimpleDateFormat(locale
233                                                 .getString("date.format"));
234                                 aDate = get.parse(date);
235                         }
236
237                         String[] listDocuses = null;
238                         if (docuses != null) {
239                                 listDocuses = docuses.split(",");
240                         }
241                         getPublicationService().versionDocument(step, user, filename,
242                                         Integer.valueOf(index), docver, summary, state, aDate,
243                                         listDocuses, docusedby);
244
245                         // Update of the open study
246                         mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
247                         // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
248
249                         return SUCCESS;
250                 } catch (FileNotFoundException error) {
251                         logger.error("Reason:", error);
252                         setErrorCode("import.file");
253                 } catch (Exception error) {
254                         logger.error("Reason:", error);
255                         setErrorCode("internal");
256                 }
257                 return ERROR;
258         }
259
260         // ==============================================================================================================================
261         // Getters and setters
262         // ==============================================================================================================================
263
264         public String getDate() {
265                 return date;
266         }
267
268         public List<Publication> getDependencies() {
269                 return usedby;
270         }
271
272         public String getDescription() {
273                 return summary;
274         }
275
276         public String getIndex() {
277                 return index;
278         }
279
280         public String getVersion() {
281                 return docver;
282         }
283
284         public void setDate(String date) {
285                 this.date = date;
286         }
287
288         public void setDefaultDescription(String summary) {
289                 if (this.summary == null)
290                         this.summary = summary;
291         }
292
293         public void setDescription(String summary) {
294                 this.summary = summary;
295         }
296
297         public void setIndex(String index) {
298                 this.index = index;
299         }
300
301         public void setUsedBy(long[] list) {
302                 this.docusedby = list;
303         }
304
305         public void setVersion(String value) {
306                 this.docver = value;
307         }
308
309         /**
310          * Get project settings.
311          * 
312          * @return Project settings service
313          */
314         private ProjectSettingsService getProjectSettings() {
315                 return _projectSettingsService;
316         }
317
318         /**
319          * Set project settings service.
320          * 
321          * @param projectSettingsService
322          *            project settings service
323          */
324         public void setProjectSettings(ProjectSettingsService projectSettingsService) {
325                 _projectSettingsService = projectSettingsService;
326         }
327
328         /**
329          * Get the publicationService.
330          * 
331          * @return the publicationService
332          */
333         public PublicationService getPublicationService() {
334                 return _publicationService;
335         }
336
337         /**
338          * Set the publicationService.
339          * 
340          * @param publicationService
341          *            the publicationService to set
342          */
343         public void setPublicationService(PublicationService publicationService) {
344                 _publicationService = publicationService;
345         }
346
347         /**
348          * Get the stepService.
349          * 
350          * @return the stepService
351          */
352         public StepService getStepService() {
353                 return _stepService;
354         }
355
356         /**
357          * Set the stepService.
358          * 
359          * @param stepService
360          *            the stepService to set
361          */
362         public void setStepService(StepService stepService) {
363                 _stepService = stepService;
364         }
365
366         /**
367          * Get the repositoryService.
368          * 
369          * @return the repositoryService
370          */
371         public RepositoryService getRepositoryService() {
372                 return _repositoryService;
373         }
374
375         /**
376          * Set the repositoryService.
377          * 
378          * @param repositoryService
379          *            the repositoryService to set
380          */
381         public void setRepositoryService(RepositoryService repositoryService) {
382                 _repositoryService = repositoryService;
383         }
384
385         /**
386          * Get the menuProperty.
387          * 
388          * @return the menuProperty
389          */
390         public String getMenuProperty() {
391                 return _menuProperty;
392         }
393
394         /**
395          * Set the menuProperty.
396          * 
397          * @param menuProperty
398          *            the menuProperty to set
399          */
400         public void setMenuProperty(String menuProperty) {
401                 this._menuProperty = menuProperty;
402         }
403
404         /**
405          * Get the _titleProperty.
406          * 
407          * @return the _titleProperty
408          */
409         public String getTitleProperty() {
410                 return _titleProperty;
411         }
412
413         /**
414          * Set the _titleProperty.
415          * 
416          * @param titleProperty
417          *            the titleProperty to set
418          */
419         public void setTitleProperty(String titleProperty) {
420                 _titleProperty = titleProperty;
421         }
422
423         /**
424          * Get the _editDisabledProperty.
425          * 
426          * @return the _editDisabledProperty
427          */
428         public String getEditDisabledProperty() {
429                 return _editDisabledProperty;
430         }
431
432         /**
433          * Set the _editDisabledProperty.
434          * 
435          * @param _editDisabledProperty
436          *            the _editDisabledProperty to set
437          */
438         public void setEditDisabledProperty(String _editDisabledProperty) {
439                 this._editDisabledProperty = _editDisabledProperty;
440         }
441 }