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
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
61
62 @Controller( controllerJsp = "ManageEntrys.jsp", controllerPath = "jsp/admin/plugins/termofservice/", right = "TERMOFSERVICE_MANAGEMENT" )
63 public class EntryJspBean extends AbstractManageTOSJspBean <Integer, Entry>
64 {
65
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
71 private static final String PARAMETER_ID_ENTRY = "id";
72
73
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
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
85 private static final String MESSAGE_CONFIRM_REMOVE_ENTRY = "termofservice.message.confirmRemoveEntry";
86
87
88 private static final String VALIDATION_ATTRIBUTES_PREFIX = "termofservice.model.entity.entry.attribute.";
89
90
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
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
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
107 private static final String ERROR_RESOURCE_NOT_FOUND = "Resource not found";
108
109
110 private Entry _entry;
111 private List<Integer> _listIdEntrys;
112
113
114
115
116
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
135
136
137
138 @Override
139 List<Entry> getItemsFromIds( List<Integer> listIds )
140 {
141 List<Entry> listEntry = EntryHome.getEntrysListByIds( listIds );
142
143
144 return listEntry.stream()
145 .sorted(Comparator.comparingInt( notif -> listIds.indexOf( notif.getId())))
146 .collect(Collectors.toList());
147 }
148
149
150
151
152 public void resetListId( )
153 {
154 _listIdEntrys = new ArrayList<>( );
155 }
156
157
158
159
160
161
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
177
178
179
180
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
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
208
209
210
211
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
227
228
229
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
246
247
248
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
271
272
273
274
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
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 }