+package org.splat.simer;
+
+import java.io.File;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ResourceBundle;
+import java.util.Set;
+
+import org.splat.dal.bo.kernel.User;
+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.service.PublicationService;
+import org.splat.service.StepService;
+import org.splat.service.StudyService;
+import org.splat.service.technical.ProjectSettingsService;
+import org.splat.som.Step;
+import org.splat.wapp.Constants;
+
+/**
+ * Base action for adding a document into a study step.
+ */
+public class BaseUploadDocumentAction extends AbstractUploadNextAction {
+
+ /**
+ * Serial version ID.
+ */
+ private static final long serialVersionUID = 7920982168638437486L;
+ /**
+ * 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 _projectSettings;
+ /**
+ * Injected publication service.
+ */
+ private PublicationService _publicationService;
+ /**
+ * Injected step service.
+ */
+ private StepService _stepService;
+ /**
+ * Injected study service.
+ */
+ private StudyService _studyService;
+ protected DocumentType _deftype = null;
+ protected List<Document> _defuses = null;
+ protected String _docname = null;
+ protected String _docuses = null;
+ protected OpenStudy _mystudy = null;
+ protected ProgressState _state = null;
+
+ /**
+ * Common initialization of the action form.
+ *
+ * @param editDisabled
+ * if the study editable or not
+ * @return downloaded file
+ */
+ protected File commonInitialize(final String editDisabled) {
+
+ if (Constants.TRUE.equals(getWriteAccess())) {
+ setToolProperty(Constants.STUDY_MENU);
+ } else {
+ setToolProperty(Constants.NONE);
+ }
+ initializationFullScreenContext(Constants.STUDY_MENU,
+ Constants.STUDY_MENU, editDisabled, getToolProperty(),
+ Constants.STUDY_MENU);
+
+ User user = getConnectedUser();
+ File updir = getRepositoryService().getDownloadDirectory(user);
+ File upfile = new File(updir.getPath() + "/" + _fileName);
+
+ _mystudy = getOpenStudy();
+ _defuses = new ArrayList<Document>();
+ return upfile;
+ }
+
+ /**
+ * Extract document date.
+ *
+ * @param tool
+ * file reader
+ * @return true if ok, false if error
+ */
+ protected boolean extractDate(final Reader tool) {
+ boolean isOk = true;
+ setDocumentDate(tool.extractProperty("date"));
+ if (getDocumentDate() == null) {
+ setDocumentDate("");
+ } else {
+ ResourceBundle locale = ResourceBundle.getBundle("som",
+ getApplicationSettings().getCurrentLocale());
+ SimpleDateFormat check = new SimpleDateFormat(locale
+ .getString("date.format"), getApplicationSettings()
+ .getCurrentLocale());
+ try {
+ check.parse(getDocumentDate());
+ } catch (ParseException e) {
+ setErrorCode("message.error.format.date");
+ isOk = false;
+ }
+ }
+ return isOk;
+ }
+
+ /**
+ * Set error message and menus.
+ *
+ * @param errorCode
+ * error message key
+ */
+ protected void setError(final String errorCode) {
+ setErrorCode(errorCode);
+
+ initializationFullScreenContext(Constants.STUDY_MENU,
+ Constants.STUDY_MENU, Constants.FALSE, Constants.NONE,
+ Constants.STUDY_MENU);
+ }
+
+ /**
+ * Date extracted from the imported file, if exist.
+ *
+ * @return the file date
+ */
+ public String getDocumentDate() {
+ return _documentDate;
+ }
+
+ /**
+ * Get version number extracted from the imported file, if exist.
+ *
+ * @return the document version
+ */
+ public String getVersion() {
+ return _version;
+ }
+
+ /**
+ * Set date extracted from the imported file.
+ *
+ * @param date
+ * the date to set
+ */
+ public void setDocumentDate(final String date) {
+ this._documentDate = date;
+ }
+
+ /**
+ * Set version number extracted from the imported file, if exist.
+ *
+ * @param value
+ * the version
+ */
+ public void setVersion(final String value) {
+ this._version = value;
+ }
+
+ /**
+ * Get project settings.
+ *
+ * @return Project settings service
+ */
+ protected ProjectSettingsService getProjectSettings() {
+ return _projectSettings;
+ }
+
+ /**
+ * Set project settings service.
+ *
+ * @param projectSettingsService
+ * project settings service
+ */
+ public void setProjectSettings(
+ final ProjectSettingsService projectSettingsService) {
+ _projectSettings = projectSettingsService;
+ }
+
+ /**
+ * Get the publicationService.
+ *
+ * @return the publicationService
+ */
+ public PublicationService getPublicationService() {
+ return _publicationService;
+ }
+
+ /**
+ * Set the publicationService.
+ *
+ * @param publicationService
+ * the publicationService to set
+ */
+ public void setPublicationService(
+ final PublicationService publicationService) {
+ _publicationService = publicationService;
+ }
+
+ /**
+ * Get the stepService.
+ *
+ * @return the stepService
+ */
+ public StepService getStepService() {
+ return _stepService;
+ }
+
+ /**
+ * Set the stepService.
+ *
+ * @param stepService
+ * the stepService to set
+ */
+ public void setStepService(final StepService stepService) {
+ _stepService = stepService;
+ }
+
+ /**
+ * Get the studyService.
+ *
+ * @return the studyService
+ */
+ public StudyService getStudyService() {
+ return _studyService;
+ }
+
+ /**
+ * Set the studyService.
+ *
+ * @param studyService
+ * the studyService to set
+ */
+ public void setStudyService(final StudyService studyService) {
+ _studyService = studyService;
+ }
+
+ // ==============================================================================================================================
+ // Getters and setters
+ // ==============================================================================================================================
+
+ public DocumentType getDefaultDocumentType() {
+ return _deftype;
+ }
+
+ public List<Document> getDefaultDocumentUses() {
+ return _defuses;
+ }
+
+ public String getDocumentName() {
+ return _docname;
+ }
+
+ public String getDocumentState() {
+ return _state.toString();
+ }
+
+ protected Publication getPublication(final int index) {
+ List<Step> steps = _mystudy.getInvolvedSteps();
+ Publication found = null, pub;
+ for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
+ List<Publication> published = i.next().getAllDocuments();
+ for (Iterator<Publication> j = published.iterator(); j.hasNext();) {
+ pub = j.next(); // In a given study step,
+ if (pub.value().getIndex() == index) {
+ found = pub; // there is only one publication of a given document
+ break;
+ }
+ }
+ }
+ return found;
+ }
+
+ public void setDocumentState(final String state) {
+ this._state = ProgressState.valueOf(state);
+ }
+
+ // ==============================================================================================================================
+ // Protected services
+ // ==============================================================================================================================
+
+ protected void setupDefaultUses(final DocumentType type) {
+ Set<DocumentType> uses = type.getDefaultUses();
+
+ for (Iterator<DocumentType> i = uses.iterator(); i.hasNext();) {
+ DocumentType usetype = i.next();
+ List<Document> usedoc = _mystudy.collectInvolvedDocuments(usetype);
+ if (usedoc.isEmpty() && (!usetype.equals(type))) {
+ setupDefaultUses(usetype);
+ } else {
+ _defuses.addAll(usedoc);
+ }
+ }
+ }
+
+ public void setUses(final String list) {
+ this._docuses = list;
+ }
+
+ public String getWriteAccess() {
+ Boolean res = (_mystudy != null);
+ if (res) {
+ res = _mystudy.isOpenForWriting();
+ }
+ return res.toString();
+ }
+}
\ No newline at end of file