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.plugins.dbpage.web;
35  
36  import fr.paris.lutece.plugins.dbpage.business.DbPage;
37  import fr.paris.lutece.plugins.dbpage.service.DbPageService;
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.portal.service.template.AppTemplateService;
40  import fr.paris.lutece.portal.service.util.AppPropertiesService;
41  import fr.paris.lutece.portal.web.xpages.XPage;
42  import fr.paris.lutece.portal.web.xpages.XPageApplication;
43  import fr.paris.lutece.util.html.HtmlTemplate;
44  
45  import java.util.HashMap;
46  import java.util.List;
47  
48  import javax.servlet.http.HttpServletRequest;
49  
50  /**
51   * This class manages dbpage.
52   */
53  public class DbpageApp implements XPageApplication
54  {
55  
56      ////////////////////////////////////////////////////////////////////////////
57      // Constants
58  
59      private static final String TEMPLATE_MANAGE_DBPAGE = "skin/plugins/dbpage/dbpage.html";
60      private static final String TEMPLATE_DBPAGE_LIST = "skin/plugins/dbpage/dbpages_list.html";
61      private static final String MARK_PAGE_TITLE = "page_title";
62      private static final String MARK_SECTIONS = "sections";
63      private static final String MARK_DBPAGES_LIST = "dbpages_list";
64      private static final String PARAMETER_DBPAGE_NAME = "dbpage";
65      private static final String PROPERTY_PAGE_TITLE = "dbpage.pageTitle";
66      private static final String PROPERTY_PAGE_PATH = "dbpage.pagePathLabel";
67      private static final String STR_PAGE_ERROR = AppPropertiesService.getProperty(
68              "dbpage.page.properties.message.error");
69      private static final String STR_PARAMETER_ERROR = AppPropertiesService.getProperty(
70              "dbpage.parameter.properties.message.error");
71  
72      /**
73       * Returns the content of the page Contact. It is composed by a form which
74       * to capture the data to send a message to a contact of the portal.
75       *
76       * @param request The http request
77       * @param nMode The current mode
78       * @param plugin The plugin object
79       * @return the Content of the page Contact
80       */
81      public XPage getPage(HttpServletRequest request, int nMode, Plugin plugin)
82      {
83          XPage page = new XPage();
84  
85          page.setTitle(AppPropertiesService.getProperty(PROPERTY_PAGE_TITLE));
86          page.setPathLabel(AppPropertiesService.getProperty(PROPERTY_PAGE_PATH));
87  
88          String strDbPageName = request.getParameter(PARAMETER_DBPAGE_NAME);
89  
90          if ((strDbPageName != null) && !strDbPageName.equals(""))
91          {
92              DbPage dbPage = DbPageService.getInstance().getDbPage(strDbPageName);
93  
94              if (dbPage != null)
95              {
96                  List<String> listValues = DbPageService.getInstance().getValues(request);
97                  String strPageContent = buildPage(dbPage, listValues, request);
98                  page.setContent(strPageContent);
99                  page.setTitle(dbPage.getTitle());
100                 page.setPathLabel(dbPage.getTitle());
101             }
102             else
103             {
104                 page.setContent(STR_PAGE_ERROR);
105             }
106         }
107         else
108         {
109             HashMap<String, Object> model = new HashMap<String, Object>();
110             model.put(MARK_DBPAGES_LIST, DbPageService.getInstance().getDbPagesCollection());
111 
112             HtmlTemplate template = AppTemplateService.getTemplate(TEMPLATE_DBPAGE_LIST, request.getLocale(), model);
113 
114             page.setContent(template.getHtml());
115         }
116 
117         return page;
118     }
119 
120     /**
121      * Returns the Html of the dbpage
122      *
123      * @param dbPage The dbPage object
124      * @param listValues The listValues substitute in the SQL request
125      * @param request The http request
126      * @return the html code of the dbPage
127      */
128     private String buildPage(DbPage dbPage, List<String> listValues, HttpServletRequest request)
129     {
130         HashMap<String, Object> model = new HashMap<String, Object>();
131         model.put(MARK_PAGE_TITLE, dbPage.getTitle());
132         model.put(MARK_SECTIONS, dbPage.getContent(listValues, request));
133 
134         HtmlTemplate template = AppTemplateService.getTemplate(TEMPLATE_MANAGE_DBPAGE, request.getLocale(), model);
135 
136         return template.getHtml();
137     }
138 }