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.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
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
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
69 private static final String PARAMETER_ID_TESTDEFINITION = "id";
70
71
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
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
83 private static final String MESSAGE_CONFIRM_REMOVE_TESTDEFINITION = "module.identitystore.test.message.confirmRemoveTestDefinition";
84
85
86 private static final String VALIDATION_ATTRIBUTES_PREFIX = "module.identitystore.test.model.entity.testdefinition.attribute.";
87
88
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
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
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
105 private static final String ERROR_RESOURCE_NOT_FOUND = "Resource not found";
106
107
108 private TestDefinition _testdefinition;
109 private List<Integer> _listIdTestDefinitions;
110
111
112
113
114
115
116
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
135
136
137
138
139 @Override
140 List<TestDefinition> getItemsFromIds( List<Integer> listIds )
141 {
142 List<TestDefinition> listTestDefinition = TestDefinitionHome.getTestDefinitionsListByIds( listIds );
143
144
145 return listTestDefinition.stream( ).sorted( Comparator.comparingInt( notif -> listIds.indexOf( notif.getId( ) ) ) ).collect( Collectors.toList( ) );
146 }
147
148
149
150
151 public void resetListId( )
152 {
153 _listIdTestDefinitions = new ArrayList<>( );
154 }
155
156
157
158
159
160
161
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
177
178
179
180
181
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
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
208
209
210
211
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
228
229
230
231
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
247
248
249
250
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
272
273
274
275
276
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
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 }