1 /*****************************************************************************
5 * Creation date 06.10.2012
8 *****************************************************************************/
10 package org.splat.service;
12 import java.util.Iterator;
13 import java.util.List;
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;
30 * Document type service implementation.
32 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
34 public class DocumentTypeServiceImpl implements DocumentTypeService {
37 * The logger for the service.
39 public final static AppLogger LOG = AppLogger
40 .getLogger(DocumentTypeServiceImpl.class);
42 * Injected document type DAO.
44 private DocumentTypeDAO _documentTypeDAO;
47 * Get all document types from the database.
49 * @return the list of all document types
51 // @Transactional(readOnly = true)
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
62 * Find all result document types.
64 * @return the list of found types
66 @Transactional(readOnly = true)
67 public List<DocumentType> selectResultTypes() {
68 return getDocumentTypeDAO().getFilteredList(
69 Restrictions.isNotNull("result"), Order.asc("result"));
73 * Get document type by the given type name.
77 * @return the found document type
79 @Transactional(readOnly = true)
80 public DocumentType selectType(final String name) {
81 return getDocumentTypeDAO().findByCriteria(
82 Restrictions.eq("name", name));
86 * Get document type by the given id.
90 * @return the found document type
92 @Transactional(readOnly = true)
93 public DocumentType selectType(final long index) {
94 return getDocumentTypeDAO().get(index);
98 * Create a new document type.
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
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()));
117 type = new DocumentType(tprop);
118 type.getDefaultUses().add(type); //RKV
119 getDocumentTypeDAO().create(type);
121 LOG.info(MessageKeyEnum.DCT_000001.toString(), tprop.getName());
122 type.setProperties(tprop); // Update properties of persistent type
128 * Get document types applicable for the given study step.
131 * the step (study activity)
132 * @return the list of found document types
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("-%")
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
149 * Approve the document type.
152 * the document type to approve
153 * @return true if approval succeeded
156 public boolean approve(final DocumentType aType) {
157 boolean res = (aType.getState() == ProgressState.inCHECK);
159 aType.setState(ProgressState.APPROVED); // The type name is supposed being localized
160 getDocumentTypeDAO().update(aType);
166 * Get the documentTypeDAO.
168 * @return the documentTypeDAO
170 public DocumentTypeDAO getDocumentTypeDAO() {
171 return _documentTypeDAO;
175 * Set the documentTypeDAO.
177 * @param documentTypeDAO
178 * the documentTypeDAO to set
180 public void setDocumentTypeDAO(final DocumentTypeDAO documentTypeDAO) {
181 _documentTypeDAO = documentTypeDAO;