View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de 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.plugins.directory.modules.multiview.web.record.filter.display.impl;
35  
36  import java.util.ArrayList;
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang.StringUtils;
44  import org.apache.commons.lang3.math.NumberUtils;
45  
46  import fr.paris.lutece.plugins.directory.business.Directory;
47  import fr.paris.lutece.plugins.directory.business.DirectoryHome;
48  import fr.paris.lutece.plugins.directory.modules.multiview.service.DirectoryMultiviewPlugin;
49  import fr.paris.lutece.plugins.directory.modules.multiview.util.RecordDirectoryNameConstants;
50  import fr.paris.lutece.plugins.directory.modules.multiview.util.RecordWorkflowStateNameConstants;
51  import fr.paris.lutece.plugins.directory.modules.multiview.util.ReferenceListFactory;
52  import fr.paris.lutece.plugins.workflowcore.business.state.State;
53  import fr.paris.lutece.portal.service.admin.AdminUserService;
54  import fr.paris.lutece.portal.service.workflow.WorkflowService;
55  import fr.paris.lutece.util.ReferenceList;
56  
57  /**
58   * Implementation of the IRecordFilterDisplay interface for the filter on workflow state
59   */
60  public class RecordFilterDisplayWorkflowState extends AbstractRecordFilterDisplay
61  {
62      // Constants
63      private static final String PARAMETER_ID_WORKFLOW_STATE = "multiview_id_state_workflow";
64      private static final String WORKFLOW_STATE_CODE_ATTRIBUTE = "id";
65      private static final String WORKFLOW_STATE_NAME_ATTRIBUTE = "name";
66      private static final int DEFAULT_DIRECTORY_VALUE = NumberUtils.INTEGER_MINUS_ONE;
67      private static final int DEFAULT_PREVIOUS_DIRECTORY_VALUE = NumberUtils.INTEGER_MINUS_ONE;
68      private static final int ID_WORKFLOW_UNSET = NumberUtils.INTEGER_ZERO;
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public String getParameterName( )
75      {
76          return PARAMETER_ID_WORKFLOW_STATE;
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public Map<String, Object> getFilterDisplayMapValues( HttpServletRequest request )
84      {
85          Map<String, Object> mapFilterNameValues = new LinkedHashMap<>( );
86  
87          int nIdDirectory = NumberUtils.toInt( request.getParameter( RecordDirectoryNameConstants.PARAMETER_ID_DIRECTORY ), DEFAULT_DIRECTORY_VALUE );
88          int nIdPreviousDirectory = NumberUtils.toInt( request.getParameter( RecordDirectoryNameConstants.PARAMETER_PREVIOUS_ID_DIRECTORY ),
89                  DEFAULT_PREVIOUS_DIRECTORY_VALUE );
90          String strIdWorkflowState = request.getParameter( PARAMETER_ID_WORKFLOW_STATE );
91  
92          if ( nIdDirectory != nIdPreviousDirectory && request.getParameter( RecordDirectoryNameConstants.PARAMETER_PREVIOUS_ID_DIRECTORY ) != null )
93          {
94              strIdWorkflowState = StringUtils.EMPTY;
95          }
96  
97          if ( StringUtils.isNotBlank( strIdWorkflowState ) )
98          {
99              mapFilterNameValues.put( RecordWorkflowStateNameConstants.FILTER_ID_WORKFLOW_STATE, strIdWorkflowState );
100         }
101 
102         setValue( strIdWorkflowState );
103 
104         return mapFilterNameValues;
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public void buildTemplate( HttpServletRequest request )
112     {
113         // If no directory has been selected we will return an empty list
114         ReferenceList referenceList = new ReferenceList( );
115 
116         int nIdDirectory = NumberUtils.toInt( request.getParameter( RecordDirectoryNameConstants.PARAMETER_ID_DIRECTORY ), DEFAULT_DIRECTORY_VALUE );
117         if ( nIdDirectory != DEFAULT_DIRECTORY_VALUE )
118         {
119             referenceList = createReferenceList( request, nIdDirectory );
120         }
121 
122         manageFilterTemplate( request, referenceList, PARAMETER_ID_WORKFLOW_STATE );
123     }
124 
125     /**
126      * Build the ReferenceList for the workflow state associated to the form
127      * 
128      * @param request
129      *            The request used to retrieve the values from
130      * @param nIdDirectory
131      *            The id of the Directory to retrieve the list of workflow state from
132      * @return the ReferenceList for the workflow state associated to the form
133      */
134     private ReferenceList createReferenceList( HttpServletRequest request, int nIdDirectory )
135     {
136         List<State> listWorkflowState = getDirectoryWorkflowStateList( request, nIdDirectory );
137 
138         ReferenceList referenceList = new ReferenceList( );
139 
140         if ( listWorkflowState != null && !listWorkflowState.isEmpty( ) )
141         {
142             ReferenceListFactory referenceListFactory = new ReferenceListFactory( listWorkflowState, WORKFLOW_STATE_CODE_ATTRIBUTE,
143                     WORKFLOW_STATE_NAME_ATTRIBUTE );
144             referenceList = referenceListFactory.createReferenceList( );
145         }
146 
147         return referenceList;
148     }
149 
150     /**
151      * Return the list of workflow state for a directory
152      * 
153      * @param request
154      *            The request
155      * @param nIdDirectory
156      *            The identifier of the directory to retrieve the state from
157      * @return the list of workflow state for a directory
158      */
159     private List<State> getDirectoryWorkflowStateList( HttpServletRequest request, int nIdDirectory )
160     {
161         List<State> listWorkflowState = new ArrayList<>( );
162         Directory directory = DirectoryHome.findByPrimaryKey( nIdDirectory, DirectoryMultiviewPlugin.getPlugin( ) );
163 
164         if ( directory != null && directory.getIdWorkflow( ) > ID_WORKFLOW_UNSET )
165         {
166             listWorkflowState.addAll( WorkflowService.getInstance( )
167                     .getAllStateByWorkflow( directory.getIdWorkflow( ), AdminUserService.getAdminUser( request ) ) );
168         }
169 
170         return listWorkflowState;
171     }
172 }