Skip to content

LUT-29994 : Paginator injection #535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions src/java/fr/paris/lutece/portal/web/style/StylesJspBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
import java.util.List;
import java.util.Map;

import jakarta.enterprise.context.SessionScoped;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.servlet.http.HttpServletRequest;

Expand All @@ -56,19 +57,18 @@
import fr.paris.lutece.portal.service.message.AdminMessageService;
import fr.paris.lutece.portal.service.security.SecurityTokenService;
import fr.paris.lutece.portal.service.template.AppTemplateService;
import fr.paris.lutece.portal.service.util.AppPropertiesService;
import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
import fr.paris.lutece.portal.web.constants.Messages;
import fr.paris.lutece.portal.web.constants.Parameters;
import fr.paris.lutece.portal.web.util.LocalizedPaginator;
import fr.paris.lutece.util.html.AbstractPaginator;
import fr.paris.lutece.portal.web.util.IPager;
import fr.paris.lutece.portal.web.util.Pager;
import fr.paris.lutece.util.html.HtmlTemplate;
import fr.paris.lutece.util.sort.AttributeComparator;

/**
* This class provides the user interface to manage Styles features
*/
@SessionScoped
@RequestScoped
@Named
public class StylesJspBean extends AdminFeaturesPageJspBean
{
Expand All @@ -91,8 +91,6 @@ public class StylesJspBean extends AdminFeaturesPageJspBean
private static final String MARK_PORTLET_TYPE_LIST = "portlet_type_list";
private static final String MARK_PORTAL_COMPONENT_LIST = "portal_component_list";
private static final String MARK_STYLE = "style";
private static final String MARK_PAGINATOR = "paginator";
private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";

// Properties
private static final String PROPERTY_STYLES_PER_PAGE = "paginator.style.itemsPerPage";
Expand All @@ -117,8 +115,10 @@ public class StylesJspBean extends AdminFeaturesPageJspBean
private static final String MESSAGE_CREATE_STYLE_ID_ALREADY_EXISTS = "portal.style.message.createStyle.idAlreadyExists";
private static final String MESSAGE_CREATE_STYLE_COMPONENT_EXISTS = "portal.style.message.createStyle.componentHasAlreadyAStyle";
private static final String MESSAGE_CONFIRM_DELETE_STYLESHEET = "portal.style.message.stylesheetConfirmDelete";
private int _nItemsPerPage;
private String _strCurrentPageIndex;

@Inject
@Pager( listBookmark = MARK_STYLE_LIST, defaultItemsPerPage = PROPERTY_STYLES_PER_PAGE)
private IPager<Style, Void> pager;

/**
* Displays the styles list
Expand All @@ -142,10 +142,7 @@ public String getStylesManagement( HttpServletRequest request )

Collections.sort( listStyles, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
}

int defaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_STYLES_PER_PAGE, 10 );
_strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
_nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, defaultItemsPerPage );
pager.setList( listStyles );

String strURL = getHomeUrl( request );

Expand All @@ -158,15 +155,10 @@ public String getStylesManagement( HttpServletRequest request )
{
strURL += ( "&" + Parameters.SORTED_ASC + "=" + strAscSort );
}
pager.setBaseUrl( strURL );

LocalizedPaginator<Style> paginator = new LocalizedPaginator<>( listStyles, _nItemsPerPage, strURL, AbstractPaginator.PARAMETER_PAGE_INDEX,
_strCurrentPageIndex, getLocale( ) );

Map<String, Object> model = new HashMap<>( );
model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
model.put( MARK_PAGINATOR, paginator );
model.put( MARK_STYLE_LIST, paginator.getPageItems( ) );

Map<String, Object> model = pager.getPaginatedListModel( request, getLocale() );

HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_STYLES, getLocale( ), model );

return getAdminPage( template.getHtml( ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
import java.util.List;
import java.util.Map;

import jakarta.enterprise.context.SessionScoped;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.servlet.http.HttpServletRequest;
import javax.xml.parsers.SAXParser;
Expand Down Expand Up @@ -76,17 +77,17 @@
import fr.paris.lutece.portal.web.constants.Messages;
import fr.paris.lutece.portal.web.constants.Parameters;
import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
import fr.paris.lutece.portal.web.util.LocalizedPaginator;
import fr.paris.lutece.portal.web.util.IPager;
import fr.paris.lutece.portal.web.util.Pager;
import fr.paris.lutece.util.ReferenceList;
import fr.paris.lutece.util.file.FileUtil;
import fr.paris.lutece.util.html.AbstractPaginator;
import fr.paris.lutece.util.html.HtmlTemplate;
import fr.paris.lutece.util.sort.AttributeComparator;

/**
* This class provides the user interface to manage StyleSheet features
*/
@SessionScoped
@RequestScoped
@Named
public class StyleSheetJspBean extends AdminFeaturesPageJspBean
{
Expand All @@ -110,8 +111,6 @@ public class StyleSheetJspBean extends AdminFeaturesPageJspBean
private static final String MARK_STYLESHEET_LIST = "stylesheet_list";
private static final String MARK_STYLE_LIST = "style_list";
private static final String MARK_STYLESHEET = "stylesheet";
private static final String MARK_PAGINATOR = "paginator";
private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
private static final String MARK_PORTAL_COMPONENT_NAME = "portal_component_name";
private static final String MARK_PORTLET_TYPE_NAME = "portlet_type_name";
private static final String MARK_STYLE_DESCRIPTION = "style_description";
Expand All @@ -131,9 +130,11 @@ public class StyleSheetJspBean extends AdminFeaturesPageJspBean
private static final String LABEL_ALL = "portal.util.labelAll";
private static final String JSP_DO_REMOVE_STYLESHEET = "jsp/admin/style/DoRemoveStyleSheet.jsp";
private static final String JSP_REMOVE_STYLE = "RemoveStyle.jsp";
private int _nItemsPerPage;
private String _strCurrentPageIndex;

@Inject
@Pager( listBookmark = MARK_STYLESHEET_LIST, defaultItemsPerPage = PROPERTY_STYLESHEETS_PER_PAGE)
private IPager<StyleSheet, Void> pager;

/**
* Displays the stylesheets list
*
Expand Down Expand Up @@ -165,11 +166,9 @@ public String getManageStyleSheet( HttpServletRequest request )
boolean bIsAscSort = Boolean.parseBoolean( strAscSort );

Collections.sort( listStyleSheets, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );

}

int defaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_STYLESHEETS_PER_PAGE, 50 );
_strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
_nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, defaultItemsPerPage );
pager.setList( listStyleSheets);

String strURL = getHomeUrl( request );

Expand All @@ -182,16 +181,14 @@ public String getManageStyleSheet( HttpServletRequest request )
{
strURL += ( "&" + Parameters.SORTED_ASC + "=" + strAscSort );
}
pager.setBaseUrl( strURL );

LocalizedPaginator<StyleSheet> paginator = new LocalizedPaginator<>( listStyleSheets, _nItemsPerPage, strURL, AbstractPaginator.PARAMETER_PAGE_INDEX,
_strCurrentPageIndex, getLocale( ) );

Map<String, Object> pagerModel = pager.getPaginatedListModel( request, getLocale() );

Map<String, Object> model = new HashMap<>( );
model.put( MARK_MODE_ID, strModeId );
model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
model.put( MARK_PAGINATOR, paginator );
model.put( MARK_STYLESHEET_LIST, paginator.getPageItems( ) );
model.put( MARK_MODE_LIST, listModes );
model.putAll( pagerModel );

HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_STYLESHEETS, getLocale( ), model );

Expand Down
100 changes: 100 additions & 0 deletions src/java/fr/paris/lutece/portal/web/util/IPager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2002-2025, City of Paris
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice
* and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* License 1.0
*/
package fr.paris.lutece.portal.web.util;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

import jakarta.servlet.http.HttpServletRequest;

/**
*
* @param <S>
* @param <T>
*/
public interface IPager<S, T>
{
/**
* Gets the name
*
* @return The name of the pager
*/
String getName( );

/**
* Returns the paged list model based on the List provided through initList method.
*
* @param <T>
* @param request
* @param locale
* @return
*/
<T> Map<String, Object> getPaginatedListModel( HttpServletRequest request, Locale locale );

/**
* Returns the paged list model based on the List provided through initIdList method and the delegate function to load the objects with their ids.
*
* @param <T>
* @param request
* @param delegate
* @param locale
* @return
*/
<T> Map<String, Object> getPaginatedListModel( HttpServletRequest request, Function<List<S>, List<T>> delegate, Locale locale );

/**
* Set the list of IDs <S>. This list will be used to load the objects <T> through getPaginatedListModel method with a delegate on the concrete method that
* will load the complete objects.
*
* @param listIds
*/
void setIdList( List<S> listIds );

/**
* Set the list of items. In that case, no delegate will be involved during the retrieval of a page. This method should be used when the pager is of type
* IPager<S, Void>.
*
* @param listItems
*/
void setList( List<S> listItems );

/**
* Allows to update the base url of the pager in case this url should be dynamic.
*
* @param strBaseUrl
*/
void setBaseUrl( String strBaseUrl );
}
61 changes: 61 additions & 0 deletions src/java/fr/paris/lutece/portal/web/util/Pager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2002-2025, City of Paris
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice
* and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* License 1.0
*/
package fr.paris.lutece.portal.web.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.enterprise.util.Nonbinding;
import jakarta.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
public @interface Pager
{

@Nonbinding
String name() default "";

@Nonbinding
String defaultItemsPerPage() default "50";

@Nonbinding
String listBookmark() default "item_list";

@Nonbinding
String baseUrl() default "";
}
Loading