1 package org.splat.simer;
4 import java.io.FileNotFoundException;
5 import java.text.SimpleDateFormat;
6 import java.util.ArrayList;
7 import java.util.Arrays;
10 import java.util.ResourceBundle;
12 import org.splat.dal.bo.kernel.User;
13 import org.splat.dal.bo.som.Document;
14 import org.splat.dal.bo.som.DocumentType;
15 import org.splat.dal.bo.som.ProgressState;
16 import org.splat.dal.bo.som.Publication;
17 import org.splat.dal.bo.som.ValidationCycle;
18 import org.splat.dal.bo.som.ValidationStep;
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.som.Revision;
24 import org.splat.som.Step;
25 import org.splat.wapp.Constants;
28 * Action for adding a document into a study step.
30 public class ImportDocumentAction extends BaseUploadDocumentAction {
35 private static final long serialVersionUID = 2587822564883588556L;
38 * Sorted list of document types for the selected study step.
40 private transient List<DocumentType> _documentTypes = null;
42 * Sorted list of document types for the selected study step.
44 private transient final List<Boolean> _reviewable = new ArrayList<Boolean>();
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 * Injected document service.
56 private DocumentService _documentService;
58 * Injected document type service.
60 private DocumentTypeService _documentTypeService;
63 * Initialize the operation.
65 * @return SUCCESS in success, otherwise - ERROR
67 public String doInitialize() {
69 File upfile = commonInitialize(Constants.FALSE);
70 String[] table = _fileName.split("\\x2E");
71 String filext = table[table.length - 1].toLowerCase();
73 _mystudy = getOpenStudy();
74 Step step = _mystudy.getSelectedStep();
75 _documentTypes = getStepService().getValidDocumentTypes(step);
76 // Set the document type by default
77 _deftype = getApplicationSettings()
78 .getDefaultDocumentType(step, filext);
79 if (_deftype != null) {
80 setDefaultDocumentType(_deftype.getIndex());
82 _defuses = new ArrayList<Document>();
83 _state = ProgressState.inWORK;
86 if (extractProperties(upfile, filext)) {
87 if (_docname == null) {
89 for (int i = 1; i < table.length - 1; i++) {
90 _docname = _docname + "." + table[i];
93 if (_deftype != null) {
94 setupDefaultUses(_deftype);
97 DocumentType[] types = _documentTypes
98 .toArray(new DocumentType[_documentTypes.size()]);
99 DocumentTypeComparator compare = new DocumentTypeComparator();
100 Arrays.sort(types, compare);
101 _documentTypes = Arrays.asList(types);
105 // Initialize isReviewable: if a type has review state or not
106 ValidationCycle cycle;
108 for (DocumentType dtype : _documentTypes) {
109 cycle = getStudyService().getValidationCycleOf(
110 _mystudy.getMystudy(), dtype);
111 hasReview = (cycle != null) && cycle.enables(ValidationStep.REVIEW);
112 _reviewable.add(hasReview);
118 * Extract properties from the uploaded file.
124 * @return true if succeeded, false if error
126 private boolean extractProperties(final File upfile, final String filext) {
128 Reader tool = Toolbox.getReader(upfile);
130 if ("pdf".equals(filext)) {
131 _state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
134 // String fileref = tool.extractProperty("reference");
135 // String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced
136 // String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced
137 // for (Iterator<DocumentType> i = _documentTypes.iterator(); i
139 // DocumentType type = i.next();
140 // if (type.getName().equals(filetype)) {
142 // _documentType = type.getIndex(); // Disables the document type field
146 // if (fileref != null) {
147 // isOk = findTypeByDocRef(fileref);
150 // if (filever != null) {
152 // Revision.Format get = new Revision.Format(
153 // getProjectSettings().getRevisionPattern());
154 // Revision version = get.parse(filever);
155 // if (version.isNull()) {
156 // throw new ParseException(filever,
157 // filever.length() - 1);
159 // if (!version.isMinor()) {
160 // _state = ProgressState.inCHECK;
162 // setVersion(version.toString());
163 // } catch (ParseException e) {
164 // setErrorCode("message.error.format.version");
169 // _docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
170 // isOk = extractDate(tool);
178 * Find document type by document reference.
181 * the document reference
182 * @return true if succeeded, false if error
184 private boolean findTypeByDocRef(final String fileref) {
186 Document slot = getDocumentService().selectDocument(fileref,
187 new Revision().toString());
189 setError("message.error.reference.undefined");
192 if (slot.isUndefined()) {
193 _reference = fileref; // Disables document name and state fields
194 _deftype = slot.getType(); // Just in case
195 _documentType = _deftype.getIndex(); // Disables the document type field
197 setError("message.error.reference.duplicate");
205 * Perform import of a document.
207 * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
209 public String doImport() {
212 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
215 if (_action == ToDo.cancel) {
217 } else if (_documentType == 0) {
218 setErrorCode("message.error.import.type");
220 initializationFullScreenContext(Constants.STUDY_MENU,
221 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
222 Constants.STUDY_MENU);
225 // Getting user inputs
226 _mystudy = getOpenStudy();
227 User user = getConnectedUser();
228 Step step = _mystudy.getSelectedStep();
230 if (getDocumentDate().length() > 0) {
231 ResourceBundle locale = ResourceBundle.getBundle("som",
232 getApplicationSettings().getCurrentLocale());
233 SimpleDateFormat get = new SimpleDateFormat(locale
234 .getString("date.format"), getApplicationSettings()
235 .getCurrentLocale());
236 aDate = get.parse(getDocumentDate());
238 // Creation of uses relations
239 List<Long> uses = new ArrayList<Long>();
240 if (_docuses != null) {
241 String[] list = _docuses.split(",");
242 for (int i = 0; i < list.length; i++) {
243 uses.add(Long.valueOf(list[i].trim()));
246 if (LOG.isDebugEnabled()) {
248 .debug("Document to be imported uses documents with following ids:");
249 for (Long usesId : uses) {
250 LOG.debug("#" + usesId);
253 Publication addoc = getPublicationService().createDoc(
254 _mystudy.getIndex(), step, _documentType,
255 user.getIndex(), _fileName, _docname, _state,
256 _reference, getVersion(), aDate, uses);
258 if (_reference.length() > 0) { // Importation of a not foreign document
259 _mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
262 // Creation of derived the document formats
263 // Document ndoc = addoc.value();
264 // Converter send = getApplicationSettings().getConverter(ndoc.getType(), ndoc.getFormat());
266 // if (send != null) send.converts(addoc); // Asynchronous process
268 if (uses.isEmpty()) {
269 _mystudy.add(addoc); // Updates the presentation
271 // Re-opening (refreshing) the currently open study
272 String selection = _mystudy.getSelection();
273 _mystudy = open(getStudyService().selectStudy(
274 _mystudy.getIndex())); // Updates the study
275 _mystudy.setSelection(selection);
278 } catch (FileNotFoundException error) {
279 LOG.error("Reason:", error);
280 setErrorCode("message.error.import.file");
281 } catch (Exception error) {
282 LOG.error("Reason:", error);
283 setErrorCode("message.error.internal");
286 if (!SUCCESS.equals(res)) {
287 initializationFullScreenContext(Constants.STUDY_MENU,
288 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
289 Constants.STUDY_MENU);
296 // ==============================================================================================================================
297 // Getters and setters
298 // ==============================================================================================================================
301 * Get sorted list of document types valid for the selected study step.
303 * @return sorted list of document types
305 public List<DocumentType> getDocumentTypes() {
306 return _documentTypes;
310 * Get document type id.
312 * @return document type id
314 public long getDocumentType() {
315 return _documentType;
319 * Get reference extracted from the imported file, if exist.
321 * @return the document reference
323 public String getReference() {
328 * Set document name entered by the user if enabled.
333 public void setDocumentName(final String name) {
334 this._docname = name; // Name entered by the user if enabled
338 * Set the default title if no title was defined.
341 * the default document title
343 public void setDocumentTitle(final String name) { // Called even if DocumentName is enabled
344 if (this._docname == null) {
345 this._docname = name;
350 * Set document type id.
355 public void setDocumentType(final Long value) {
356 this._documentType = value;
360 * Set the default state if no state was selected.
365 public void setDefaultDocumentState(final String state) { // Called even if DocumentState is enabled
366 if (this._state == null) {
367 this._state = ProgressState.valueOf(state);
372 * Set the default type if no type was selected.
375 * the default document type id
377 public void setDefaultDocumentType(final Long value) { // Called even if DocumentType is enabled
378 if (this._documentType == 0) {
379 this._documentType = value;
384 * Set document reference extracted from the imported file, if exist.
389 public void setReference(final String value) {
390 this._reference = value;
394 * Get the documentService.
396 * @return the documentService
398 public DocumentService getDocumentService() {
399 return _documentService;
403 * Set the documentService.
405 * @param documentService
406 * the documentService to set
408 public void setDocumentService(final DocumentService documentService) {
409 _documentService = documentService;
413 * Get the documentTypeService.
415 * @return the documentTypeService
417 public DocumentTypeService getDocumentTypeService() {
418 return _documentTypeService;
422 * Set the documentTypeService.
424 * @param documentTypeService
425 * the documentTypeService to set
427 public void setDocumentTypeService(
428 final DocumentTypeService documentTypeService) {
429 _documentTypeService = documentTypeService;
433 * Get sorted list of available document states.
435 * @return the documentStates
437 public List<ProgressState> getDocumentStates() {
438 List<ProgressState> states = new ArrayList<ProgressState>();
439 states.add(ProgressState.inWORK);
440 states.add(ProgressState.inDRAFT);
441 states.add(ProgressState.EXTERN);
446 * Get the isReviewable.
448 * @return the isReviewable
450 public List<Boolean> getReviewable() {