]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/service/DocumentServiceImpl.java
Salome HOME
1d06100c05b1246a3fa52378dd7a1f13fae1c528
[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.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;
31
32 /**
33  * Document service implementation.
34  * 
35  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
36  * 
37  */
38 public class DocumentServiceImpl implements DocumentService {
39
40         /**
41          * Injected study service.
42          */
43         private StudyService _studyService;
44         /**
45          * Injected project settings service.
46          */
47         private ProjectSettingsService _projectSettingsService;
48         /**
49          * Injected repository service.
50          */
51         private RepositoryService _repositoryService;
52         /**
53          * Injected document service.
54          */
55         private DocumentDAO _documentDAO;
56
57         /**
58          * {@inheritDoc}
59          * 
60          * @see org.splat.service.DocumentService#selectDocument(long)
61          */
62         @Transactional(readOnly = true)
63         public Document selectDocument(long index) {
64                 return getDocumentDAO().get(index);
65         }
66
67         /**
68          * {@inheritDoc}
69          * 
70          * @see org.splat.service.DocumentService#selectDocument(java.lang.String, java.lang.String)
71          */
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);
77         }
78
79         /**
80          * {@inheritDoc}
81          * 
82          * @see org.splat.service.DocumentService#generateDocumentId(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
83          */
84         public void generateDocumentId(Document aDoc, Properties dprop) {
85                 Study owner = null;
86                 if (dprop.getOwner() instanceof Study)
87                         owner = (Study) dprop.getOwner();
88                 else
89                         owner = ((Scenario) dprop.getOwner()).getOwnerStudy();
90
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
104                                 .toString();
105                 aDoc.getFile().changePath(path);
106         }
107
108         /**
109          * Generate encoded document file name according to the naming scheme from project settings.
110          * 
111          * @param aDoc
112          *            the document
113          * @param scope
114          *            the study
115          * @return document reference name
116          */
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);
121
122                 int number = getStudyService().generateLocalIndex(scope);
123
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));
130                 }
131                 return encoding.toString();
132         }
133
134         /**
135          * Get encoded root name for a document file.
136          * 
137          * @param aDoc
138          *            the document
139          * @param scope
140          *            the study
141          * @return file name
142          */
143         private String getEncodedRootName(Document aDoc, Study scope) {
144                 FileNaming scheme = getProjectSettings().getFileNamingScheme();
145
146                 if (scheme == FileNaming.encoded)
147                         return scope.getReference();
148                 else
149                         return aDoc.getTitle();
150         }
151
152         /**
153          * {@inheritDoc}
154          * 
155          * @see org.splat.service.DocumentService#initialize(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
156          */
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");
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                 getDocumentDAO().update(aDoc);
185         }
186
187         /**
188          * {@inheritDoc}
189          * 
190          * @see org.splat.service.DocumentService#getSaveDirectory(org.splat.dal.bo.som.Document)
191          */
192         public java.io.File getSaveDirectory(Document aDoc) {
193                 String mypath = getRepositoryService().getRepositoryVaultPath()
194                                 + aDoc.getSourceFile().getRelativePath();
195                 String[] table = mypath.split("/");
196
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());
202         }
203
204         /**
205          * Get the studyService.
206          * 
207          * @return the studyService
208          */
209         public StudyService getStudyService() {
210                 return _studyService;
211         }
212
213         /**
214          * Set the studyService.
215          * 
216          * @param studyService
217          *            the studyService to set
218          */
219         public void setStudyService(StudyService studyService) {
220                 _studyService = studyService;
221         }
222
223         /**
224          * Get project settings.
225          * 
226          * @return Project settings service
227          */
228         private ProjectSettingsService getProjectSettings() {
229                 return _projectSettingsService;
230         }
231
232         /**
233          * Set project settings service.
234          * 
235          * @param projectSettingsService
236          *            project settings service
237          */
238         public void setProjectSettings(ProjectSettingsService projectSettingsService) {
239                 _projectSettingsService = projectSettingsService;
240         }
241
242         /**
243          * Get the documentDAO.
244          * 
245          * @return the documentDAO
246          */
247         public DocumentDAO getDocumentDAO() {
248                 return _documentDAO;
249         }
250
251         /**
252          * Set the documentDAO.
253          * 
254          * @param documentDAO
255          *            the documentDAO to set
256          */
257         public void setDocumentDAO(DocumentDAO documentDAO) {
258                 _documentDAO = documentDAO;
259         }
260
261         /**
262          * Get the repositoryService.
263          * 
264          * @return the repositoryService
265          */
266         public RepositoryService getRepositoryService() {
267                 return _repositoryService;
268         }
269
270         /**
271          * Set the repositoryService.
272          * 
273          * @param repositoryService
274          *            the repositoryService to set
275          */
276         public void setRepositoryService(RepositoryService repositoryService) {
277                 _repositoryService = repositoryService;
278         }
279
280 }