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;
16 import org.splat.dal.bo.som.Document;
17 import org.splat.dal.bo.som.Scenario;
18 import org.splat.dal.bo.som.Study;
19 import org.splat.dal.bo.som.Document.Properties;
20 import org.splat.dal.dao.som.Database;
21 import org.splat.kernel.InvalidPropertyException;
22 import org.splat.kernel.MissedPropertyException;
23 import org.splat.kernel.NotApplicableException;
24 import org.splat.service.technical.ProjectSettingsService;
25 import org.splat.service.technical.ProjectSettingsServiceImpl;
26 import org.splat.service.technical.ProjectSettingsServiceImpl.FileNaming;
27 import org.splat.som.Step;
33 public class DocumentServiceImpl implements DocumentService {
35 private StudyService _studyService;
36 private ProjectSettingsService _projectSettingsService;
38 public void generateDocumentId(Document aDoc, Properties dprop) {
40 if (dprop.getOwner() instanceof Study)
41 owner = (Study) dprop.getOwner();
43 owner = ((Scenario) dprop.getOwner()).getOwnerStudy();
45 SimpleDateFormat tostring = new SimpleDateFormat("yyyy");
46 String year = tostring.format(owner.getDate());
47 String filename = generateEncodedName(aDoc, owner);
48 String path = owner.getReference();
49 ProjectSettingsService.Step step = ProjectSettingsServiceImpl
50 .getStep(aDoc.getStep());
51 aDoc.setDid( new StringBuffer(path).append(".%").append(aDoc.suformat).toString()); // Document reference
52 path = new StringBuffer(year).append("/").append(path).append("/")
53 .append(step.getPath()) // File path relative to the repository vault
54 .append(filename).append(".").append(aDoc.getFile().getFormat()) // File name and extension
56 aDoc.getFile().changePath(path);
59 private String generateEncodedName(Document aDoc, Study scope) {
60 // ------------------------------------------------
61 StringBuffer encoding = new StringBuffer();
62 FileNaming scheme = getProjectSettings().getFileNamingScheme();
63 DecimalFormat tostring = new DecimalFormat(aDoc.suformat);
65 int number = getStudyService().generateLocalIndex(scope);
67 if (scheme == FileNaming.encoded) {
68 encoding.append(scope.getReference()).append(".").append(
69 tostring.format(number));
70 } else { // title and (temporarily) asis
71 encoding.append(aDoc.getTitle()).append(".").append(tostring.format(number));
73 return encoding.toString();
76 private String getEncodedRootName(Document aDoc, Study scope) {
77 // -----------------------------------------------
78 FileNaming scheme = getProjectSettings().getFileNamingScheme();
80 if (scheme == FileNaming.encoded)
81 return scope.getReference();
83 return aDoc.getTitle();
87 * Defines this document.
90 * the properties of the document
92 * @see Step#createDocument(Properties)
95 public void initialize(Document aDoc, Properties dprop) throws MissedPropertyException,
96 InvalidPropertyException, NotApplicableException {
97 // --------------------------------------------
98 if (!aDoc.isUndefined())
99 throw new NotApplicableException(
100 "Cannot initialize an existing Document");
101 if (dprop.getName() == null)
102 throw new MissedPropertyException("name");
103 if (dprop.getName().length() == 0)
104 throw new InvalidPropertyException("name");
105 if (dprop.getOwner() == null)
106 throw new MissedPropertyException("owner");
107 // if (dprop.owner instanceof Study && !ProjectSettings.getStep(step).appliesTo(Study.class)) {
108 // throw new InvalidPropertyException("step");
110 aDoc.setTitle(dprop.getName());
111 aDoc.getFile().changePath(aDoc.getFile().getRelativePath().replace("%n",
112 getEncodedRootName(aDoc, (Study) dprop.getOwner())));
113 if (aDoc.getHistory() == -1)
114 aDoc.setHistory( 0 );
115 if (dprop.getDate() == null) {
116 Calendar current = Calendar.getInstance();
117 aDoc.setLastModificationDate(current.getTime()); // Today
119 aDoc.setLastModificationDate(dprop.getDate());
121 Database.getSession().update(aDoc);
125 * Get the studyService.
126 * @return the studyService
128 public StudyService getStudyService() {
129 return _studyService;
133 * Set the studyService.
134 * @param studyService the studyService to set
136 public void setStudyService(StudyService studyService) {
137 _studyService = studyService;
141 * Get project settings.
143 * @return Project settings service
145 private ProjectSettingsService getProjectSettings() {
146 return _projectSettingsService;
150 * Set project settings service.
152 * @param projectSettingsService
153 * project settings service
155 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
156 _projectSettingsService = projectSettingsService;