Salome HOME
Database class usage is removed from StudyService.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / DocumentServiceImpl.java
1 /*****************************************************************************
2  * Company         EURIWARE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   06.10.2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package org.splat.service;
11
12 import java.text.DecimalFormat;
13 import java.text.SimpleDateFormat;
14 import java.util.Calendar;
15
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;
29
30 /**
31  * Document service implementation.
32  * 
33  * @author RKV
34  * 
35  */
36 public class DocumentServiceImpl implements DocumentService {
37
38         /**
39          * Injected study service.
40          */
41         private StudyService _studyService;
42         /**
43          * Injected project settings service.
44          */
45         private ProjectSettingsService _projectSettingsService;
46         /**
47          * Injected document service.
48          */
49         private DocumentDAO _documentDAO;
50
51         /**
52          * {@inheritDoc}
53          * 
54          * @see org.splat.service.DocumentService#selectDocument(long)
55          */
56         @Transactional(readOnly = true)
57         public Document selectDocument(long index) {
58                 // -------------------------------------------------
59                 return getDocumentDAO().get(index);
60         }
61
62         /**
63          * {@inheritDoc}
64          * 
65          * @see org.splat.service.DocumentService#selectDocument(java.lang.String, java.lang.String)
66          */
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)
71                                 .append("'");
72                 return (Document) Database.getSession().createQuery(query.toString())
73                                 .uniqueResult();
74         }
75
76         /**
77          * {@inheritDoc}
78          * 
79          * @see org.splat.service.DocumentService#generateDocumentId(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
80          */
81         public void generateDocumentId(Document aDoc, Properties dprop) {
82                 Study owner = null;
83                 if (dprop.getOwner() instanceof Study)
84                         owner = (Study) dprop.getOwner();
85                 else
86                         owner = ((Scenario) dprop.getOwner()).getOwnerStudy();
87
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
101                                 .toString();
102                 aDoc.getFile().changePath(path);
103         }
104
105         /**
106          * Generate encoded document file name according to the naming scheme from project settings.
107          * 
108          * @param aDoc
109          *            the document
110          * @param scope
111          *            the study
112          * @return document reference name
113          */
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);
119
120                 int number = getStudyService().generateLocalIndex(scope);
121
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));
128                 }
129                 return encoding.toString();
130         }
131
132         /**
133          * Get encoded root name for a document file.
134          * 
135          * @param aDoc
136          *            the document
137          * @param scope
138          *            the study
139          * @return file name
140          */
141         private String getEncodedRootName(Document aDoc, Study scope) {
142                 // -----------------------------------------------
143                 FileNaming scheme = getProjectSettings().getFileNamingScheme();
144
145                 if (scheme == FileNaming.encoded)
146                         return scope.getReference();
147                 else
148                         return aDoc.getTitle();
149         }
150
151         /**
152          * {@inheritDoc}
153          * 
154          * @see org.splat.service.DocumentService#initialize(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
155          */
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");
171                 // }
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)
177                         aDoc.setHistory(0);
178                 if (dprop.getDate() == null) {
179                         Calendar current = Calendar.getInstance();
180                         aDoc.setLastModificationDate(current.getTime()); // Today
181                 } else {
182                         aDoc.setLastModificationDate(dprop.getDate());
183                 }
184                 Database.getSession().update(aDoc);
185         }
186
187         public java.io.File getSaveDirectory(Document aDoc) {
188                 String mypath = Database.getRepositoryVaultPath()
189                                 + aDoc.getSourceFile().getRelativePath();
190                 String[] table = mypath.split("/");
191
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());
197         }
198
199         /**
200          * Get the studyService.
201          * 
202          * @return the studyService
203          */
204         public StudyService getStudyService() {
205                 return _studyService;
206         }
207
208         /**
209          * Set the studyService.
210          * 
211          * @param studyService
212          *            the studyService to set
213          */
214         public void setStudyService(StudyService studyService) {
215                 _studyService = studyService;
216         }
217
218         /**
219          * Get project settings.
220          * 
221          * @return Project settings service
222          */
223         private ProjectSettingsService getProjectSettings() {
224                 return _projectSettingsService;
225         }
226
227         /**
228          * Set project settings service.
229          * 
230          * @param projectSettingsService
231          *            project settings service
232          */
233         public void setProjectSettings(ProjectSettingsService projectSettingsService) {
234                 _projectSettingsService = projectSettingsService;
235         }
236
237         /**
238          * Get the documentDAO.
239          * 
240          * @return the documentDAO
241          */
242         public DocumentDAO getDocumentDAO() {
243                 return _documentDAO;
244         }
245
246         /**
247          * Set the documentDAO.
248          * 
249          * @param documentDAO
250          *            the documentDAO to set
251          */
252         public void setDocumentDAO(DocumentDAO documentDAO) {
253                 _documentDAO = documentDAO;
254         }
255
256 }