1 /*****************************************************************************
5 * Creation date 06.10.2012
8 *****************************************************************************/
10 package org.splat.service;
12 import java.text.DecimalFormat;
13 import java.text.SimpleDateFormat;
14 import java.util.Calendar;
15 import java.util.Iterator;
16 import java.util.List;
18 import org.hibernate.criterion.Criterion;
19 import org.hibernate.criterion.Restrictions;
20 import org.splat.dal.bo.kernel.Relation;
21 import org.splat.dal.bo.som.ConvertsRelation;
22 import org.splat.dal.bo.som.Document;
23 import org.splat.dal.bo.som.DocumentType;
24 import org.splat.dal.bo.som.File;
25 import org.splat.dal.bo.som.ProgressState;
26 import org.splat.dal.bo.som.ProjectElement;
27 import org.splat.dal.bo.som.Scenario;
28 import org.splat.dal.bo.som.StampRelation;
29 import org.splat.dal.bo.som.Study;
30 import org.splat.dal.bo.som.Timestamp;
31 import org.splat.dal.bo.som.ValidationStep;
32 import org.splat.dal.bo.som.Document.Properties;
33 import org.splat.dal.dao.kernel.RelationDAO;
34 import org.splat.dal.dao.som.DocumentDAO;
35 import org.splat.dal.dao.som.DocumentTypeDAO;
36 import org.splat.dal.dao.som.FileDAO;
37 import org.splat.dal.dao.som.StudyDAO;
38 import org.splat.kernel.InvalidPropertyException;
39 import org.splat.kernel.MissedPropertyException;
40 import org.splat.kernel.NotApplicableException;
41 import org.splat.log.AppLogger;
42 import org.splat.manox.Reader;
43 import org.splat.manox.Toolbox;
44 import org.splat.service.technical.ProjectSettingsService;
45 import org.splat.service.technical.RepositoryService;
46 import org.splat.service.technical.ProjectSettingsServiceImpl.FileNaming;
47 import org.splat.som.Revision;
48 import org.springframework.transaction.annotation.Transactional;
51 * Document service implementation.
53 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
56 public class DocumentServiceImpl implements DocumentService {
59 * The logger for the service.
61 public final static AppLogger LOG = AppLogger
62 .getLogger(DocumentServiceImpl.class);
65 * Injected study service.
67 private StudyService _studyService;
69 * Injected project settings service.
71 private ProjectSettingsService _projectSettings;
73 * Injected repository service.
75 private RepositoryService _repositoryService;
77 * Injected document DAO.
79 private DocumentDAO _documentDAO;
81 * Injected relation DAO.
83 private RelationDAO _relationDAO;
85 * Injected document type DAO.
87 private DocumentTypeDAO _documentTypeDAO;
91 private FileDAO _fileDAO;
95 private StudyDAO _studyDAO;
100 * @see org.splat.service.DocumentService#selectDocument(long)
102 @Transactional(readOnly = true)
103 public Document selectDocument(final long index) {
104 return getDocumentDAO().get(index);
110 * @see org.splat.service.DocumentService#selectDocument(java.lang.String, java.lang.String)
112 @Transactional(readOnly = true)
113 public Document selectDocument(final String refid, final String version) {
114 Criterion aCondition = Restrictions.and(Restrictions.eq("did", refid),
115 Restrictions.eq("version", version));
116 return getDocumentDAO().findByCriteria(aCondition);
122 * @see org.splat.service.DocumentService#generateDocumentId(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
125 public void generateDocumentId(final Document aDoc, final Properties dprop) {
126 Study owner = dprop.getOwner().getOwnerStudy();
128 // Synchronize the object with the current Hibernate session.
129 // owner = getStudyDAO().merge(owner);
131 SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); // RKV: NOPMD: TODO: Use locale here?
132 String year = tostring.format(owner.getDate());
133 String filename = generateEncodedName(aDoc, owner);
134 String path = owner.getReference();
135 ProjectSettingsService.Step step = getProjectSettings().getStep(
137 aDoc.setDid(new StringBuffer(path).append(".%").append(
138 Document.suformat).toString()); // Document reference
139 path = new StringBuffer(year).append("/").append(path).append("/")
140 .append(step.getPath())
141 // File path relative to the repository vault
142 .append(filename).append(".")
143 .append(aDoc.getFile().getFormat()) // File name and extension
145 aDoc.getFile().changePath(path);
146 owner = getStudyDAO().merge(owner);
147 // getStudyDAO().update(owner);
151 * Generate encoded document file name according to the naming scheme from project settings.
157 * @return document reference name
159 private String generateEncodedName(final Document aDoc, final Study scope) {
160 StringBuffer encoding = new StringBuffer();
161 FileNaming scheme = getProjectSettings().getFileNamingScheme();
162 DecimalFormat tostring = new DecimalFormat(Document.suformat);
164 int number = getStudyService().generateLocalIndex(scope);
166 if (scheme == FileNaming.encoded) {
167 encoding.append(scope.getReference()).append("_").append(
168 tostring.format(number));
169 } else { // title and (temporarily) asis
170 encoding.append(tostring.format(number)).append("_").append(
171 aDoc.getFile().getName());
173 return encoding.toString();
177 * Get encoded root name for a document file.
185 private String getEncodedRootName(final Document aDoc, final Study scope) {
186 FileNaming scheme = getProjectSettings().getFileNamingScheme();
188 if (scheme == FileNaming.encoded) {
189 res = scope.getReference();
190 }else if (scheme == FileNaming.asis) {
191 res = aDoc.getFile().getName();
193 res = aDoc.getTitle();
201 * @see org.splat.service.DocumentService#initialize(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
204 public void initialize(final Document aDoc, final Properties dprop)
205 throws MissedPropertyException, InvalidPropertyException,
206 NotApplicableException {
207 if (!aDoc.isUndefined()) {
208 throw new NotApplicableException(
209 "Cannot initialize an existing Document");
211 if (dprop.getName() == null) {
212 throw new MissedPropertyException("name");
214 if (dprop.getName().length() == 0) {
215 throw new InvalidPropertyException("name");
217 if (dprop.getOwner() == null) {
218 throw new MissedPropertyException("owner");
220 // if (dprop.owner instanceof Study && !ProjectSettings.getStep(step).appliesTo(Study.class)) {
221 // throw new InvalidPropertyException("step");
223 aDoc.setTitle(dprop.getName());
224 aDoc.getFile().changePath(
225 aDoc.getFile().getRelativePath().replace("%n",
226 getEncodedRootName(aDoc, (Study) dprop.getOwner())));
227 if (aDoc.getHistory() == -1) {
230 if (dprop.getDate() == null) {
231 Calendar current = Calendar.getInstance();
232 aDoc.setLastModificationDate(current.getTime()); // Today
234 aDoc.setLastModificationDate(dprop.getDate());
236 getDocumentDAO().update(aDoc);
242 * @see org.splat.service.DocumentService#getSaveDirectory(org.splat.dal.bo.som.Document)
244 public java.io.File getSaveDirectory(final Document aDoc) {
245 String mypath = getRepositoryService().getRepositoryVaultPath()
246 + aDoc.getSourceFile().getRelativePath();
247 String[] table = mypath.split("/");
249 // Cutting the filename
250 StringBuffer path = new StringBuffer(table[0]);
251 for (int i = 1; i < table.length - 1; i++) {
252 path = path.append("/").append(table[i]);
254 return new java.io.File(path.append("/").toString());
258 * Extract title and reference properties from the given file.
262 * @return the extracted properties
264 public Properties extractProperties(final java.io.File file) {
265 Properties fprop = new Properties();
266 Reader tool = Toolbox.getReader(file);
270 value = tool.extractProperty("title");
272 fprop.setName(value);
275 value = tool.extractProperty("reference");
277 fprop.setReference(value);
279 } catch (Exception e) {
280 LOG.debug(e.getMessage(), e);
287 * Create "Converts" relation for the given document and the given format.
293 * @return the created "Converts" relation
296 public ConvertsRelation attach(final Document aDoc, final String format) {
297 return attach(aDoc, format, null);
301 * Create "Converts" relation for the given document, format and description.
308 * the description of the relation
309 * @return the created "Converts" relation
312 public ConvertsRelation attach(final Document aDoc, final String format,
313 final String description) {
314 String path = aDoc.getRelativePath();
315 File export = new File(path + "." + format);
316 ConvertsRelation attach = new ConvertsRelation(aDoc, export,
319 getFileDAO().create(export);
320 getRelationDAO().create(attach);
322 aDoc.getAllRelations().add(attach);
323 getDocumentDAO().merge(aDoc);
329 * Build a reference (document id) for the given document in the given study basing on the given original document.
332 * the document to set set the new reference to
336 * the original document
337 * @return true if the new reference is set
339 public boolean buildReferenceFrom(final Document aDoc,
340 final ProjectElement scope, final Document lineage) {
341 boolean res = (aDoc.getProgressState() == ProgressState.inWORK);
343 Study owner = scope.getOwnerStudy();
344 Scenario context = null;
345 if (scope instanceof Scenario) {
346 context = ((Scenario) scope);
348 aDoc.setDid(lineage.getDid());
350 && (lineage.isVersioned() || owner.shares(lineage))) {
351 aDoc.setVersion(new Revision(aDoc.getVersion()).setBranch(
352 context.getReference()).toString());
359 * Build a reference (document id) for the given document in the given study.
362 * the document to set set the new reference to
365 * @return true if the new reference is set
367 public boolean buildReferenceFrom(final Document aDoc, final Study scope) {
368 if (aDoc.getProgressState() != ProgressState.inWORK
369 && aDoc.getProgressState() != ProgressState.EXTERN) {
372 DecimalFormat tostring = new DecimalFormat(Document.suformat);
374 aDoc.setDid(aDoc.getDid().replace("%" + Document.suformat,
375 tostring.format(scope.getLastLocalIndex())));
383 * the document to demote
384 * @return true if demoting succeeded
387 public boolean demote(final Document aDoc) {
388 ValidationStep torem;
390 if (aDoc.getProgressState() == ProgressState.inCHECK) {
391 aDoc.setProgressState(ProgressState.inDRAFT);
392 torem = ValidationStep.REVIEW;
393 // This operation must not change the version number of documents.
394 // Consequently, inDRAFT documents may have a minor version number equal to zero.
395 } else if (aDoc.getProgressState() == ProgressState.inDRAFT) {
396 aDoc.setProgressState(ProgressState.inWORK);
397 torem = ValidationStep.PROMOTION;
401 for (Iterator<Relation> i = aDoc.getAllRelations().iterator(); i
403 Relation link = i.next();
404 if (!(link instanceof StampRelation)) {
407 if (((StampRelation) link).getStampType() != torem) {
413 getDocumentDAO().update(aDoc);
418 * Promote a document.
421 * the document to promote
423 * the timestamp of the promotion
424 * @return true if promotion succeeded
427 public boolean promote(final Document aDoc, final Timestamp stamp) {
428 ProgressState newstate = null;
430 if (aDoc.getProgressState() == ProgressState.inWORK) {
431 newstate = ProgressState.inDRAFT; // Promotion to being reviewed
432 } else if (aDoc.getProgressState() == ProgressState.inDRAFT) {
433 newstate = ProgressState.inCHECK; // Promotion to approval
434 Revision myvers = new Revision(aDoc.getVersion());
435 if (myvers.isMinor()) {
436 aDoc.setVersion(myvers.incrementAs(newstate).toString());
437 // TODO: If my physical file is programatically editable, update its (property) version number
438 // ISSUE: What about attached files such as PDF if exist, should we remove them ?
440 } else if (aDoc.getProgressState() == ProgressState.inCHECK) {
441 newstate = ProgressState.APPROVED;
443 aDoc.setProgressState(newstate);
445 // RKV: aDoc.addRelation(stamp.getContext());
446 aDoc.getAllRelations().add(stamp.getContext());
448 getDocumentDAO().update(aDoc);
453 * Increments the reference count of this document following its publication in a Study step.
456 * the document to hold
460 public void hold(final Document aDoc) {
461 aDoc.setCountag(aDoc.getCountag() + 1);
462 if (aDoc.isSaved()) {
463 getDocumentDAO().update(aDoc);
468 * Decrements the reference count of this document following the removal of a Publication from a Study step.
471 * the document to release
475 public void release(final Document aDoc) {
476 aDoc.setCountag(aDoc.getCountag() - 1);
477 if (aDoc.isSaved()) {
478 getDocumentDAO().merge(aDoc);
486 * the document to rename
488 * the new document title
489 * @throws InvalidPropertyException
490 * if the new title is empty
493 public void rename(final Document aDoc, final String title)
494 throws InvalidPropertyException {
495 if (title.length() == 0) {
496 throw new InvalidPropertyException("name");
499 Calendar current = Calendar.getInstance();
500 aDoc.setTitle(title);
501 aDoc.setLastModificationDate(current.getTime()); // Today
502 getDocumentDAO().update(aDoc);
506 * Update a version of the given document.
513 public void updateAs(final Document aDoc, final Revision newvers) {
514 aDoc.setVersion(newvers.setBranch(aDoc.getVersion()).toString()); // Branch names are propagated by the versionning
515 ProgressState newstate = ProgressState.inCHECK;
516 if (newvers.isMinor()) {
517 newstate = ProgressState.inWORK;
519 aDoc.setProgressState(null); // Just to tell updateAs(state) to not increment the version number
520 updateAs(aDoc, newstate);
524 * Update a state of the given document.
532 public void updateAs(final Document aDoc, final ProgressState state) {
533 Document previous = null;
535 // Set of version number
536 if (state == ProgressState.EXTERN) {
537 if (aDoc.getProgressState() != ProgressState.EXTERN) {
538 aDoc.setVersion(null); // Strange use-case...
541 Revision myvers = new Revision(aDoc.getVersion());
542 if (!myvers.isNull()) { // Versionning context
543 previous = aDoc.getPreviousVersion();
545 if (aDoc.getProgressState() != null) {
546 myvers.incrementAs(state); // Incrementation if the reversion number is not imposed
548 aDoc.setVersion(myvers.toString());
550 // Update this document and the previous version, if exist
551 if (previous != null) {
552 previous.setHistory(previous.getHistory() + 1);
553 getDocumentDAO().merge(previous);
555 aDoc.setProgressState(state);
556 // RKV: getDocumentDAO().update(aDoc);
559 // protected void upgrade () {
560 // -------------------------
561 // if (this.state != ProgressState.inWORK) return;
563 // Calendar current = Calendar.getInstance();
564 // for (Iterator<Relation> i=getAllRelations().iterator(); i.hasNext();) {
565 // Relation link = i.next();
566 // if (!link.getClass().equals(UsesRelation.class)) continue;
568 // Document used = (Document)link.getTo();
569 // if (!used.isVersioned()) continue;
570 // TODO: Update the uses relation
573 // this.lasdate = current.getTime(); // Today
574 // Database.getSession().update(this);
576 // TODO: Promote documents using this one
580 * Checks if documents of this type are result of a study. A document is the result of a study when it is the result of the last step of
585 * @return true if documents of this type are result of a study.
586 * @see #isStepResult()
587 * @see #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
589 public boolean isStudyResult(final DocumentType aType) {
590 List<ProjectSettingsService.Step> step = getProjectSettings()
592 ProjectSettingsService.Step lastep = step.get(step.size() - 1);
593 return (aType.isResultOf(lastep));
597 * Get document by its path.
601 * @return the document if found or null
603 @Transactional(readOnly = true)
604 public Document getDocumentByPath(String path) {
605 String[] parse = path.split("'");
608 for (int i = 1; i < parse.length; i++) {
609 path = path + "''" + parse[i];
611 Criterion aCondition = Restrictions.eq("path", path);
612 return getDocumentDAO().findByCriteria(aCondition);
616 * Get the studyService.
618 * @return the studyService
620 public StudyService getStudyService() {
621 return _studyService;
625 * Set the studyService.
627 * @param studyService
628 * the studyService to set
630 public void setStudyService(final StudyService studyService) {
631 _studyService = studyService;
635 * Get project settings.
637 * @return Project settings service
639 private ProjectSettingsService getProjectSettings() {
640 return _projectSettings;
644 * Set project settings service.
646 * @param projectSettingsService
647 * project settings service
649 public void setProjectSettings(
650 final ProjectSettingsService projectSettingsService) {
651 _projectSettings = projectSettingsService;
655 * Get the documentDAO.
657 * @return the documentDAO
659 public DocumentDAO getDocumentDAO() {
664 * Set the documentDAO.
667 * the documentDAO to set
669 public void setDocumentDAO(final DocumentDAO documentDAO) {
670 _documentDAO = documentDAO;
674 * Get the repositoryService.
676 * @return the repositoryService
678 public RepositoryService getRepositoryService() {
679 return _repositoryService;
683 * Set the repositoryService.
685 * @param repositoryService
686 * the repositoryService to set
688 public void setRepositoryService(final RepositoryService repositoryService) {
689 _repositoryService = repositoryService;
693 * Get the documentTypeDAO.
695 * @return the documentTypeDAO
697 public DocumentTypeDAO getDocumentTypeDAO() {
698 return _documentTypeDAO;
702 * Set the documentTypeDAO.
704 * @param documentTypeDAO
705 * the documentTypeDAO to set
707 public void setDocumentTypeDAO(final DocumentTypeDAO documentTypeDAO) {
708 _documentTypeDAO = documentTypeDAO;
714 * @return the fileDAO
716 public FileDAO getFileDAO() {
726 public void setFileDAO(final FileDAO fileDAO) {
733 * @return the studyDAO
735 public StudyDAO getStudyDAO() {
743 * the studyDAO to set
745 public void setStudyDAO(final StudyDAO studyDAO) {
746 _studyDAO = studyDAO;
750 * Get the relationDAO.
752 * @return the relationDAO
754 public RelationDAO getRelationDAO() {
759 * Set the relationDAO.
762 * the relationDAO to set
764 public void setRelationDAO(final RelationDAO relationDAO) {
765 _relationDAO = relationDAO;