X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=Workspace%2FSiman%2Fsrc%2Forg%2Fsplat%2Fsimer%2FImportDocumentAction.java;h=0a45e294ae61ddce30b4bc49459990f40478e309;hb=f58726d58df847144c71d10de8c6f1918c28c77a;hp=02d9070f37596f0b3e42b6d1e9de294fe97a2b1d;hpb=330b4ef8e6c87a25897847aaade03d2445bafb8c;p=tools%2Fsiman.git diff --git a/Workspace/Siman/src/org/splat/simer/ImportDocumentAction.java b/Workspace/Siman/src/org/splat/simer/ImportDocumentAction.java index 02d9070..0a45e29 100644 --- a/Workspace/Siman/src/org/splat/simer/ImportDocumentAction.java +++ b/Workspace/Siman/src/org/splat/simer/ImportDocumentAction.java @@ -4,32 +4,33 @@ import java.io.File; import java.io.FileNotFoundException; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.ResourceBundle; -import java.util.Vector; -import org.hibernate.HibernateException; -import org.hibernate.Session; -import org.hibernate.Transaction; import org.splat.dal.bo.kernel.User; -import org.splat.manox.Reader; -import org.splat.manox.Toolbox; -import org.splat.dal.dao.som.Database; import org.splat.dal.bo.som.Document; +import org.splat.dal.bo.som.DocumentType; import org.splat.dal.bo.som.ProgressState; +import org.splat.dal.bo.som.Publication; +import org.splat.manox.Reader; +import org.splat.manox.Toolbox; import org.splat.service.DocumentService; import org.splat.service.DocumentTypeService; import org.splat.service.PublicationService; import org.splat.service.StepService; import org.splat.service.technical.ProjectSettingsService; import org.splat.service.technical.RepositoryService; -import org.splat.dal.bo.som.Publication; -import org.splat.dal.bo.som.DocumentType; import org.splat.som.Revision; import org.splat.som.Step; +import org.splat.wapp.Constants; +/** + * Action for adding a document into a study step. + */ public class ImportDocumentAction extends UploadBaseNextAction { /** @@ -37,15 +38,24 @@ public class ImportDocumentAction extends UploadBaseNextAction { */ private static final long serialVersionUID = 2587822564883588556L; - private List doctypes = null; - private long doctype = 0; - private String docref = null; // Reference extracted from the imported file, if exist - private String docver = ""; // Version number extracted from the imported file, if exist - private String date = ""; // Date extracted from the imported file, if exist + private transient List _documentTypes = null; + private long _documentType = 0; + /** + * Reference extracted from the imported file, if exist. + */ + private String _reference = null; + /** + * Version number extracted from the imported file, if exist. + */ + private String _version = ""; + /** + * Date extracted from the imported file, if exist. + */ + private String _documentDate = ""; /** * Injected project settings service. */ - private ProjectSettingsService _projectSettingsService; + private ProjectSettingsService _projectSettings; /** * Injected publication service. */ @@ -66,6 +76,31 @@ public class ImportDocumentAction extends UploadBaseNextAction { * Injected repository service. */ private RepositoryService _repositoryService; + /** + * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help. + */ + private String _menuProperty; + + /** + * Value of the title bar property. It can be: study, knowledge. + */ + private String _titleProperty; + + /** + * Value of the tool bar property. It can be: none, standard, study, back. + */ + private String _toolProperty; + + /** + * Value of the left menu property. It can be: open, study, knowledge, scenario. + */ + private String _leftMenuProperty; + + /** + * 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 + * menu also can be called. It is necessary for correct building the title bar. + */ + private String _editDisabledProperty = "false"; // ============================================================================================================================== // Action methods @@ -73,12 +108,23 @@ public class ImportDocumentAction extends UploadBaseNextAction { /** * Initialize the operation. + * * @return SUCCESS in success, otherwise - ERROR */ public String doInitialize() { - // ----------------------------- - Session connex = Database.getSession(); - Transaction transax = connex.beginTransaction(); + + setMenuProperty(Constants.STUDY_MENU); + setTitleProperty(Constants.STUDY_MENU); + setEditDisabledProperty("true"); + if ("true".equals(getWriteAccess())) { + setToolProperty(Constants.STUDY_MENU); + } else { + setToolProperty(Constants.NONE); + } + setLeftMenuProperty(Constants.STUDY_MENU); + initializationFullScreenContext(_menuProperty, _titleProperty, + _editDisabledProperty, _toolProperty, _leftMenuProperty); + User user = getConnectedUser(); File updir = getRepositoryService().getDownloadDirectory(user); File upfile = new File(updir.getPath() + "/" + filename); @@ -87,201 +133,231 @@ public class ImportDocumentAction extends UploadBaseNextAction { mystudy = getOpenStudy(); Step step = mystudy.getSelectedStep(); - doctypes = getStepService().getValidDocumentTypes(step); - deftype = ApplicationSettings.getDefaultDocumentType(step, filext); - defuses = new Vector(); + _documentTypes = getStepService().getValidDocumentTypes(step); + deftype = getApplicationSettings().getDefaultDocumentType(step, filext); + defuses = new ArrayList(); state = ProgressState.inWORK; + String res = ERROR; + if (extractProperties(upfile, filext)) { + if (docname == null) { + docname = table[0]; + for (int i = 1; i < table.length - 1; i++) { + docname = docname + "." + table[i]; + } + } + if (deftype != null) { + setupDefaultUses(deftype); + } + + DocumentType[] types = _documentTypes + .toArray(new DocumentType[_documentTypes.size()]); + DocumentTypeComparator compare = new DocumentTypeComparator(); + Arrays.sort(types, compare); + _documentTypes = Arrays.asList(types); + res = SUCCESS; + } + return res; + } + + /** + * Extract properties from the uploaded file. + * + * @param upfile + * the file to parse + * @param filext + * the file extension + * @return true if succeeded, false if error + */ + private boolean extractProperties(final File upfile, final String filext) { + boolean isOk = true; Reader tool = Toolbox.getReader(upfile); - if (tool != null) { + if (tool == null) { + if ("pdf".equals(filext)) { + state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ? + } + } else { String fileref = tool.extractProperty("reference"); String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced - for (Iterator i = doctypes.iterator(); i.hasNext();) { + for (Iterator i = _documentTypes.iterator(); i + .hasNext();) { DocumentType type = i.next(); - if (!type.getName().equals(filetype)) - continue; - deftype = type; - doctype = type.getIndex(); // Disables the document type field - break; + if (type.getName().equals(filetype)) { + deftype = type; + _documentType = type.getIndex(); // Disables the document type field + break; + } } if (fileref != null) { - Document slot = getDocumentService().selectDocument(fileref, - new Revision().toString()); - if (slot == null) { - setErrorCode("reference.undefined"); - return ERROR; - } else { - if (!slot.isUndefined()) { - setErrorCode("reference.duplicate"); - return ERROR; - } - docref = fileref; // Disables document name and state fields - deftype = slot.getType(); // Just in case - doctype = deftype.getIndex(); // Disables the document type field - } + isOk = findTypeByDocRef(fileref); } - if (filever != null) - try { - Revision.Format get = new Revision.Format( - getProjectSettings().getRevisionPattern()); - Revision version = get.parse(filever); - if (version.isNull()) - throw new ParseException(filever, filever.length() - 1); - if (!version.isMinor()) - state = ProgressState.inCHECK; - docver = version.toString(); - } catch (ParseException e) { - setErrorCode("format.version"); - return ERROR; + if (isOk) { + if (filever != null) { + try { + Revision.Format get = new Revision.Format( + getProjectSettings().getRevisionPattern()); + Revision version = get.parse(filever); + if (version.isNull()) { + throw new ParseException(filever, + filever.length() - 1); + } + if (!version.isMinor()) { + state = ProgressState.inCHECK; + } + _version = version.toString(); + } catch (ParseException e) { + setError("message.error.format.version"); + isOk = false; + } } - docname = tool.extractProperty("title"); // Property kept even if the file is not referenced - date = tool.extractProperty("date"); - if (date != null) { - ResourceBundle locale = ResourceBundle.getBundle("som", - ApplicationSettings.getCurrentLocale()); - SimpleDateFormat check = new SimpleDateFormat( - locale.getString("date.format")); - try { - check.parse(date); - } catch (ParseException e) { - setErrorCode("format.date"); - return ERROR; + if (isOk) { + docname = tool.extractProperty("title"); // Property kept even if the file is not referenced + _documentDate = tool.extractProperty("date"); + if (_documentDate == null) { + _documentDate = ""; + } else { + ResourceBundle locale = ResourceBundle.getBundle("som", + getApplicationSettings().getCurrentLocale()); + SimpleDateFormat check = new SimpleDateFormat(locale + .getString("date.format")); + try { + check.parse(_documentDate); + } catch (ParseException e) { + setError("message.error.format.date"); + isOk = false; + } + } } - } else - date = ""; - } else if (filext.equals("pdf")) - state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ? - if (docname == null) { - docname = table[0]; - for (int i = 1; i < table.length - 1; i++) - docname = docname + "." + table[i]; + } } - if (deftype != null) - setupDefaultUses(deftype); + return isOk; + } - DocumentType[] types = doctypes.toArray(new DocumentType[doctypes - .size()]); - DocumentTypeComparator compare = new DocumentTypeComparator(); - Arrays.sort(types, compare); - doctypes = Arrays.asList(types); + /** + * Find document type by document reference. + * + * @param fileref + * the document reference + * @return true if succeeded, false if error + */ + private boolean findTypeByDocRef(final String fileref) { + boolean isOk = true; + Document slot = getDocumentService().selectDocument(fileref, + new Revision().toString()); + if (slot == null) { + setError("message.error.reference.undefined"); + isOk = false; + } else { + if (slot.isUndefined()) { + _reference = fileref; // Disables document name and state fields + deftype = slot.getType(); // Just in case + _documentType = deftype.getIndex(); // Disables the document type field + } else { + setError("message.error.reference.duplicate"); + isOk = false; + } + } + return isOk; + } + + /** + * Set error message and menus. + * + * @param errorCode + * error message key + */ + private void setError(final String errorCode) { + setErrorCode(errorCode); - transax.commit(); - return SUCCESS; + setToolProperty(Constants.NONE); + initializationFullScreenContext(_menuProperty, _titleProperty, + _editDisabledProperty, _toolProperty, _leftMenuProperty); } /** * Perform import of a document. + * * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error */ public String doImport() { - // ------------------------- - if (action == ToDo.cancel) + + setMenuProperty(Constants.STUDY_MENU); + setTitleProperty(Constants.STUDY_MENU); + setEditDisabledProperty("true"); + initializationScreenContext(_menuProperty, _titleProperty, + _editDisabledProperty); + + if (action == ToDo.cancel) { return "cancel"; - if (doctype == 0) { - setErrorCode("import.type"); + } + if (_documentType == 0) { + setErrorCode("message.error.import.type"); + + setToolProperty(Constants.NONE); + setLeftMenuProperty(Constants.STUDY_MENU); + initializationFullScreenContext(_menuProperty, _titleProperty, + _editDisabledProperty, _toolProperty, _leftMenuProperty); + return ERROR; } - Session connex = Database.getSession(); - Transaction transax = connex.beginTransaction(); try { // Getting user inputs mystudy = getOpenStudy(); User user = getConnectedUser(); Step step = mystudy.getSelectedStep(); - DocumentType type = getDocumentTypeService().selectType((int)doctype); - File updir = getRepositoryService().getDownloadDirectory(user); - File upfile = new File(updir.getPath() + "/" + filename); - String[] table = filename.split("\\x2E"); - - // Creation of the document - Document.Properties dprop = new Document.Properties(); - Publication addoc; - - if (docref.length() == 0) { // Importation of a foreign document - // TODO: Extract property of supported documents (DOCX, ODT...) - addoc = getStepService().createDocument(step, dprop.setName(docname) - .setType(type).setFormat(table[table.length - 1]) - .setAuthor(user)); - updir = addoc.getSourceFile().asFile(); - if (logger.isInfoEnabled()) - logger.info("Moving \"" + upfile.getName() + "\" to \"" - + updir.getPath() + "\"."); - upfile.renameTo(updir); - try { - getPublicationService().saveAs(addoc, state); // May throw FileNotFound if rename was not done - } catch (FileNotFoundException saverror) { - Thread.sleep(1000); - logger.info("Waiting for the file."); - upfile.renameTo(updir); - getPublicationService().saveAs(addoc, state); // Forget it if throw again FileNotFound - } - } else { // Importation of a previously created template-based document - if (date.length() > 0) { - ResourceBundle locale = ResourceBundle.getBundle("som", - ApplicationSettings.getCurrentLocale()); - SimpleDateFormat get = new SimpleDateFormat( - locale.getString("date.format")); - dprop.setDate(get.parse(date)); - } - addoc = getStepService().assignDocument(step, dprop.setReference(docref).setName( - docname)); - updir = addoc.getSourceFile().asFile(); - if (logger.isInfoEnabled()) - logger.info("Moving \"" + upfile.getName() + "\" to \"" - + updir.getPath() + "\"."); - upfile.renameTo(updir); - try { - if (docver.length() > 0) - getPublicationService().saveAs(addoc, - new Revision(docver)); - else - getPublicationService().saveAs(addoc, state); - } catch (FileNotFoundException saverror) { - Thread.sleep(1000); - logger.info("Waiting for the file."); - upfile.renameTo(updir); - if (docver.length() > 0) - getPublicationService().saveAs(addoc, - new Revision(docver)); - else - getPublicationService().saveAs(addoc, state); - } - mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document + Date docdate = null; + if (_documentDate.length() > 0) { + ResourceBundle locale = ResourceBundle.getBundle("som", + getApplicationSettings().getCurrentLocale()); + SimpleDateFormat get = new SimpleDateFormat(locale + .getString("date.format")); + docdate = get.parse(_documentDate); } // Creation of uses relations + List uses = new ArrayList(); if (docuses != null) { String[] list = docuses.split(","); for (int i = 0; i < list.length; i++) { - Integer index = Integer.valueOf(list[i].trim()); - Publication used = getPublication(index); - addoc.addDependency(used); + uses.add(Long.valueOf(list[i].trim())); } } + if (LOG.isDebugEnabled()) { + LOG.debug("Document to be imported uses documents with following ids:"); + for (Long usesId: uses) { + LOG.debug("#" + usesId); + } + } + Publication addoc = getPublicationService().createDoc( + mystudy.getIndex(), step, _documentType, user.getIndex(), + filename, docname, state, _reference, _version, docdate, + uses); + + if (_reference.length() > 0) { // Importation of a not foreign document + mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document + } + // Creation of derived the document formats // Document ndoc = addoc.value(); - // Converter send = ApplicationSettings.getConverter(ndoc.getType(), ndoc.getFormat()); + // Converter send = getApplicationSettings().getConverter(ndoc.getType(), ndoc.getFormat()); // // if (send != null) send.converts(addoc); // Asynchronous process - transax.commit(); mystudy.add(addoc); // Updates the presentation return SUCCESS; } catch (FileNotFoundException error) { - logger.error("Reason:", error); - setErrorCode("import.file"); + LOG.error("Reason:", error); + setErrorCode("message.error.import.file"); } catch (Exception error) { - logger.error("Reason:", error); - setErrorCode("internal"); - } - if (transax != null && transax.isActive()) { // Probably useless test - // Second try-catch as the rollback could fail as well - try { - transax.rollback(); - } catch (HibernateException backerror) { - logger.debug("Error rolling back transaction", backerror); - } + LOG.error("Reason:", error); + setErrorCode("message.error.internal"); } + + setToolProperty(Constants.NONE); + setLeftMenuProperty(Constants.STUDY_MENU); + initializationFullScreenContext(_menuProperty, _titleProperty, + _editDisabledProperty, _toolProperty, _leftMenuProperty); + return ERROR; } @@ -289,72 +365,112 @@ public class ImportDocumentAction extends UploadBaseNextAction { // Getters and setters // ============================================================================================================================== + /** + * Date extracted from the imported file, if exist. + * + * @return the file date + */ public String getDocumentDate() { - // -------------------------------- - return date; + return _documentDate; } public List getDocumentTypes() { - // --------------------------------------------- - return doctypes; + return _documentTypes; } + /** + * Get document type id. + * + * @return document type id + */ public long getDocumentType() { - // ----------------------------- - return doctype; + return _documentType; } + /** + * Get reference extracted from the imported file, if exist. + * + * @return the document reference + */ public String getReference() { - // ----------------------------- - return docref; + return _reference; } + /** + * Get version number extracted from the imported file, if exist. + * + * @return the document version + */ public String getVersion() { - // --------------------------- - return docver; + return _version; } - public void setDocumentDate(String date) { - // ----------------------------------------- - this.date = date; + /** + * Set date extracted from the imported file. + * + * @param date + * the date to set + */ + public void setDocumentDate(final String date) { + this._documentDate = date; } - public void setDocumentName(String name) { - // ----------------------------------------- + /** + * Set document name entered by the user if enabled. + * + * @param name + * the document name + */ + public void setDocumentName(final String name) { this.docname = name; // Name entered by the user if enabled } - public void setDocumentTitle(String name) { // Called even if DocumentName is enabled - // ----------------------------------------- - if (this.docname == null) + public void setDocumentTitle(final String name) { // Called even if DocumentName is enabled + if (this.docname == null) { this.docname = name; + } } - public void setDocumentType(String value) { - // ------------------------------------------ - this.doctype = Integer.valueOf(value); + /** + * Set document type id. + * + * @param value + * the id as string + */ + public void setDocumentType(final String value) { + this._documentType = Integer.valueOf(value); } - public void setDefaultDocumentState(String state) { // Called even if DocumentState is enabled - // -------------------------------------------------- - if (this.state == null) + public void setDefaultDocumentState(final String state) { // Called even if DocumentState is enabled + if (this.state == null) { this.state = ProgressState.valueOf(state); + } } - public void setDefaultDocumentType(String value) { // Called even if DocumentType is enabled - // -------------------------------------------------- - if (this.doctype == 0) - this.doctype = Integer.valueOf(value); + public void setDefaultDocumentType(final String value) { // Called even if DocumentType is enabled + if (this._documentType == 0) { + this._documentType = Integer.valueOf(value); + } } - public void setReference(String value) { - // --------------------------------------- - this.docref = value; + /** + * Set document reference extracted from the imported file, if exist. + * + * @param value + * the reference + */ + public void setReference(final String value) { + this._reference = value; } - public void setVersion(String value) { - // ------------------------------------- - this.docver = value; + /** + * Set version number extracted from the imported file, if exist. + * + * @param value + * the version + */ + public void setVersion(final String value) { + this._version = value; } /** @@ -363,7 +479,7 @@ public class ImportDocumentAction extends UploadBaseNextAction { * @return Project settings service */ private ProjectSettingsService getProjectSettings() { - return _projectSettingsService; + return _projectSettings; } /** @@ -372,8 +488,9 @@ public class ImportDocumentAction extends UploadBaseNextAction { * @param projectSettingsService * project settings service */ - public void setProjectSettings(ProjectSettingsService projectSettingsService) { - _projectSettingsService = projectSettingsService; + public void setProjectSettings( + final ProjectSettingsService projectSettingsService) { + _projectSettings = projectSettingsService; } /** @@ -391,12 +508,14 @@ public class ImportDocumentAction extends UploadBaseNextAction { * @param publicationService * the publicationService to set */ - public void setPublicationService(PublicationService publicationService) { + public void setPublicationService( + final PublicationService publicationService) { _publicationService = publicationService; } /** * Get the stepService. + * * @return the stepService */ public StepService getStepService() { @@ -405,14 +524,17 @@ public class ImportDocumentAction extends UploadBaseNextAction { /** * Set the stepService. - * @param stepService the stepService to set + * + * @param stepService + * the stepService to set */ - public void setStepService(StepService stepService) { + public void setStepService(final StepService stepService) { _stepService = stepService; } /** * Get the documentService. + * * @return the documentService */ public DocumentService getDocumentService() { @@ -421,14 +543,17 @@ public class ImportDocumentAction extends UploadBaseNextAction { /** * Set the documentService. - * @param documentService the documentService to set + * + * @param documentService + * the documentService to set */ - public void setDocumentService(DocumentService documentService) { + public void setDocumentService(final DocumentService documentService) { _documentService = documentService; } /** * Get the repositoryService. + * * @return the repositoryService */ public RepositoryService getRepositoryService() { @@ -437,14 +562,17 @@ public class ImportDocumentAction extends UploadBaseNextAction { /** * Set the repositoryService. - * @param repositoryService the repositoryService to set + * + * @param repositoryService + * the repositoryService to set */ - public void setRepositoryService(RepositoryService repositoryService) { + public void setRepositoryService(final RepositoryService repositoryService) { _repositoryService = repositoryService; } /** * Get the documentTypeService. + * * @return the documentTypeService */ public DocumentTypeService getDocumentTypeService() { @@ -453,9 +581,107 @@ public class ImportDocumentAction extends UploadBaseNextAction { /** * Set the documentTypeService. - * @param documentTypeService the documentTypeService to set + * + * @param documentTypeService + * the documentTypeService to set */ - public void setDocumentTypeService(DocumentTypeService documentTypeService) { + public void setDocumentTypeService( + final DocumentTypeService documentTypeService) { _documentTypeService = documentTypeService; } + + /** + * Get the menuProperty. + * + * @return the menuProperty + */ + public String getMenuProperty() { + return _menuProperty; + } + + /** + * Set the menuProperty. + * + * @param menuProperty + * the menuProperty to set + */ + public void setMenuProperty(final String menuProperty) { + this._menuProperty = menuProperty; + } + + /** + * Get the _titleProperty. + * + * @return the _titleProperty + */ + public String getTitleProperty() { + return _titleProperty; + } + + /** + * Set the titleProperty. + * + * @param titleProperty + * the titleProperty to set + */ + public void setTitleProperty(final String titleProperty) { + _titleProperty = titleProperty; + } + + /** + * Get the editDisabledProperty. + * + * @return the editDisabledProperty + */ + public final String getEditDisabledProperty() { + return _editDisabledProperty; + } + + /** + * Set the editDisabledProperty. + * + * @param editDisabledProperty + * the editDisabledProperty to set + */ + public final void setEditDisabledProperty(final String editDisabledProperty) { + _editDisabledProperty = editDisabledProperty; + } + + /** + * Get the toolProperty. + * + * @return the toolProperty + */ + public String getToolProperty() { + return _toolProperty; + } + + /** + * Set the toolProperty. + * + * @param toolProperty + * the toolProperty to set + */ + public void setToolProperty(final String toolProperty) { + _toolProperty = toolProperty; + } + + /** + * Get the leftMenuProperty. + * + * @return the leftMenuProperty + */ + public String getLeftMenuProperty() { + return _leftMenuProperty; + } + + /** + * Set the leftMenuProperty. + * + * @param leftMenuProperty + * the leftMenuProperty to set + */ + public void setLeftMenuProperty(final String leftMenuProperty) { + _leftMenuProperty = leftMenuProperty; + } } \ No newline at end of file