View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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   	
35   
36  package fr.paris.lutece.plugins.formresponsxpage.web;
37  
38  import fr.paris.lutece.portal.service.message.AdminMessage;
39  import fr.paris.lutece.portal.service.message.AdminMessageService;
40  import fr.paris.lutece.portal.service.security.SecurityTokenService;
41  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
42  import fr.paris.lutece.portal.service.util.AppException;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
46  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
47  import fr.paris.lutece.util.url.UrlItem;
48  import fr.paris.lutece.util.html.AbstractPaginator;
49  
50  import java.util.Comparator;
51  import java.util.HashMap;
52  import java.util.ArrayList;
53  import java.util.List;
54  import java.util.Map;
55  import java.util.Optional;
56  import java.util.stream.Collectors;
57  import javax.servlet.http.HttpServletRequest;
58  
59  import org.apache.commons.lang3.StringUtils;
60  
61  
62  import fr.paris.lutece.plugins.formresponsxpage.business.Formsreponseedito;
63  import fr.paris.lutece.plugins.formresponsxpage.business.FormsreponseeditoHome;
64  
65  /**
66   * This class provides the user interface to manage Formsreponseedito features ( manage, create, modify, remove )
67   */
68  @Controller( controllerJsp = "ManageFormsreponseeditos.jsp", controllerPath = "jsp/admin/plugins/formresponsxpage/", right = "FORMRESPONSXPAGE_MANAGEMENT" )
69  public class FormsreponseeditoJspBean extends AbstractJspBean <Integer, Formsreponseedito>
70  {
71  
72  	// Rights
73  	public static final String RIGHT_MANAGEADMINRESPONSEEDITO = "FORMRESPONSXPAGE_MANAGEMENT";
74  		
75      // Templates
76      private static final String TEMPLATE_MANAGE_FORMSREPONSEEDITOS = "/admin/plugins/formresponsxpage/manage_formsreponseeditos.html";
77      private static final String TEMPLATE_CREATE_FORMSREPONSEEDITO = "/admin/plugins/formresponsxpage/create_formsreponseedito.html";
78      private static final String TEMPLATE_MODIFY_FORMSREPONSEEDITO = "/admin/plugins/formresponsxpage/modify_formsreponseedito.html";
79  
80      // Parameters
81      private static final String PARAMETER_ID_FORMSREPONSEEDITO = "id";
82  
83      // Properties for page titles
84      private static final String PROPERTY_PAGE_TITLE_MANAGE_FORMSREPONSEEDITOS = "formresponsxpage.manage_formsreponseeditos.pageTitle";
85      private static final String PROPERTY_PAGE_TITLE_MODIFY_FORMSREPONSEEDITO = "formresponsxpage.modify_formsreponseedito.pageTitle";
86      private static final String PROPERTY_PAGE_TITLE_CREATE_FORMSREPONSEEDITO = "formresponsxpage.create_formsreponseedito.pageTitle";
87  
88      // Markers
89      private static final String MARK_FORMSREPONSEEDITO_LIST = "formsreponseedito_list";
90      private static final String MARK_FORMSREPONSEEDITO = "formsreponseedito";
91  
92      private static final String JSP_MANAGE_FORMSREPONSEEDITOS = "jsp/admin/plugins/formresponsxpage/ManageFormsreponseeditos.jsp";
93  
94      // Properties
95      private static final String MESSAGE_CONFIRM_REMOVE_FORMSREPONSEEDITO = "formresponsxpage.message.confirmRemoveFormsreponseedito";
96  
97      // Validations
98      private static final String VALIDATION_ATTRIBUTES_PREFIX = "formresponsxpage.model.entity.formsreponseedito.attribute.";
99  
100     // Views
101     private static final String VIEW_MANAGE_FORMSREPONSEEDITOS = "manageFormsreponseeditos";
102     private static final String VIEW_CREATE_FORMSREPONSEEDITO = "createFormsreponseedito";
103     private static final String VIEW_MODIFY_FORMSREPONSEEDITO = "modifyFormsreponseedito";
104 
105     // Actions
106     private static final String ACTION_CREATE_FORMSREPONSEEDITO = "createFormsreponseedito";
107     private static final String ACTION_MODIFY_FORMSREPONSEEDITO = "modifyFormsreponseedito";
108     private static final String ACTION_REMOVE_FORMSREPONSEEDITO = "removeFormsreponseedito";
109     private static final String ACTION_CONFIRM_REMOVE_FORMSREPONSEEDITO = "confirmRemoveFormsreponseedito";
110 
111     // Infos
112     private static final String INFO_FORMSREPONSEEDITO_CREATED = "formresponsxpage.info.formsreponseedito.created";
113     private static final String INFO_FORMSREPONSEEDITO_UPDATED = "formresponsxpage.info.formsreponseedito.updated";
114     private static final String INFO_FORMSREPONSEEDITO_REMOVED = "formresponsxpage.info.formsreponseedito.removed";
115     
116     // Errors
117     private static final String ERROR_RESOURCE_NOT_FOUND = "Resource not found";
118     
119     // Session variable to store working values
120     private Formsreponseedito _formsreponseedito;
121     private List<Integer> _listIdFormsreponseeditos;
122     private HashMap<String,String> _mapFilterCriteria = new HashMap<>();
123     private String _optionOrderBy;
124     
125     /**
126      * Build the Manage View
127      * @param request The HTTP request
128      * @return The page
129      */
130     @View( value = VIEW_MANAGE_FORMSREPONSEEDITOS )
131     public String getManageFormsreponseeditos( HttpServletRequest request )
132     {
133         _formsreponseedito = null;
134         
135         // new search only if in pagination mode
136         if ( request.getParameter( AbstractPaginator.PARAMETER_PAGE_INDEX) == null )
137         {
138         	// if sorting request : new search with the existing filter criteria, ordered 
139         	// example of order by parameter : orderby=name
140         	if ( StringUtils.isNotBlank( (String)request.getParameter(PARAMETER_SEARCH_ORDER_BY) ) )
141         	{
142         		
143         		String strOrderByColumn =  (String)request.getParameter(PARAMETER_SEARCH_ORDER_BY);
144         		String strSortMode = getSortMode(); 
145         		
146         		_listIdFormsreponseeditos = FormsreponseeditoHome.getIdFormsreponseeditosList( _mapFilterCriteria, strOrderByColumn, strSortMode );
147                	
148 	       	}
149 	       	else
150 	       	{
151 	       		// reload the filter criteria and search
152 	       		_mapFilterCriteria = (HashMap<String, String>) getFilterCriteriaFromRequest( request );
153 	       		_listIdFormsreponseeditos = FormsreponseeditoHome.getIdFormsreponseeditosList( _mapFilterCriteria, null ,null);
154 	       	}
155         	
156         	//set CurrentPageIndex of Paginator to null in aim of displays the first page of results
157         	resetCurrentPageIndexOfPaginator();
158         }
159        	
160        	Map<String, Object> model = getPaginatedListModel( request, MARK_FORMSREPONSEEDITO_LIST, _listIdFormsreponseeditos, JSP_MANAGE_FORMSREPONSEEDITOS );
161              
162         addSearchParameters(model,_mapFilterCriteria); //allow the persistence of search values in inputs search bar inputs
163                      
164         return getPage( PROPERTY_PAGE_TITLE_MANAGE_FORMSREPONSEEDITOS, TEMPLATE_MANAGE_FORMSREPONSEEDITOS, model );
165 
166     }
167 
168 	/**
169      * Get Items from Ids list
170      * @param listIds
171      * @return the populated list of items corresponding to the id List
172      */
173 	@Override
174 	List<Formsreponseedito> getItemsFromIds( List<Integer> listIds ) 
175 	{
176 		List<Formsreponseedito> listFormsreponseedito = FormsreponseeditoHome.getFormsreponseeditosListByIds( listIds );
177 		
178 		// keep original order
179         return listFormsreponseedito.stream()
180                  .sorted(Comparator.comparingInt( notif -> listIds.indexOf( notif.getId())))
181                  .collect(Collectors.toList());
182 	}
183 	
184 	@Override
185 	int getPluginDefaultNumberOfItemPerPage( ) {
186 		return AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE, 50 );
187 	}
188     
189     /**
190     * reset the _listIdFormsreponseeditos list
191     */
192     public void resetListId( )
193     {
194     	_listIdFormsreponseeditos = new ArrayList<>( );
195     }
196     
197     
198     /**
199      * Returns the form to update info about a formsreponseedito
200      *
201      * @param request The Http request
202      * @return The HTML form to update info
203      */
204     @View( value = VIEW_MODIFY_FORMSREPONSEEDITO, defaultView = true )
205     public String getModifyFormsreponseedito( HttpServletRequest request )
206     {
207         //int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_FORMSREPONSEEDITO ) );
208     	int nId = 1;
209 
210         if ( _formsreponseedito == null || ( _formsreponseedito.getId(  ) != nId ) )
211         {
212             Optional<Formsreponseedito> optFormsreponseedito = FormsreponseeditoHome.findByPrimaryKey( nId );
213             _formsreponseedito = optFormsreponseedito.orElseThrow( ( ) -> new AppException(ERROR_RESOURCE_NOT_FOUND ) );
214         }
215 
216 
217         Map<String, Object> model = getModel(  );
218         model.put( MARK_FORMSREPONSEEDITO, _formsreponseedito );
219         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_MODIFY_FORMSREPONSEEDITO ) );
220 
221         return getPage( PROPERTY_PAGE_TITLE_MODIFY_FORMSREPONSEEDITO, TEMPLATE_MODIFY_FORMSREPONSEEDITO, model );
222     }
223 
224     /**
225      * Process the change form of a formsreponseedito
226      *
227      * @param request The Http request
228      * @return The Jsp URL of the process result
229      * @throws AccessDeniedException
230      */
231     @Action( ACTION_MODIFY_FORMSREPONSEEDITO )
232     public String doModifyFormsreponseedito( HttpServletRequest request ) throws AccessDeniedException
233     {   
234         populate( _formsreponseedito, request, getLocale( ) );
235 		
236 		
237         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_MODIFY_FORMSREPONSEEDITO ) )
238         {
239             throw new AccessDeniedException ( "Invalid security token" );
240         }
241 
242         // Check constraints
243         if ( !validateBean( _formsreponseedito, VALIDATION_ATTRIBUTES_PREFIX ) )
244         {
245             return redirect( request, VIEW_MODIFY_FORMSREPONSEEDITO, PARAMETER_ID_FORMSREPONSEEDITO, _formsreponseedito.getId( ) );
246         }
247 
248         FormsreponseeditoHome.update( _formsreponseedito );
249         addInfo( INFO_FORMSREPONSEEDITO_UPDATED, getLocale(  ) );
250         resetListId( );
251 
252         return redirectView( request, VIEW_MODIFY_FORMSREPONSEEDITO );
253     }
254 }