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.setLastLocalIndex(getStudyDAO().get(owner.getIndex())
130 .getLastLocalIndex());
132 SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); // RKV: NOPMD: TODO: Use locale here?
133 String year = tostring.format(owner.getDate());
134 String filename = generateEncodedName(aDoc, owner);
135 String path = owner.getReference();
136 ProjectSettingsService.Step step = getProjectSettings().getStep(
138 aDoc.setDid(new StringBuffer(path).append(".%").append(
139 Document.suformat).toString()); // Document reference
140 StringBuffer pathBuf = new StringBuffer(year).append("/").append(path)
141 .append("/").append(step.getPath())
142 // File path relative to the repository vault
144 if (!FileNaming.asis.equals(getProjectSettings().getFileNamingScheme())) {
145 // Use format as extension if file naming strategy is not "asis".
146 pathBuf.append(".").append(aDoc.getFile().getFormat()); // File name and extension
148 aDoc.getFile().changePath(pathBuf.toString());
149 owner = getStudyDAO().merge(owner);
153 * Generate encoded document file name according to the naming scheme from project settings.
159 * @return document reference name
161 private String generateEncodedName(final Document aDoc, final Study scope) {
162 StringBuffer encoding = new StringBuffer();
163 FileNaming scheme = getProjectSettings().getFileNamingScheme();
164 DecimalFormat tostring = new DecimalFormat(Document.suformat);
166 int number = getStudyService().generateLocalIndex(scope);
168 if (scheme == FileNaming.encoded) {
169 encoding.append(scope.getReference()).append("_").append(
170 tostring.format(number));
171 } else { // title and (temporarily) asis
172 encoding.append(tostring.format(number)).append("_").append(
173 aDoc.getFile().getName());
175 return encoding.toString();
179 * Get encoded root name for a document file.
187 private String getEncodedRootName(final Document aDoc, final Study scope) {
188 FileNaming scheme = getProjectSettings().getFileNamingScheme();
190 if (scheme == FileNaming.encoded) {
191 res = scope.getReference();
192 } else if (scheme == FileNaming.asis) {
193 res = aDoc.getFile().getName();
195 res = aDoc.getTitle();
203 * @see org.splat.service.DocumentService#initialize(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
206 public void initialize(final Document aDoc, final Properties dprop)
207 throws MissedPropertyException, InvalidPropertyException,
208 NotApplicableException {
209 if (!aDoc.isUndefined()) {
210 throw new NotApplicableException(
211 "Cannot initialize an existing Document");
213 if (dprop.getName() == null) {
214 throw new MissedPropertyException("name");
216 if (dprop.getName().length() == 0) {
217 throw new InvalidPropertyException("name");
219 if (dprop.getOwner() == null) {
220 throw new MissedPropertyException("owner");
222 // if (dprop.owner instanceof Study && !ProjectSettings.getStep(step).appliesTo(Study.class)) {
223 // throw new InvalidPropertyException("step");
225 aDoc.setTitle(dprop.getName());
226 aDoc.getFile().changePath(
227 aDoc.getFile().getRelativePath().replace("%n",
228 getEncodedRootName(aDoc, (Study) dprop.getOwner())));
229 if (aDoc.getHistory() == -1) {
232 if (dprop.getDate() == null) {
233 Calendar current = Calendar.getInstance();
234 aDoc.setLastModificationDate(current.getTime()); // Today
236 aDoc.setLastModificationDate(dprop.getDate());
238 getDocumentDAO().update(aDoc);
244 * @see org.splat.service.DocumentService#getSaveDirectory(org.splat.dal.bo.som.Document)
246 public java.io.File getSaveDirectory(final Document aDoc) {
247 String mypath = getRepositoryService().getRepositoryVaultPath()
248 + aDoc.getSourceFile().getRelativePath();
249 String[] table = mypath.split("/");
251 // Cutting the filename
252 StringBuffer path = new StringBuffer(table[0]);
253 for (int i = 1; i < table.length - 1; i++) {
254 path = path.append("/").append(table[i]);
256 return new java.io.File(path.append("/").toString());
260 * Extract title and reference properties from the given file.
264 * @return the extracted properties
266 public Properties extractProperties(final java.io.File file) {
267 Properties fprop = new Properties();
268 Reader tool = Toolbox.getReader(file);
272 value = tool.extractProperty("title");
274 fprop.setName(value);
277 value = tool.extractProperty("reference");
279 fprop.setReference(value);
281 } catch (Exception e) {
282 LOG.debug(e.getMessage(), e);
289 * Create "Converts" relation for the given document and the given format.
295 * @return the created "Converts" relation
298 public ConvertsRelation attach(final Document aDoc, final String format) {
299 return attach(aDoc, format, null);
303 * Create "Converts" relation for the given document, format and description.
310 * the description of the relation
311 * @return the created "Converts" relation
314 public ConvertsRelation attach(final Document aDoc, final String format,
315 final String description) {
316 String path = aDoc.getRelativePath();
317 File export = new File(path + "." + format);
318 ConvertsRelation attach = new ConvertsRelation(aDoc, export,
321 getFileDAO().create(export);
322 getRelationDAO().create(attach);
324 aDoc.getAllRelations().add(attach);
325 getDocumentDAO().merge(aDoc);
331 * Build a reference (document id) for the given document in the given study basing on the given original document.
334 * the document to set set the new reference to
338 * the original document
339 * @return true if the new reference is set
342 public boolean buildReferenceFrom(final Document aDoc,
343 final ProjectElement scope, final Document lineage) {
344 boolean res = (aDoc.getProgressState() == ProgressState.inWORK);
346 Study owner = scope.getOwnerStudy();
347 Scenario context = null;
348 if (scope instanceof Scenario) {
349 context = ((Scenario) scope);
351 aDoc.setDid(lineage.getDid());
353 && (lineage.isVersioned() || owner.shares(lineage))) {
354 aDoc.setVersion(new Revision(aDoc.getVersion()).setBranch(
355 context.getReference()).toString());
362 * Build a reference (document id) for the given document in the given study.
365 * the document to set set the new reference to
368 * @return true if the new reference is set
371 public boolean buildReferenceFrom(final Document aDoc, final Study scope) {
372 if (aDoc.getProgressState() != ProgressState.inWORK
373 && aDoc.getProgressState() != ProgressState.EXTERN) {
376 DecimalFormat tostring = new DecimalFormat(Document.suformat);
378 aDoc.setDid(aDoc.getDid().replace("%" + Document.suformat,
379 tostring.format(scope.getLastLocalIndex())));
387 * the document to demote
388 * @return true if demoting succeeded
391 public boolean demote(final Document aDoc) {
392 ValidationStep torem;
394 if (aDoc.getProgressState() == ProgressState.inCHECK) {
395 aDoc.setProgressState(ProgressState.inDRAFT);
396 torem = ValidationStep.REVIEW;
397 // This operation must not change the version number of documents.
398 // Consequently, inDRAFT documents may have a minor version number equal to zero.
399 } else if (aDoc.getProgressState() == ProgressState.inDRAFT) {
400 aDoc.setProgressState(ProgressState.inWORK);
401 torem = ValidationStep.PROMOTION;
405 for (Iterator<Relation> i = aDoc.getAllRelations().iterator(); i
407 Relation link = i.next();
408 if (!(link instanceof StampRelation)) {
411 if (((StampRelation) link).getStampType() != torem) {
417 getDocumentDAO().update(aDoc);
422 * Promote a document.
425 * the document to promote
427 * the timestamp of the promotion
428 * @return true if promotion succeeded
431 public boolean promote(final Document aDoc, final Timestamp stamp) {
432 ProgressState newstate = null;
434 if (aDoc.getProgressState() == ProgressState.inWORK) {
435 newstate = ProgressState.inDRAFT; // Promotion to being reviewed
436 } else if (aDoc.getProgressState() == ProgressState.inDRAFT) {
437 newstate = ProgressState.inCHECK; // Promotion to approval
438 Revision myvers = new Revision(aDoc.getVersion());
439 if (myvers.isMinor()) {
440 aDoc.setVersion(myvers.incrementAs(newstate).toString());
441 // TODO: If my physical file is programatically editable, update its (property) version number
442 // ISSUE: What about attached files such as PDF if exist, should we remove them ?
444 } else if (aDoc.getProgressState() == ProgressState.inCHECK) {
445 newstate = ProgressState.APPROVED;
447 aDoc.setProgressState(newstate);
449 // RKV: aDoc.addRelation(stamp.getContext());
450 aDoc.getAllRelations().add(stamp.getContext());
452 getDocumentDAO().update(aDoc);
457 * Increments the reference count of this document following its publication in a Study step.
460 * the document to hold
464 public void hold(final Document aDoc) {
465 aDoc.setCountag(aDoc.getCountag() + 1);
466 if (aDoc.isSaved()) {
467 getDocumentDAO().update(aDoc);
472 * Decrements the reference count of this document following the removal of a Publication from a Study step.
475 * the document to release
479 public void release(final Document aDoc) {
480 aDoc.setCountag(aDoc.getCountag() - 1);
481 if (aDoc.isSaved()) {
482 getDocumentDAO().merge(aDoc);
490 * the document to rename
492 * the new document title
493 * @throws InvalidPropertyException
494 * if the new title is empty
497 public void rename(final Document aDoc, final String title)
498 throws InvalidPropertyException {
499 if (title.length() == 0) {
500 throw new InvalidPropertyException("name");
503 Calendar current = Calendar.getInstance();
504 aDoc.setTitle(title);
505 aDoc.setLastModificationDate(current.getTime()); // Today
506 getDocumentDAO().update(aDoc);
510 * Update a version of the given document.
517 public void updateAs(final Document aDoc, final Revision newvers) {
518 aDoc.setVersion(newvers.setBranch(aDoc.getVersion()).toString()); // Branch names are propagated by the versionning
519 ProgressState newstate = ProgressState.inCHECK;
520 if (newvers.isMinor()) {
521 newstate = ProgressState.inWORK;
523 aDoc.setProgressState(null); // Just to tell updateAs(state) to not increment the version number
524 updateAs(aDoc, newstate);
528 * Update a state of the given document.
536 public void updateAs(final Document aDoc, final ProgressState state) {
537 Document previous = null;
539 // Set of version number
540 if (state == ProgressState.EXTERN) {
541 if (aDoc.getProgressState() != ProgressState.EXTERN) {
542 aDoc.setVersion(null); // Strange use-case...
545 Revision myvers = new Revision(aDoc.getVersion());
546 if (!myvers.isNull()) { // Versionning context
547 previous = aDoc.getPreviousVersion();
549 if (aDoc.getProgressState() != null) {
550 myvers.incrementAs(state); // Incrementation if the reversion number is not imposed
552 aDoc.setVersion(myvers.toString());
554 // Update this document and the previous version, if exist
555 if (previous != null) {
556 previous.setHistory(previous.getHistory() + 1);
557 getDocumentDAO().merge(previous);
559 aDoc.setProgressState(state);
560 // RKV: getDocumentDAO().update(aDoc);
563 // protected void upgrade () {
564 // -------------------------
565 // if (this.state != ProgressState.inWORK) return;
567 // Calendar current = Calendar.getInstance();
568 // for (Iterator<Relation> i=getAllRelations().iterator(); i.hasNext();) {
569 // Relation link = i.next();
570 // if (!link.getClass().equals(UsesRelation.class)) continue;
572 // Document used = (Document)link.getTo();
573 // if (!used.isVersioned()) continue;
574 // TODO: Update the uses relation
577 // this.lasdate = current.getTime(); // Today
578 // Database.getSession().update(this);
580 // TODO: Promote documents using this one
584 * 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
589 * @return true if documents of this type are result of a study.
590 * @see #isStepResult()
591 * @see #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
593 public boolean isStudyResult(final DocumentType aType) {
594 List<ProjectSettingsService.Step> step = getProjectSettings()
596 ProjectSettingsService.Step lastep = step.get(step.size() - 1);
597 return (aType.isResultOf(lastep));
601 * Get document by its path.
605 * @return the document if found or null
607 @Transactional(readOnly = true)
608 public Document getDocumentByPath(String path) {
609 String[] parse = path.split("'");
612 for (int i = 1; i < parse.length; i++) {
613 path = path + "''" + parse[i];
615 Criterion aCondition = Restrictions.eq("path", path);
616 return getDocumentDAO().findByCriteria(aCondition);
620 * Get the studyService.
622 * @return the studyService
624 public StudyService getStudyService() {
625 return _studyService;
629 * Set the studyService.
631 * @param studyService
632 * the studyService to set
634 public void setStudyService(final StudyService studyService) {
635 _studyService = studyService;
639 * Get project settings.
641 * @return Project settings service
643 private ProjectSettingsService getProjectSettings() {
644 return _projectSettings;
648 * Set project settings service.
650 * @param projectSettingsService
651 * project settings service
653 public void setProjectSettings(
654 final ProjectSettingsService projectSettingsService) {
655 _projectSettings = projectSettingsService;
659 * Get the documentDAO.
661 * @return the documentDAO
663 public DocumentDAO getDocumentDAO() {
668 * Set the documentDAO.
671 * the documentDAO to set
673 public void setDocumentDAO(final DocumentDAO documentDAO) {
674 _documentDAO = documentDAO;
678 * Get the repositoryService.
680 * @return the repositoryService
682 public RepositoryService getRepositoryService() {
683 return _repositoryService;
687 * Set the repositoryService.
689 * @param repositoryService
690 * the repositoryService to set
692 public void setRepositoryService(final RepositoryService repositoryService) {
693 _repositoryService = repositoryService;
697 * Get the documentTypeDAO.
699 * @return the documentTypeDAO
701 public DocumentTypeDAO getDocumentTypeDAO() {
702 return _documentTypeDAO;
706 * Set the documentTypeDAO.
708 * @param documentTypeDAO
709 * the documentTypeDAO to set
711 public void setDocumentTypeDAO(final DocumentTypeDAO documentTypeDAO) {
712 _documentTypeDAO = documentTypeDAO;
718 * @return the fileDAO
720 public FileDAO getFileDAO() {
730 public void setFileDAO(final FileDAO fileDAO) {
737 * @return the studyDAO
739 public StudyDAO getStudyDAO() {
747 * the studyDAO to set
749 public void setStudyDAO(final StudyDAO studyDAO) {
750 _studyDAO = studyDAO;
754 * Get the relationDAO.
756 * @return the relationDAO
758 public RelationDAO getRelationDAO() {
763 * Set the relationDAO.
766 * the relationDAO to set
768 public void setRelationDAO(final RelationDAO relationDAO) {
769 _relationDAO = relationDAO;