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.Mode;
37  import fr.paris.lutece.portal.business.style.ModeHome;
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.template.AppTemplateService;
41  import fr.paris.lutece.portal.service.util.AppPathService;
42  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
43  import fr.paris.lutece.portal.web.constants.Messages;
44  import fr.paris.lutece.portal.web.constants.Parameters;
45  import fr.paris.lutece.util.html.HtmlTemplate;
46  
47  import java.io.File;
48  
49  import java.util.HashMap;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  
54  /**
55   * This class provides the user interface to manage modes features ( manage,
56   * create, modify, remove)
57   */
58  public class ModesJspBean extends AdminFeaturesPageJspBean
59  {
60      // Right
61      /**
62       * Right to manage modes
63       */
64      public static final String RIGHT_MANAGE_MODES = "CORE_MODES_MANAGEMENT";
65  
66      /**
67       * Serial version UID
68       */
69      private static final long serialVersionUID = 8205010720652090863L;
70  
71      // Markers
72      private static final String MARK_MODES_LIST = "mode_list";
73      private static final String MARK_MODE = "mode";
74  
75      // Properties for page titles
76      private static final String PROPERTY_PAGE_TITLE_MODE_LIST = "portal.style.manage_mode.pageTitle";
77      private static final String PROPERTY_PAGE_TITLE_CREATE_MODE = "portal.style.create_mode.pageTitle";
78      private static final String PROPERTY_PAGE_TITLE_MODIFY_MODE = "portal.style.modify_mode.pageTitle";
79  
80      // Templates files path
81      private static final String TEMPLATE_MANAGE_MODES = "admin/style/manage_modes.html";
82      private static final String TEMPLATE_CREATE_MODE = "admin/style/create_mode.html";
83      private static final String TEMPLATE_MODIFY_MODE = "admin/style/modify_mode.html";
84  
85      // Properties
86      private static final String PROPERTY_PATH_XSL = "path.stylesheet";
87  
88      // Jsp definition
89      private static final String JSP_MANAGE_MODES = "ManageModes.jsp";
90  
91      /**
92       * Returns the list of modes
93       *
94       * @param request The Http request
95       * @return the html code for display the modes list
96       */
97      public String getManageModes( HttpServletRequest request )
98      {
99          setPageTitleProperty( PROPERTY_PAGE_TITLE_MODE_LIST );
100 
101         HashMap<String, Object> model = new HashMap<String, Object>(  );
102         model.put( MARK_MODES_LIST, ModeHome.getModesList(  ) );
103 
104         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_MODES, getLocale(  ), model );
105 
106         return getAdminPage( template.getHtml(  ) );
107     }
108 
109     /**
110      * Returns the mode form of creation
111      *
112      * @param request The Http request
113      * @return the html code of the mode
114      */
115     public String getCreateMode( HttpServletRequest request )
116     {
117         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_MODE );
118 
119         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_MODE, getLocale(  ) );
120 
121         return getAdminPage( template.getHtml(  ) );
122     }
123 
124     /**
125      * Processes the creation form of a new mode by recovering the parameters in
126      * the http request
127      *
128      * @param request the http request
129      * @return The Jsp URL of the process result
130      */
131     public String doCreateMode( HttpServletRequest request )
132     {
133         String strDescription = request.getParameter( Parameters.MODE_DESCRIPTION );
134         String strPath = request.getParameter( Parameters.MODE_PATH );
135 
136         //Mandatory fields
137         if ( strDescription.equals( "" ) || strPath.equals( "" ) )
138         {
139             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
140         }
141 
142         if ( !strPath.endsWith( "/" ) && !strPath.endsWith( "\\" ) )
143         {
144             strPath += File.separator;
145         }
146 
147         File dirPath = new File( AppPathService.getPath( PROPERTY_PATH_XSL ) + strPath );
148 
149         if ( dirPath.exists(  ) )
150         {
151             return AdminMessageService.getMessageUrl( request, Messages.PATH_ALREADY_EXISTS, AdminMessage.TYPE_STOP );
152         }
153 
154         if ( !dirPath.mkdir(  ) )
155         {
156             return AdminMessageService.getMessageUrl( request, Messages.PATH_CREATION_ERROR, AdminMessage.TYPE_ERROR );
157         }
158 
159         Mode mode = new Mode(  );
160         mode.setDescription( strDescription );
161         mode.setPath( strPath );
162 
163         /* Since v1.2 */
164         mode.setOutputXslPropertyMethod( request.getParameter( Parameters.MODE_OUTPUT_XSL_METHOD ) );
165         mode.setOutputXslPropertyVersion( request.getParameter( Parameters.MODE_OUTPUT_XSL_VERSION ) );
166         mode.setOutputXslPropertyMediaType( request.getParameter( Parameters.MODE_OUTPUT_XSL_MEDIA_TYPE ) );
167         mode.setOutputXslPropertyEncoding( request.getParameter( Parameters.MODE_OUTPUT_XSL_ENCODING ) );
168         mode.setOutputXslPropertyIndent( request.getParameter( Parameters.MODE_OUTPUT_XSL_INDENT ) );
169         mode.setOutputXslPropertyOmitXmlDeclaration( request.getParameter( 
170                 Parameters.MODE_OUTPUT_XSL_OMIT_XML_DECLARATION ) );
171         mode.setOutputXslPropertyStandalone( request.getParameter( Parameters.MODE_OUTPUT_XSL_STANDALONE ) );
172 
173         ModeHome.create( mode );
174 
175         // If the process is successfull, redirects towards the theme view
176         return JSP_MANAGE_MODES;
177     }
178 
179     /**
180      * Returns the mode form of update
181      *
182      * @param request The Http request
183      * @return the html code of the mode form
184      */
185     public String getModifyMode( HttpServletRequest request )
186     {
187         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_MODE );
188 
189         String strId = request.getParameter( Parameters.MODE_ID );
190 
191         HashMap<String, Object> model = new HashMap<String, Object>(  );
192         model.put( MARK_MODE, ModeHome.findByPrimaryKey( Integer.parseInt( strId ) ) );
193 
194         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_MODE, getLocale(  ), model );
195 
196         return getAdminPage( template.getHtml(  ) );
197     }
198 
199     /**
200      * Processes the updating form of a mode whose new parameters are stored in
201      * the http request
202      *
203      * @param request The http request
204      * @return The Jsp URL of the process result
205      */
206     public String doModifyMode( HttpServletRequest request )
207     {
208         String strId = request.getParameter( Parameters.MODE_ID );
209         String strDescription = request.getParameter( Parameters.MODE_DESCRIPTION );
210 
211         if ( strDescription.equals( "" ) )
212         {
213             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
214         }
215 
216         Mode mode = ModeHome.findByPrimaryKey( Integer.parseInt( strId ) );
217         mode.setDescription( strDescription );
218 
219         /* Since v1.2 */
220         mode.setOutputXslPropertyMethod( request.getParameter( Parameters.MODE_OUTPUT_XSL_METHOD ) );
221         mode.setOutputXslPropertyVersion( request.getParameter( Parameters.MODE_OUTPUT_XSL_VERSION ) );
222         mode.setOutputXslPropertyMediaType( request.getParameter( Parameters.MODE_OUTPUT_XSL_MEDIA_TYPE ) );
223         mode.setOutputXslPropertyEncoding( request.getParameter( Parameters.MODE_OUTPUT_XSL_ENCODING ) );
224         mode.setOutputXslPropertyIndent( request.getParameter( Parameters.MODE_OUTPUT_XSL_INDENT ) );
225         mode.setOutputXslPropertyOmitXmlDeclaration( request.getParameter( 
226                 Parameters.MODE_OUTPUT_XSL_OMIT_XML_DECLARATION ) );
227         mode.setOutputXslPropertyStandalone( request.getParameter( Parameters.MODE_OUTPUT_XSL_STANDALONE ) );
228 
229         ModeHome.update( mode );
230 
231         // If the process is successfull, redirects towards the mode management page
232         return JSP_MANAGE_MODES;
233     }
234 }