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.dal.dao.som.DocumentDAO;
22 import org.splat.kernel.InvalidPropertyException;
23 import org.splat.kernel.MissedPropertyException;
24 import org.splat.kernel.NotApplicableException;
25 import org.splat.service.technical.ProjectSettingsService;
26 import org.splat.service.technical.ProjectSettingsServiceImpl;
27 import org.splat.service.technical.ProjectSettingsServiceImpl.FileNaming;
28 import org.springframework.transaction.annotation.Transactional;
31 * Document service implementation.
36 public class DocumentServiceImpl implements DocumentService {
39 * Injected study service.
41 private StudyService _studyService;
43 * Injected project settings service.
45 private ProjectSettingsService _projectSettingsService;
47 * Injected document service.
49 private DocumentDAO _documentDAO;
54 * @see org.splat.service.DocumentService#selectDocument(long)
56 @Transactional(readOnly = true)
57 public Document selectDocument(long index) {
58 // -------------------------------------------------
59 return getDocumentDAO().get(index);
65 * @see org.splat.service.DocumentService#selectDocument(java.lang.String, java.lang.String)
67 public Document selectDocument(String refid, String version) {
68 // --------------------------------------------------------------------
69 StringBuffer query = new StringBuffer("from Document where did='")
70 .append(refid).append("' and version='").append(version)
72 return (Document) Database.getSession().createQuery(query.toString())
79 * @see org.splat.service.DocumentService#generateDocumentId(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
81 public void generateDocumentId(Document aDoc, Properties dprop) {
83 if (dprop.getOwner() instanceof Study)
84 owner = (Study) dprop.getOwner();
86 owner = ((Scenario) dprop.getOwner()).getOwnerStudy();
88 SimpleDateFormat tostring = new SimpleDateFormat("yyyy");
89 String year = tostring.format(owner.getDate());
90 String filename = generateEncodedName(aDoc, owner);
91 String path = owner.getReference();
92 ProjectSettingsService.Step step = ProjectSettingsServiceImpl
93 .getStep(aDoc.getStep());
94 aDoc.setDid(new StringBuffer(path).append(".%").append(
95 Document.suformat).toString()); // Document reference
96 path = new StringBuffer(year).append("/").append(path).append("/")
97 .append(step.getPath())
98 // File path relative to the repository vault
99 .append(filename).append(".")
100 .append(aDoc.getFile().getFormat()) // File name and extension
102 aDoc.getFile().changePath(path);
106 * Generate encoded document file name according to the naming scheme from project settings.
112 * @return document reference name
114 private String generateEncodedName(Document aDoc, Study scope) {
115 // ------------------------------------------------
116 StringBuffer encoding = new StringBuffer();
117 FileNaming scheme = getProjectSettings().getFileNamingScheme();
118 DecimalFormat tostring = new DecimalFormat(Document.suformat);
120 int number = getStudyService().generateLocalIndex(scope);
122 if (scheme == FileNaming.encoded) {
123 encoding.append(scope.getReference()).append(".").append(
124 tostring.format(number));
125 } else { // title and (temporarily) asis
126 encoding.append(aDoc.getTitle()).append(".").append(
127 tostring.format(number));
129 return encoding.toString();
133 * Get encoded root name for a document file.
141 private String getEncodedRootName(Document aDoc, Study scope) {
142 // -----------------------------------------------
143 FileNaming scheme = getProjectSettings().getFileNamingScheme();
145 if (scheme == FileNaming.encoded)
146 return scope.getReference();
148 return aDoc.getTitle();
154 * @see org.splat.service.DocumentService#initialize(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
156 public void initialize(Document aDoc, Properties dprop)
157 throws MissedPropertyException, InvalidPropertyException,
158 NotApplicableException {
159 // --------------------------------------------
160 if (!aDoc.isUndefined())
161 throw new NotApplicableException(
162 "Cannot initialize an existing Document");
163 if (dprop.getName() == null)
164 throw new MissedPropertyException("name");
165 if (dprop.getName().length() == 0)
166 throw new InvalidPropertyException("name");
167 if (dprop.getOwner() == null)
168 throw new MissedPropertyException("owner");
169 // if (dprop.owner instanceof Study && !ProjectSettings.getStep(step).appliesTo(Study.class)) {
170 // throw new InvalidPropertyException("step");
172 aDoc.setTitle(dprop.getName());
173 aDoc.getFile().changePath(
174 aDoc.getFile().getRelativePath().replace("%n",
175 getEncodedRootName(aDoc, (Study) dprop.getOwner())));
176 if (aDoc.getHistory() == -1)
178 if (dprop.getDate() == null) {
179 Calendar current = Calendar.getInstance();
180 aDoc.setLastModificationDate(current.getTime()); // Today
182 aDoc.setLastModificationDate(dprop.getDate());
184 Database.getSession().update(aDoc);
187 public java.io.File getSaveDirectory(Document aDoc) {
188 String mypath = Database.getRepositoryVaultPath()
189 + aDoc.getSourceFile().getRelativePath();
190 String[] table = mypath.split("/");
192 // Cutting the filename
193 StringBuffer path = new StringBuffer(table[0]);
194 for (int i = 1; i < table.length - 1; i++)
195 path = path.append("/").append(table[i]);
196 return new java.io.File(path.append("/").toString());
200 * Get the studyService.
202 * @return the studyService
204 public StudyService getStudyService() {
205 return _studyService;
209 * Set the studyService.
211 * @param studyService
212 * the studyService to set
214 public void setStudyService(StudyService studyService) {
215 _studyService = studyService;
219 * Get project settings.
221 * @return Project settings service
223 private ProjectSettingsService getProjectSettings() {
224 return _projectSettingsService;
228 * Set project settings service.
230 * @param projectSettingsService
231 * project settings service
233 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
234 _projectSettingsService = projectSettingsService;
238 * Get the documentDAO.
240 * @return the documentDAO
242 public DocumentDAO getDocumentDAO() {
247 * Set the documentDAO.
250 * the documentDAO to set
252 public void setDocumentDAO(DocumentDAO documentDAO) {
253 _documentDAO = documentDAO;