WorkflowService.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.portal.service.workflow;

  35. import java.util.Collection;
  36. import java.util.List;
  37. import java.util.Locale;
  38. import java.util.Map;

  39. import javax.servlet.http.HttpServletRequest;

  40. import org.apache.commons.lang3.StringUtils;
  41. import org.springframework.beans.factory.BeanDefinitionStoreException;
  42. import org.springframework.beans.factory.CannotLoadBeanClassException;
  43. import org.springframework.beans.factory.NoSuchBeanDefinitionException;

  44. import fr.paris.lutece.api.user.User;
  45. import fr.paris.lutece.plugins.workflowcore.business.action.Action;
  46. import fr.paris.lutece.plugins.workflowcore.business.state.State;
  47. import fr.paris.lutece.plugins.workflowcore.service.workflow.IWorkflowService;
  48. import fr.paris.lutece.portal.business.event.ResourceEvent;
  49. import fr.paris.lutece.portal.business.user.AdminUser;
  50. import fr.paris.lutece.portal.service.event.ResourceEventManager;
  51. import fr.paris.lutece.portal.service.plugin.PluginService;
  52. import fr.paris.lutece.portal.service.spring.SpringContextService;
  53. import fr.paris.lutece.portal.service.util.AppException;
  54. import fr.paris.lutece.portal.service.util.AppLogService;
  55. import fr.paris.lutece.util.ReferenceList;
  56. import fr.paris.lutece.util.sql.TransactionManager;

  57. /**
  58.  *
  59.  * WorkflowService
  60.  *
  61.  */
  62. public final class WorkflowService
  63. {
  64.     private static final String PLUGIN_WORKFLOW_NAME = "workflow";
  65.     private static final String BEAN_WORKFLOW_PROVIDER = "workflow.workflowProvider";
  66.     private static WorkflowService _singleton;
  67.     private boolean _bServiceAvailable = true;
  68.     private IWorkflowService _service;
  69.     private IWorkflowProvider _provider;

  70.     /**
  71.      * Private constructor
  72.      */
  73.     private WorkflowService( )
  74.     {
  75.         try
  76.         {
  77.             _service = SpringContextService.getBean( fr.paris.lutece.plugins.workflowcore.service.workflow.WorkflowService.BEAN_SERVICE );
  78.             _provider = SpringContextService.getBean( BEAN_WORKFLOW_PROVIDER );
  79.             _bServiceAvailable = ( _service != null ) && ( _provider != null );
  80.         }
  81.         catch( CannotLoadBeanClassException | NoSuchBeanDefinitionException | BeanDefinitionStoreException e )
  82.         {
  83.             _bServiceAvailable = false;
  84.         }
  85.     }

  86.     /**
  87.      * Returns the unique instance of the service
  88.      *
  89.      * @return The instance of the service
  90.      */
  91.     public static synchronized WorkflowService getInstance( )
  92.     {
  93.         if ( _singleton == null )
  94.         {
  95.             _singleton = new WorkflowService( );
  96.         }
  97.         return _singleton;
  98.     }

  99.     /**
  100.      * Check if the workflow service is available. To be available, the following conditions must be verified :
  101.      * <ul>
  102.      * <li>the Bean service is not null</li>
  103.      * <li>the plugin-workflow must be enable</li>
  104.      * </ul>
  105.      *
  106.      * @return true if the workflow service is available
  107.      */
  108.     public boolean isAvailable( )
  109.     {
  110.         // LUTECE-1273 : Condition ( _service != null && _provider != null ) in case the
  111.         // plugin-workflow is removed from a webapp
  112.         return _bServiceAvailable && ( _service != null ) && ( _provider != null ) && PluginService.isPluginEnable( PLUGIN_WORKFLOW_NAME );
  113.     }

  114.     /**
  115.      * returns a list of actions possible for a given document based on the status of the document in the workflow and the user role
  116.      *
  117.      * @param nIdResource
  118.      *            the document id
  119.      * @param strResourceType
  120.      *            the document type
  121.      * @param user
  122.      *            the adminUser
  123.      * @param nIdWorkflow
  124.      *            the workflow id
  125.      * @return a list of Action
  126.      */
  127.     public Collection<Action> getActions( int nIdResource, String strResourceType, int nIdWorkflow, User user )
  128.     {
  129.         if ( isAvailable( ) )
  130.         {
  131.             Collection<Action> listActions = _service.getActions( nIdResource, strResourceType, nIdWorkflow );

  132.             return _provider.getActions( nIdResource, strResourceType, listActions, user );
  133.         }

  134.         return null;
  135.     }

  136.     /**
  137.      * returns a list of actions possible for a given document based on the status of the document in the workflow and the user role
  138.      *
  139.      * @param nIdResource
  140.      *            the document id
  141.      * @param strResourceType
  142.      *            the document type
  143.      * @param user
  144.      *            the adminUser
  145.      * @param nIdWorkflow
  146.      *            the workflow id
  147.      * @return a list of Action
  148.      * @deprecated use getActions( int, String, int, User )
  149.      */
  150.     @Deprecated
  151.     public Collection<Action> getActions( int nIdResource, String strResourceType, int nIdWorkflow, AdminUser user )
  152.     {
  153.         return getActions( nIdResource, strResourceType, nIdWorkflow, (User) user );
  154.     }

  155.     /**
  156.      * returns a list of actions possible for a given document based on the status of the document in the workflow and the user role
  157.      *
  158.      * @param listIdResource
  159.      *            the list of resource id
  160.      * @param strResourceType
  161.      *            the document type
  162.      * @param nIdExternalParentId
  163.      *            the external parent identifier
  164.      * @param nIdWorkflow
  165.      *            the workflow id
  166.      * @param user
  167.      *            the User
  168.      * @return a list of Action
  169.      */
  170.     public Map<Integer, List<Action>> getActions( List<Integer> listIdResource, String strResourceType, Integer nIdExternalParentId, int nIdWorkflow,
  171.             User user )
  172.     {
  173.         if ( isAvailable( ) )
  174.         {
  175.             Map<Integer, List<Action>> mapActions = _service.getActions( listIdResource, strResourceType, nIdExternalParentId, nIdWorkflow );

  176.             return _provider.getActions( strResourceType, mapActions, user );
  177.         }

  178.         return null;
  179.     }

  180.     /**
  181.      * returns a list of actions possible for a given document based on the status of the document in the workflow and the user role
  182.      *
  183.      * @param listIdResource
  184.      *            the list of resource id
  185.      * @param strResourceType
  186.      *            the document type
  187.      * @param nIdExternalParentId
  188.      *            the external parent identifier
  189.      * @param nIdWorkflow
  190.      *            the workflow id
  191.      * @param user
  192.      *            the User
  193.      * @return a list of Action
  194.      * @deprecated getActions( List, String, Integer, int, User )
  195.      */
  196.     @Deprecated
  197.     public Map<Integer, List<Action>> getActions( List<Integer> listIdResource, String strResourceType, Integer nIdExternalParentId, int nIdWorkflow,
  198.             AdminUser user )
  199.     {
  200.         return getActions( listIdResource, strResourceType, nIdExternalParentId, nIdWorkflow, (User) user );
  201.     }

  202.     /**
  203.      * return true if a form is associate to the action
  204.      *
  205.      * @param nIdAction
  206.      *            the action id
  207.      * @param locale
  208.      *            the loacle
  209.      * @return true if a form is associate to the action
  210.      */
  211.     public boolean isDisplayTasksForm( int nIdAction, Locale locale )
  212.     {
  213.         return isAvailable( ) && _service.isDisplayTasksForm( nIdAction, locale );
  214.     }

  215.     /**
  216.      * Proceed action given in parameter
  217.      *
  218.      * @param nIdResource
  219.      *            the resource id
  220.      * @param strResourceType
  221.      *            the resource type
  222.      * @param nIdAction
  223.      *            the action id
  224.      * @param nExternalParentId
  225.      *            the external parent id
  226.      * @param request
  227.      *            the request
  228.      * @param locale
  229.      *            locale
  230.      * @param bIsAutomatic
  231.      *            Is automatic
  232.      * @deprecated use doProcessAction( int, String, int, Integer, HttpServletRequest, Locale, boolean, User )
  233.      */
  234.     @Deprecated
  235.     public void doProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request, Locale locale,
  236.             boolean bIsAutomatic )
  237.     {
  238.         doProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, locale, bIsAutomatic, null );
  239.     }

  240.     /**
  241.      * Proceed action given in parameter
  242.      *
  243.      * @param nIdResource
  244.      *            the resource id
  245.      * @param strResourceType
  246.      *            the resource type
  247.      * @param nIdAction
  248.      *            the action id
  249.      * @param nExternalParentId
  250.      *            the external parent id
  251.      * @param request
  252.      *            the request
  253.      * @param locale
  254.      *            locale
  255.      * @param bIsAutomatic
  256.      *            Is automatic
  257.      * @param user
  258.      *            The User
  259.      */
  260.     public void doProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request, Locale locale,
  261.             boolean bIsAutomatic, User user )
  262.     {
  263.         if ( isAvailable( ) && canProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, bIsAutomatic, user ) )
  264.         {
  265.             TransactionManager.beginTransaction( null );

  266.             try
  267.             {
  268.                 String strUserAccessCode = bIsAutomatic ? null : _provider.getUserAccessCode( request, user );
  269.                 _service.doProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, locale, bIsAutomatic, strUserAccessCode, user );
  270.                 TransactionManager.commitTransaction( null );

  271.                 registerResourceEvent( nIdResource, strResourceType );
  272.             }
  273.             catch( Exception e )
  274.             {
  275.                 TransactionManager.rollBack( null );
  276.                 throw new AppException( e.getMessage( ), e );
  277.             }
  278.         }
  279.     }

  280.     /**
  281.      * returns the actions history performed on a resource
  282.      *
  283.      * @param nIdResource
  284.      *            the resource id
  285.      * @param strResourceType
  286.      *            the resource type
  287.      * @param request
  288.      *            the request
  289.      * @param nIdWorkflow
  290.      *            the workflow id
  291.      * @param locale
  292.      *            the locale
  293.      * @return the history of actions performed on a resource
  294.      * @deprecated use getDisplayDocumentHistory( int, String, int, HttpServletRequest, Locale, User )
  295.      */
  296.     @Deprecated
  297.     public String getDisplayDocumentHistory( int nIdResource, String strResourceType, int nIdWorkflow, HttpServletRequest request, Locale locale )
  298.     {
  299.         return getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, null );
  300.     }

  301.     /**
  302.      * returns the actions history performed on a resource
  303.      *
  304.      * @param nIdResource
  305.      *            the resource id
  306.      * @param strResourceType
  307.      *            the resource type
  308.      * @param request
  309.      *            the request
  310.      * @param nIdWorkflow
  311.      *            the workflow id
  312.      * @param locale
  313.      *            the locale
  314.      * @param user
  315.      *            The User
  316.      * @return the history of actions performed on a resource
  317.      */
  318.     public String getDisplayDocumentHistory( int nIdResource, String strResourceType, int nIdWorkflow, HttpServletRequest request, Locale locale, User user )
  319.     {
  320.         return isAvailable( ) ? _provider.getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, user ) : null;
  321.     }

  322.     /**
  323.      * returns the actions history performed on a resource
  324.      *
  325.      * @param nIdResource
  326.      *            the resource id
  327.      * @param strResourceType
  328.      *            the resource type
  329.      * @param request
  330.      *            the request
  331.      * @param nIdWorkflow
  332.      *            the workflow id
  333.      * @param locale
  334.      *            the locale
  335.      * @param model
  336.      *            The model to add to the default model
  337.      * @param strTemplate
  338.      *            The template
  339.      * @return the history of actions performed on a resource
  340.      * @deprecated use getDisplayDocumentHistory( int, String, int, HttpServletRequest, Locale, Map, String, User )
  341.      */
  342.     @Deprecated
  343.     public String getDisplayDocumentHistory( int nIdResource, String strResourceType, int nIdWorkflow, HttpServletRequest request, Locale locale,
  344.             Map<String, Object> model, String strTemplate )
  345.     {
  346.         return getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, model, strTemplate, null );
  347.     }

  348.     /**
  349.      * returns the actions history performed on a resource
  350.      *
  351.      * @param nIdResource
  352.      *            the resource id
  353.      * @param strResourceType
  354.      *            the resource type
  355.      * @param request
  356.      *            the request
  357.      * @param nIdWorkflow
  358.      *            the workflow id
  359.      * @param locale
  360.      *            the locale
  361.      * @param model
  362.      *            The model to add to the default model
  363.      * @param strTemplate
  364.      *            The template
  365.      * @param user
  366.      *            The User
  367.      * @return the history of actions performed on a resource
  368.      */
  369.     public String getDisplayDocumentHistory( int nIdResource, String strResourceType, int nIdWorkflow, HttpServletRequest request, Locale locale,
  370.             Map<String, Object> model, String strTemplate, User user )
  371.     {
  372.         if ( !isAvailable( ) )
  373.         {
  374.             return null;
  375.         }
  376.         try
  377.         {
  378.             return _provider.getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, model, strTemplate, user );
  379.         }
  380.         catch( NoSuchMethodError ex )
  381.         {
  382.             AppLogService.error( "You are using a too old Workflow provider version. Please upgrade." );
  383.             return _provider.getDisplayDocumentHistory( nIdResource, strResourceType, nIdWorkflow, request, locale, user );
  384.         }
  385.     }

  386.     /**
  387.      * Perform the information on the various tasks associated with the given action specified in parameter
  388.      *
  389.      * @param nIdResource
  390.      *            the resource id
  391.      * @param strResourceType
  392.      *            the resource type
  393.      * @param nExternalParentId
  394.      *            the external parent id
  395.      * @param request
  396.      *            the request
  397.      * @param nIdAction
  398.      *            the action id
  399.      * @param locale
  400.      *            the locale
  401.      * @return null if there is no error in the task form else return the error message url
  402.      * @deprecated use doSaveTasksForm( int, String, int, Integer, HttpServletRequest, Locale, User )
  403.      */
  404.     @Deprecated
  405.     public String doSaveTasksForm( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request,
  406.             Locale locale )
  407.     {
  408.         return doSaveTasksForm( nIdResource, strResourceType, nIdAction, nExternalParentId, request, locale, null );
  409.     }

  410.     /**
  411.      * Perform the information on the various tasks associated with the given action specified in parameter
  412.      *
  413.      * @param nIdResource
  414.      *            the resource id
  415.      * @param strResourceType
  416.      *            the resource type
  417.      * @param nExternalParentId
  418.      *            the external parent id
  419.      * @param request
  420.      *            the request
  421.      * @param nIdAction
  422.      *            the action id
  423.      * @param locale
  424.      *            the locale
  425.      * @param user
  426.      *            the user
  427.      * @return null if there is no error in the task form else return the error message url
  428.      */
  429.     public String doSaveTasksForm( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request, Locale locale,
  430.             User user )
  431.     {
  432.         if ( isAvailable( ) )
  433.         {
  434.             String strError = _provider.doValidateTasksForm( nIdResource, strResourceType, nIdAction, request, locale, user );

  435.             if ( StringUtils.isNotBlank( strError ) )
  436.             {
  437.                 return strError;
  438.             }

  439.             doProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, locale, false, user );
  440.         }

  441.         return null;
  442.     }

  443.     /**
  444.      * Get the list of ids of resources of a given type that are in a given state
  445.      *
  446.      * @param nIdState
  447.      *            The id of the state of resources to get
  448.      * @param strResourceType
  449.      *            The type of resources to get
  450.      * @return The list of resources matching both given state id and resource given. Return an empty list if no resource was found, or if the state does not
  451.      *         exist.
  452.      */
  453.     public List<Integer> getResourceIdListByIdState( int nIdState, String strResourceType )
  454.     {
  455.         if ( isAvailable( ) )
  456.         {
  457.             return _service.getResourceIdListByIdState( nIdState, strResourceType );
  458.         }

  459.         return null;
  460.     }

  461.     /**
  462.      * Get the list of ids of resources of a given type that are in a given state
  463.      * and have a specific external parent id
  464.      *
  465.      * @param nIdState
  466.      *            The id of the state of resources to get
  467.      * @param strResourceType
  468.      *            The type of resources to get
  469.      * @return The list of resources matching both given state id and resource given. Return an empty list if no resource was found, or if the state does not
  470.      *         exist.
  471.      */
  472.     public List<Integer> getResourceIdListByIdState( int nIdState, String strResourceType, int nExternalParentId )
  473.     {
  474.         if ( isAvailable( ) )
  475.         {
  476.             return _service.getResourceIdListByIdState( nIdState, strResourceType, nExternalParentId );
  477.         }

  478.         return null;
  479.     }
  480.    
  481.    
  482.     /**
  483.      * Remove in every workflows the resource specified in parameter
  484.      *
  485.      * @param nIdResource
  486.      *            the resource id
  487.      * @param strResourceType
  488.      *            the resource type
  489.      */
  490.     public void doRemoveWorkFlowResource( int nIdResource, String strResourceType )
  491.     {
  492.         if ( isAvailable( ) )
  493.         {
  494.             TransactionManager.beginTransaction( null );

  495.             try
  496.             {
  497.                 _service.doRemoveWorkFlowResource( nIdResource, strResourceType );
  498.                 TransactionManager.commitTransaction( null );
  499.             }
  500.             catch( Exception e )
  501.             {
  502.                 TransactionManager.rollBack( null );
  503.                 throw new AppException( e.getMessage( ), e );
  504.             }
  505.         }
  506.     }

  507.     /**
  508.      * Remove list of resource workflow by list id
  509.      *
  510.      * @param lListIdResource
  511.      *            list of id resource
  512.      * @param strResourceType
  513.      *            the ressource type
  514.      * @param nIdWorflow
  515.      *            the workflow id
  516.      */
  517.     public void doRemoveWorkFlowResourceByListId( List<Integer> lListIdResource, String strResourceType, Integer nIdWorflow )
  518.     {
  519.         if ( isAvailable( ) )
  520.         {
  521.             TransactionManager.beginTransaction( null );

  522.             try
  523.             {
  524.                 _service.doRemoveWorkFlowResourceByListId( lListIdResource, strResourceType, nIdWorflow );
  525.                 TransactionManager.commitTransaction( null );
  526.             }
  527.             catch( Exception e )
  528.             {
  529.                 TransactionManager.rollBack( null );
  530.                 throw new AppException( e.getMessage( ), e );
  531.             }
  532.         }
  533.     }

  534.     /**
  535.      * returns the tasks form
  536.      *
  537.      * @param nIdResource
  538.      *            the document id
  539.      * @param strResourceType
  540.      *            the document type
  541.      * @param request
  542.      *            the request
  543.      * @param nIdAction
  544.      *            the action id
  545.      * @param locale
  546.      *            the locale
  547.      * @return the tasks form associated to the action
  548.      *
  549.      * @deprecated use getDisplayTasksForm( int, String, int, HttpServletRequest, Locale, User )
  550.      */
  551.     @Deprecated
  552.     public String getDisplayTasksForm( int nIdResource, String strResourceType, int nIdAction, HttpServletRequest request, Locale locale )
  553.     {
  554.         return getDisplayTasksForm( nIdResource, strResourceType, nIdAction, request, locale, null );

  555.     }

  556.     /**
  557.      * returns the tasks form
  558.      *
  559.      * @param nIdResource
  560.      *            the document id
  561.      * @param strResourceType
  562.      *            the document type
  563.      * @param request
  564.      *            the request
  565.      * @param nIdAction
  566.      *            the action id
  567.      * @param locale
  568.      *            the locale
  569.      * @param user
  570.      *            the user
  571.      * @return the tasks form associated to the action
  572.      *
  573.      */
  574.     public String getDisplayTasksForm( int nIdResource, String strResourceType, int nIdAction, HttpServletRequest request, Locale locale, User user )
  575.     {
  576.         return isAvailable( ) ? _provider.getDisplayTasksForm( nIdResource, strResourceType, nIdAction, request, locale, user ) : null;
  577.     }

  578.     /**
  579.      * Check that a given user is allowed to view a resource depending the state of the resource
  580.      *
  581.      * @param nIdResource
  582.      *            the document id
  583.      * @param strResourceType
  584.      *            the document type
  585.      * @param user
  586.      *            the User
  587.      * @param nIdWorkflow
  588.      *            the workflow id
  589.      * @return a list of Action
  590.      */
  591.     public boolean isAuthorized( int nIdResource, String strResourceType, int nIdWorkflow, User user )
  592.     {
  593.         return isAvailable( ) && _provider.isAuthorized( nIdResource, strResourceType, nIdWorkflow, user );
  594.     }

  595.     /**
  596.      * Check that a given user is allowed to view a resource depending the state of the resource
  597.      *
  598.      * @param nIdResource
  599.      *            the document id
  600.      * @param strResourceType
  601.      *            the document type
  602.      * @param user
  603.      *            the User
  604.      * @param nIdWorkflow
  605.      *            the workflow id
  606.      * @return a list of Action
  607.      * @deprecated use isAuthorized( int, String, int, User )
  608.      */
  609.     @Deprecated
  610.     public boolean isAuthorized( int nIdResource, String strResourceType, int nIdWorkflow, AdminUser user )
  611.     {
  612.         return isAuthorized( nIdResource, strResourceType, nIdWorkflow, (User) user );
  613.     }

  614.     /**
  615.      * Get all authorized resource Id
  616.      *
  617.      * @param strResourceType
  618.      *            the resource type
  619.      * @param nIdWorkflow
  620.      *            the workflow id
  621.      * @param nIdWorkflowState
  622.      *            The workflow state id or -1 for all workflow states
  623.      * @param nExternalParentId
  624.      *            The external parent id
  625.      * @param user
  626.      *            the User
  627.      * @return a list resource id
  628.      */
  629.     public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, int nIdWorkflowState, Integer nExternalParentId, User user )
  630.     {
  631.         return isAvailable( ) ? _provider.getAuthorizedResourceList( strResourceType, nIdWorkflow, nIdWorkflowState, nExternalParentId, user ) : null;
  632.     }

  633.     /**
  634.      * Get all authorized resource Id
  635.      *
  636.      * @param strResourceType
  637.      *            the resource type
  638.      * @param nIdWorkflow
  639.      *            the workflow id
  640.      * @param nIdWorkflowState
  641.      *            The workflow state id or -1 for all workflow states
  642.      * @param nExternalParentId
  643.      *            The external parent id
  644.      * @param user
  645.      *            the User
  646.      * @return a list resource id
  647.      * @deprecated use getAuthorizedResourceList( String, int, int, Integer, User )
  648.      */
  649.     @Deprecated
  650.     public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, int nIdWorkflowState, Integer nExternalParentId, AdminUser user )
  651.     {
  652.         return getAuthorizedResourceList( strResourceType, nIdWorkflow, nIdWorkflowState, nExternalParentId, (User) user );

  653.     }

  654.     /**
  655.      * Get all authorized resource Id by list of state
  656.      *
  657.      * @param strResourceType
  658.      *            the resource type
  659.      * @param nIdWorkflow
  660.      *            the workflow id
  661.      * @param lListIdWorkflowState
  662.      *            The workflow state <b>id or null</b> for all workflow states
  663.      * @param nExternalParentId
  664.      *            the externbal parent identifier
  665.      * @param user
  666.      *            the User
  667.      * @return a list resource id
  668.      */
  669.     public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, List<Integer> lListIdWorkflowState, Integer nExternalParentId,
  670.             User user )
  671.     {
  672.         return isAvailable( ) ? _provider.getAuthorizedResourceList( strResourceType, nIdWorkflow, lListIdWorkflowState, nExternalParentId, user ) : null;
  673.     }

  674.     /**
  675.      * Get all authorized resource Id by list of state
  676.      *
  677.      * @param strResourceType
  678.      *            the resource type
  679.      * @param nIdWorkflow
  680.      *            the workflow id
  681.      * @param lListIdWorkflowState
  682.      *            The workflow state <b>id or null</b> for all workflow states
  683.      * @param nExternalParentId
  684.      *            the externbal parent identifier
  685.      * @param user
  686.      *            the User
  687.      * @return a list resource id
  688.      * @deprecated use getAuthorizedResourceList( String, int, List, Integer, User )
  689.      */
  690.     @Deprecated
  691.     public List<Integer> getAuthorizedResourceList( String strResourceType, int nIdWorkflow, List<Integer> lListIdWorkflowState, Integer nExternalParentId,
  692.             AdminUser user )
  693.     {
  694.         return getAuthorizedResourceList( strResourceType, nIdWorkflow, lListIdWorkflowState, nExternalParentId, (User) user );
  695.     }

  696.     /**
  697.      * return a reference list which contains a list enabled workflow
  698.      *
  699.      * @param user
  700.      *            the User
  701.      * @param locale
  702.      *            the locale
  703.      * @return a reference list which contains a list enabled workflow
  704.      */
  705.     public ReferenceList getWorkflowsEnabled( User user, Locale locale )
  706.     {
  707.         return isAvailable( ) ? _provider.getWorkflowsEnabled( user, locale ) : null;
  708.     }

  709.     /**
  710.      * return a reference list which contains a list enabled workflow
  711.      *
  712.      * @param user
  713.      *            the User
  714.      * @param locale
  715.      *            the locale
  716.      * @return a reference list which contains a list enabled workflow
  717.      * @deprecated use getWorkflowsEnabled( User, Locale )
  718.      */
  719.     @Deprecated
  720.     public ReferenceList getWorkflowsEnabled( AdminUser user, Locale locale )
  721.     {
  722.         return getWorkflowsEnabled( (User) user, locale );
  723.     }

  724.     /**
  725.      * returns all state of a given workflow
  726.      *
  727.      * @param user
  728.      *            the user
  729.      * @param nIdWorkflow
  730.      *            the workflow id
  731.      * @return the state of a given document
  732.      */
  733.     public Collection<State> getAllStateByWorkflow( int nIdWorkflow, User user )
  734.     {
  735.         if ( isAvailable( ) )
  736.         {
  737.             Collection<State> listStates = _service.getAllStateByWorkflow( nIdWorkflow );

  738.             return _provider.getAllStateByWorkflow( listStates, user );
  739.         }

  740.         return null;
  741.     }

  742.     /**
  743.      * returns all state of a given workflow
  744.      *
  745.      * @param user
  746.      *            the adminUser
  747.      * @param nIdWorkflow
  748.      *            the workflow id
  749.      * @return the state of a given document
  750.      * @deprecated use getAllStateByWorkflow( int, User )
  751.      */
  752.     @Deprecated
  753.     public Collection<State> getAllStateByWorkflow( int nIdWorkflow, AdminUser user )
  754.     {
  755.         return getAllStateByWorkflow( nIdWorkflow, (User) user );
  756.     }

  757.     /**
  758.      * returns the state of a given document of the document in the workflow and the user role
  759.      *
  760.      * @param nIdResource
  761.      *            the document id
  762.      * @param strResourceType
  763.      *            the document type
  764.      * @param nIdWorkflow
  765.      *            the workflow id
  766.      * @param nIdExternalParentId
  767.      *            the external parent id
  768.      * @return the state of a given document
  769.      */
  770.     public State getState( int nIdResource, String strResourceType, int nIdWorkflow, Integer nIdExternalParentId )
  771.     {
  772.         if ( isAvailable( ) )
  773.         {
  774.             State state = null;
  775.             TransactionManager.beginTransaction( null );

  776.             try
  777.             {
  778.                 state = _service.getState( nIdResource, strResourceType, nIdWorkflow, nIdExternalParentId );
  779.                 TransactionManager.commitTransaction( null );
  780.             }
  781.             catch( Exception e )
  782.             {
  783.                 TransactionManager.rollBack( null );
  784.                 throw new AppException( e.getMessage( ), e );
  785.             }

  786.             return state;
  787.         }

  788.         return null;
  789.     }

  790.     /**
  791.      * Execute action automatic
  792.      *
  793.      * @param nIdResource
  794.      *            the document id
  795.      * @param strResourceType
  796.      *            the document type
  797.      * @param nIdWorkflow
  798.      *            the workflow id
  799.      * @param nExternalParentId
  800.      *            the external parent id
  801.      * @deprecated use executeActionAutomatic( int, String, int, Integer, User )
  802.      */
  803.     @Deprecated
  804.     public void executeActionAutomatic( int nIdResource, String strResourceType, int nIdWorkflow, Integer nExternalParentId )
  805.     {
  806.         executeActionAutomatic( nIdResource, strResourceType, nIdWorkflow, nExternalParentId, null );
  807.     }

  808.     /**
  809.      * Execute action automatic
  810.      *
  811.      * @param nIdResource
  812.      *            the document id
  813.      * @param strResourceType
  814.      *            the document type
  815.      * @param nIdWorkflow
  816.      *            the workflow id
  817.      * @param nExternalParentId
  818.      *            the external parent id
  819.      * @param user
  820.      *            the user
  821.      */
  822.     public void executeActionAutomatic( int nIdResource, String strResourceType, int nIdWorkflow, Integer nExternalParentId, User user )
  823.     {
  824.         if ( isAvailable( ) )
  825.         {
  826.             TransactionManager.beginTransaction( null );

  827.             try
  828.             {
  829.                 _service.executeActionAutomatic( nIdResource, strResourceType, nIdWorkflow, nExternalParentId, user );
  830.                 TransactionManager.commitTransaction( null );

  831.                 registerResourceEvent( nIdResource, strResourceType );
  832.             }
  833.             catch( Exception e )
  834.             {
  835.                 TransactionManager.rollBack( null );
  836.                 throw new AppException( e.getMessage( ), e );
  837.             }
  838.         }
  839.     }

  840.     /**
  841.      * Get the list of mass actions from a given id workflow
  842.      *
  843.      * @param nIdWorkflow
  844.      *            the id workflow
  845.      * @return the list of mass actions
  846.      */
  847.     public List<Action> getMassActions( int nIdWorkflow )
  848.     {
  849.         return isAvailable( ) ? _service.getMassActions( nIdWorkflow ) : null;
  850.     }

  851.     /**
  852.      * Get the list of mass actions from a given id workflow
  853.      *
  854.      * @param nIdWorkflow
  855.      *            the id workflow
  856.      * @param nIdState
  857.      * @param user
  858.      * @return the list of mass actions
  859.      */
  860.     public Collection<Action> getMassActions( int nIdWorkflow, int nIdState, User user )
  861.     {
  862.         if ( !isAvailable( ) )
  863.         {
  864.             return null;
  865.         }

  866.         Collection<Action> listActions = _service.getMassActions( nIdWorkflow, nIdState );

  867.         return _provider.getAuthorizedActions( listActions, user );
  868.     }

  869.     /**
  870.      * Check if the action can be proceed for the given resource
  871.      *
  872.      * @param nIdResource
  873.      *            the id resource
  874.      * @param strResourceType
  875.      *            the resource type
  876.      * @param nIdAction
  877.      *            the id action
  878.      * @param nExternalParentId
  879.      *            the external parent id
  880.      * @param request
  881.      *            the HTTP request
  882.      * @param bIsAutomatic
  883.      *            is automatic action
  884.      * @return true if the action can proceed, false otherwise
  885.      * @deprecated use canProcessAction( int, String, int, Integer, HttpServletRequest, User )
  886.      */
  887.     @Deprecated
  888.     public boolean canProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request,
  889.             boolean bIsAutomatic )
  890.     {
  891.         return canProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId, request, bIsAutomatic, null );
  892.     }

  893.     /**
  894.      * Check if the action can be proceed for the given resource
  895.      *
  896.      * @param nIdResource
  897.      *            the id resource
  898.      * @param strResourceType
  899.      *            the resource type
  900.      * @param nIdAction
  901.      *            the id action
  902.      * @param nExternalParentId
  903.      *            the external parent id
  904.      * @param request
  905.      *            the HTTP request
  906.      * @param bIsAutomatic
  907.      *            is automatic action
  908.      * @param user
  909.      *            the RBACUser
  910.      * @return true if the action can proceed, false otherwise
  911.      */
  912.     public boolean canProcessAction( int nIdResource, String strResourceType, int nIdAction, Integer nExternalParentId, HttpServletRequest request,
  913.             boolean bIsAutomatic, User user )
  914.     {
  915.         if ( isAvailable( ) && _service.canProcessAction( nIdResource, strResourceType, nIdAction, nExternalParentId ) )
  916.         {
  917.             if ( bIsAutomatic )
  918.             {
  919.                 return true;
  920.             }

  921.             return _provider.canProcessAction( nIdResource, strResourceType, nIdAction, request, user );
  922.         }

  923.         return false;
  924.     }

  925.     /**
  926.      * Proceed automatic reflexive actions of state given in parameter. This method should be called anytime a service changed the state of a resource without
  927.      * proceeding a workflow action
  928.      *
  929.      * @param nIdResource
  930.      *            the resource id
  931.      * @param strResourceType
  932.      *            the resource type
  933.      * @param nIdState
  934.      *            the state of the resource id
  935.      * @param nIdExternalParent
  936.      *            the external parent id*
  937.      * @param locale
  938.      *            locale
  939.      * @deprecated use doProcessAutomaticReflexiveActions( int, String, int, Integer, Locale, User )
  940.      */
  941.     @Deprecated
  942.     public void doProcessAutomaticReflexiveActions( int nIdResource, String strResourceType, int nIdState, Integer nIdExternalParent, Locale locale )
  943.     {
  944.         doProcessAutomaticReflexiveActions( nIdResource, strResourceType, nIdState, nIdExternalParent, locale, null );
  945.     }

  946.     /**
  947.      * Proceed automatic reflexive actions of state given in parameter. This method should be called anytime a service changed the state of a resource without
  948.      * proceeding a workflow action
  949.      *
  950.      * @param nIdResource
  951.      *            the resource id
  952.      * @param strResourceType
  953.      *            the resource type
  954.      * @param nIdState
  955.      *            the state of the resource id
  956.      * @param nIdExternalParent
  957.      *            the external parent id*
  958.      * @param locale
  959.      *            locale
  960.      * @param user
  961.      *            the user
  962.      */
  963.     public void doProcessAutomaticReflexiveActions( int nIdResource, String strResourceType, int nIdState, Integer nIdExternalParent, Locale locale, User user )
  964.     {
  965.         if ( isAvailable( ) )
  966.         {
  967.             TransactionManager.beginTransaction( null );

  968.             try
  969.             {
  970.                 _service.doProcessAutomaticReflexiveActions( nIdResource, strResourceType, nIdState, nIdExternalParent, locale, user );
  971.                 TransactionManager.commitTransaction( null );

  972.                 registerResourceEvent( nIdResource, strResourceType );
  973.             }
  974.             catch( Exception e )
  975.             {
  976.                 TransactionManager.rollBack( null );
  977.                 throw new AppException( e.getMessage( ), e );
  978.             }
  979.         }
  980.     }

  981.     /**
  982.      * Create and process a ResourceEvent.
  983.      *
  984.      * @param nIdResource
  985.      * @param strResourceType
  986.      */
  987.     private void registerResourceEvent( int nIdResource, String strResourceType )
  988.     {
  989.         ResourceEvent formResponseEvent = new ResourceEvent( );
  990.         formResponseEvent.setIdResource( String.valueOf( nIdResource ) );
  991.         formResponseEvent.setTypeResource( strResourceType );

  992.         ResourceEventManager.fireUpdatedResource( formResponseEvent );
  993.     }
  994. }