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.sitelabels.web;
35
36 import fr.paris.lutece.plugins.sitelabels.business.Label;
37 import fr.paris.lutece.plugins.sitelabels.service.LabelService;
38 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
39 import fr.paris.lutece.portal.service.message.AdminMessage;
40 import fr.paris.lutece.portal.service.message.AdminMessageService;
41 import fr.paris.lutece.portal.service.security.SecurityTokenService;
42 import fr.paris.lutece.portal.service.util.AppPropertiesService;
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.portal.web.util.LocalizedPaginator;
47 import fr.paris.lutece.util.html.Paginator;
48 import fr.paris.lutece.util.url.UrlItem;
49
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53
54 import javax.servlet.http.HttpServletRequest;
55
56
57
58
59
60 @Controller( controllerJsp = "ManageLabels.jsp", controllerPath = "jsp/admin/plugins/sitelabels/", right = "SITELABELS_MANAGEMENT" )
61 public class LabelJspBean extends ManageSiteLabelsJspBean
62 {
63
64
65
66
67 private static final String TEMPLATE_MANAGE_LABELS = "/admin/plugins/sitelabels/manage_labels.html";
68 private static final String TEMPLATE_CREATE_LABEL = "/admin/plugins/sitelabels/create_label.html";
69 private static final String TEMPLATE_MODIFY_LABEL = "/admin/plugins/sitelabels/modify_label.html";
70
71
72 private static final String PARAMETER_KEY = "id";
73
74
75 private static final String PROPERTY_PAGE_TITLE_MANAGE_LABELS = "sitelabels.manage_labels.pageTitle";
76 private static final String PROPERTY_PAGE_TITLE_MODIFY_LABEL = "sitelabels.modify_label.pageTitle";
77 private static final String PROPERTY_PAGE_TITLE_CREATE_LABEL = "sitelabels.create_label.pageTitle";
78
79
80 private static final String MARK_LABEL_LIST = "label_list";
81 private static final String MARK_LABEL = "label";
82 private static final String JSP_MANAGE_LABELS = "jsp/admin/plugins/sitelabels/ManageLabels.jsp";
83
84
85 private static final String MESSAGE_CONFIRM_REMOVE_LABEL = "sitelabels.message.confirmRemoveLabel";
86 private static final String PROPERTY_DEFAULT_LIST_LABEL_PER_PAGE = "sitelabels.listLabels.itemsPerPage";
87 private static final String VALIDATION_ATTRIBUTES_PREFIX = "sitelabels.model.entity.label.attribute.";
88
89
90 private static final String VIEW_MANAGE_LABELS = "manageLabels";
91 private static final String VIEW_CREATE_LABEL = "createLabel";
92 private static final String VIEW_MODIFY_LABEL = "modifyLabel";
93
94
95 private static final String ACTION_CREATE_LABEL = "createLabel";
96 private static final String ACTION_MODIFY_LABEL = "modifyLabel";
97 private static final String ACTION_REMOVE_LABEL = "removeLabel";
98 private static final String ACTION_CONFIRM_REMOVE_LABEL = "confirmRemoveLabel";
99
100
101 private static final String INFO_LABEL_CREATED = "sitelabels.info.label.created";
102 private static final String INFO_LABEL_UPDATED = "sitelabels.info.label.updated";
103 private static final String INFO_LABEL_REMOVED = "sitelabels.info.label.removed";
104
105
106 private Label _label;
107
108 private int _nDefaultItemsPerPage;
109 private String _strCurrentPageIndex;
110 private int _nItemsPerPage;
111
112
113
114
115
116
117 @View( value = VIEW_MANAGE_LABELS, defaultView = true )
118 public String getManageLabels( HttpServletRequest request )
119 {
120 _label = null;
121 _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
122 _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_LABEL_PER_PAGE, 50 );
123 _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
124 _nDefaultItemsPerPage );
125
126 UrlItem url = new UrlItem( JSP_MANAGE_LABELS );
127 String strUrl = url.getUrl( );
128 List<Label> listLabels = (List<Label>) LabelService.getLabelsList( );
129
130
131 LocalizedPaginator paginator = new LocalizedPaginator( listLabels, _nItemsPerPage, strUrl,
132 PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
133
134 Map<String, Object> model = getModel( );
135
136 model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
137 model.put( MARK_PAGINATOR, paginator );
138 model.put( MARK_LABEL_LIST, paginator.getPageItems( ) );
139
140 return getPage( PROPERTY_PAGE_TITLE_MANAGE_LABELS, TEMPLATE_MANAGE_LABELS, model );
141 }
142
143
144
145
146
147
148
149 @View( VIEW_CREATE_LABEL )
150 public String getCreateLabel( HttpServletRequest request )
151 {
152 _label = ( _label != null ) ? _label : new Label( );
153
154 Map<String, Object> model = getModel( );
155 model.put( MARK_LABEL, _label );
156 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_CREATE_LABEL ) );
157
158 return getPage( PROPERTY_PAGE_TITLE_CREATE_LABEL, TEMPLATE_CREATE_LABEL, model );
159 }
160
161
162
163
164
165
166
167
168 @Action( ACTION_CREATE_LABEL )
169 public String doCreateLabel( HttpServletRequest request ) throws AccessDeniedException
170 {
171 if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_CREATE_LABEL ) ) {
172 throw new AccessDeniedException( "Invalid CSRF token" );
173 }
174
175 populate( _label, request );
176
177
178 if ( !validateBean( _label, VALIDATION_ATTRIBUTES_PREFIX ) )
179 {
180 return redirectView( request, VIEW_CREATE_LABEL );
181 }
182
183 _label.setKey( LabelService.PREFIX + _label.getKey( ) );
184 LabelService.create( _label );
185 addInfo( INFO_LABEL_CREATED, getLocale( ) );
186
187 return redirectView( request, VIEW_MANAGE_LABELS );
188 }
189
190
191
192
193
194
195
196
197 @Action( ACTION_CONFIRM_REMOVE_LABEL )
198 public String getConfirmRemoveLabel( HttpServletRequest request )
199 {
200 String strKey = request.getParameter( PARAMETER_KEY );
201 UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_LABEL ) );
202 url.addParameter( PARAMETER_KEY, strKey );
203 url.addParameter( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_REMOVE_LABEL ) );
204
205 String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_LABEL,
206 new Object[] { strKey }, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
207
208 return redirect( request, strMessageUrl );
209 }
210
211
212
213
214
215
216
217
218 @Action( ACTION_REMOVE_LABEL )
219 public String doRemoveLabel( HttpServletRequest request ) throws AccessDeniedException
220 {
221 if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_REMOVE_LABEL ) ) {
222 throw new AccessDeniedException( "Invalid CSRF token" );
223 }
224
225 String strKey = request.getParameter( PARAMETER_KEY );
226 LabelService.remove( strKey );
227 addInfo( INFO_LABEL_REMOVED, getLocale( ) );
228
229 return redirectView( request, VIEW_MANAGE_LABELS );
230 }
231
232
233
234
235
236
237
238 @View( VIEW_MODIFY_LABEL )
239 public String getModifyLabel( HttpServletRequest request )
240 {
241 String strKey = request.getParameter( PARAMETER_KEY );
242
243 if ( _label == null || ( !_label.getKey().equals( strKey )))
244 {
245 _label = LabelService.findByPrimaryKey( strKey );
246 }
247
248 Map<String, Object> model = getModel( );
249 model.put( MARK_LABEL, _label );
250 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_MODIFY_LABEL ) );
251
252 return getPage( PROPERTY_PAGE_TITLE_MODIFY_LABEL, TEMPLATE_MODIFY_LABEL, model );
253 }
254
255
256
257
258
259
260
261
262 @Action( ACTION_MODIFY_LABEL )
263 public String doModifyLabel( HttpServletRequest request ) throws AccessDeniedException
264 {
265 if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_MODIFY_LABEL ) ) {
266 throw new AccessDeniedException( "Invalid CSRF token" );
267 }
268
269 populate( _label, request );
270
271
272 if ( !validateBean( _label, VALIDATION_ATTRIBUTES_PREFIX ) )
273 {
274 Map<String, String> mapParameters = new HashMap<String, String>( );
275 mapParameters.put( PARAMETER_KEY, _label.getKey( ) );
276
277 return redirect( request, VIEW_MODIFY_LABEL, mapParameters );
278 }
279
280 LabelService.update( _label );
281 addInfo( INFO_LABEL_UPDATED, getLocale( ) );
282
283 return redirectView( request, VIEW_MANAGE_LABELS );
284 }
285 }