View Javadoc
1   /*
2    * Copyright (c) 2002-2024, 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  package fr.paris.lutece.plugins.identitystore.modules.test.web;
35  
36  import fr.paris.lutece.portal.service.message.AdminMessage;
37  import fr.paris.lutece.portal.service.message.AdminMessageService;
38  import fr.paris.lutece.portal.service.security.SecurityTokenService;
39  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
40  import fr.paris.lutece.portal.service.util.AppException;
41  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
42  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
43  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
44  import fr.paris.lutece.util.url.UrlItem;
45  import fr.paris.lutece.util.html.AbstractPaginator;
46  
47  import java.util.Comparator;
48  import java.util.ArrayList;
49  import java.util.List;
50  import java.util.Map;
51  import java.util.Optional;
52  import java.util.stream.Collectors;
53  import javax.servlet.http.HttpServletRequest;
54  import fr.paris.lutece.plugins.identitystore.modules.test.business.TestDefinition;
55  import fr.paris.lutece.plugins.identitystore.modules.test.business.TestDefinitionHome;
56  
57  /**
58   * This class provides the user interface to manage TestDefinition features ( manage, create, modify, remove )
59   */
60  @Controller( controllerJsp = "ManageTestDefinitions.jsp", controllerPath = "jsp/admin/plugins/identitystore/modules/test/", right = "IDENTITYSTORE_TEST_MANAGEMENT" )
61  public class TestDefinitionJspBean extends AbstractManageTesterJspBean<Integer, TestDefinition>
62  {
63      // Templates
64      private static final String TEMPLATE_MANAGE_TESTDEFINITIONS = "/admin/plugins/identitystore/modules/test/manage_testdefinitions.html";
65      private static final String TEMPLATE_CREATE_TESTDEFINITION = "/admin/plugins/identitystore/modules/test/create_testdefinition.html";
66      private static final String TEMPLATE_MODIFY_TESTDEFINITION = "/admin/plugins/identitystore/modules/test/modify_testdefinition.html";
67  
68      // Parameters
69      private static final String PARAMETER_ID_TESTDEFINITION = "id";
70  
71      // Properties for page titles
72      private static final String PROPERTY_PAGE_TITLE_MANAGE_TESTDEFINITIONS = "module.identitystore.test.manage_testdefinitions.pageTitle";
73      private static final String PROPERTY_PAGE_TITLE_MODIFY_TESTDEFINITION = "module.identitystore.test.modify_testdefinition.pageTitle";
74      private static final String PROPERTY_PAGE_TITLE_CREATE_TESTDEFINITION = "module.identitystore.test.create_testdefinition.pageTitle";
75  
76      // Markers
77      private static final String MARK_TESTDEFINITION_LIST = "testdefinition_list";
78      private static final String MARK_TESTDEFINITION = "testdefinition";
79  
80      private static final String JSP_MANAGE_TESTDEFINITIONS = "jsp/admin/plugins/identitystore/modules/test/ManageTestDefinitions.jsp";
81  
82      // Properties
83      private static final String MESSAGE_CONFIRM_REMOVE_TESTDEFINITION = "module.identitystore.test.message.confirmRemoveTestDefinition";
84  
85      // Validations
86      private static final String VALIDATION_ATTRIBUTES_PREFIX = "module.identitystore.test.model.entity.testdefinition.attribute.";
87  
88      // Views
89      private static final String VIEW_MANAGE_TESTDEFINITIONS = "manageTestDefinitions";
90      private static final String VIEW_CREATE_TESTDEFINITION = "createTestDefinition";
91      private static final String VIEW_MODIFY_TESTDEFINITION = "modifyTestDefinition";
92  
93      // Actions
94      private static final String ACTION_CREATE_TESTDEFINITION = "createTestDefinition";
95      private static final String ACTION_MODIFY_TESTDEFINITION = "modifyTestDefinition";
96      private static final String ACTION_REMOVE_TESTDEFINITION = "removeTestDefinition";
97      private static final String ACTION_CONFIRM_REMOVE_TESTDEFINITION = "confirmRemoveTestDefinition";
98  
99      // Infos
100     private static final String INFO_TESTDEFINITION_CREATED = "module.identitystore.test.info.testdefinition.created";
101     private static final String INFO_TESTDEFINITION_UPDATED = "module.identitystore.test.info.testdefinition.updated";
102     private static final String INFO_TESTDEFINITION_REMOVED = "module.identitystore.test.info.testdefinition.removed";
103 
104     // Errors
105     private static final String ERROR_RESOURCE_NOT_FOUND = "Resource not found";
106 
107     // Session variable to store working values
108     private TestDefinition _testdefinition;
109     private List<Integer> _listIdTestDefinitions;
110 
111     /**
112      * Build the Manage View
113      * 
114      * @param request
115      *            The HTTP request
116      * @return The page
117      */
118     @View( value = VIEW_MANAGE_TESTDEFINITIONS, defaultView = true )
119     public String getManageTestDefinitions( HttpServletRequest request )
120     {
121         _testdefinition = null;
122 
123         if ( request.getParameter( AbstractPaginator.PARAMETER_PAGE_INDEX ) == null || _listIdTestDefinitions.isEmpty( ) )
124         {
125             _listIdTestDefinitions = TestDefinitionHome.getIdTestDefinitionsList( );
126         }
127 
128         Map<String, Object> model = getPaginatedListModel( request, MARK_TESTDEFINITION_LIST, _listIdTestDefinitions, JSP_MANAGE_TESTDEFINITIONS );
129 
130         return getPage( PROPERTY_PAGE_TITLE_MANAGE_TESTDEFINITIONS, TEMPLATE_MANAGE_TESTDEFINITIONS, model );
131     }
132 
133     /**
134      * Get Items from Ids list
135      * 
136      * @param listIds
137      * @return the populated list of items corresponding to the id List
138      */
139     @Override
140     List<TestDefinition> getItemsFromIds( List<Integer> listIds )
141     {
142         List<TestDefinition> listTestDefinition = TestDefinitionHome.getTestDefinitionsListByIds( listIds );
143 
144         // keep original order
145         return listTestDefinition.stream( ).sorted( Comparator.comparingInt( notif -> listIds.indexOf( notif.getId( ) ) ) ).collect( Collectors.toList( ) );
146     }
147 
148     /**
149      * reset the _listIdTestDefinitions list
150      */
151     public void resetListId( )
152     {
153         _listIdTestDefinitions = new ArrayList<>( );
154     }
155 
156     /**
157      * Returns the form to create a testdefinition
158      *
159      * @param request
160      *            The Http request
161      * @return the html code of the testdefinition form
162      */
163     @View( VIEW_CREATE_TESTDEFINITION )
164     public String getCreateTestDefinition( HttpServletRequest request )
165     {
166         _testdefinition = ( _testdefinition != null ) ? _testdefinition : new TestDefinition( );
167 
168         Map<String, Object> model = getModel( );
169         model.put( MARK_TESTDEFINITION, _testdefinition );
170         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_CREATE_TESTDEFINITION ) );
171 
172         return getPage( PROPERTY_PAGE_TITLE_CREATE_TESTDEFINITION, TEMPLATE_CREATE_TESTDEFINITION, model );
173     }
174 
175     /**
176      * Process the data capture form of a new testdefinition
177      *
178      * @param request
179      *            The Http Request
180      * @return The Jsp URL of the process result
181      * @throws AccessDeniedException
182      */
183     @Action( ACTION_CREATE_TESTDEFINITION )
184     public String doCreateTestDefinition( HttpServletRequest request ) throws AccessDeniedException
185     {
186         populate( _testdefinition, request, getLocale( ) );
187 
188         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_CREATE_TESTDEFINITION ) )
189         {
190             throw new AccessDeniedException( "Invalid security token" );
191         }
192 
193         // Check constraints
194         if ( !validateBean( _testdefinition, VALIDATION_ATTRIBUTES_PREFIX ) )
195         {
196             return redirectView( request, VIEW_CREATE_TESTDEFINITION );
197         }
198 
199         TestDefinitionHome.create( _testdefinition );
200         addInfo( INFO_TESTDEFINITION_CREATED, getLocale( ) );
201         resetListId( );
202 
203         return redirectView( request, VIEW_MANAGE_TESTDEFINITIONS );
204     }
205 
206     /**
207      * Manages the removal form of a testdefinition whose identifier is in the http request
208      *
209      * @param request
210      *            The Http request
211      * @return the html code to confirm
212      */
213     @Action( ACTION_CONFIRM_REMOVE_TESTDEFINITION )
214     public String getConfirmRemoveTestDefinition( HttpServletRequest request )
215     {
216         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_TESTDEFINITION ) );
217         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_TESTDEFINITION ) );
218         url.addParameter( PARAMETER_ID_TESTDEFINITION, nId );
219 
220         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_TESTDEFINITION, url.getUrl( ),
221                 AdminMessage.TYPE_CONFIRMATION );
222 
223         return redirect( request, strMessageUrl );
224     }
225 
226     /**
227      * Handles the removal form of a testdefinition
228      *
229      * @param request
230      *            The Http request
231      * @return the jsp URL to display the form to manage testdefinitions
232      */
233     @Action( ACTION_REMOVE_TESTDEFINITION )
234     public String doRemoveTestDefinition( HttpServletRequest request )
235     {
236         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_TESTDEFINITION ) );
237 
238         TestDefinitionHome.remove( nId );
239         addInfo( INFO_TESTDEFINITION_REMOVED, getLocale( ) );
240         resetListId( );
241 
242         return redirectView( request, VIEW_MANAGE_TESTDEFINITIONS );
243     }
244 
245     /**
246      * Returns the form to update info about a testdefinition
247      *
248      * @param request
249      *            The Http request
250      * @return The HTML form to update info
251      */
252     @View( VIEW_MODIFY_TESTDEFINITION )
253     public String getModifyTestDefinition( HttpServletRequest request )
254     {
255         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_TESTDEFINITION ) );
256 
257         if ( _testdefinition == null || ( _testdefinition.getId( ) != nId ) )
258         {
259             Optional<TestDefinition> optTestDefinition = TestDefinitionHome.findByPrimaryKey( nId );
260             _testdefinition = optTestDefinition.orElseThrow( ( ) -> new AppException( ERROR_RESOURCE_NOT_FOUND ) );
261         }
262 
263         Map<String, Object> model = getModel( );
264         model.put( MARK_TESTDEFINITION, _testdefinition );
265         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_MODIFY_TESTDEFINITION ) );
266 
267         return getPage( PROPERTY_PAGE_TITLE_MODIFY_TESTDEFINITION, TEMPLATE_MODIFY_TESTDEFINITION, model );
268     }
269 
270     /**
271      * Process the change form of a testdefinition
272      *
273      * @param request
274      *            The Http request
275      * @return The Jsp URL of the process result
276      * @throws AccessDeniedException
277      */
278     @Action( ACTION_MODIFY_TESTDEFINITION )
279     public String doModifyTestDefinition( HttpServletRequest request ) throws AccessDeniedException
280     {
281         populate( _testdefinition, request, getLocale( ) );
282 
283         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_MODIFY_TESTDEFINITION ) )
284         {
285             throw new AccessDeniedException( "Invalid security token" );
286         }
287 
288         // Check constraints
289         if ( !validateBean( _testdefinition, VALIDATION_ATTRIBUTES_PREFIX ) )
290         {
291             return redirect( request, VIEW_MODIFY_TESTDEFINITION, PARAMETER_ID_TESTDEFINITION, _testdefinition.getId( ) );
292         }
293 
294         TestDefinitionHome.update( _testdefinition );
295         addInfo( INFO_TESTDEFINITION_UPDATED, getLocale( ) );
296         resetListId( );
297 
298         return redirectView( request, VIEW_MANAGE_TESTDEFINITIONS );
299     }
300 }