View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.adminquery.web;
35  
36  import fr.paris.lutece.plugins.adminquery.business.AdminQueryHome;
37  import fr.paris.lutece.portal.service.template.AppTemplateService;
38  import fr.paris.lutece.portal.service.util.AppException;
39  import fr.paris.lutece.portal.service.util.AppPropertiesService;
40  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
41  import fr.paris.lutece.util.ReferenceList;
42  import fr.paris.lutece.util.html.HtmlTemplate;
43  
44  import java.util.ArrayList;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  
52  /*
53   * This class provides the user interface to manage adminquery features
54   */
55  public class AdminQueryJspBean extends PluginAdminPageJspBean
56  {
57      /////////////////////////////////////////////////////////////////////////////////////////
58      // Constants
59  
60      // Right
61      public static final String RIGHT_MANAGE_ADMIN_QUERY = "ADMIN_QUERY_MANAGEMENT";
62  
63      //Properties
64      private static final String PROPERTY_QUERY = "adminquery.query";
65  
66      //Parameters
67      private static final String PARAMETER_REQUEST = "sql_request";
68  
69      //Markers
70      private static final String MARK_COLUMN_NAME_LIST = "column_name";
71      private static final String MARK_ROW_LIST = "row_list";
72      private static final String MARK_LINE_LIST = "line_list";
73      private static final String MARK_REQUEST_LIST = "sql_request_list";
74  
75      //Templates
76      private static final String TEMPLATE_MANAGE_ADMIN_QUERY = "admin/plugins/adminquery/manage_admin_query.html";
77      private static final String TEMPLATE_RESULTS_LINE = "admin/plugins/adminquery/results_line.html";
78  
79      // Constants
80      private static final String SUFFIX_LABEL = ".label";
81      private static final String SUFFIX_SQL = ".sql";
82  
83      /**
84       * Creates a new AdminQueryJspBean object.
85       */
86      public AdminQueryJspBean(  )
87      {
88      }
89  
90      /**
91       * Returns the Sql request management form
92       * @param request the SQL request
93       * @return the HTML page
94       */
95      public String getAdminQuery( HttpServletRequest request )
96      {
97          return getAdminPage( getResults( request.getParameter( PARAMETER_REQUEST ) ) );
98      }
99  
100     /**
101      * Returns the result of the sql request in html form
102      * @param strRequest the SQL request
103      * @return  String the result of the request of row
104      */
105     private String getResults( String strRequest )
106     {
107         Map<String, Object> rootModel = new HashMap<String, Object>(  );
108         rootModel.put( MARK_REQUEST_LIST, getRequestList(  ) );
109 
110         if ( !( ( strRequest == null ) || strRequest.equals( "0" ) ) )
111         {
112             try
113             {
114                 strRequest = AppPropertiesService.getProperty( PROPERTY_QUERY + strRequest + SUFFIX_SQL );
115 
116                 List<String> listColumnNames = AdminQueryHome.selectColumnNames( strRequest, getPlugin(  ) );
117 
118                 if ( listColumnNames != null )
119                 {
120                     rootModel.put( MARK_COLUMN_NAME_LIST, listColumnNames );
121                     rootModel.put( MARK_ROW_LIST, getRows( strRequest ) );
122                 }
123             }
124             catch ( AppException e )
125             {
126                 throw new AppException( "Request error in adminquery.properties" );
127             }
128         }
129 
130         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_ADMIN_QUERY, getLocale(  ), rootModel );
131 
132         return template.getHtml(  );
133     }
134 
135     /**
136      * Returns a List of row witch is in html form
137      * Fill in the lines of the rows
138      * @param strRequest the SQL request
139      * @return  List of row
140      */
141     private List<String> getRows( String strRequest )
142     {
143         List<String> listRow = new ArrayList<String>(  );
144 
145         //for row's values
146         for ( List<String> listLine : AdminQueryHome.selectRows( strRequest, getPlugin(  ) ) )
147         {
148             Map<String, List<String>> model = new HashMap<String, List<String>>(  );
149             model.put( MARK_LINE_LIST, listLine );
150 
151             HtmlTemplate tLine = AppTemplateService.getTemplate( TEMPLATE_RESULTS_LINE, getLocale(  ), model );
152             listRow.add( tLine.getHtml(  ) );
153         }
154 
155         return listRow;
156     }
157 
158     /**
159      * Return the list of requests from the file .properties
160      * @return The list of requests
161      */
162     private ReferenceList getRequestList(  )
163     {
164         ReferenceList list = new ReferenceList(  );
165         list.addItem( 0, "" );
166 
167         String strQueryLabel = null;
168 
169         int i = 1;
170         String strKey = PROPERTY_QUERY + i + SUFFIX_LABEL;
171 
172         while ( ( strQueryLabel = AppPropertiesService.getProperty( strKey ) ) != null )
173         {
174             list.addItem( i, strQueryLabel );
175             i++;
176             strKey = PROPERTY_QUERY + i + SUFFIX_LABEL;
177         }
178 
179         return list;
180     }
181 }