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.style;
35  
36  import fr.paris.lutece.portal.business.style.Theme;
37  import fr.paris.lutece.portal.business.style.ThemeHome;
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.portal.ThemesService;
41  import fr.paris.lutece.portal.service.template.AppTemplateService;
42  import fr.paris.lutece.portal.service.util.AppPathService;
43  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
44  import fr.paris.lutece.portal.web.constants.Messages;
45  import fr.paris.lutece.util.html.HtmlTemplate;
46  import fr.paris.lutece.util.http.SecurityUtil;
47  
48  import java.util.HashMap;
49  
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpServletResponse;
52  
53  
54  /**
55   * @deprecated As of 2.4.3. Use plugin-theme instead.
56   */
57  public class ThemesJspBean extends AdminFeaturesPageJspBean
58  {
59      // Right
60      /**
61       * Right to manage themes
62       */
63      public static final String RIGHT_MANAGE_THEMES = "CORE_THEMES_MANAGEMENT";
64  
65      /**
66       * Serial version UID
67       */
68      private static final long serialVersionUID = -8201123021058332937L;
69  
70      // Templates files path
71      private static final String TEMPLATE_MANAGE_THEMES = "admin/style/manage_themes.html";
72      private static final String TEMPLATE_CREATE_THEME = "admin/style/create_theme.html";
73      private static final String TEMPLATE_MODIFY_THEME = "admin/style/modify_theme.html";
74  
75      // Markers
76      private static final String MARK_THEMES_LIST = "themes_list";
77      private static final String MARK_THEME = "theme_default";
78  
79      // Parameters
80      private static final String PARAMETER_THEME = "theme";
81      private static final String PARAMETER_URL = "url";
82      private static final String BASE_URL = "base_url";
83      private static final String THEME_LICENCE = "theme_licence";
84      private static final String THEME_VERSION = "theme_version";
85      private static final String THEME_AUTHOR_URL = "theme_author_url";
86      private static final String THEME_AUTHOR = "theme_author";
87      private static final String PATH_CSS = "path_css";
88      private static final String PATH_JS = "path_js";
89      private static final String PATH_IMAGES = "path_images";
90      private static final String THEME_DESCRIPTION = "theme_description";
91      private static final String CODE_THEME = "code_theme";
92  
93      /**
94       * Returns the list of Themes
95       *
96       * @param request The Http request
97       * @return the html code for display the manage themes page
98       */
99      public String getManageThemes( HttpServletRequest request )
100     {
101         HashMap<String, Object> model = new HashMap<String, Object>(  );
102 
103         model.put( MARK_THEMES_LIST, ThemeHome.getThemesList(  ) );
104         model.put( MARK_THEME, ThemesService.getGlobalThemeObject(  ) );
105         model.put( BASE_URL, AppPathService.getBaseUrl( request ) );
106 
107         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_THEMES, getLocale(  ), model );
108 
109         return getAdminPage( template.getHtml(  ) );
110     }
111 
112     /**
113      * Modify the global theme
114      *
115      * @param request The Http request
116      * @return the html code for display the manage themes page
117      */
118     public String doModifyGlobalTheme( HttpServletRequest request )
119     {
120         String strTheme = request.getParameter( PARAMETER_THEME );
121         ThemesService.setGlobalTheme( strTheme );
122 
123         return this.getHomeUrl( request );
124     }
125 
126     /**
127      * Modify the User theme
128      *
129      * @param request The Http request
130      * @param response The Http response
131      * @return the html code for display the manage themes page
132      */
133     public String doModifyUserTheme( HttpServletRequest request, HttpServletResponse response )
134     {
135         String strTheme = request.getParameter( PARAMETER_THEME );
136         String strForwardUrl = request.getParameter( PARAMETER_URL );
137 
138         if ( !SecurityUtil.containsCleanParameters( request ) )
139         {
140             return AppPathService.getBaseUrl( request );
141         }
142 
143         ThemesService.setUserTheme( request, response, strTheme );
144 
145         return strForwardUrl;
146     }
147 
148     /**
149      *
150      * @param request The HttpServletRequest
151      * @return the html code to create a theme
152      */
153     public String getCreateTheme( HttpServletRequest request )
154     {
155         HashMap<String, Object> model = new HashMap<String, Object>(  );
156         model.put( BASE_URL, AppPathService.getBaseUrl( request ) );
157 
158         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_THEME, getLocale(  ), model );
159 
160         return getAdminPage( template.getHtml(  ) );
161     }
162 
163     /**
164      *
165      * @param request The HttpServletRequest
166      * @return the html page to modifiy a theme
167      */
168     public String getModifyTheme( HttpServletRequest request )
169     {
170         Theme themeToModify = ThemeHome.findByPrimaryKey( request.getParameter( CODE_THEME ) );
171 
172         HashMap<String, Object> model = new HashMap<String, Object>(  );
173         model.put( BASE_URL, AppPathService.getBaseUrl( request ) );
174         model.put( PARAMETER_THEME, themeToModify );
175 
176         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_THEME, getLocale(  ), model );
177 
178         return getAdminPage( template.getHtml(  ) );
179     }
180 
181     /**
182      *
183      * @param request The HttpServletRequest
184      * @return the html code for the theme list
185      */
186     public String doModifyTheme( HttpServletRequest request )
187     {
188         Theme theme = getThemeFromRequest( request );
189 
190         // Mandatory fields
191         if ( isMissingFields( request ) )
192         {
193             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
194         }
195 
196         ThemeHome.update( theme );
197 
198         return this.getHomeUrl( request );
199     }
200 
201     /**
202      *
203      * @param request The HttpServletRequest
204      * @return the html code for the theme list
205      */
206     public String doCreateTheme( HttpServletRequest request )
207     {
208         Theme theme = getThemeFromRequest( request );
209 
210         // Mandatory fields
211         if ( isMissingFields( request ) )
212         {
213             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
214         }
215 
216         ThemeHome.create( theme );
217 
218         return this.getHomeUrl( request );
219     }
220 
221     /**
222      *
223      * @param request The HttpServletRequest
224      * @return the theme object from request parameter
225      */
226     private Theme getThemeFromRequest( HttpServletRequest request )
227     {
228         Theme theme = new Theme(  );
229 
230         theme.setCodeTheme( request.getParameter( CODE_THEME ) );
231         theme.setThemeDescription( request.getParameter( THEME_DESCRIPTION ) );
232         theme.setPathImages( request.getParameter( PATH_IMAGES ) );
233         theme.setPathCss( request.getParameter( PATH_CSS ) );
234         theme.setPathJs( request.getParameter( PATH_JS ) );
235         theme.setThemeAuthor( request.getParameter( THEME_AUTHOR ) );
236         theme.setThemeAuthorUrl( request.getParameter( THEME_AUTHOR_URL ) );
237         theme.setThemeVersion( request.getParameter( THEME_VERSION ) );
238         theme.setThemeLicence( request.getParameter( THEME_LICENCE ) );
239 
240         return theme;
241     }
242 
243     /**
244      * @param request The HttpServletRequest
245      * @return true if 1 field is missing
246      */
247     private boolean isMissingFields( HttpServletRequest request )
248     {
249         return request.getParameter( CODE_THEME ).equals( "" ) ||
250         request.getParameter( THEME_DESCRIPTION ).equals( "" ) || request.getParameter( PATH_IMAGES ).equals( "" ) ||
251         request.getParameter( PATH_CSS ).equals( "" ) || request.getParameter( PATH_JS ).equals( "" );
252     }
253 }