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