Salome HOME
Each document type now uses itself. So default uses iincludes documents of the type...
[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                         type.getDefaultUses().add(type); //RKV
119                         getDocumentTypeDAO().create(type);
120                 } else {
121                         LOG.info(MessageKeyEnum.DCT_000001.toString(), tprop.getName());
122                         type.setProperties(tprop); // Update properties of persistent type
123                 }
124                 return type;
125         }
126
127         /**
128          * Get document types applicable for the given study step.
129          * 
130          * @param step
131          *            the step (study activity)
132          * @return the list of found document types
133          */
134         @Transactional(readOnly = true)
135         public List<DocumentType> selectTypesOf(
136                         final ProjectSettingsService.Step step) {
137                 Integer number = step.getNumber();
138                 String sampleStr = new StringBuffer("%-").append(number).append("-%")
139                                 .toString();
140                 List<DocumentType> types = getDocumentTypeDAO().getFilteredList(
141                                 Restrictions.like("step", sampleStr));
142                 for (Iterator<DocumentType> i = types.iterator(); i.hasNext();) {
143                         Hibernate.initialize(i.next()); // For fetching document types
144                 }
145                 return types;
146         }
147
148         /**
149          * Approve the document type.
150          * 
151          * @param aType
152          *            the document type to approve
153          * @return true if approval succeeded
154          */
155         @Transactional
156         public boolean approve(final DocumentType aType) {
157                 boolean res = (aType.getState() == ProgressState.inCHECK);
158                 if (res) {
159                         aType.setState(ProgressState.APPROVED); // The type name is supposed being localized
160                         getDocumentTypeDAO().update(aType);
161                 }
162                 return res;
163         }
164
165         /**
166          * Get the documentTypeDAO.
167          * 
168          * @return the documentTypeDAO
169          */
170         public DocumentTypeDAO getDocumentTypeDAO() {
171                 return _documentTypeDAO;
172         }
173
174         /**
175          * Set the documentTypeDAO.
176          * 
177          * @param documentTypeDAO
178          *            the documentTypeDAO to set
179          */
180         public void setDocumentTypeDAO(final DocumentTypeDAO documentTypeDAO) {
181                 _documentTypeDAO = documentTypeDAO;
182         }
183 }