+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.PublicationService#createDoc(long, org.splat.som.Step, long, long, java.lang.String, java.lang.String,
+ * org.splat.dal.bo.som.ProgressState, java.lang.String, java.lang.String, java.util.Date, java.util.List)
+ */
+ @Transactional
+ public Publication createDoc(final long ownerId, final Step step,
+ final long documentTypeId, final long userId, final String fname,
+ final String doctitle, final ProgressState docstate,
+ final String reference, final String version, final Date docDate,
+ final List<Long> docuses) throws MissedPropertyException,
+ InvalidPropertyException, MultiplyDefinedException, IOException,
+ NotApplicableException, InterruptedException, ParseException {
+ DocumentType type = getDocumentTypeService().selectType(
+ (int) documentTypeId);
+ User user = getUserService().selectUser(userId);
+ File updir = getRepositoryService().getDownloadDirectory(user);
+ File upfile = new File(updir.getPath() + "/" + fname);
+ String[] table = fname.split("\\x2E");
+
+ // Creation of the document
+ Document.Properties dprop = new Document.Properties();
+ Publication addoc;
+
+ if (reference.length() == 0) { // Importation of a foreign document
+ // TODO: Extract property of supported documents (DOCX, ODT...)
+ addoc = getStepService().createDocument(
+ step,
+ dprop.setName(doctitle).setType(type).setFormat(
+ table[table.length - 1]).setAuthor(user));
+ updir = addoc.getSourceFile().asFile();
+ if (LOG.isInfoEnabled()) {
+ LOG.info("Moving \"" + upfile.getName() + "\" to \""
+ + updir.getPath() + "\".");
+ }
+ upfile.renameTo(updir);
+ try {
+ saveAs(addoc, docstate); // May throw FileNotFound if rename was not done
+ } catch (FileNotFoundException saverror) {
+ Thread.sleep(1000);
+ LOG.info("Waiting for the file.");
+ upfile.renameTo(updir);
+ saveAs(addoc, docstate); // Forget it if throw again FileNotFound
+ }
+ } else { // Importation of a previously created template-based document
+ if (docDate != null) {
+ dprop.setDate(docDate);
+ }
+ addoc = getStepService().assignDocument(step,
+ dprop.setReference(reference).setName(doctitle));
+ updir = addoc.getSourceFile().asFile();
+ if (LOG.isInfoEnabled()) {
+ LOG.info("Moving \"" + upfile.getName() + "\" to \""
+ + updir.getPath() + "\".");
+ }
+ upfile.renameTo(updir);
+ try {
+ if (version.length() > 0) {
+ saveAs(addoc, new Revision(version));
+ } else {
+ saveAs(addoc, docstate);
+ }
+ } catch (FileNotFoundException saverror) {
+ Thread.sleep(1000);
+ LOG.info("Waiting for the file.");
+ upfile.renameTo(updir);
+ if (version.length() > 0) {
+ saveAs(addoc, new Revision(version));
+ } else {
+ saveAs(addoc, docstate);
+ }
+ }
+ }
+ // Creation of uses relations
+ if (docuses != null) {
+ for (Long index : docuses) {
+ Document used = getDocumentService().selectDocument(index);
+ addoc.addDependency(used);
+ }
+ }
+ return addoc;
+ }
+