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