1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
54
55 public class AdminQueryJspBean extends PluginAdminPageJspBean
56 {
57
58
59
60
61 public static final String RIGHT_MANAGE_ADMIN_QUERY = "ADMIN_QUERY_MANAGEMENT";
62
63
64 private static final String PROPERTY_QUERY = "adminquery.query";
65
66
67 private static final String PARAMETER_REQUEST = "sql_request";
68
69
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
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
80 private static final String SUFFIX_LABEL = ".label";
81 private static final String SUFFIX_SQL = ".sql";
82
83
84
85
86 public AdminQueryJspBean( )
87 {
88 }
89
90
91
92
93
94
95 public String getAdminQuery( HttpServletRequest request )
96 {
97 return getAdminPage( getResults( request.getParameter( PARAMETER_REQUEST ) ) );
98 }
99
100
101
102
103
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
137
138
139
140
141 private List<String> getRows( String strRequest )
142 {
143 List<String> listRow = new ArrayList<String>( );
144
145
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
160
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 }