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