Salome HOME
33b15cf69f8ba509eac70e958feb9c109b801b3c
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / DocumentTypeServiceImpl.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
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.util.Iterator;
13 import java.util.List;
14
15 import org.hibernate.Hibernate;
16 import org.hibernate.criterion.Order;
17 import org.hibernate.criterion.Restrictions;
18 import org.splat.common.properties.MessageKeyEnum;
19 import org.splat.dal.bo.som.DocumentType;
20 import org.splat.dal.bo.som.ProgressState;
21 import org.splat.dal.dao.som.DocumentTypeDAO;
22 import org.splat.kernel.InvalidPropertyException;
23 import org.splat.kernel.MissedPropertyException;
24 import org.splat.kernel.MultiplyDefinedException;
25 import org.splat.log.AppLogger;
26 import org.splat.service.technical.ProjectSettingsService;
27 import org.springframework.transaction.annotation.Transactional;
28
29 /**
30  * Document type service implementation.
31  * 
32  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
33  */
34 public class DocumentTypeServiceImpl implements DocumentTypeService {
35
36         /**
37          * The logger for the service.
38          */
39         public final static AppLogger LOG = AppLogger
40                         .getLogger(DocumentTypeServiceImpl.class);
41         /**
42          * Injected document type DAO.
43          */
44         private DocumentTypeDAO _documentTypeDAO;
45
46         /**
47          * Get all document types from the database.
48          * 
49          * @return the list of all document types
50          */
51         // @Transactional(readOnly = true)
52         @Transactional
53         public List<DocumentType> selectAllTypes() {
54                 List<DocumentType> types = getDocumentTypeDAO().getAll();
55                 for (Iterator<DocumentType> i = types.iterator(); i.hasNext();) {
56                         Hibernate.initialize(i.next()); // Supposed fetching document types
57                 }
58                 return types;
59         }
60
61         /**
62          * Find all result document types.
63          * 
64          * @return the list of found types
65          */
66         @Transactional(readOnly = true)
67         public List<DocumentType> selectResultTypes() {
68                 return getDocumentTypeDAO().getFilteredList(
69                                 Restrictions.isNotNull("result"), Order.asc("result"));
70         }
71
72         /**
73          * Get document type by the given type name.
74          * 
75          * @param name
76          *            the type name
77          * @return the found document type
78          */
79         @Transactional(readOnly = true)
80         public DocumentType selectType(final String name) {
81                 return getDocumentTypeDAO().findByCriteria(
82                                 Restrictions.eq("name", name));
83         }
84
85         /**
86          * Get document type by the given id.
87          * 
88          * @param index
89          *            the id
90          * @return the found document type
91          */
92         @Transactional(readOnly = true)
93         public DocumentType selectType(final long index) {
94                 return getDocumentTypeDAO().get(index);
95         }
96
97         /**
98          * Create a new document type.
99          * 
100          * @param tprop
101          *            properties of the new document type
102          * @return the created document type
103          * @throws MissedPropertyException
104          *             if a mandatory property is missed
105          * @throws InvalidPropertyException
106          *             if some property doesn't exist
107          * @throws MultiplyDefinedException
108          *             if some property is defined several times
109          */
110         @Transactional
111         public DocumentType createType(final DocumentType.Properties tprop)
112                         throws MissedPropertyException, InvalidPropertyException,
113                         MultiplyDefinedException {
114                 DocumentType type = getDocumentTypeDAO().findByCriteria(
115                                 Restrictions.eq("name", tprop.getName()));
116                 if (type == null) {
117                         type = new DocumentType(tprop);
118                         getDocumentTypeDAO().create(type);
119                 } else {
120                         LOG.info(MessageKeyEnum.DCT_000001.toString(), tprop.getName());
121                         type.setProperties(tprop); // Update properties of persistent type
122                 }
123                 return type;
124         }
125
126         /**
127          * Get document types applicable for the given study step.
128          * 
129          * @param step
130          *            the step (study activity)
131          * @return the list of found document types
132          */
133         @Transactional(readOnly = true)
134         public List<DocumentType> selectTypesOf(
135                         final ProjectSettingsService.Step step) {
136                 Integer number = step.getNumber();
137                 String sampleStr = new StringBuffer("%-").append(number).append("-%")
138                                 .toString();
139                 List<DocumentType> types = getDocumentTypeDAO().getFilteredList(
140                                 Restrictions.like("step", sampleStr));
141                 for (Iterator<DocumentType> i = types.iterator(); i.hasNext();) {
142                         Hibernate.initialize(i.next()); // For fetching document types
143                 }
144                 return types;
145         }
146
147         /**
148          * Approve the document type.
149          * 
150          * @param aType
151          *            the document type to approve
152          * @return true if approval succeeded
153          */
154         @Transactional
155         public boolean approve(final DocumentType aType) {
156                 boolean res = (aType.getState() == ProgressState.inCHECK);
157                 if (res) {
158                         aType.setState(ProgressState.APPROVED); // The type name is supposed being localized
159                         getDocumentTypeDAO().update(aType);
160                 }
161                 return res;
162         }
163
164         /**
165          * Get the documentTypeDAO.
166          * 
167          * @return the documentTypeDAO
168          */
169         public DocumentTypeDAO getDocumentTypeDAO() {
170                 return _documentTypeDAO;
171         }
172
173         /**
174          * Set the documentTypeDAO.
175          * 
176          * @param documentTypeDAO
177          *            the documentTypeDAO to set
178          */
179         public void setDocumentTypeDAO(final DocumentTypeDAO documentTypeDAO) {
180                 _documentTypeDAO = documentTypeDAO;
181         }
182 }