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.portal.web.admin;
35
36 import fr.paris.lutece.portal.business.right.Right;
37 import fr.paris.lutece.portal.business.right.RightHome;
38 import fr.paris.lutece.portal.business.user.AdminUser;
39 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
40 import fr.paris.lutece.portal.service.admin.AdminUserService;
41 import fr.paris.lutece.portal.service.admin.PasswordResetException;
42 import fr.paris.lutece.portal.service.i18n.I18nService;
43 import fr.paris.lutece.portal.service.template.AppTemplateService;
44 import fr.paris.lutece.portal.service.util.AppPathService;
45 import fr.paris.lutece.portal.service.util.AppPropertiesService;
46 import fr.paris.lutece.util.bean.BeanUtil;
47 import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
48 import fr.paris.lutece.util.beanvalidation.ValidationError;
49 import fr.paris.lutece.util.beanvalidation.ValidationErrorConfig;
50 import fr.paris.lutece.util.html.HtmlTemplate;
51
52 import java.io.Serializable;
53
54 import java.util.HashMap;
55 import java.util.List;
56 import java.util.Locale;
57 import java.util.Map;
58 import java.util.Set;
59
60 import javax.servlet.http.HttpServletRequest;
61
62 import javax.validation.ConstraintViolation;
63
64
65
66
67
68 public abstract class AdminFeaturesPageJspBean implements Serializable
69 {
70
71
72
73 private static final long serialVersionUID = -7952383741759547934L;
74
75
76 private static final String TEMPLATE_MAIN = "/admin/feature_frameset.html";
77
78
79 private static final String MARK_FEATURE_URL = "feature_url";
80 private static final String MARK_FEATURE_TITLE = "feature_title";
81 private static final String MARK_FEATURE_ICON = "feature_icon";
82 private static final String MARK_FEATURE_DOCUMENTATION = "feature_documentation";
83 private static final String MARK_FEATURE_GROUP = "feature_group";
84 private static final String MARK_PAGE_TITLE = "page_title";
85 private static final String MARK_PAGE_CONTENT = "page_content";
86
87
88 private static final String PROPERTY_DEFAULT_FEATURE_ICON = "lutece.admin.feature.default.icon";
89 private static final String PROPERTY_RESET_EXCEPTION_MESSAGE = "User must reset his password.";
90
91
92 private String _strFeatureLabel;
93 private String _strFeatureUrl;
94 private String _strFeatureIcon;
95 private String _strFeatureDocumentation;
96 private String _strFeatureGroup;
97 private String _strPageTitleKey;
98 private Locale _locale;
99 private AdminUser _user;
100
101
102
103
104
105
106
107
108
109 public void init( HttpServletRequest request, String strRight )
110 throws AccessDeniedException, PasswordResetException
111 {
112 _user = AdminUserService.getAdminUser( request );
113
114 if ( !_user.checkRight( strRight ) )
115 {
116 throw new AccessDeniedException( "User " + _user.getAccessCode( ) + " does not have " + strRight +
117 " right." );
118 }
119
120 if ( _user.isPasswordReset( ) )
121 {
122 throw new PasswordResetException( PROPERTY_RESET_EXCEPTION_MESSAGE );
123 }
124
125
126 _locale = _user.getLocale( );
127
128 Right right = RightHome.findByPrimaryKey( strRight );
129 right.setLocale( _locale );
130 _strFeatureLabel = right.getName( );
131 _strFeatureUrl = right.getUrl( );
132 _strFeatureIcon = right.getIconUrl( );
133 _strFeatureDocumentation = right.getDocumentationUrl( );
134 _strFeatureGroup = right.getFeatureGroup( );
135 }
136
137
138
139
140
141 public void setPageTitleProperty( String strPageTitleKey )
142 {
143 _strPageTitleKey = strPageTitleKey;
144 }
145
146
147
148
149
150 public String getPageTitle( )
151 {
152 return ( _strPageTitleKey != null ) ? I18nService.getLocalizedString( _strPageTitleKey, getLocale( ) ) : "";
153 }
154
155
156
157
158
159
160 public Locale getLocale( )
161 {
162 return _locale;
163 }
164
165
166
167
168
169
170 public AdminUser getUser( )
171 {
172 return _user;
173 }
174
175
176
177
178
179
180 public String getHomeUrl( HttpServletRequest request )
181 {
182 return AppPathService.getBaseUrl( request ) + _strFeatureUrl;
183 }
184
185
186
187
188
189 public String getFeatureIcon( )
190 {
191 return _strFeatureIcon;
192 }
193
194
195
196
197
198 public void setFeatureIcon( String strFeatureIcon )
199 {
200 _strFeatureIcon = strFeatureIcon;
201 }
202
203
204
205
206
207 public void setFeatureGroup( String strFeatureGroup )
208 {
209 _strFeatureGroup = strFeatureGroup;
210 }
211
212
213
214
215
216
217
218 public String getAdminPage( String strContent )
219 {
220 Map<String, String> rootModel = new HashMap<String, String>( );
221
222 rootModel.put( MARK_FEATURE_URL, _strFeatureUrl );
223 rootModel.put( MARK_FEATURE_TITLE, _strFeatureLabel );
224
225 String strIconUrl = ( _strFeatureIcon != null ) ? _strFeatureIcon
226 : AppPropertiesService.getProperty( PROPERTY_DEFAULT_FEATURE_ICON );
227 rootModel.put( MARK_FEATURE_ICON, strIconUrl );
228
229 String strDocumentationUrl = null;
230
231 if ( _strFeatureDocumentation != null )
232 {
233 strDocumentationUrl = _strFeatureDocumentation;
234 }
235
236 rootModel.put( MARK_FEATURE_DOCUMENTATION, strDocumentationUrl );
237 rootModel.put( MARK_FEATURE_GROUP, _strFeatureGroup );
238
239 rootModel.put( MARK_PAGE_TITLE, getPageTitle( ) );
240 rootModel.put( MARK_PAGE_CONTENT, strContent );
241
242 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MAIN, getLocale( ), rootModel );
243
244 return template.getHtml( );
245 }
246
247
248
249
250
251
252 protected void populate( Object bean, HttpServletRequest request )
253 {
254 BeanUtil.populate( bean, request );
255 }
256
257
258
259
260
261
262
263
264 public <T> Set<ConstraintViolation<T>> validate( T bean )
265 {
266 return BeanValidationUtil.validate( bean );
267 }
268
269
270
271
272
273
274
275
276 public <T> List<ValidationError> validate( T bean, String strFieldsKeyPrefix )
277 {
278 return BeanValidationUtil.validate( bean, getLocale( ), strFieldsKeyPrefix );
279 }
280
281
282
283
284
285
286
287
288 public <T> List<ValidationError> validate( T bean, ValidationErrorConfig config )
289 {
290 return BeanValidationUtil.validate( bean, getLocale( ), config );
291 }
292 }