1 package org.splat.simer;
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.Iterator;
10 import java.util.List;
11 import java.util.ResourceBundle;
13 import org.splat.dal.bo.kernel.User;
14 import org.splat.dal.bo.som.Document;
15 import org.splat.dal.bo.som.DocumentType;
16 import org.splat.dal.bo.som.ProgressState;
17 import org.splat.dal.bo.som.Publication;
18 import org.splat.manox.Reader;
19 import org.splat.manox.Toolbox;
20 import org.splat.service.DocumentService;
21 import org.splat.service.DocumentTypeService;
22 import org.splat.service.PublicationService;
23 import org.splat.service.StepService;
24 import org.splat.service.technical.ProjectSettingsService;
25 import org.splat.service.technical.RepositoryService;
26 import org.splat.som.Revision;
27 import org.splat.som.Step;
28 import org.splat.wapp.Constants;
31 * Action for adding a document into a study step.
33 public class ImportDocumentAction extends UploadBaseNextAction {
38 private static final long serialVersionUID = 2587822564883588556L;
40 private transient List<DocumentType> _documentTypes = null;
41 private long _documentType = 0;
43 * Reference extracted from the imported file, if exist.
45 private String _reference = null;
47 * Version number extracted from the imported file, if exist.
49 private String _version = "";
51 * Date extracted from the imported file, if exist.
53 private String _documentDate = "";
55 * Injected project settings service.
57 private ProjectSettingsService _projectSettings;
59 * Injected publication service.
61 private PublicationService _publicationService;
63 * Injected step service.
65 private StepService _stepService;
67 * Injected document service.
69 private DocumentService _documentService;
71 * Injected document type service.
73 private DocumentTypeService _documentTypeService;
75 * Injected repository service.
77 private RepositoryService _repositoryService;
79 * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
81 private String _menuProperty;
84 * Value of the title bar property. It can be: study, knowledge.
86 private String _titleProperty;
89 * Value of the tool bar property. It can be: none, standard, study, back.
91 private String _toolProperty;
94 * Value of the left menu property. It can be: open, study, knowledge, scenario.
96 private String _leftMenuProperty;
99 * 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
100 * menu also can be called. It is necessary for correct building the title bar.
102 private String _editDisabledProperty = "false";
104 // ==============================================================================================================================
106 // ==============================================================================================================================
109 * Initialize the operation.
111 * @return SUCCESS in success, otherwise - ERROR
113 public String doInitialize() {
115 setMenuProperty(Constants.STUDY_MENU);
116 setTitleProperty(Constants.STUDY_MENU);
117 setEditDisabledProperty("true");
118 if ("true".equals(getWriteAccess())) {
119 setToolProperty(Constants.STUDY_MENU);
121 setToolProperty(Constants.NONE);
123 setLeftMenuProperty(Constants.STUDY_MENU);
124 initializationFullScreenContext(_menuProperty, _titleProperty,
125 _editDisabledProperty, _toolProperty, _leftMenuProperty);
127 User user = getConnectedUser();
128 File updir = getRepositoryService().getDownloadDirectory(user);
129 File upfile = new File(updir.getPath() + "/" + filename);
130 String[] table = filename.split("\\x2E");
131 String filext = table[table.length - 1].toLowerCase();
133 mystudy = getOpenStudy();
134 Step step = mystudy.getSelectedStep();
135 _documentTypes = getStepService().getValidDocumentTypes(step);
136 deftype = getApplicationSettings().getDefaultDocumentType(step, filext);
137 defuses = new ArrayList<Document>();
138 state = ProgressState.inWORK;
141 if (extractProperties(upfile, filext)) {
142 if (docname == null) {
144 for (int i = 1; i < table.length - 1; i++) {
145 docname = docname + "." + table[i];
148 if (deftype != null) {
149 setupDefaultUses(deftype);
152 DocumentType[] types = _documentTypes
153 .toArray(new DocumentType[_documentTypes.size()]);
154 DocumentTypeComparator compare = new DocumentTypeComparator();
155 Arrays.sort(types, compare);
156 _documentTypes = Arrays.asList(types);
163 * Extract properties from the uploaded file.
169 * @return true if succeeded, false if error
171 private boolean extractProperties(final File upfile, final String filext) {
173 Reader tool = Toolbox.getReader(upfile);
175 if ("pdf".equals(filext)) {
176 state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
179 String fileref = tool.extractProperty("reference");
180 String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced
181 String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced
182 for (Iterator<DocumentType> i = _documentTypes.iterator(); i
184 DocumentType type = i.next();
185 if (type.getName().equals(filetype)) {
187 _documentType = type.getIndex(); // Disables the document type field
191 if (fileref != null) {
192 isOk = findTypeByDocRef(fileref);
195 if (filever != null) {
197 Revision.Format get = new Revision.Format(
198 getProjectSettings().getRevisionPattern());
199 Revision version = get.parse(filever);
200 if (version.isNull()) {
201 throw new ParseException(filever,
202 filever.length() - 1);
204 if (!version.isMinor()) {
205 state = ProgressState.inCHECK;
207 _version = version.toString();
208 } catch (ParseException e) {
209 setError("message.error.format.version");
214 docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
215 _documentDate = tool.extractProperty("date");
216 if (_documentDate == null) {
219 ResourceBundle locale = ResourceBundle.getBundle("som",
220 getApplicationSettings().getCurrentLocale());
221 SimpleDateFormat check = new SimpleDateFormat(locale
222 .getString("date.format"));
224 check.parse(_documentDate);
225 } catch (ParseException e) {
226 setError("message.error.format.date");
237 * Find document type by document reference.
240 * the document reference
241 * @return true if succeeded, false if error
243 private boolean findTypeByDocRef(final String fileref) {
245 Document slot = getDocumentService().selectDocument(fileref,
246 new Revision().toString());
248 setError("message.error.reference.undefined");
251 if (slot.isUndefined()) {
252 _reference = fileref; // Disables document name and state fields
253 deftype = slot.getType(); // Just in case
254 _documentType = deftype.getIndex(); // Disables the document type field
256 setError("message.error.reference.duplicate");
264 * Set error message and menus.
269 private void setError(final String errorCode) {
270 setErrorCode(errorCode);
272 setToolProperty(Constants.NONE);
273 initializationFullScreenContext(_menuProperty, _titleProperty,
274 _editDisabledProperty, _toolProperty, _leftMenuProperty);
278 * Perform import of a document.
280 * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
282 public String doImport() {
284 setMenuProperty(Constants.STUDY_MENU);
285 setTitleProperty(Constants.STUDY_MENU);
286 setEditDisabledProperty("true");
287 initializationScreenContext(_menuProperty, _titleProperty,
288 _editDisabledProperty);
290 if (action == ToDo.cancel) {
293 if (_documentType == 0) {
294 setErrorCode("message.error.import.type");
296 setToolProperty(Constants.NONE);
297 setLeftMenuProperty(Constants.STUDY_MENU);
298 initializationFullScreenContext(_menuProperty, _titleProperty,
299 _editDisabledProperty, _toolProperty, _leftMenuProperty);
304 // Getting user inputs
305 mystudy = getOpenStudy();
306 User user = getConnectedUser();
307 Step step = mystudy.getSelectedStep();
308 DocumentType type = getDocumentTypeService().selectType(
309 (int) _documentType);
310 File updir = getRepositoryService().getDownloadDirectory(user);
311 File upfile = new File(updir.getPath() + "/" + filename);
312 String[] table = filename.split("\\x2E");
314 // Creation of the document
315 Document.Properties dprop = new Document.Properties();
318 if (_reference.length() == 0) { // Importation of a foreign document
319 // TODO: Extract property of supported documents (DOCX, ODT...)
320 addoc = getStepService().createDocument(
322 dprop.setName(docname).setType(type).setFormat(
323 table[table.length - 1]).setAuthor(user));
324 updir = addoc.getSourceFile().asFile();
325 if (LOG.isInfoEnabled()) {
326 LOG.info("Moving \"" + upfile.getName() + "\" to \""
327 + updir.getPath() + "\".");
329 upfile.renameTo(updir);
331 getPublicationService().saveAs(addoc, state); // May throw FileNotFound if rename was not done
332 } catch (FileNotFoundException saverror) {
334 LOG.info("Waiting for the file.");
335 upfile.renameTo(updir);
336 getPublicationService().saveAs(addoc, state); // Forget it if throw again FileNotFound
338 } else { // Importation of a previously created template-based document
339 if (_documentDate.length() > 0) {
340 ResourceBundle locale = ResourceBundle.getBundle("som",
341 getApplicationSettings().getCurrentLocale());
342 SimpleDateFormat get = new SimpleDateFormat(locale
343 .getString("date.format"));
344 dprop.setDate(get.parse(_documentDate));
346 addoc = getStepService().assignDocument(step,
347 dprop.setReference(_reference).setName(docname));
348 updir = addoc.getSourceFile().asFile();
349 if (LOG.isInfoEnabled()) {
350 LOG.info("Moving \"" + upfile.getName() + "\" to \""
351 + updir.getPath() + "\".");
353 upfile.renameTo(updir);
355 if (_version.length() > 0) {
356 getPublicationService().saveAs(addoc,
357 new Revision(_version));
359 getPublicationService().saveAs(addoc, state);
361 } catch (FileNotFoundException saverror) {
363 LOG.info("Waiting for the file.");
364 upfile.renameTo(updir);
365 if (_version.length() > 0) {
366 getPublicationService().saveAs(addoc,
367 new Revision(_version));
369 getPublicationService().saveAs(addoc, state);
372 mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
374 // Creation of uses relations
375 if (docuses != null) {
376 String[] list = docuses.split(",");
377 for (int i = 0; i < list.length; i++) {
378 Integer index = Integer.valueOf(list[i].trim());
379 Publication used = getPublication(index);
380 addoc.addDependency(used);
383 // Creation of derived the document formats
384 // Document ndoc = addoc.value();
385 // Converter send = getApplicationSettings().getConverter(ndoc.getType(), ndoc.getFormat());
387 // if (send != null) send.converts(addoc); // Asynchronous process
389 mystudy.add(addoc); // Updates the presentation
391 } catch (FileNotFoundException error) {
392 LOG.error("Reason:", error);
393 setErrorCode("message.error.import.file");
394 } catch (Exception error) {
395 LOG.error("Reason:", error);
396 setErrorCode("message.error.internal");
399 setToolProperty(Constants.NONE);
400 setLeftMenuProperty(Constants.STUDY_MENU);
401 initializationFullScreenContext(_menuProperty, _titleProperty,
402 _editDisabledProperty, _toolProperty, _leftMenuProperty);
407 // ==============================================================================================================================
408 // Getters and setters
409 // ==============================================================================================================================
412 * Date extracted from the imported file, if exist.
414 * @return the file date
416 public String getDocumentDate() {
417 return _documentDate;
420 public List<DocumentType> getDocumentTypes() {
421 return _documentTypes;
425 * Get document type id.
427 * @return document type id
429 public long getDocumentType() {
430 return _documentType;
434 * Get reference extracted from the imported file, if exist.
436 * @return the document reference
438 public String getReference() {
443 * Get version number extracted from the imported file, if exist.
445 * @return the document version
447 public String getVersion() {
452 * Set date extracted from the imported file.
457 public void setDocumentDate(final String date) {
458 this._documentDate = date;
462 * Set document name entered by the user if enabled.
467 public void setDocumentName(final String name) {
468 this.docname = name; // Name entered by the user if enabled
471 public void setDocumentTitle(final String name) { // Called even if DocumentName is enabled
472 if (this.docname == null) {
478 * Set document type id.
483 public void setDocumentType(final String value) {
484 this._documentType = Integer.valueOf(value);
487 public void setDefaultDocumentState(final String state) { // Called even if DocumentState is enabled
488 if (this.state == null) {
489 this.state = ProgressState.valueOf(state);
493 public void setDefaultDocumentType(final String value) { // Called even if DocumentType is enabled
494 if (this._documentType == 0) {
495 this._documentType = Integer.valueOf(value);
500 * Set document reference extracted from the imported file, if exist.
505 public void setReference(final String value) {
506 this._reference = value;
510 * Set version number extracted from the imported file, if exist.
515 public void setVersion(final String value) {
516 this._version = value;
520 * Get project settings.
522 * @return Project settings service
524 private ProjectSettingsService getProjectSettings() {
525 return _projectSettings;
529 * Set project settings service.
531 * @param projectSettingsService
532 * project settings service
534 public void setProjectSettings(
535 final ProjectSettingsService projectSettingsService) {
536 _projectSettings = projectSettingsService;
540 * Get the publicationService.
542 * @return the publicationService
544 public PublicationService getPublicationService() {
545 return _publicationService;
549 * Set the publicationService.
551 * @param publicationService
552 * the publicationService to set
554 public void setPublicationService(
555 final PublicationService publicationService) {
556 _publicationService = publicationService;
560 * Get the stepService.
562 * @return the stepService
564 public StepService getStepService() {
569 * Set the stepService.
572 * the stepService to set
574 public void setStepService(final StepService stepService) {
575 _stepService = stepService;
579 * Get the documentService.
581 * @return the documentService
583 public DocumentService getDocumentService() {
584 return _documentService;
588 * Set the documentService.
590 * @param documentService
591 * the documentService to set
593 public void setDocumentService(final DocumentService documentService) {
594 _documentService = documentService;
598 * Get the repositoryService.
600 * @return the repositoryService
602 public RepositoryService getRepositoryService() {
603 return _repositoryService;
607 * Set the repositoryService.
609 * @param repositoryService
610 * the repositoryService to set
612 public void setRepositoryService(final RepositoryService repositoryService) {
613 _repositoryService = repositoryService;
617 * Get the documentTypeService.
619 * @return the documentTypeService
621 public DocumentTypeService getDocumentTypeService() {
622 return _documentTypeService;
626 * Set the documentTypeService.
628 * @param documentTypeService
629 * the documentTypeService to set
631 public void setDocumentTypeService(
632 final DocumentTypeService documentTypeService) {
633 _documentTypeService = documentTypeService;
637 * Get the menuProperty.
639 * @return the menuProperty
641 public String getMenuProperty() {
642 return _menuProperty;
646 * Set the menuProperty.
648 * @param menuProperty
649 * the menuProperty to set
651 public void setMenuProperty(final String menuProperty) {
652 this._menuProperty = menuProperty;
656 * Get the _titleProperty.
658 * @return the _titleProperty
660 public String getTitleProperty() {
661 return _titleProperty;
665 * Set the titleProperty.
667 * @param titleProperty
668 * the titleProperty to set
670 public void setTitleProperty(final String titleProperty) {
671 _titleProperty = titleProperty;
675 * Get the editDisabledProperty.
677 * @return the editDisabledProperty
679 public final String getEditDisabledProperty() {
680 return _editDisabledProperty;
684 * Set the editDisabledProperty.
686 * @param editDisabledProperty
687 * the editDisabledProperty to set
689 public final void setEditDisabledProperty(final String editDisabledProperty) {
690 _editDisabledProperty = editDisabledProperty;
694 * Get the toolProperty.
696 * @return the toolProperty
698 public String getToolProperty() {
699 return _toolProperty;
703 * Set the toolProperty.
705 * @param toolProperty
706 * the toolProperty to set
708 public void setToolProperty(final String toolProperty) {
709 _toolProperty = toolProperty;
713 * Get the leftMenuProperty.
715 * @return the leftMenuProperty
717 public String getLeftMenuProperty() {
718 return _leftMenuProperty;
722 * Set the leftMenuProperty.
724 * @param leftMenuProperty
725 * the leftMenuProperty to set
727 public void setLeftMenuProperty(final String leftMenuProperty) {
728 _leftMenuProperty = leftMenuProperty;