/*****************************************************************************
- * Company EURIWARE
+ * Company OPEN CASCADE
* Application SIMAN
* File $Id$
* Creation date 06.10.2012
* @version $Revision$
*****************************************************************************/
-package org.splat.service;
+package org.splat.service;
+import java.util.Iterator;
import java.util.List;
+import org.hibernate.Hibernate;
+import org.hibernate.criterion.Order;
+import org.hibernate.criterion.Restrictions;
import org.splat.dal.bo.som.DocumentType;
+import org.splat.dal.bo.som.ProgressState;
+import org.splat.dal.dao.som.Database;
+import org.splat.dal.dao.som.DocumentTypeDAO;
+import org.splat.kernel.InvalidPropertyException;
+import org.splat.kernel.MissedPropertyException;
+import org.splat.kernel.MultiplyDefinedException;
import org.splat.service.technical.ProjectSettingsService;
+import org.springframework.transaction.annotation.Transactional;
/**
- * @author RKV
+ * Document type service implementation.
*
+ * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
*/
public class DocumentTypeServiceImpl implements DocumentTypeService {
- private ProjectSettingsService _projectSettingsService;
+ /**
+ * Injected document type DAO.
+ */
+ private DocumentTypeDAO _documentTypeDAO;
+
+ /**
+ * Get all document types from the database.
+ *
+ * @return the list of all document types
+ */
+// @Transactional(readOnly = true)
+ @Transactional
+ public List<DocumentType> selectAllTypes() {
+ List<DocumentType> types = getDocumentTypeDAO().getAll();
+ for (Iterator<DocumentType> i = types.iterator(); i.hasNext();) {
+ Hibernate.initialize(i.next()); // Supposed fetching document types
+ }
+ return types;
+ }
+
+ /**
+ * Find all result document types.
+ *
+ * @return the list of found types
+ */
+ @Transactional(readOnly = true)
+ public List<DocumentType> selectResultTypes() {
+ return getDocumentTypeDAO().getFilteredList(
+ Restrictions.isNotNull("result"), Order.asc("result"));
+ }
+
+ /**
+ * Get document type by the given type name.
+ *
+ * @param name
+ * the type name
+ * @return the found document type
+ */
+ @Transactional(readOnly = true)
+ public DocumentType selectType(String name) {
+ return getDocumentTypeDAO().findByCriteria(
+ Restrictions.eq("name", name));
+ }
/**
- * Checks if documents of this type are result of a study.
- * A document is the result of a study when it is the result of the last step of the study.
+ * Get document type by the given id.
*
- * @return true if documents of this type are result of a study.
- * @see #isStepResult()
- * @see #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
+ * @param index
+ * the id
+ * @return the found document type
*/
- public boolean isStudyResult (DocumentType aType) {
- // -------------------------------
- List<ProjectSettingsService.Step> step = getProjectSettings().getAllSteps();
- ProjectSettingsService.Step lastep = step.get( step.size()-1 );
- return (aType.isResultOf(lastep));
- }
+ @Transactional(readOnly = true)
+ public DocumentType selectType(long index) {
+ return getDocumentTypeDAO().get(index);
+ }
/**
- * @return
+ * Create a new document type.
+ *
+ * @param tprop
+ * properties of the new document type
+ * @return the created document type
+ * @throws MissedPropertyException
+ * if a mandatory property is missed
+ * @throws InvalidPropertyException
+ * if some property doesn't exist
+ * @throws MultiplyDefinedException
+ * if some property is defined several times
*/
- public ProjectSettingsService getProjectSettings() {
- return _projectSettingsService;
+ @Transactional
+ public DocumentType createType(DocumentType.Properties tprop)
+ throws MissedPropertyException, InvalidPropertyException,
+ MultiplyDefinedException {
+ // TODO: Check for duplicate definition
+ DocumentType type = new DocumentType(tprop);
+ getDocumentTypeDAO().create(type);
+ return type;
}
- public void setProjectSettings(
- ProjectSettingsService projectSettingsService) {
- _projectSettingsService = projectSettingsService;
+ /**
+ * Get document types applicable for the given study step.
+ *
+ * @param step
+ * the step (study activity)
+ * @return the list of found document types
+ */
+ @Transactional(readOnly = true)
+ public List<DocumentType> selectTypesOf(ProjectSettingsService.Step step) {
+ Integer number = step.getNumber();
+ String sampleStr = new StringBuffer("%-").append(number).append("-%")
+ .toString();
+ List<DocumentType> types = getDocumentTypeDAO().getFilteredList(
+ Restrictions.like("step", sampleStr));
+ for (Iterator<DocumentType> i = types.iterator(); i.hasNext();) {
+ Hibernate.initialize(i.next()); // For fetching document types
+ }
+ return types;
}
+ /**
+ * Approve the document type.
+ * @param aType the document type to approve
+ * @return true if approval succeeded
+ */
+ @Transactional
+ public boolean approve (DocumentType aType) {
+ if (aType.getState() != ProgressState.inCHECK) return false;
+ aType.setState(ProgressState.APPROVED); // The type name is supposed being localized
+ getDocumentTypeDAO().update(aType);
+ return true;
+ }
+
+ /**
+ * Get the documentTypeDAO.
+ *
+ * @return the documentTypeDAO
+ */
+ public DocumentTypeDAO getDocumentTypeDAO() {
+ return _documentTypeDAO;
+ }
+
+ /**
+ * Set the documentTypeDAO.
+ *
+ * @param documentTypeDAO
+ * the documentTypeDAO to set
+ */
+ public void setDocumentTypeDAO(DocumentTypeDAO documentTypeDAO) {
+ _documentTypeDAO = documentTypeDAO;
+ }
}