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