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.hibernate.criterion.Criterion;
17 import org.hibernate.criterion.Restrictions;
18 import org.splat.dal.bo.som.Document;
19 import org.splat.dal.bo.som.Scenario;
20 import org.splat.dal.bo.som.Study;
21 import org.splat.dal.bo.som.Document.Properties;
22 import org.splat.dal.dao.som.DocumentDAO;
23 import org.splat.kernel.InvalidPropertyException;
24 import org.splat.kernel.MissedPropertyException;
25 import org.splat.kernel.NotApplicableException;
26 import org.splat.service.technical.ProjectSettingsService;
27 import org.splat.service.technical.ProjectSettingsServiceImpl;
28 import org.splat.service.technical.RepositoryService;
29 import org.splat.service.technical.ProjectSettingsServiceImpl.FileNaming;
30 import org.springframework.transaction.annotation.Transactional;
33 * Document service implementation.
35 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
38 public class DocumentServiceImpl implements DocumentService {
41 * Injected study service.
43 private StudyService _studyService;
45 * Injected project settings service.
47 private ProjectSettingsService _projectSettingsService;
49 * Injected repository service.
51 private RepositoryService _repositoryService;
53 * Injected document service.
55 private DocumentDAO _documentDAO;
60 * @see org.splat.service.DocumentService#selectDocument(long)
62 @Transactional(readOnly = true)
63 public Document selectDocument(long index) {
64 return getDocumentDAO().get(index);
70 * @see org.splat.service.DocumentService#selectDocument(java.lang.String, java.lang.String)
72 @Transactional(readOnly = true)
73 public Document selectDocument(String refid, String version) {
74 Criterion aCondition = Restrictions.and(Restrictions.eq("did", refid),
75 Restrictions.eq("version", version));
76 return getDocumentDAO().findByCriteria(aCondition);
82 * @see org.splat.service.DocumentService#generateDocumentId(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
84 public void generateDocumentId(Document aDoc, Properties dprop) {
86 if (dprop.getOwner() instanceof Study)
87 owner = (Study) dprop.getOwner();
89 owner = ((Scenario) dprop.getOwner()).getOwnerStudy();
91 SimpleDateFormat tostring = new SimpleDateFormat("yyyy");
92 String year = tostring.format(owner.getDate());
93 String filename = generateEncodedName(aDoc, owner);
94 String path = owner.getReference();
95 ProjectSettingsService.Step step = ProjectSettingsServiceImpl
96 .getStep(aDoc.getStep());
97 aDoc.setDid(new StringBuffer(path).append(".%").append(
98 Document.suformat).toString()); // Document reference
99 path = new StringBuffer(year).append("/").append(path).append("/")
100 .append(step.getPath())
101 // File path relative to the repository vault
102 .append(filename).append(".")
103 .append(aDoc.getFile().getFormat()) // File name and extension
105 aDoc.getFile().changePath(path);
109 * Generate encoded document file name according to the naming scheme from project settings.
115 * @return document reference name
117 private String generateEncodedName(Document aDoc, Study scope) {
118 StringBuffer encoding = new StringBuffer();
119 FileNaming scheme = getProjectSettings().getFileNamingScheme();
120 DecimalFormat tostring = new DecimalFormat(Document.suformat);
122 int number = getStudyService().generateLocalIndex(scope);
124 if (scheme == FileNaming.encoded) {
125 encoding.append(scope.getReference()).append(".").append(
126 tostring.format(number));
127 } else { // title and (temporarily) asis
128 encoding.append(aDoc.getTitle()).append(".").append(
129 tostring.format(number));
131 return encoding.toString();
135 * Get encoded root name for a document file.
143 private String getEncodedRootName(Document aDoc, Study scope) {
144 FileNaming scheme = getProjectSettings().getFileNamingScheme();
146 if (scheme == FileNaming.encoded)
147 return scope.getReference();
149 return aDoc.getTitle();
155 * @see org.splat.service.DocumentService#initialize(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
157 public void initialize(Document aDoc, Properties dprop)
158 throws MissedPropertyException, InvalidPropertyException,
159 NotApplicableException {
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 getDocumentDAO().update(aDoc);
190 * @see org.splat.service.DocumentService#getSaveDirectory(org.splat.dal.bo.som.Document)
192 public java.io.File getSaveDirectory(Document aDoc) {
193 String mypath = getRepositoryService().getRepositoryVaultPath()
194 + aDoc.getSourceFile().getRelativePath();
195 String[] table = mypath.split("/");
197 // Cutting the filename
198 StringBuffer path = new StringBuffer(table[0]);
199 for (int i = 1; i < table.length - 1; i++)
200 path = path.append("/").append(table[i]);
201 return new java.io.File(path.append("/").toString());
205 * Get the studyService.
207 * @return the studyService
209 public StudyService getStudyService() {
210 return _studyService;
214 * Set the studyService.
216 * @param studyService
217 * the studyService to set
219 public void setStudyService(StudyService studyService) {
220 _studyService = studyService;
224 * Get project settings.
226 * @return Project settings service
228 private ProjectSettingsService getProjectSettings() {
229 return _projectSettingsService;
233 * Set project settings service.
235 * @param projectSettingsService
236 * project settings service
238 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
239 _projectSettingsService = projectSettingsService;
243 * Get the documentDAO.
245 * @return the documentDAO
247 public DocumentDAO getDocumentDAO() {
252 * Set the documentDAO.
255 * the documentDAO to set
257 public void setDocumentDAO(DocumentDAO documentDAO) {
258 _documentDAO = documentDAO;
262 * Get the repositoryService.
264 * @return the repositoryService
266 public RepositoryService getRepositoryService() {
267 return _repositoryService;
271 * Set the repositoryService.
273 * @param repositoryService
274 * the repositoryService to set
276 public void setRepositoryService(RepositoryService repositoryService) {
277 _repositoryService = repositoryService;