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.Comparator;
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  
45  import fr.paris.lutece.plugins.directory.business.DirectoryFilter;
46  import fr.paris.lutece.plugins.directory.business.DirectoryHome;
47  import fr.paris.lutece.plugins.directory.business.Directory;
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.ReferenceListFactory;
51  import fr.paris.lutece.plugins.directory.service.DirectoryResourceIdService;
52  import fr.paris.lutece.portal.service.admin.AdminUserService;
53  import fr.paris.lutece.portal.service.rbac.RBACService;
54  import fr.paris.lutece.portal.service.template.AppTemplateService;
55  import fr.paris.lutece.util.ReferenceList;
56  import fr.paris.lutece.util.html.HtmlTemplate;
57  import java.util.ArrayList;
58  
59  /**
60   * Implementation of the IRecordFilterDisplay interface for the filter on directory
61   */
62  public class RecordFilterDisplayDirectory extends AbstractRecordFilterDisplay
63  {
64      // Templates
65      private static final String DIRECTORY_FILTER_TEMPLATE_NAME = "admin/plugins/directory/modules/multiview/filter/record_directory_filter.html";
66  
67      // Constants
68      private static final String DIRECTORY_CODE_ATTRIBUTE = "idDirectory";
69      private static final String DIRECTORY_NAME_ATTRIBUTE = "title";
70      private static final String DEFAULT_ID_DIRECTORY = "-1";
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public String getParameterName( )
77      {
78          return RecordDirectoryNameConstants.PARAMETER_ID_DIRECTORY;
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public Map<String, Object> getFilterDisplayMapValues( HttpServletRequest request )
86      {
87          String strIdDirectoryValue = DEFAULT_ID_DIRECTORY;
88          Map<String, Object> mapFilterNameValues = new LinkedHashMap<>( );
89  
90          String strIdDirectory = request.getParameter( RecordDirectoryNameConstants.PARAMETER_ID_DIRECTORY );
91          if ( StringUtils.isNotBlank( strIdDirectory ) )
92          {
93              mapFilterNameValues.put( RecordDirectoryNameConstants.FILTER_ID_DIRECTORY, strIdDirectory );
94              strIdDirectoryValue = strIdDirectory;
95          }
96  
97          setValue( strIdDirectoryValue );
98  
99          return mapFilterNameValues;
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public void buildTemplate( HttpServletRequest request )
107     {
108         String strTemplateResult = StringUtils.EMPTY;
109 
110         Map<String, Object> model = new LinkedHashMap<>( );
111         model.put( MARK_FILTER_LIST, createReferenceList( request ) );
112         model.put( MARK_FILTER_LIST_VALUE, getValue( ) );
113         model.put( MARK_FILTER_NAME, RecordDirectoryNameConstants.PARAMETER_ID_DIRECTORY );
114         model.put( RecordDirectoryNameConstants.PARAMETER_PREVIOUS_ID_DIRECTORY, request.getParameter( RecordDirectoryNameConstants.PARAMETER_ID_DIRECTORY ) );
115 
116         HtmlTemplate htmlTemplate = AppTemplateService.getTemplate( DIRECTORY_FILTER_TEMPLATE_NAME, request.getLocale( ), model );
117         if ( htmlTemplate != null )
118         {
119             strTemplateResult = htmlTemplate.getHtml( );
120         }
121 
122         setTemplate( strTemplateResult );
123     }
124 
125     /**
126      * Create the ReferenceList of the available directories on which we can filter
127      * 
128      * @return the ReferenceList of Directory on which we can filter
129      */
130     private ReferenceList createReferenceList( HttpServletRequest request )
131     {
132         ReferenceListFactory referenceListFactory = new ReferenceListFactory( getDirectoryList( request ), DIRECTORY_CODE_ATTRIBUTE, DIRECTORY_NAME_ATTRIBUTE );
133 
134         return referenceListFactory.createReferenceList( );
135     }
136 
137     /**
138      * Return the list of all Directory associated to a workflow
139      * 
140      * @return the list of all Directory associated to a workflow
141      */
142     private List<Directory> getDirectoryList( HttpServletRequest request )
143     {
144         DirectoryFilter filter = new DirectoryFilter( );
145 
146         List<Directory> listDirectory = DirectoryHome.getDirectoryList( filter, DirectoryMultiviewPlugin.getPlugin( ) );
147 
148         List<Directory> listAuthorizedDirectory = new ArrayList<>( RBACService.getAuthorizedCollection( listDirectory,
149                 DirectoryResourceIdService.PERMISSION_VISUALISATION_RECORD, AdminUserService.getAdminUser( request ) ) );
150 
151         if ( listAuthorizedDirectory != null && !listAuthorizedDirectory.isEmpty( ) )
152         {
153 
154             listAuthorizedDirectory.sort( Comparator.comparing( fr.paris.lutece.plugins.directory.business.Directory::getTitle,
155                     Comparator.nullsLast( Comparator.naturalOrder( ) ) ) );
156         }
157 
158         return listAuthorizedDirectory;
159     }
160 }