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;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.ResourceBundle;
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;
32 * Action for adding a document into a study step.
34 public class ImportDocumentAction extends UploadBaseNextAction {
39 private static final long serialVersionUID = 2587822564883588556L;
41 private transient List<DocumentType> _documentTypes = null;
42 private long _documentType = 0;
44 * Reference extracted from the imported file, if exist.
46 private String _reference = null;
48 * Version number extracted from the imported file, if exist.
50 private String _version = "";
52 * Date extracted from the imported file, if exist.
54 private String _documentDate = "";
56 * Injected project settings service.
58 private ProjectSettingsService _projectSettings;
60 * Injected publication service.
62 private PublicationService _publicationService;
64 * Injected step service.
66 private StepService _stepService;
68 * Injected document service.
70 private DocumentService _documentService;
72 * Injected document type service.
74 private DocumentTypeService _documentTypeService;
76 * Injected repository service.
78 private RepositoryService _repositoryService;
80 * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
82 private String _menuProperty;
85 * Value of the title bar property. It can be: study, knowledge.
87 private String _titleProperty;
90 * Value of the tool bar property. It can be: none, standard, study, back.
92 private String _toolProperty;
95 * Value of the left menu property. It can be: open, study, knowledge, scenario.
97 private String _leftMenuProperty;
100 * 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
101 * menu also can be called. It is necessary for correct building the title bar.
103 private String _editDisabledProperty = "false";
105 // ==============================================================================================================================
107 // ==============================================================================================================================
110 * Initialize the operation.
112 * @return SUCCESS in success, otherwise - ERROR
114 public String doInitialize() {
116 setMenuProperty(Constants.STUDY_MENU);
117 setTitleProperty(Constants.STUDY_MENU);
118 setEditDisabledProperty("true");
119 if ("true".equals(getWriteAccess())) {
120 setToolProperty(Constants.STUDY_MENU);
122 setToolProperty(Constants.NONE);
124 setLeftMenuProperty(Constants.STUDY_MENU);
125 initializationFullScreenContext(_menuProperty, _titleProperty,
126 _editDisabledProperty, _toolProperty, _leftMenuProperty);
128 User user = getConnectedUser();
129 File updir = getRepositoryService().getDownloadDirectory(user);
130 File upfile = new File(updir.getPath() + "/" + filename);
131 String[] table = filename.split("\\x2E");
132 String filext = table[table.length - 1].toLowerCase();
134 mystudy = getOpenStudy();
135 Step step = mystudy.getSelectedStep();
136 _documentTypes = getStepService().getValidDocumentTypes(step);
137 deftype = getApplicationSettings().getDefaultDocumentType(step, filext);
138 defuses = new ArrayList<Document>();
139 state = ProgressState.inWORK;
142 if (extractProperties(upfile, filext)) {
143 if (docname == null) {
145 for (int i = 1; i < table.length - 1; i++) {
146 docname = docname + "." + table[i];
149 if (deftype != null) {
150 setupDefaultUses(deftype);
153 DocumentType[] types = _documentTypes
154 .toArray(new DocumentType[_documentTypes.size()]);
155 DocumentTypeComparator compare = new DocumentTypeComparator();
156 Arrays.sort(types, compare);
157 _documentTypes = Arrays.asList(types);
164 * Extract properties from the uploaded file.
170 * @return true if succeeded, false if error
172 private boolean extractProperties(final File upfile, final String filext) {
174 Reader tool = Toolbox.getReader(upfile);
176 if ("pdf".equals(filext)) {
177 state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
180 String fileref = tool.extractProperty("reference");
181 String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced
182 String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced
183 for (Iterator<DocumentType> i = _documentTypes.iterator(); i
185 DocumentType type = i.next();
186 if (type.getName().equals(filetype)) {
188 _documentType = type.getIndex(); // Disables the document type field
192 if (fileref != null) {
193 isOk = findTypeByDocRef(fileref);
196 if (filever != null) {
198 Revision.Format get = new Revision.Format(
199 getProjectSettings().getRevisionPattern());
200 Revision version = get.parse(filever);
201 if (version.isNull()) {
202 throw new ParseException(filever,
203 filever.length() - 1);
205 if (!version.isMinor()) {
206 state = ProgressState.inCHECK;
208 _version = version.toString();
209 } catch (ParseException e) {
210 setError("message.error.format.version");
215 docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
216 _documentDate = tool.extractProperty("date");
217 if (_documentDate == null) {
220 ResourceBundle locale = ResourceBundle.getBundle("som",
221 getApplicationSettings().getCurrentLocale());
222 SimpleDateFormat check = new SimpleDateFormat(locale
223 .getString("date.format"));
225 check.parse(_documentDate);
226 } catch (ParseException e) {
227 setError("message.error.format.date");
238 * Find document type by document reference.
241 * the document reference
242 * @return true if succeeded, false if error
244 private boolean findTypeByDocRef(final String fileref) {
246 Document slot = getDocumentService().selectDocument(fileref,
247 new Revision().toString());
249 setError("message.error.reference.undefined");
252 if (slot.isUndefined()) {
253 _reference = fileref; // Disables document name and state fields
254 deftype = slot.getType(); // Just in case
255 _documentType = deftype.getIndex(); // Disables the document type field
257 setError("message.error.reference.duplicate");
265 * Set error message and menus.
270 private void setError(final String errorCode) {
271 setErrorCode(errorCode);
273 setToolProperty(Constants.NONE);
274 initializationFullScreenContext(_menuProperty, _titleProperty,
275 _editDisabledProperty, _toolProperty, _leftMenuProperty);
279 * Perform import of a document.
281 * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
283 public String doImport() {
285 setMenuProperty(Constants.STUDY_MENU);
286 setTitleProperty(Constants.STUDY_MENU);
287 setEditDisabledProperty("true");
288 initializationScreenContext(_menuProperty, _titleProperty,
289 _editDisabledProperty);
291 if (action == ToDo.cancel) {
294 if (_documentType == 0) {
295 setErrorCode("message.error.import.type");
297 setToolProperty(Constants.NONE);
298 setLeftMenuProperty(Constants.STUDY_MENU);
299 initializationFullScreenContext(_menuProperty, _titleProperty,
300 _editDisabledProperty, _toolProperty, _leftMenuProperty);
305 // Getting user inputs
306 mystudy = getOpenStudy();
307 User user = getConnectedUser();
308 Step step = mystudy.getSelectedStep();
310 if (_documentDate.length() > 0) {
311 ResourceBundle locale = ResourceBundle.getBundle("som",
312 getApplicationSettings().getCurrentLocale());
313 SimpleDateFormat get = new SimpleDateFormat(locale
314 .getString("date.format"));
315 docdate = get.parse(_documentDate);
317 // Creation of uses relations
318 List<Long> uses = new ArrayList<Long>();
319 if (docuses != null) {
320 String[] list = docuses.split(",");
321 for (int i = 0; i < list.length; i++) {
322 uses.add(Long.valueOf(list[i].trim()));
325 if (LOG.isDebugEnabled()) {
326 LOG.debug("Document to be imported uses documents with following ids:");
327 for (Long usesId: uses) {
328 LOG.debug("#" + usesId);
331 Publication addoc = getPublicationService().createDoc(
332 mystudy.getIndex(), step, _documentType, user.getIndex(),
333 filename, docname, state, _reference, _version, docdate,
336 if (_reference.length() > 0) { // Importation of a not foreign document
337 mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
340 // Creation of derived the document formats
341 // Document ndoc = addoc.value();
342 // Converter send = getApplicationSettings().getConverter(ndoc.getType(), ndoc.getFormat());
344 // if (send != null) send.converts(addoc); // Asynchronous process
346 mystudy.add(addoc); // Updates the presentation
348 } catch (FileNotFoundException error) {
349 LOG.error("Reason:", error);
350 setErrorCode("message.error.import.file");
351 } catch (Exception error) {
352 LOG.error("Reason:", error);
353 setErrorCode("message.error.internal");
356 setToolProperty(Constants.NONE);
357 setLeftMenuProperty(Constants.STUDY_MENU);
358 initializationFullScreenContext(_menuProperty, _titleProperty,
359 _editDisabledProperty, _toolProperty, _leftMenuProperty);
364 // ==============================================================================================================================
365 // Getters and setters
366 // ==============================================================================================================================
369 * Date extracted from the imported file, if exist.
371 * @return the file date
373 public String getDocumentDate() {
374 return _documentDate;
377 public List<DocumentType> getDocumentTypes() {
378 return _documentTypes;
382 * Get document type id.
384 * @return document type id
386 public long getDocumentType() {
387 return _documentType;
391 * Get reference extracted from the imported file, if exist.
393 * @return the document reference
395 public String getReference() {
400 * Get version number extracted from the imported file, if exist.
402 * @return the document version
404 public String getVersion() {
409 * Set date extracted from the imported file.
414 public void setDocumentDate(final String date) {
415 this._documentDate = date;
419 * Set document name entered by the user if enabled.
424 public void setDocumentName(final String name) {
425 this.docname = name; // Name entered by the user if enabled
428 public void setDocumentTitle(final String name) { // Called even if DocumentName is enabled
429 if (this.docname == null) {
435 * Set document type id.
440 public void setDocumentType(final String value) {
441 this._documentType = Integer.valueOf(value);
444 public void setDefaultDocumentState(final String state) { // Called even if DocumentState is enabled
445 if (this.state == null) {
446 this.state = ProgressState.valueOf(state);
450 public void setDefaultDocumentType(final String value) { // Called even if DocumentType is enabled
451 if (this._documentType == 0) {
452 this._documentType = Integer.valueOf(value);
457 * Set document reference extracted from the imported file, if exist.
462 public void setReference(final String value) {
463 this._reference = value;
467 * Set version number extracted from the imported file, if exist.
472 public void setVersion(final String value) {
473 this._version = value;
477 * Get project settings.
479 * @return Project settings service
481 private ProjectSettingsService getProjectSettings() {
482 return _projectSettings;
486 * Set project settings service.
488 * @param projectSettingsService
489 * project settings service
491 public void setProjectSettings(
492 final ProjectSettingsService projectSettingsService) {
493 _projectSettings = projectSettingsService;
497 * Get the publicationService.
499 * @return the publicationService
501 public PublicationService getPublicationService() {
502 return _publicationService;
506 * Set the publicationService.
508 * @param publicationService
509 * the publicationService to set
511 public void setPublicationService(
512 final PublicationService publicationService) {
513 _publicationService = publicationService;
517 * Get the stepService.
519 * @return the stepService
521 public StepService getStepService() {
526 * Set the stepService.
529 * the stepService to set
531 public void setStepService(final StepService stepService) {
532 _stepService = stepService;
536 * Get the documentService.
538 * @return the documentService
540 public DocumentService getDocumentService() {
541 return _documentService;
545 * Set the documentService.
547 * @param documentService
548 * the documentService to set
550 public void setDocumentService(final DocumentService documentService) {
551 _documentService = documentService;
555 * Get the repositoryService.
557 * @return the repositoryService
559 public RepositoryService getRepositoryService() {
560 return _repositoryService;
564 * Set the repositoryService.
566 * @param repositoryService
567 * the repositoryService to set
569 public void setRepositoryService(final RepositoryService repositoryService) {
570 _repositoryService = repositoryService;
574 * Get the documentTypeService.
576 * @return the documentTypeService
578 public DocumentTypeService getDocumentTypeService() {
579 return _documentTypeService;
583 * Set the documentTypeService.
585 * @param documentTypeService
586 * the documentTypeService to set
588 public void setDocumentTypeService(
589 final DocumentTypeService documentTypeService) {
590 _documentTypeService = documentTypeService;
594 * Get the menuProperty.
596 * @return the menuProperty
598 public String getMenuProperty() {
599 return _menuProperty;
603 * Set the menuProperty.
605 * @param menuProperty
606 * the menuProperty to set
608 public void setMenuProperty(final String menuProperty) {
609 this._menuProperty = menuProperty;
613 * Get the _titleProperty.
615 * @return the _titleProperty
617 public String getTitleProperty() {
618 return _titleProperty;
622 * Set the titleProperty.
624 * @param titleProperty
625 * the titleProperty to set
627 public void setTitleProperty(final String titleProperty) {
628 _titleProperty = titleProperty;
632 * Get the editDisabledProperty.
634 * @return the editDisabledProperty
636 public final String getEditDisabledProperty() {
637 return _editDisabledProperty;
641 * Set the editDisabledProperty.
643 * @param editDisabledProperty
644 * the editDisabledProperty to set
646 public final void setEditDisabledProperty(final String editDisabledProperty) {
647 _editDisabledProperty = editDisabledProperty;
651 * Get the toolProperty.
653 * @return the toolProperty
655 public String getToolProperty() {
656 return _toolProperty;
660 * Set the toolProperty.
662 * @param toolProperty
663 * the toolProperty to set
665 public void setToolProperty(final String toolProperty) {
666 _toolProperty = toolProperty;
670 * Get the leftMenuProperty.
672 * @return the leftMenuProperty
674 public String getLeftMenuProperty() {
675 return _leftMenuProperty;
679 * Set the leftMenuProperty.
681 * @param leftMenuProperty
682 * the leftMenuProperty to set
684 public void setLeftMenuProperty(final String leftMenuProperty) {
685 _leftMenuProperty = leftMenuProperty;