View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.termofservice.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.util.mvc.admin.annotations.Controller;
44  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
46  import fr.paris.lutece.util.url.UrlItem;
47  import fr.paris.lutece.util.html.AbstractPaginator;
48  
49  import java.util.Comparator;
50  import java.util.ArrayList;
51  import java.util.List;
52  import java.util.Map;
53  import java.util.Optional;
54  import java.util.stream.Collectors;
55  import javax.servlet.http.HttpServletRequest;
56  import fr.paris.lutece.plugins.termofservice.business.Entry;
57  import fr.paris.lutece.plugins.termofservice.business.EntryHome;
58  
59  /**
60   * This class provides the user interface to manage Entry features ( manage, create, modify, remove )
61   */
62  @Controller( controllerJsp = "ManageEntrys.jsp", controllerPath = "jsp/admin/plugins/termofservice/", right = "TERMOFSERVICE_MANAGEMENT" )
63  public class EntryJspBean extends AbstractManageTOSJspBean <Integer, Entry>
64  {
65      // Templates
66      private static final String TEMPLATE_MANAGE_ENTRYS = "/admin/plugins/termofservice/manage_entrys.html";
67      private static final String TEMPLATE_CREATE_ENTRY = "/admin/plugins/termofservice/create_entry.html";
68      private static final String TEMPLATE_MODIFY_ENTRY = "/admin/plugins/termofservice/modify_entry.html";
69  
70      // Parameters
71      private static final String PARAMETER_ID_ENTRY = "id";
72  
73      // Properties for page titles
74      private static final String PROPERTY_PAGE_TITLE_MANAGE_ENTRYS = "termofservice.manage_entrys.pageTitle";
75      private static final String PROPERTY_PAGE_TITLE_MODIFY_ENTRY = "termofservice.modify_entry.pageTitle";
76      private static final String PROPERTY_PAGE_TITLE_CREATE_ENTRY = "termofservice.create_entry.pageTitle";
77  
78      // Markers
79      private static final String MARK_ENTRY_LIST = "entry_list";
80      private static final String MARK_ENTRY = "entry";
81  
82      private static final String JSP_MANAGE_ENTRYS = "jsp/admin/plugins/termofservice/ManageEntrys.jsp";
83  
84      // Properties
85      private static final String MESSAGE_CONFIRM_REMOVE_ENTRY = "termofservice.message.confirmRemoveEntry";
86  
87      // Validations
88      private static final String VALIDATION_ATTRIBUTES_PREFIX = "termofservice.model.entity.entry.attribute.";
89  
90      // Views
91      private static final String VIEW_MANAGE_ENTRYS = "manageEntrys";
92      private static final String VIEW_CREATE_ENTRY = "createEntry";
93      private static final String VIEW_MODIFY_ENTRY = "modifyEntry";
94  
95      // Actions
96      private static final String ACTION_CREATE_ENTRY = "createEntry";
97      private static final String ACTION_MODIFY_ENTRY = "modifyEntry";
98      private static final String ACTION_REMOVE_ENTRY = "removeEntry";
99      private static final String ACTION_CONFIRM_REMOVE_ENTRY = "confirmRemoveEntry";
100 
101     // Infos
102     private static final String INFO_ENTRY_CREATED = "termofservice.info.entry.created";
103     private static final String INFO_ENTRY_UPDATED = "termofservice.info.entry.updated";
104     private static final String INFO_ENTRY_REMOVED = "termofservice.info.entry.removed";
105     
106     // Errors
107     private static final String ERROR_RESOURCE_NOT_FOUND = "Resource not found";
108     
109     // Session variable to store working values
110     private Entry _entry;
111     private List<Integer> _listIdEntrys;
112     
113     /**
114      * Build the Manage View
115      * @param request The HTTP request
116      * @return The page
117      */
118     @View( value = VIEW_MANAGE_ENTRYS, defaultView = true )
119     public String getManageEntrys( HttpServletRequest request )
120     {
121         _entry = null;
122         
123         if ( request.getParameter( AbstractPaginator.PARAMETER_PAGE_INDEX) == null || _listIdEntrys.isEmpty( ) )
124         {
125         	_listIdEntrys = EntryHome.getIdEntrysList(  );
126         }
127         
128         Map<String, Object> model = getPaginatedListModel( request, MARK_ENTRY_LIST, _listIdEntrys, JSP_MANAGE_ENTRYS );
129 
130         return getPage( PROPERTY_PAGE_TITLE_MANAGE_ENTRYS, TEMPLATE_MANAGE_ENTRYS, model );
131     }
132 
133 	/**
134      * Get Items from Ids list
135      * @param listIds
136      * @return the populated list of items corresponding to the id List
137      */
138 	@Override
139 	List<Entry> getItemsFromIds( List<Integer> listIds ) 
140 	{
141 		List<Entry> listEntry = EntryHome.getEntrysListByIds( listIds );
142 		
143 		// keep original order
144         return listEntry.stream()
145                  .sorted(Comparator.comparingInt( notif -> listIds.indexOf( notif.getId())))
146                  .collect(Collectors.toList());
147 	}
148     
149     /**
150     * reset the _listIdEntrys list
151     */
152     public void resetListId( )
153     {
154     	_listIdEntrys = new ArrayList<>( );
155     }
156 
157     /**
158      * Returns the form to create a entry
159      *
160      * @param request The Http request
161      * @return the html code of the entry form
162      */
163     @View( VIEW_CREATE_ENTRY )
164     public String getCreateEntry( HttpServletRequest request )
165     {
166         _entry = ( _entry != null ) ? _entry : new Entry(  );
167 
168         Map<String, Object> model = getModel(  );
169         model.put( MARK_ENTRY, _entry );
170         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_CREATE_ENTRY ) );
171 
172         return getPage( PROPERTY_PAGE_TITLE_CREATE_ENTRY, TEMPLATE_CREATE_ENTRY, model );
173     }
174 
175     /**
176      * Process the data capture form of a new entry
177      *
178      * @param request The Http Request
179      * @return The Jsp URL of the process result
180      * @throws AccessDeniedException
181      */
182     @Action( ACTION_CREATE_ENTRY )
183     public String doCreateEntry( HttpServletRequest request ) throws AccessDeniedException
184     {
185         populate( _entry, request, getLocale( ) );
186         
187 
188         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_CREATE_ENTRY ) )
189         {
190             throw new AccessDeniedException ( "Invalid security token" );
191         }
192 
193         // Check constraints
194         if ( !validateBean( _entry, VALIDATION_ATTRIBUTES_PREFIX ) )
195         {
196             return redirectView( request, VIEW_CREATE_ENTRY );
197         }
198 
199         EntryHome.create( _entry );
200         addInfo( INFO_ENTRY_CREATED, getLocale(  ) );
201         resetListId( );
202 
203         return redirectView( request, VIEW_MANAGE_ENTRYS );
204     }
205 
206     /**
207      * Manages the removal form of a entry whose identifier is in the http
208      * request
209      *
210      * @param request The Http request
211      * @return the html code to confirm
212      */
213     @Action( ACTION_CONFIRM_REMOVE_ENTRY )
214     public String getConfirmRemoveEntry( HttpServletRequest request )
215     {
216         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENTRY ) );
217         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_ENTRY ) );
218         url.addParameter( PARAMETER_ID_ENTRY, nId );
219 
220         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_ENTRY, url.getUrl(  ), AdminMessage.TYPE_CONFIRMATION );
221 
222         return redirect( request, strMessageUrl );
223     }
224 
225     /**
226      * Handles the removal form of a entry
227      *
228      * @param request The Http request
229      * @return the jsp URL to display the form to manage entrys
230      */
231     @Action( ACTION_REMOVE_ENTRY )
232     public String doRemoveEntry( HttpServletRequest request )
233     {
234         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENTRY ) );
235         
236         
237         EntryHome.remove( nId );
238         addInfo( INFO_ENTRY_REMOVED, getLocale(  ) );
239         resetListId( );
240 
241         return redirectView( request, VIEW_MANAGE_ENTRYS );
242     }
243 
244     /**
245      * Returns the form to update info about a entry
246      *
247      * @param request The Http request
248      * @return The HTML form to update info
249      */
250     @View( VIEW_MODIFY_ENTRY )
251     public String getModifyEntry( HttpServletRequest request )
252     {
253         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENTRY ) );
254 
255         if ( _entry == null || ( _entry.getId(  ) != nId ) )
256         {
257             Optional<Entry> optEntry = EntryHome.findByPrimaryKey( nId );
258             _entry = optEntry.orElseThrow( ( ) -> new AppException(ERROR_RESOURCE_NOT_FOUND ) );
259         }
260 
261 
262         Map<String, Object> model = getModel(  );
263         model.put( MARK_ENTRY, _entry );
264         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_MODIFY_ENTRY ) );
265 
266         return getPage( PROPERTY_PAGE_TITLE_MODIFY_ENTRY, TEMPLATE_MODIFY_ENTRY, model );
267     }
268 
269     /**
270      * Process the change form of a entry
271      *
272      * @param request The Http request
273      * @return The Jsp URL of the process result
274      * @throws AccessDeniedException
275      */
276     @Action( ACTION_MODIFY_ENTRY )
277     public String doModifyEntry( HttpServletRequest request ) throws AccessDeniedException
278     {   
279         populate( _entry, request, getLocale( ) );
280 		
281 		
282         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_MODIFY_ENTRY ) )
283         {
284             throw new AccessDeniedException ( "Invalid security token" );
285         }
286 
287         // Check constraints
288         if ( !validateBean( _entry, VALIDATION_ATTRIBUTES_PREFIX ) )
289         {
290             return redirect( request, VIEW_MODIFY_ENTRY, PARAMETER_ID_ENTRY, _entry.getId( ) );
291         }
292 
293         EntryHome.update( _entry );
294         addInfo( INFO_ENTRY_UPDATED, getLocale(  ) );
295         resetListId( );
296 
297         return redirectView( request, VIEW_MANAGE_ENTRYS );
298     }
299 }