View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.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   * Provides generic methods for jspBeans
67   */
68  public abstract class AdminFeaturesPageJspBean implements Serializable
69  {
70      /**
71       * Serial version UID
72       */
73      private static final long serialVersionUID = -7952383741759547934L;
74  
75      // template for all admin pages
76      private static final String TEMPLATE_MAIN = "/admin/feature_frameset.html";
77  
78      // bookmarks
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      // Properties
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      // private fields
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      * Initialize the jspbean data
103      * Allows to set the feature url and feature title associated
104      * @param request the HTTP request
105      * @param strRight The right
106      * @throws AccessDeniedException Access denied exception
107      * @throws PasswordResetException Password reset exception
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         // get the locale
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      * Set the page title property
139      * @param strPageTitleKey The page title property
140      */
141     public void setPageTitleProperty( String strPageTitleKey )
142     {
143         _strPageTitleKey = strPageTitleKey;
144     }
145 
146     /**
147      * Get the page title
148      * @return The page title
149      */
150     public String getPageTitle(  )
151     {
152         return ( _strPageTitleKey != null ) ? I18nService.getLocalizedString( _strPageTitleKey, getLocale(  ) ) : "";
153     }
154 
155     /**
156      * Returns the Locale
157      *
158      * @return The Locale
159      */
160     public Locale getLocale(  )
161     {
162         return _locale;
163     }
164 
165     /**
166      * Returns the AdminUser
167      *
168      * @return The AdminUser
169      */
170     public AdminUser getUser(  )
171     {
172         return _user;
173     }
174 
175     /**
176      * Returns the feature home Url
177      * @param request The HTTP request
178      * @return The feature home Url
179      */
180     public String getHomeUrl( HttpServletRequest request )
181     {
182         return AppPathService.getBaseUrl( request ) + _strFeatureUrl;
183     }
184 
185     /**
186      * Returns the feature icon Url
187      * @return The feature icon Url
188      */
189     public String getFeatureIcon(  )
190     {
191         return _strFeatureIcon;
192     }
193 
194     /**
195      * Sets the feature icon url
196      * @param strFeatureIcon the feature icon url
197      */
198     public void setFeatureIcon( String strFeatureIcon )
199     {
200         _strFeatureIcon = strFeatureIcon;
201     }
202 
203     /**
204      * Sets the feature group
205      * @param strFeatureGroup the feature group
206      */
207     public void setFeatureGroup( String strFeatureGroup )
208     {
209         _strFeatureGroup = strFeatureGroup;
210     }
211 
212     /**
213      * Get the admin page from a content data
214      *
215      * @return the html code for the admin page for the given content
216      * @param strContent the data to load in the admin page
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      * Populate a bean using parameters in http request
249      * @param bean bean to populate
250      * @param request http request
251      */
252     protected void populate( Object bean, HttpServletRequest request )
253     {
254         BeanUtil.populate( bean, request );
255     }
256 
257     /**
258      * Validates a bean.
259      *
260      * @param <T> the bean type
261      * @param bean the bean to validate
262      * @return the sets of constraints that has been violated
263      */
264     public <T> Set<ConstraintViolation<T>> validate( T bean )
265     {
266         return BeanValidationUtil.validate( bean );
267     }
268 
269     /**
270      * Validates a bean
271      * @param <T> The bean type
272      * @param bean The bean to validate
273      * @param strFieldsKeyPrefix The fields keys prefix in resources files
274      * @return The error list
275      */
276     public <T> List<ValidationError> validate( T bean, String strFieldsKeyPrefix )
277     {
278         return BeanValidationUtil.validate( bean, getLocale(  ), strFieldsKeyPrefix );
279     }
280 
281     /**
282      * Validates a bean
283      * @param <T> The bean type
284      * @param bean The bean to validate
285      * @param config  The config for Error validation rendering
286      * @return The error list
287      */
288     public <T> List<ValidationError> validate( T bean, ValidationErrorConfig config )
289     {
290         return BeanValidationUtil.validate( bean, getLocale(  ), config );
291     }
292 }