import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
-import java.util.Vector;
import org.splat.dal.bo.kernel.User;
-import org.splat.manox.Reader;
-import org.splat.manox.Toolbox;
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.
*/
private static final long serialVersionUID = 2587822564883588556L;
- private List<DocumentType> 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<DocumentType> _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.
*/
/**
* Injected repository service.
*/
- private RepositoryService _repositoryService;
+ private RepositoryService _repositoryService;
/**
- * Value of the menu property.
- * It can be: none, create, open, study, knowledge, sysadmin, help.
+ * 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.
+ * 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.
+ * 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.
+ * 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.
+ * 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";
/**
* Initialize the operation.
+ *
* @return SUCCESS in success, otherwise - ERROR
*/
public String doInitialize() {
-
- setMenuProperty("study");
- setTitleProperty("study");
+
+ setMenuProperty(Constants.STUDY_MENU);
+ setTitleProperty(Constants.STUDY_MENU);
setEditDisabledProperty("true");
if ("true".equals(getWriteAccess())) {
- setToolProperty("study");
+ setToolProperty(Constants.STUDY_MENU);
} else {
- setToolProperty("none");
+ setToolProperty(Constants.NONE);
}
- setLeftMenuProperty("study");
- initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
-
+ 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);
mystudy = getOpenStudy();
Step step = mystudy.getSelectedStep();
- doctypes = getStepService().getValidDocumentTypes(step);
+ _documentTypes = getStepService().getValidDocumentTypes(step);
deftype = getApplicationSettings().getDefaultDocumentType(step, filext);
- defuses = new Vector<Document>();
+ defuses = new ArrayList<Document>();
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<DocumentType> i = doctypes.iterator(); i.hasNext();) {
+ for (Iterator<DocumentType> 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("message.error.reference.undefined");
-
- setToolProperty("none");
- initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
-
- return ERROR;
- } else {
- if (!slot.isUndefined()) {
- setErrorCode("message.error.reference.duplicate");
-
- setToolProperty("none");
- initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
-
- 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("message.error.format.version");
-
- setToolProperty("none");
- initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
-
- 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",
- getApplicationSettings().getCurrentLocale());
- SimpleDateFormat check = new SimpleDateFormat(
- locale.getString("date.format"));
- try {
- check.parse(date);
- } catch (ParseException e) {
- setErrorCode("message.error.format.date");
-
- setToolProperty("none");
- initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
-
- 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];
+ }
+ }
+ return isOk;
+ }
+
+ /**
+ * 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;
+ }
}
- 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);
+ /**
+ * Set error message and menus.
+ *
+ * @param errorCode
+ * error message key
+ */
+ private void setError(final String errorCode) {
+ setErrorCode(errorCode);
- 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() {
- // -------------------------
-
- setMenuProperty("study");
- setTitleProperty("study");
+
+ setMenuProperty(Constants.STUDY_MENU);
+ setTitleProperty(Constants.STUDY_MENU);
setEditDisabledProperty("true");
- initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
-
- if (action == ToDo.cancel)
+ initializationScreenContext(_menuProperty, _titleProperty,
+ _editDisabledProperty);
+
+ if (action == ToDo.cancel) {
return "cancel";
- if (doctype == 0) {
+ }
+ if (_documentType == 0) {
setErrorCode("message.error.import.type");
-
- setToolProperty("none");
- setLeftMenuProperty("study");
- initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
-
+
+ setToolProperty(Constants.NONE);
+ setLeftMenuProperty(Constants.STUDY_MENU);
+ initializationFullScreenContext(_menuProperty, _titleProperty,
+ _editDisabledProperty, _toolProperty, _leftMenuProperty);
+
return ERROR;
}
try {
mystudy = getOpenStudy();
User user = getConnectedUser();
Step step = mystudy.getSelectedStep();
- DocumentType type = getDocumentTypeService().selectType((int)doctype);
+ DocumentType type = getDocumentTypeService().selectType(
+ (int) _documentType);
File updir = getRepositoryService().getDownloadDirectory(user);
File upfile = new File(updir.getPath() + "/" + filename);
String[] table = filename.split("\\x2E");
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));
+ if (_reference.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 (LOG.isInfoEnabled())
+ if (LOG.isInfoEnabled()) {
LOG.info("Moving \"" + upfile.getName() + "\" to \""
+ updir.getPath() + "\".");
+ }
upfile.renameTo(updir);
try {
getPublicationService().saveAs(addoc, state); // May throw FileNotFound if rename was not done
getPublicationService().saveAs(addoc, state); // Forget it if throw again FileNotFound
}
} else { // Importation of a previously created template-based document
- if (date.length() > 0) {
+ if (_documentDate.length() > 0) {
ResourceBundle locale = ResourceBundle.getBundle("som",
getApplicationSettings().getCurrentLocale());
- SimpleDateFormat get = new SimpleDateFormat(
- locale.getString("date.format"));
- dprop.setDate(get.parse(date));
+ SimpleDateFormat get = new SimpleDateFormat(locale
+ .getString("date.format"));
+ dprop.setDate(get.parse(_documentDate));
}
- addoc = getStepService().assignDocument(step, dprop.setReference(docref).setName(
- docname));
+ addoc = getStepService().assignDocument(step,
+ dprop.setReference(_reference).setName(docname));
updir = addoc.getSourceFile().asFile();
- if (LOG.isInfoEnabled())
+ if (LOG.isInfoEnabled()) {
LOG.info("Moving \"" + upfile.getName() + "\" to \""
+ updir.getPath() + "\".");
+ }
upfile.renameTo(updir);
try {
- if (docver.length() > 0)
+ if (_version.length() > 0) {
getPublicationService().saveAs(addoc,
- new Revision(docver));
- else
+ new Revision(_version));
+ } else {
getPublicationService().saveAs(addoc, state);
+ }
} catch (FileNotFoundException saverror) {
Thread.sleep(1000);
LOG.info("Waiting for the file.");
upfile.renameTo(updir);
- if (docver.length() > 0)
+ if (_version.length() > 0) {
getPublicationService().saveAs(addoc,
- new Revision(docver));
- else
+ new Revision(_version));
+ } else {
getPublicationService().saveAs(addoc, state);
+ }
}
mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
}
LOG.error("Reason:", error);
setErrorCode("message.error.internal");
}
-
- setToolProperty("none");
- setLeftMenuProperty("study");
- initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
-
+
+ setToolProperty(Constants.NONE);
+ setLeftMenuProperty(Constants.STUDY_MENU);
+ initializationFullScreenContext(_menuProperty, _titleProperty,
+ _editDisabledProperty, _toolProperty, _leftMenuProperty);
+
return ERROR;
}
// Getters and setters
// ==============================================================================================================================
+ /**
+ * Date extracted from the imported file, if exist.
+ *
+ * @return the file date
+ */
public String getDocumentDate() {
- // --------------------------------
- return date;
+ return _documentDate;
}
public List<DocumentType> 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;
}
+ /**
+ * Set date extracted from the imported file.
+ *
+ * @param date
+ * the date to set
+ */
public void setDocumentDate(final String date) {
- // -----------------------------------------
- this.date = date;
+ this._documentDate = date;
}
+ /**
+ * 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(final String name) { // Called even if DocumentName is enabled
- // -----------------------------------------
- if (this.docname == null)
+ if (this.docname == null) {
this.docname = name;
+ }
}
+ /**
+ * Set document type id.
+ *
+ * @param value
+ * the id as string
+ */
public void setDocumentType(final String value) {
- // ------------------------------------------
- this.doctype = Integer.valueOf(value);
+ this._documentType = Integer.valueOf(value);
}
public void setDefaultDocumentState(final String state) { // Called even if DocumentState is enabled
- // --------------------------------------------------
- if (this.state == null)
+ if (this.state == null) {
this.state = ProgressState.valueOf(state);
+ }
}
public void setDefaultDocumentType(final String value) { // Called even if DocumentType is enabled
- // --------------------------------------------------
- if (this.doctype == 0)
- this.doctype = Integer.valueOf(value);
+ if (this._documentType == 0) {
+ this._documentType = Integer.valueOf(value);
+ }
}
+ /**
+ * Set document reference extracted from the imported file, if exist.
+ *
+ * @param value
+ * the reference
+ */
public void setReference(final String value) {
- // ---------------------------------------
- this.docref = value;
+ this._reference = value;
}
+ /**
+ * Set version number extracted from the imported file, if exist.
+ *
+ * @param value
+ * the version
+ */
public void setVersion(final String value) {
- // -------------------------------------
- this.docver = value;
+ this._version = value;
}
/**
* @return Project settings service
*/
private ProjectSettingsService getProjectSettings() {
- return _projectSettingsService;
+ return _projectSettings;
}
/**
* @param projectSettingsService
* project settings service
*/
- public void setProjectSettings(final ProjectSettingsService projectSettingsService) {
- _projectSettingsService = projectSettingsService;
+ public void setProjectSettings(
+ final ProjectSettingsService projectSettingsService) {
+ _projectSettings = projectSettingsService;
}
/**
* @param publicationService
* the publicationService to set
*/
- public void setPublicationService(final PublicationService publicationService) {
+ public void setPublicationService(
+ final PublicationService publicationService) {
_publicationService = publicationService;
}
/**
* Get the stepService.
+ *
* @return the stepService
*/
public StepService getStepService() {
/**
* Set the stepService.
- * @param stepService the stepService to set
+ *
+ * @param stepService
+ * the stepService to set
*/
public void setStepService(final StepService stepService) {
_stepService = stepService;
/**
* Get the documentService.
+ *
* @return the documentService
*/
public DocumentService getDocumentService() {
/**
* Set the documentService.
- * @param documentService the documentService to set
+ *
+ * @param documentService
+ * the documentService to set
*/
public void setDocumentService(final DocumentService documentService) {
_documentService = documentService;
/**
* Get the repositoryService.
+ *
* @return the repositoryService
*/
public RepositoryService getRepositoryService() {
/**
* Set the repositoryService.
- * @param repositoryService the repositoryService to set
+ *
+ * @param repositoryService
+ * the repositoryService to set
*/
public void setRepositoryService(final RepositoryService repositoryService) {
_repositoryService = repositoryService;
/**
* Get the documentTypeService.
+ *
* @return the documentTypeService
*/
public DocumentTypeService getDocumentTypeService() {
/**
* Set the documentTypeService.
- * @param documentTypeService the documentTypeService to set
+ *
+ * @param documentTypeService
+ * the documentTypeService to set
*/
- public void setDocumentTypeService(final DocumentTypeService documentTypeService) {
+ public void setDocumentTypeService(
+ final DocumentTypeService documentTypeService) {
_documentTypeService = documentTypeService;
}
-
+
/**
* Get the menuProperty.
+ *
* @return the menuProperty
*/
public String getMenuProperty() {
/**
* Set the menuProperty.
- * @param menuProperty the menuProperty to set
+ *
+ * @param menuProperty
+ * the menuProperty to set
*/
public void setMenuProperty(final String menuProperty) {
this._menuProperty = menuProperty;
}
-
+
/**
* Get the _titleProperty.
+ *
* @return the _titleProperty
*/
public String getTitleProperty() {
}
/**
- * Set the _titleProperty.
- * @param _titleProperty the titleProperty to set
+ * 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() {
/**
* Set the editDisabledProperty.
- * @param editDisabledProperty the editDisabledProperty to set
+ *
+ * @param editDisabledProperty
+ * the editDisabledProperty to set
*/
public final void setEditDisabledProperty(final String editDisabledProperty) {
_editDisabledProperty = editDisabledProperty;
/**
* Get the toolProperty.
+ *
* @return the toolProperty
*/
public String getToolProperty() {
/**
* Set the toolProperty.
- * @param toolProperty the toolProperty to set
+ *
+ * @param toolProperty
+ * the toolProperty to set
*/
public void setToolProperty(final String toolProperty) {
_toolProperty = toolProperty;
}
-
+
/**
* Get the leftMenuProperty.
+ *
* @return the leftMenuProperty
*/
public String getLeftMenuProperty() {
/**
* Set the leftMenuProperty.
- * @param leftMenuProperty the leftMenuProperty to set
+ *
+ * @param leftMenuProperty
+ * the leftMenuProperty to set
*/
public void setLeftMenuProperty(final String leftMenuProperty) {
_leftMenuProperty = leftMenuProperty;