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 // ==============================================================================================================================
82 // ==============================================================================================================================
85 * Initialize the operation.
87 * @return SUCCESS in success, otherwise - ERROR
89 public String doInitialize() {
91 if (Constants.TRUE.equals(getWriteAccess())) {
92 setToolProperty(Constants.STUDY_MENU);
94 setToolProperty(Constants.NONE);
96 initializationFullScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
97 Constants.FALSE, getToolProperty(), Constants.STUDY_MENU);
99 User user = getConnectedUser();
100 File updir = getRepositoryService().getDownloadDirectory(user);
101 File upfile = new File(updir.getPath() + "/" + filename);
102 String[] table = filename.split("\\x2E");
103 String filext = table[table.length - 1].toLowerCase();
105 mystudy = getOpenStudy();
106 Step step = mystudy.getSelectedStep();
107 _documentTypes = getStepService().getValidDocumentTypes(step);
108 deftype = getApplicationSettings().getDefaultDocumentType(step, filext);
109 defuses = new ArrayList<Document>();
110 state = ProgressState.inWORK;
113 if (extractProperties(upfile, filext)) {
114 if (docname == null) {
116 for (int i = 1; i < table.length - 1; i++) {
117 docname = docname + "." + table[i];
120 if (deftype != null) {
121 setupDefaultUses(deftype);
124 DocumentType[] types = _documentTypes
125 .toArray(new DocumentType[_documentTypes.size()]);
126 DocumentTypeComparator compare = new DocumentTypeComparator();
127 Arrays.sort(types, compare);
128 _documentTypes = Arrays.asList(types);
135 * Extract properties from the uploaded file.
141 * @return true if succeeded, false if error
143 private boolean extractProperties(final File upfile, final String filext) {
145 Reader tool = Toolbox.getReader(upfile);
147 if ("pdf".equals(filext)) {
148 state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
151 String fileref = tool.extractProperty("reference");
152 String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced
153 String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced
154 for (Iterator<DocumentType> i = _documentTypes.iterator(); i
156 DocumentType type = i.next();
157 if (type.getName().equals(filetype)) {
159 _documentType = type.getIndex(); // Disables the document type field
163 if (fileref != null) {
164 isOk = findTypeByDocRef(fileref);
167 if (filever != null) {
169 Revision.Format get = new Revision.Format(
170 getProjectSettings().getRevisionPattern());
171 Revision version = get.parse(filever);
172 if (version.isNull()) {
173 throw new ParseException(filever,
174 filever.length() - 1);
176 if (!version.isMinor()) {
177 state = ProgressState.inCHECK;
179 _version = version.toString();
180 } catch (ParseException e) {
181 setError("message.error.format.version");
186 docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
187 _documentDate = tool.extractProperty("date");
188 if (_documentDate == null) {
191 ResourceBundle locale = ResourceBundle.getBundle("som",
192 getApplicationSettings().getCurrentLocale());
193 SimpleDateFormat check = new SimpleDateFormat(locale
194 .getString("date.format"));
196 check.parse(_documentDate);
197 } catch (ParseException e) {
198 setError("message.error.format.date");
209 * Find document type by document reference.
212 * the document reference
213 * @return true if succeeded, false if error
215 private boolean findTypeByDocRef(final String fileref) {
217 Document slot = getDocumentService().selectDocument(fileref,
218 new Revision().toString());
220 setError("message.error.reference.undefined");
223 if (slot.isUndefined()) {
224 _reference = fileref; // Disables document name and state fields
225 deftype = slot.getType(); // Just in case
226 _documentType = deftype.getIndex(); // Disables the document type field
228 setError("message.error.reference.duplicate");
236 * Set error message and menus.
241 private void setError(final String errorCode) {
242 setErrorCode(errorCode);
244 initializationFullScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
245 Constants.FALSE, Constants.NONE, Constants.STUDY_MENU);
249 * Perform import of a document.
251 * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
253 public String doImport() {
255 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
258 if (action == ToDo.cancel) {
261 if (_documentType == 0) {
262 setErrorCode("message.error.import.type");
264 initializationFullScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
265 Constants.TRUE, Constants.NONE, Constants.STUDY_MENU);
270 // Getting user inputs
271 mystudy = getOpenStudy();
272 User user = getConnectedUser();
273 Step step = mystudy.getSelectedStep();
275 if (_documentDate.length() > 0) {
276 ResourceBundle locale = ResourceBundle.getBundle("som",
277 getApplicationSettings().getCurrentLocale());
278 SimpleDateFormat get = new SimpleDateFormat(locale
279 .getString("date.format"));
280 docdate = get.parse(_documentDate);
282 // Creation of uses relations
283 List<Long> uses = new ArrayList<Long>();
284 if (docuses != null) {
285 String[] list = docuses.split(",");
286 for (int i = 0; i < list.length; i++) {
287 uses.add(Long.valueOf(list[i].trim()));
290 if (LOG.isDebugEnabled()) {
291 LOG.debug("Document to be imported uses documents with following ids:");
292 for (Long usesId: uses) {
293 LOG.debug("#" + usesId);
296 Publication addoc = getPublicationService().createDoc(
297 mystudy.getIndex(), step, _documentType, user.getIndex(),
298 filename, docname, state, _reference, _version, docdate,
301 if (_reference.length() > 0) { // Importation of a not foreign document
302 mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
305 // Creation of derived the document formats
306 // Document ndoc = addoc.value();
307 // Converter send = getApplicationSettings().getConverter(ndoc.getType(), ndoc.getFormat());
309 // if (send != null) send.converts(addoc); // Asynchronous process
311 mystudy.add(addoc); // Updates the presentation
313 } catch (FileNotFoundException error) {
314 LOG.error("Reason:", error);
315 setErrorCode("message.error.import.file");
316 } catch (Exception error) {
317 LOG.error("Reason:", error);
318 setErrorCode("message.error.internal");
321 initializationFullScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
322 Constants.TRUE, Constants.NONE, Constants.STUDY_MENU);
327 // ==============================================================================================================================
328 // Getters and setters
329 // ==============================================================================================================================
332 * Date extracted from the imported file, if exist.
334 * @return the file date
336 public String getDocumentDate() {
337 return _documentDate;
340 public List<DocumentType> getDocumentTypes() {
341 return _documentTypes;
345 * Get document type id.
347 * @return document type id
349 public long getDocumentType() {
350 return _documentType;
354 * Get reference extracted from the imported file, if exist.
356 * @return the document reference
358 public String getReference() {
363 * Get version number extracted from the imported file, if exist.
365 * @return the document version
367 public String getVersion() {
372 * Set date extracted from the imported file.
377 public void setDocumentDate(final String date) {
378 this._documentDate = date;
382 * Set document name entered by the user if enabled.
387 public void setDocumentName(final String name) {
388 this.docname = name; // Name entered by the user if enabled
391 public void setDocumentTitle(final String name) { // Called even if DocumentName is enabled
392 if (this.docname == null) {
398 * Set document type id.
403 public void setDocumentType(final String value) {
404 this._documentType = Integer.valueOf(value);
407 public void setDefaultDocumentState(final String state) { // Called even if DocumentState is enabled
408 if (this.state == null) {
409 this.state = ProgressState.valueOf(state);
413 public void setDefaultDocumentType(final String value) { // Called even if DocumentType is enabled
414 if (this._documentType == 0) {
415 this._documentType = Integer.valueOf(value);
420 * Set document reference extracted from the imported file, if exist.
425 public void setReference(final String value) {
426 this._reference = value;
430 * Set version number extracted from the imported file, if exist.
435 public void setVersion(final String value) {
436 this._version = value;
440 * Get project settings.
442 * @return Project settings service
444 private ProjectSettingsService getProjectSettings() {
445 return _projectSettings;
449 * Set project settings service.
451 * @param projectSettingsService
452 * project settings service
454 public void setProjectSettings(
455 final ProjectSettingsService projectSettingsService) {
456 _projectSettings = projectSettingsService;
460 * Get the publicationService.
462 * @return the publicationService
464 public PublicationService getPublicationService() {
465 return _publicationService;
469 * Set the publicationService.
471 * @param publicationService
472 * the publicationService to set
474 public void setPublicationService(
475 final PublicationService publicationService) {
476 _publicationService = publicationService;
480 * Get the stepService.
482 * @return the stepService
484 public StepService getStepService() {
489 * Set the stepService.
492 * the stepService to set
494 public void setStepService(final StepService stepService) {
495 _stepService = stepService;
499 * Get the documentService.
501 * @return the documentService
503 public DocumentService getDocumentService() {
504 return _documentService;
508 * Set the documentService.
510 * @param documentService
511 * the documentService to set
513 public void setDocumentService(final DocumentService documentService) {
514 _documentService = documentService;
518 * Get the repositoryService.
520 * @return the repositoryService
522 public RepositoryService getRepositoryService() {
523 return _repositoryService;
527 * Set the repositoryService.
529 * @param repositoryService
530 * the repositoryService to set
532 public void setRepositoryService(final RepositoryService repositoryService) {
533 _repositoryService = repositoryService;
537 * Get the documentTypeService.
539 * @return the documentTypeService
541 public DocumentTypeService getDocumentTypeService() {
542 return _documentTypeService;
546 * Set the documentTypeService.
548 * @param documentTypeService
549 * the documentTypeService to set
551 public void setDocumentTypeService(
552 final DocumentTypeService documentTypeService) {
553 _documentTypeService = documentTypeService;