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;
42 * Sorted list of document types for the selected study step.
44 private transient List<DocumentType> _documentTypes = null;
46 * The selected document type.
48 private long _documentType = 0;
50 * Reference extracted from the imported file, if exist.
52 private String _reference = null;
54 * Version number extracted from the imported file, if exist.
56 private String _version = "";
58 * Date extracted from the imported file, if exist.
60 private String _documentDate = "";
62 * Injected project settings service.
64 private ProjectSettingsService _projectSettings;
66 * Injected publication service.
68 private PublicationService _publicationService;
70 * Injected step service.
72 private StepService _stepService;
74 * Injected document service.
76 private DocumentService _documentService;
78 * Injected document type service.
80 private DocumentTypeService _documentTypeService;
82 * Injected repository service.
84 private RepositoryService _repositoryService;
86 // ==============================================================================================================================
88 // ==============================================================================================================================
91 * Initialize the operation.
93 * @return SUCCESS in success, otherwise - ERROR
95 public String doInitialize() {
97 if (Constants.TRUE.equals(getWriteAccess())) {
98 setToolProperty(Constants.STUDY_MENU);
100 setToolProperty(Constants.NONE);
102 initializationFullScreenContext(Constants.STUDY_MENU,
103 Constants.STUDY_MENU, Constants.FALSE, getToolProperty(),
104 Constants.STUDY_MENU);
106 User user = getConnectedUser();
107 File updir = getRepositoryService().getDownloadDirectory(user);
108 File upfile = new File(updir.getPath() + "/" + filename);
109 String[] table = filename.split("\\x2E");
110 String filext = table[table.length - 1].toLowerCase();
112 mystudy = getOpenStudy();
113 Step step = mystudy.getSelectedStep();
114 _documentTypes = getStepService().getValidDocumentTypes(step);
115 // Set the document type by default
116 deftype = getApplicationSettings().getDefaultDocumentType(step, filext);
117 if (deftype != null) {
118 setDefaultDocumentType(deftype.getIndex());
120 defuses = new ArrayList<Document>();
121 state = ProgressState.inWORK;
124 if (extractProperties(upfile, filext)) {
125 if (docname == null) {
127 for (int i = 1; i < table.length - 1; i++) {
128 docname = docname + "." + table[i];
131 if (deftype != null) {
132 setupDefaultUses(deftype);
135 DocumentType[] types = _documentTypes
136 .toArray(new DocumentType[_documentTypes.size()]);
137 DocumentTypeComparator compare = new DocumentTypeComparator();
138 Arrays.sort(types, compare);
139 _documentTypes = Arrays.asList(types);
146 * Extract properties from the uploaded file.
152 * @return true if succeeded, false if error
154 private boolean extractProperties(final File upfile, final String filext) {
156 Reader tool = Toolbox.getReader(upfile);
158 if ("pdf".equals(filext)) {
159 state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
162 String fileref = tool.extractProperty("reference");
163 String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced
164 String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced
165 for (Iterator<DocumentType> i = _documentTypes.iterator(); i
167 DocumentType type = i.next();
168 if (type.getName().equals(filetype)) {
170 _documentType = type.getIndex(); // Disables the document type field
174 if (fileref != null) {
175 isOk = findTypeByDocRef(fileref);
178 if (filever != null) {
180 Revision.Format get = new Revision.Format(
181 getProjectSettings().getRevisionPattern());
182 Revision version = get.parse(filever);
183 if (version.isNull()) {
184 throw new ParseException(filever,
185 filever.length() - 1);
187 if (!version.isMinor()) {
188 state = ProgressState.inCHECK;
190 _version = version.toString();
191 } catch (ParseException e) {
192 setError("message.error.format.version");
197 docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
198 _documentDate = tool.extractProperty("date");
199 if (_documentDate == null) {
202 ResourceBundle locale = ResourceBundle.getBundle("som",
203 getApplicationSettings().getCurrentLocale());
204 SimpleDateFormat check = new SimpleDateFormat(locale
205 .getString("date.format"),
206 getApplicationSettings().getCurrentLocale());
208 check.parse(_documentDate);
209 } catch (ParseException e) {
210 setError("message.error.format.date");
221 * Find document type by document reference.
224 * the document reference
225 * @return true if succeeded, false if error
227 private boolean findTypeByDocRef(final String fileref) {
229 Document slot = getDocumentService().selectDocument(fileref,
230 new Revision().toString());
232 setError("message.error.reference.undefined");
235 if (slot.isUndefined()) {
236 _reference = fileref; // Disables document name and state fields
237 deftype = slot.getType(); // Just in case
238 _documentType = deftype.getIndex(); // Disables the document type field
240 setError("message.error.reference.duplicate");
248 * Set error message and menus.
253 private void setError(final String errorCode) {
254 setErrorCode(errorCode);
256 initializationFullScreenContext(Constants.STUDY_MENU,
257 Constants.STUDY_MENU, Constants.FALSE, Constants.NONE,
258 Constants.STUDY_MENU);
262 * Perform import of a document.
264 * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
266 public String doImport() {
267 String result = ERROR;
269 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
272 if (action == ToDo.cancel) {
274 } else if (_documentType == 0) {
275 setErrorCode("message.error.import.type");
277 initializationFullScreenContext(Constants.STUDY_MENU,
278 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
279 Constants.STUDY_MENU);
282 // Getting user inputs
283 mystudy = getOpenStudy();
284 User user = getConnectedUser();
285 Step step = mystudy.getSelectedStep();
287 if (_documentDate.length() > 0) {
288 ResourceBundle locale = ResourceBundle.getBundle("som",
289 getApplicationSettings().getCurrentLocale());
290 SimpleDateFormat get = new SimpleDateFormat(locale
291 .getString("date.format"), getApplicationSettings()
292 .getCurrentLocale());
293 docdate = get.parse(_documentDate);
295 // Creation of uses relations
296 List<Long> uses = new ArrayList<Long>();
297 if (docuses != null) {
298 String[] list = docuses.split(",");
299 for (int i = 0; i < list.length; i++) {
300 uses.add(Long.valueOf(list[i].trim()));
303 if (LOG.isDebugEnabled()) {
305 .debug("Document to be imported uses documents with following ids:");
306 for (Long usesId : uses) {
307 LOG.debug("#" + usesId);
310 Publication addoc = getPublicationService().createDoc(
311 mystudy.getIndex(), step, _documentType,
312 user.getIndex(), filename, docname, state, _reference,
313 _version, docdate, uses);
315 if (_reference.length() > 0) { // Importation of a not foreign document
316 mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
319 // Creation of derived the document formats
320 // Document ndoc = addoc.value();
321 // Converter send = getApplicationSettings().getConverter(ndoc.getType(), ndoc.getFormat());
323 // if (send != null) send.converts(addoc); // Asynchronous process
325 mystudy.add(addoc); // Updates the presentation
327 } catch (FileNotFoundException error) {
328 LOG.error("Reason:", error);
329 setErrorCode("message.error.import.file");
330 } catch (Exception error) {
331 LOG.error("Reason:", error);
332 setErrorCode("message.error.internal");
335 if (!SUCCESS.equals(result)) {
336 initializationFullScreenContext(Constants.STUDY_MENU,
337 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
338 Constants.STUDY_MENU);
345 // ==============================================================================================================================
346 // Getters and setters
347 // ==============================================================================================================================
350 * Date extracted from the imported file, if exist.
352 * @return the file date
354 public String getDocumentDate() {
355 return _documentDate;
359 * Get sorted list of document types valid for the selected study step.
361 * @return sorted list of document types
363 public List<DocumentType> getDocumentTypes() {
364 return _documentTypes;
368 * Get document type id.
370 * @return document type id
372 public long getDocumentType() {
373 return _documentType;
377 * Get reference extracted from the imported file, if exist.
379 * @return the document reference
381 public String getReference() {
386 * Get version number extracted from the imported file, if exist.
388 * @return the document version
390 public String getVersion() {
395 * Set date extracted from the imported file.
400 public void setDocumentDate(final String date) {
401 this._documentDate = date;
405 * Set document name entered by the user if enabled.
410 public void setDocumentName(final String name) {
411 this.docname = name; // Name entered by the user if enabled
415 * Set the default title if no title was defined.
418 * the default document title
420 public void setDocumentTitle(final String name) { // Called even if DocumentName is enabled
421 if (this.docname == null) {
427 * Set document type id.
432 public void setDocumentType(final Long value) {
433 this._documentType = value;
437 * Set the default state if no state was selected.
442 public void setDefaultDocumentState(final String state) { // Called even if DocumentState is enabled
443 if (this.state == null) {
444 this.state = ProgressState.valueOf(state);
449 * Set the default type if no type was selected.
452 * the default document type id
454 public void setDefaultDocumentType(final Long value) { // Called even if DocumentType is enabled
455 if (this._documentType == 0) {
456 this._documentType = value;
461 * Set document reference extracted from the imported file, if exist.
466 public void setReference(final String value) {
467 this._reference = value;
471 * Set version number extracted from the imported file, if exist.
476 public void setVersion(final String value) {
477 this._version = value;
481 * Get project settings.
483 * @return Project settings service
485 private ProjectSettingsService getProjectSettings() {
486 return _projectSettings;
490 * Set project settings service.
492 * @param projectSettingsService
493 * project settings service
495 public void setProjectSettings(
496 final ProjectSettingsService projectSettingsService) {
497 _projectSettings = projectSettingsService;
501 * Get the publicationService.
503 * @return the publicationService
505 public PublicationService getPublicationService() {
506 return _publicationService;
510 * Set the publicationService.
512 * @param publicationService
513 * the publicationService to set
515 public void setPublicationService(
516 final PublicationService publicationService) {
517 _publicationService = publicationService;
521 * Get the stepService.
523 * @return the stepService
525 public StepService getStepService() {
530 * Set the stepService.
533 * the stepService to set
535 public void setStepService(final StepService stepService) {
536 _stepService = stepService;
540 * Get the documentService.
542 * @return the documentService
544 public DocumentService getDocumentService() {
545 return _documentService;
549 * Set the documentService.
551 * @param documentService
552 * the documentService to set
554 public void setDocumentService(final DocumentService documentService) {
555 _documentService = documentService;
559 * Get the repositoryService.
561 * @return the repositoryService
563 public RepositoryService getRepositoryService() {
564 return _repositoryService;
568 * Set the repositoryService.
570 * @param repositoryService
571 * the repositoryService to set
573 public void setRepositoryService(final RepositoryService repositoryService) {
574 _repositoryService = repositoryService;
578 * Get the documentTypeService.
580 * @return the documentTypeService
582 public DocumentTypeService getDocumentTypeService() {
583 return _documentTypeService;
587 * Set the documentTypeService.
589 * @param documentTypeService
590 * the documentTypeService to set
592 public void setDocumentTypeService(
593 final DocumentTypeService documentTypeService) {
594 _documentTypeService = documentTypeService;
598 * Get sorted list of available document states.
600 * @return the documentStates
602 public List<ProgressState> getDocumentStates() {
603 List<ProgressState> states = new ArrayList<ProgressState>();
604 states.add(ProgressState.inWORK);
605 states.add(ProgressState.inDRAFT);
606 states.add(ProgressState.EXTERN);