]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/service/DocumentTypeServiceImpl.java
Salome HOME
Modifications done to respect PMD rules. Versioning a document is fixed. Validation...
[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                 // TODO: Check for duplicate definition
108                 DocumentType type = new DocumentType(tprop);
109                 getDocumentTypeDAO().create(type);
110                 return type;
111         }
112
113         /**
114          * Get document types applicable for the given study step.
115          * 
116          * @param step
117          *            the step (study activity)
118          * @return the list of found document types
119          */
120         @Transactional(readOnly = true)
121         public List<DocumentType> selectTypesOf(final ProjectSettingsService.Step step) {
122                 Integer number = step.getNumber();
123                 String sampleStr = new StringBuffer("%-").append(number).append("-%")
124                                 .toString();
125                 List<DocumentType> types = getDocumentTypeDAO().getFilteredList(
126                                 Restrictions.like("step", sampleStr));
127                 for (Iterator<DocumentType> i = types.iterator(); i.hasNext();) {
128                         Hibernate.initialize(i.next()); // For fetching document types
129                 }
130                 return types;
131         }
132
133         /**
134          * Approve the document type.
135          * @param aType the document type to approve
136          * @return true if approval succeeded
137          */
138         @Transactional
139     public boolean approve(final DocumentType aType) {
140                 boolean res = (aType.getState() == ProgressState.inCHECK);
141                 if (res) {
142                         aType.setState(ProgressState.APPROVED); // The type name is supposed being localized
143                         getDocumentTypeDAO().update(aType);
144                 }
145                 return res;
146         }
147
148         /**
149          * Get the documentTypeDAO.
150          * 
151          * @return the documentTypeDAO
152          */
153         public DocumentTypeDAO getDocumentTypeDAO() {
154                 return _documentTypeDAO;
155         }
156
157         /**
158          * Set the documentTypeDAO.
159          * 
160          * @param documentTypeDAO
161          *            the documentTypeDAO to set
162          */
163         public void setDocumentTypeDAO(final DocumentTypeDAO documentTypeDAO) {
164                 _documentTypeDAO = documentTypeDAO;
165         }
166 }