1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.extend.web.action;
35
36 import fr.paris.lutece.plugins.extend.business.extender.ResourceExtenderDTO;
37 import fr.paris.lutece.plugins.extend.business.extender.ResourceExtenderDTOFilter;
38 import fr.paris.lutece.plugins.extend.service.extender.IResourceExtenderService;
39 import fr.paris.lutece.plugins.extend.service.extender.ResourceExtenderService;
40 import fr.paris.lutece.plugins.extend.service.type.ExtendableResourceTypeService;
41 import fr.paris.lutece.plugins.extend.service.type.IExtendableResourceTypeService;
42 import fr.paris.lutece.portal.business.user.AdminUser;
43 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
44 import fr.paris.lutece.portal.service.admin.AdminUserService;
45 import fr.paris.lutece.portal.service.i18n.I18nService;
46 import fr.paris.lutece.portal.service.spring.SpringContextService;
47 import fr.paris.lutece.portal.service.util.AppPropertiesService;
48 import fr.paris.lutece.portal.web.constants.Parameters;
49 import fr.paris.lutece.portal.web.util.LocalizedPaginator;
50 import fr.paris.lutece.util.ReferenceList;
51 import fr.paris.lutece.util.html.Paginator;
52 import fr.paris.lutece.util.url.UrlItem;
53
54 import org.apache.commons.lang3.StringUtils;
55
56 import java.io.Serializable;
57
58 import java.util.List;
59 import java.util.Map;
60
61 import javax.servlet.http.HttpServletRequest;
62
63
64
65
66
67
68 public class ResourceExtenderSearchFields implements IResourceExtenderSearchFields, Serializable
69 {
70 private static final long serialVersionUID = 5171135962785175642L;
71
72
73 private static final String PROPERTY_DEFAULT_LIST_RESOURCES_EXTENDERS_PER_PAGE = "extend.listResourceExtenders.itemsPerPage";
74 private static final String PROPERTY_LABEL_ALL = "extend.labelAll";
75
76
77 private static final String PARAMETER_SESSION = "session";
78 private static final String PARAMETER_RESET = "reset";
79
80
81 private static final String MARK_LIST_RESOURCE_EXTENDERS = "listResourceExtenders";
82 private static final String MARK_LIST_EXTENDERS = "listExtenders";
83 private static final String MARK_FILTER = "filter";
84 private static final String MARK_PAGINATOR = "paginator";
85 private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
86 private static final String MARK_RESOURCE_TYPES_FOR_FILTER = "resourceTypesForFilter";
87 private static final String MARK_EXTENDER_TYPES_FOR_FILTER = "extenderTypesForFilter";
88 private static final String MARK_MAP_ACTION_PERMISSIONS = "mapActionPermissions";
89
90
91 private int _nItemsPerPage;
92 private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_RESOURCES_EXTENDERS_PER_PAGE, 50 );
93 private String _strCurrentPageIndex;
94 private String _strSortedAttributeName;
95 private boolean _bIsAscSort;
96 private ResourceExtenderDTOFilter _filter;
97
98
99
100
101 @Override
102 public String getCurrentPageIndex( )
103 {
104 return _strCurrentPageIndex;
105 }
106
107
108
109
110 @Override
111 public int getDefaultItemsPerPage( )
112 {
113 return _nDefaultItemsPerPage;
114 }
115
116
117
118
119 @Override
120 public void setCurrentPageIndex( String strCurrentPageIndex )
121 {
122 _strCurrentPageIndex = strCurrentPageIndex;
123 }
124
125
126
127
128 @Override
129 public void setDefaultItemsPerPage( int nDefaultItemsPerPage )
130 {
131 _nDefaultItemsPerPage = nDefaultItemsPerPage;
132 }
133
134
135
136
137 @Override
138 public int getItemsPerPage( )
139 {
140 return _nItemsPerPage;
141 }
142
143
144
145
146 @Override
147 public void setItemsPerPage( int nItemsPerPage )
148 {
149 _nItemsPerPage = nItemsPerPage;
150 }
151
152
153
154
155 @Override
156 public String getSortedAttributeName( )
157 {
158 return _strSortedAttributeName;
159 }
160
161
162
163
164 @Override
165 public void setSortedAttributeName( HttpServletRequest request )
166 {
167 if ( StringUtils.isNotBlank( request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME ) ) )
168 {
169 _strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
170 }
171 else
172 {
173 _strSortedAttributeName = StringUtils.EMPTY;
174 }
175 }
176
177
178
179
180 @Override
181 public boolean isAscSort( )
182 {
183 return _bIsAscSort;
184 }
185
186
187
188
189 @Override
190 public void setAscSort( HttpServletRequest request )
191 {
192 if ( StringUtils.isNotBlank( request.getParameter( Parameters.SORTED_ASC ) ) )
193 {
194 _bIsAscSort = Boolean.parseBoolean( request.getParameter( Parameters.SORTED_ASC ) );
195 }
196 }
197
198
199
200
201 @Override
202 public void fillModel( String strBaseUrl, HttpServletRequest request, Map<String, Object> model, AdminUser user ) throws AccessDeniedException
203 {
204 fillModel( strBaseUrl, request, model, null, user );
205 }
206
207
208
209
210 @Override
211 public void fillModel( String strBaseUrl, HttpServletRequest request, Map<String, Object> model, String strIdExtendableResource, AdminUser user )
212 throws AccessDeniedException
213 {
214 initFilter( request );
215
216
217 setSortedAttributeName( request );
218 setAscSort( request );
219 _filter.setSortedAttributeName( _strSortedAttributeName );
220 _filter.setAscSort( _bIsAscSort );
221
222 if ( StringUtils.isNotBlank( strIdExtendableResource ) )
223 {
224 _filter.setFilterIdExtendableResource( strIdExtendableResource );
225 }
226
227 UrlItem url = new UrlItem( strBaseUrl );
228
229 if ( getSortedAttributeName( ) != null )
230 {
231 url.addParameter( Parameters.SORTED_ATTRIBUTE_NAME, getSortedAttributeName( ) );
232 url.addParameter( Parameters.SORTED_ASC, Boolean.toString( isAscSort( ) ) );
233 }
234
235 url.addParameter( PARAMETER_SESSION, PARAMETER_SESSION );
236
237
238 _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
239 _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_RESOURCES_EXTENDERS_PER_PAGE, 50 );
240 _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
241
242 IResourceExtenderService resourceExtenderService = SpringContextService.getBean( ResourceExtenderService.BEAN_SERVICE );
243 IExtendableResourceTypeService resourceTypeService = SpringContextService.getBean( ExtendableResourceTypeService.BEAN_SERVICE );
244 List<Integer> listIdsExtenders = resourceExtenderService.findIdsByFilter( _filter );
245
246 LocalizedPaginator<Integer> paginator = new LocalizedPaginator<Integer>( listIdsExtenders, getItemsPerPage( ), url.getUrl( ),
247 Paginator.PARAMETER_PAGE_INDEX, getCurrentPageIndex( ), request.getLocale( ) );
248
249 List<ResourceExtenderDTO> listResourceExtenders = resourceExtenderService.findByListIds( paginator.getPageItems( ) );
250
251 Map<String, Map<String, Boolean>> mapActionPermissions = resourceExtenderService.getActionPermissions( paginator.getPageItems( ), user );
252
253
254 ReferenceList listResourceTypes = resourceTypeService.findAllAsRef( AdminUserService.getLocale( request ) );
255 listResourceTypes.addItem( StringUtils.EMPTY, I18nService.getLocalizedString( PROPERTY_LABEL_ALL, request.getLocale( ) ) );
256
257
258 ReferenceList listExtenderTypes = resourceExtenderService.getExtenderTypes( request.getLocale( ) );
259 listExtenderTypes.addItem( StringUtils.EMPTY, I18nService.getLocalizedString( PROPERTY_LABEL_ALL, request.getLocale( ) ) );
260
261 model.put( MARK_LIST_RESOURCE_EXTENDERS, listResourceExtenders );
262 model.put( MARK_LIST_EXTENDERS, resourceExtenderService.getResourceExtenders( ) );
263 model.put( MARK_FILTER, _filter );
264 model.put( MARK_PAGINATOR, paginator );
265 model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( paginator.getItemsPerPage( ) ) );
266 model.put( MARK_RESOURCE_TYPES_FOR_FILTER, listResourceTypes );
267 model.put( MARK_EXTENDER_TYPES_FOR_FILTER, listExtenderTypes );
268 model.put( MARK_MAP_ACTION_PERMISSIONS, mapActionPermissions );
269 }
270
271
272
273
274 @Override
275 public void initFilter( HttpServletRequest request )
276 {
277 if ( StringUtils.isNotBlank( request.getParameter( PARAMETER_RESET ) ) )
278 {
279 _filter = new ResourceExtenderDTOFilter( );
280 }
281 else
282 if ( StringUtils.isBlank( request.getParameter( PARAMETER_SESSION ) ) || ( _filter == null ) )
283 {
284 _filter = new ResourceExtenderDTOFilter( );
285 _filter.init( request );
286 }
287 }
288 }