]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman-Common/src/org/splat/service/DocumentTypeServiceImpl.java
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
index c88ec88d9a22a923dd06e3821e26c8ed0df9b46b..63fecb5b6236687d0585a6eabae003b7b2fa6dc0 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * 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.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)
+       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;
+       }
 
        /**
-        * 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.
+        * Find all result document types.
         * 
-        * @return true if documents of this type are result of a study.
-        * @see    #isStepResult()
-        * @see    #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
+        * @return the list of found types
         */
-           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 List<DocumentType> selectResultTypes() {
+               return getDocumentTypeDAO().getFilteredList(
+                               Restrictions.isNotNull("result"), Order.asc("result"));
+       }
 
        /**
-        * @return
+        * Get document type by the given type name.
+        * 
+        * @param name
+        *            the type name
+        * @return the found document type
         */
-       public ProjectSettingsService getProjectSettings() {
-               return _projectSettingsService;
+       @Transactional(readOnly = true)
+       public DocumentType selectType(String name) {
+               return getDocumentTypeDAO().findByCriteria(
+                               Restrictions.eq("name", name));
        }
 
-       public void setProjectSettings(
-                       ProjectSettingsService projectSettingsService) {
-               _projectSettingsService = projectSettingsService;
+       /**
+        * Get document type by the given id.
+        * 
+        * @param index
+        *            the id
+        * @return the found document type
+        */
+       @Transactional(readOnly = true)
+       public DocumentType selectType(long index) {
+               return getDocumentTypeDAO().get(index);
        }
 
+       /**
+        * 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
+        */
+       @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;
+       }
+
+       /**
+        * 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;
+       }
+
+       /**
+        * 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;
+       }
 }