]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/ImportDocumentAction.java
Salome HOME
Tiles is added
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / ImportDocumentAction.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.Arrays;
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.dal.bo.kernel.User;
14 import org.splat.manox.Reader;
15 import org.splat.manox.Toolbox;
16 import org.splat.dal.bo.som.Document;
17 import org.splat.dal.bo.som.ProgressState;
18 import org.splat.service.DocumentService;
19 import org.splat.service.DocumentTypeService;
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.dal.bo.som.DocumentType;
26 import org.splat.som.Revision;
27 import org.splat.som.Step;
28
29 /**
30  * Action for adding a document into a study step.
31  */
32 public class ImportDocumentAction extends UploadBaseNextAction {
33
34         /**
35          * Serial version ID.
36          */
37         private static final long serialVersionUID = 2587822564883588556L;
38
39         private List<DocumentType> doctypes = null;
40         private long doctype = 0;
41         private String docref = null; // Reference extracted from the imported file, if exist
42         private String docver = ""; // Version number extracted from the imported file, if exist
43         private String date = ""; // Date extracted from the imported file, if exist
44         /**
45          * Injected project settings service.
46          */
47         private ProjectSettingsService _projectSettingsService;
48         /**
49          * Injected publication service.
50          */
51         private PublicationService _publicationService;
52         /**
53          * Injected step service.
54          */
55         private StepService _stepService;
56         /**
57          * Injected document service.
58          */
59         private DocumentService _documentService;
60         /**
61          * Injected document type service.
62          */
63         private DocumentTypeService _documentTypeService;
64         /**
65          * Injected repository service.
66          */
67         private RepositoryService _repositoryService;   
68         /**
69          * Value of the menu property. 
70          * It can be: none, create, open, study, knowledge, sysadmin, help.
71          */
72         private String _menuProperty;
73         
74         /**
75          * Value of the title bar property. 
76          * It can be: study, knowledge.
77          */
78         private String _titleProperty;
79         
80         /**
81          * Value of the tool bar property. 
82          * It can be: none, standard, study, back.
83          */
84         private String _toolProperty;
85         
86         /**
87          * Value of the left menu property. 
88          * It can be: open, study, knowledge, scenario.
89          */
90         private String _leftMenuProperty;
91         
92         /**
93          * Property that indicates whether the current open study is editable or not.
94          * On the screen it looks like pen on the status icon, pop-up menu also can be called.
95          * It is necessary for correct building the title bar.
96          */
97         private String _editDisabledProperty = "false";
98
99         // ==============================================================================================================================
100         // Action methods
101         // ==============================================================================================================================
102
103         /**
104          * Initialize the operation.
105          * @return SUCCESS in success, otherwise - ERROR
106          */
107         public String doInitialize() {
108                 
109                 setMenuProperty("study");
110                 setTitleProperty("study");
111                 setEditDisabledProperty("true");
112                 if ("true".equals(getWriteAccess())) {
113                         setToolProperty("study");
114                 } else {
115                         setToolProperty("none");
116                 }
117                 setLeftMenuProperty("study");
118         initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
119               
120                 User user = getConnectedUser();
121                 File updir = getRepositoryService().getDownloadDirectory(user);
122                 File upfile = new File(updir.getPath() + "/" + filename);
123                 String[] table = filename.split("\\x2E");
124                 String filext = table[table.length - 1].toLowerCase();
125
126                 mystudy = getOpenStudy();
127                 Step step = mystudy.getSelectedStep();
128                 doctypes = getStepService().getValidDocumentTypes(step);
129                 deftype = getApplicationSettings().getDefaultDocumentType(step, filext);
130                 defuses = new Vector<Document>();
131                 state = ProgressState.inWORK;
132
133                 Reader tool = Toolbox.getReader(upfile);
134                 if (tool != null) {
135                         String fileref = tool.extractProperty("reference");
136                         String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced
137                         String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced
138                         for (Iterator<DocumentType> i = doctypes.iterator(); i.hasNext();) {
139                                 DocumentType type = i.next();
140                                 if (!type.getName().equals(filetype))
141                                         continue;
142                                 deftype = type;
143                                 doctype = type.getIndex(); // Disables the document type field
144                                 break;
145                         }
146                         if (fileref != null) {
147                                 Document slot = getDocumentService().selectDocument(fileref,
148                                                 new Revision().toString());
149                                 if (slot == null) {
150                                         setErrorCode("message.error.reference.undefined");
151                                         
152                                         setToolProperty("none");
153                                         initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
154                                         
155                                         return ERROR;
156                                 } else {
157                                         if (!slot.isUndefined()) {
158                                                 setErrorCode("message.error.reference.duplicate");
159                                                 
160                                                 setToolProperty("none");
161                                                 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
162                                                 
163                                                 return ERROR;
164                                         }
165                                         docref = fileref; // Disables document name and state fields
166                                         deftype = slot.getType(); // Just in case
167                                         doctype = deftype.getIndex(); // Disables the document type field
168                                 }
169                         }
170                         if (filever != null)
171                                 try {
172                                         Revision.Format get = new Revision.Format(
173                                                         getProjectSettings().getRevisionPattern());
174                                         Revision version = get.parse(filever);
175                                         if (version.isNull())
176                                                 throw new ParseException(filever, filever.length() - 1);
177                                         if (!version.isMinor())
178                                                 state = ProgressState.inCHECK;
179                                         docver = version.toString();
180                                 } catch (ParseException e) {
181                                         setErrorCode("message.error.format.version");
182                                         
183                                         setToolProperty("none");
184                                         initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
185                                         
186                                         return ERROR;
187                                 }
188                         docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
189                         date = tool.extractProperty("date");
190                         if (date != null) {
191                                 ResourceBundle locale = ResourceBundle.getBundle("som",
192                                                 getApplicationSettings().getCurrentLocale());
193                                 SimpleDateFormat check = new SimpleDateFormat(
194                                                 locale.getString("date.format"));
195                                 try {
196                                         check.parse(date);
197                                 } catch (ParseException e) {
198                                         setErrorCode("message.error.format.date");
199                                         
200                                         setToolProperty("none");
201                                         initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
202                                         
203                                         return ERROR;
204                                 }
205                         } else
206                                 date = "";
207                 } else if (filext.equals("pdf"))
208                         state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
209                 if (docname == null) {
210                         docname = table[0];
211                         for (int i = 1; i < table.length - 1; i++)
212                                 docname = docname + "." + table[i];
213                 }
214                 if (deftype != null)
215                         setupDefaultUses(deftype);
216
217                 DocumentType[] types = doctypes.toArray(new DocumentType[doctypes
218                                 .size()]);
219                 DocumentTypeComparator compare = new DocumentTypeComparator();
220                 Arrays.sort(types, compare);
221                 doctypes = Arrays.asList(types);
222
223                 return SUCCESS;
224         }
225
226         /**
227          * Perform import of a document.
228          * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
229          */
230         public String doImport() {
231                 // -------------------------
232                 
233                 setMenuProperty("study");
234                 setTitleProperty("study");
235                 setEditDisabledProperty("true");
236         initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
237             
238                 if (action == ToDo.cancel)
239                         return "cancel";
240                 if (doctype == 0) {
241                         setErrorCode("message.error.import.type");
242                         
243                         setToolProperty("none");
244                         setLeftMenuProperty("study");
245                         initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
246                         
247                         return ERROR;
248                 }
249                 try {
250                         // Getting user inputs
251                         mystudy = getOpenStudy();
252                         User user = getConnectedUser();
253                         Step step = mystudy.getSelectedStep();
254                         DocumentType type = getDocumentTypeService().selectType((int)doctype);
255                         File updir = getRepositoryService().getDownloadDirectory(user);
256                         File upfile = new File(updir.getPath() + "/" + filename);
257                         String[] table = filename.split("\\x2E");
258
259                         // Creation of the document
260                         Document.Properties dprop = new Document.Properties();
261                         Publication addoc;
262
263                         if (docref.length() == 0) { // Importation of a foreign document
264                         // TODO: Extract property of supported documents (DOCX, ODT...)
265                                 addoc = getStepService().createDocument(step, dprop.setName(docname)
266                                                 .setType(type).setFormat(table[table.length - 1])
267                                                 .setAuthor(user));
268                                 updir = addoc.getSourceFile().asFile();
269                                 if (LOG.isInfoEnabled())
270                                         LOG.info("Moving \"" + upfile.getName() + "\" to \""
271                                                         + updir.getPath() + "\".");
272                                 upfile.renameTo(updir);
273                                 try {
274                                         getPublicationService().saveAs(addoc, state); // May throw FileNotFound if rename was not done
275                                 } catch (FileNotFoundException saverror) {
276                                         Thread.sleep(1000);
277                                         LOG.info("Waiting for the file.");
278                                         upfile.renameTo(updir);
279                                         getPublicationService().saveAs(addoc, state); // Forget it if throw again FileNotFound
280                                 }
281                         } else { // Importation of a previously created template-based document
282                                 if (date.length() > 0) {
283                                         ResourceBundle locale = ResourceBundle.getBundle("som",
284                                                         getApplicationSettings().getCurrentLocale());
285                                         SimpleDateFormat get = new SimpleDateFormat(
286                                                         locale.getString("date.format"));
287                                         dprop.setDate(get.parse(date));
288                                 }
289                                 addoc = getStepService().assignDocument(step, dprop.setReference(docref).setName(
290                                                 docname));
291                                 updir = addoc.getSourceFile().asFile();
292                                 if (LOG.isInfoEnabled())
293                                         LOG.info("Moving \"" + upfile.getName() + "\" to \""
294                                                         + updir.getPath() + "\".");
295                                 upfile.renameTo(updir);
296                                 try {
297                                         if (docver.length() > 0)
298                                                 getPublicationService().saveAs(addoc,
299                                                                 new Revision(docver));
300                                         else
301                                                 getPublicationService().saveAs(addoc, state);
302                                 } catch (FileNotFoundException saverror) {
303                                         Thread.sleep(1000);
304                                         LOG.info("Waiting for the file.");
305                                         upfile.renameTo(updir);
306                                         if (docver.length() > 0)
307                                                 getPublicationService().saveAs(addoc,
308                                                                 new Revision(docver));
309                                         else
310                                                 getPublicationService().saveAs(addoc, state);
311                                 }
312                                 mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
313                         }
314                         // Creation of uses relations
315                         if (docuses != null) {
316                                 String[] list = docuses.split(",");
317                                 for (int i = 0; i < list.length; i++) {
318                                         Integer index = Integer.valueOf(list[i].trim());
319                                         Publication used = getPublication(index);
320                                         addoc.addDependency(used);
321                                 }
322                         }
323                         // Creation of derived the document formats
324                         // Document ndoc = addoc.value();
325                         // Converter send = getApplicationSettings().getConverter(ndoc.getType(), ndoc.getFormat());
326                         //
327                         // if (send != null) send.converts(addoc); // Asynchronous process
328
329                         mystudy.add(addoc); // Updates the presentation
330                         return SUCCESS;
331                 } catch (FileNotFoundException error) {
332                         LOG.error("Reason:", error);
333                         setErrorCode("message.error.import.file");
334                 } catch (Exception error) {
335                         LOG.error("Reason:", error);
336                         setErrorCode("message.error.internal");
337                 }
338                 
339                 setToolProperty("none");
340                 setLeftMenuProperty("study");
341                 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
342                 
343                 return ERROR;
344         }
345
346         // ==============================================================================================================================
347         // Getters and setters
348         // ==============================================================================================================================
349
350         public String getDocumentDate() {
351                 // --------------------------------
352                 return date;
353         }
354
355         public List<DocumentType> getDocumentTypes() {
356                 // ---------------------------------------------
357                 return doctypes;
358         }
359
360         public long getDocumentType() {
361                 // -----------------------------
362                 return doctype;
363         }
364
365         public String getReference() {
366                 // -----------------------------
367                 return docref;
368         }
369
370         public String getVersion() {
371                 // ---------------------------
372                 return docver;
373         }
374
375         public void setDocumentDate(final String date) {
376                 // -----------------------------------------
377                 this.date = date;
378         }
379
380         public void setDocumentName(final String name) {
381                 // -----------------------------------------
382                 this.docname = name; // Name entered by the user if enabled
383         }
384
385         public void setDocumentTitle(final String name) { // Called even if DocumentName is enabled
386         // -----------------------------------------
387                 if (this.docname == null)
388                         this.docname = name;
389         }
390
391         public void setDocumentType(final String value) {
392                 // ------------------------------------------
393                 this.doctype = Integer.valueOf(value);
394         }
395
396         public void setDefaultDocumentState(final String state) { // Called even if DocumentState is enabled
397         // --------------------------------------------------
398                 if (this.state == null)
399                         this.state = ProgressState.valueOf(state);
400         }
401
402         public void setDefaultDocumentType(final String value) { // Called even if DocumentType is enabled
403         // --------------------------------------------------
404                 if (this.doctype == 0)
405                         this.doctype = Integer.valueOf(value);
406         }
407
408         public void setReference(final String value) {
409                 // ---------------------------------------
410                 this.docref = value;
411         }
412
413         public void setVersion(final String value) {
414                 // -------------------------------------
415                 this.docver = value;
416         }
417
418         /**
419          * Get project settings.
420          * 
421          * @return Project settings service
422          */
423         private ProjectSettingsService getProjectSettings() {
424                 return _projectSettingsService;
425         }
426
427         /**
428          * Set project settings service.
429          * 
430          * @param projectSettingsService
431          *            project settings service
432          */
433         public void setProjectSettings(final ProjectSettingsService projectSettingsService) {
434                 _projectSettingsService = projectSettingsService;
435         }
436
437         /**
438          * Get the publicationService.
439          * 
440          * @return the publicationService
441          */
442         public PublicationService getPublicationService() {
443                 return _publicationService;
444         }
445
446         /**
447          * Set the publicationService.
448          * 
449          * @param publicationService
450          *            the publicationService to set
451          */
452         public void setPublicationService(final PublicationService publicationService) {
453                 _publicationService = publicationService;
454         }
455
456         /**
457          * Get the stepService.
458          * @return the stepService
459          */
460         public StepService getStepService() {
461                 return _stepService;
462         }
463
464         /**
465          * Set the stepService.
466          * @param stepService the stepService to set
467          */
468         public void setStepService(final StepService stepService) {
469                 _stepService = stepService;
470         }
471
472         /**
473          * Get the documentService.
474          * @return the documentService
475          */
476         public DocumentService getDocumentService() {
477                 return _documentService;
478         }
479
480         /**
481          * Set the documentService.
482          * @param documentService the documentService to set
483          */
484         public void setDocumentService(final DocumentService documentService) {
485                 _documentService = documentService;
486         }
487
488         /**
489          * Get the repositoryService.
490          * @return the repositoryService
491          */
492         public RepositoryService getRepositoryService() {
493                 return _repositoryService;
494         }
495
496         /**
497          * Set the repositoryService.
498          * @param repositoryService the repositoryService to set
499          */
500         public void setRepositoryService(final RepositoryService repositoryService) {
501                 _repositoryService = repositoryService;
502         }
503
504         /**
505          * Get the documentTypeService.
506          * @return the documentTypeService
507          */
508         public DocumentTypeService getDocumentTypeService() {
509                 return _documentTypeService;
510         }
511
512         /**
513          * Set the documentTypeService.
514          * @param documentTypeService the documentTypeService to set
515          */
516         public void setDocumentTypeService(final DocumentTypeService documentTypeService) {
517                 _documentTypeService = documentTypeService;
518         }
519         
520         /**
521          * Get the menuProperty.
522          * @return the menuProperty
523          */
524         public String getMenuProperty() {
525                 return _menuProperty;
526         }
527
528         /**
529          * Set the menuProperty.
530          * @param menuProperty the menuProperty to set
531          */
532         public void setMenuProperty(final String menuProperty) {
533                 this._menuProperty = menuProperty;
534         }
535         
536         /**
537          * Get the _titleProperty.
538          * @return the _titleProperty
539          */
540         public String getTitleProperty() {
541                 return _titleProperty;
542         }
543
544         /**
545          * Set the _titleProperty.
546          * @param _titleProperty the titleProperty to set
547          */
548         public void setTitleProperty(final String titleProperty) {
549                 _titleProperty = titleProperty;
550         }
551
552         /**
553          * Get the editDisabledProperty.
554          * @return the editDisabledProperty
555          */
556         public final String getEditDisabledProperty() {
557                 return _editDisabledProperty;
558         }
559
560         /**
561          * Set the editDisabledProperty.
562          * @param editDisabledProperty the editDisabledProperty to set
563          */
564         public final void setEditDisabledProperty(final String editDisabledProperty) {
565                 _editDisabledProperty = editDisabledProperty;
566         }
567
568         /**
569          * Get the toolProperty.
570          * @return the toolProperty
571          */
572         public String getToolProperty() {
573                 return _toolProperty;
574         }
575
576         /**
577          * Set the toolProperty.
578          * @param toolProperty the toolProperty to set
579          */
580         public void setToolProperty(final String toolProperty) {
581                 _toolProperty = toolProperty;
582         }
583         
584         /**
585          * Get the leftMenuProperty.
586          * @return the leftMenuProperty
587          */
588         public String getLeftMenuProperty() {
589                 return _leftMenuProperty;
590         }
591
592         /**
593          * Set the leftMenuProperty.
594          * @param leftMenuProperty the leftMenuProperty to set
595          */
596         public void setLeftMenuProperty(final String leftMenuProperty) {
597                 _leftMenuProperty = leftMenuProperty;
598         }
599 }