1- /*
2- * Knowage, Open Source Business Intelligence suite
3- * Copyright (C) 2016 Engineering Ingegneria Informatica S.p.A.
4- *
5- * Knowage is free software: you can redistribute it and/or modify
6- * it under the terms of the GNU Affero General Public License as published by
7- * the Free Software Foundation, either version 3 of the License, or
8- * (at your option) any later version.
9- *
10- * Knowage is distributed in the hope that it will be useful,
11- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13- * GNU Affero General Public License for more details.
14- *
15- * You should have received a copy of the GNU Affero General Public License
16- * along with this program. If not, see <http://www.gnu.org/licenses/>.
17- */
18- package it .eng .spagobi .engines .qbe .services .dataset ;
19-
20- import java .io .IOException ;
1+ package it .eng .spagobi .engines .qbe .api ;
2+
213import java .util .List ;
224import java .util .regex .Matcher ;
235import java .util .regex .Pattern ;
246
7+ import javax .ws .rs .FormParam ;
8+ import javax .ws .rs .POST ;
9+ import javax .ws .rs .Path ;
10+ import javax .ws .rs .Produces ;
11+ import javax .ws .rs .core .MediaType ;
12+ import javax .ws .rs .core .Response ;
13+
2514import org .apache .log4j .Logger ;
2615import org .hibernate .jdbc .util .BasicFormatterImpl ;
2716import org .json .JSONObject ;
3221import it .eng .qbe .query .ISelectField ;
3322import it .eng .qbe .query .Query ;
3423import it .eng .qbe .statement .IStatement ;
35- import it .eng .spago .base .SourceBean ;
3624import it .eng .spagobi .commons .bo .UserProfile ;
3725import it .eng .spagobi .commons .utilities .StringUtilities ;
38- import it .eng .spagobi .engines . qbe . services . core . AbstractQbeEngineAction ;
26+ import it .eng .spagobi .container . ObjectUtils ;
3927import it .eng .spagobi .engines .qbe .services .core .ExecuteQueryAction ;
4028import it .eng .spagobi .utilities .assertion .Assert ;
4129import it .eng .spagobi .utilities .engines .EngineConstants ;
42- import it .eng .spagobi .utilities .engines .SpagoBIEngineServiceException ;
4330import it .eng .spagobi .utilities .engines .SpagoBIEngineServiceExceptionHandler ;
44- import it .eng .spagobi .utilities .service .JSONSuccess ;
45-
46- /**
47- * The Class GetSQLQueryAction.
48- *
49- * @author Davide Zerbetto (davide.zerbetto@eng.it)
50- */
51- public class GetSQLQueryAction extends AbstractQbeEngineAction {
52-
53- // INPUT PARAMETERS
5431
55- // OUTPUT PARAMETERS
32+ @ Path ("/GetSqlQuery" )
33+ public class QbeGetSQLQueryResource extends AbstractQbeEngineResource {
5634
57- // SESSION PARAMETRES
58-
59- // AVAILABLE PUBLISHERS
60-
61- /** Logger component. */
62- private static final Logger LOGGER = Logger .getLogger (GetSQLQueryAction .class );
6335
6436 public static final String ENGINE_NAME = "SpagoBIQbeEngine" ;
65- public static final String REPLACE_PARAMETERS_WITH_QUESTION = "replaceParametersWithQuestion" ;
6637
67- @ Override
68- public void service (SourceBean request , SourceBean response ) {
38+ public static transient Logger logger = Logger .getLogger (QbeGetSQLQueryResource .class );
6939
40+ @ POST
41+ @ Produces ({ MediaType .APPLICATION_JSON , MediaType .APPLICATION_XML })
42+ public Response getSQLQuery (@ FormParam ("queryId" ) String queryId
43+ , @ FormParam ("replaceParametersWithQuestion" ) Boolean replaceParametersWithQuestion
44+ , @ FormParam ("promptableFilters" ) String promptableFilters ) {
45+
7046 Query query = null ;
7147 IStatement statement = null ;
7248
73- LOGGER .debug ("IN" );
49+ logger .debug ("IN" );
7450
7551 try {
76- super .service (request , response );
77-
78- boolean replaceParametersWithQuestion = getAttributeAsBoolean ("replaceParametersWithQuestion" );
79-
80- // retrieving query specified by id on request
81- query = getEngineInstance ().getQueryCatalogue ().getQuery (getAttributeAsString ("queryId" ));
52+ query = getEngineInstance ().getQueryCatalogue ().getQuery (queryId );
8253 if (query == null ) {
8354 query = getEngineInstance ().getQueryCatalogue ().getFirstQuery ();
8455
8556 }
8657 Assert .assertNotNull (query , "Query not found!!" );
8758
88- UserProfile userProfile = (UserProfile ) getEnv ().get (EngineConstants .ENV_USER_PROFILE );
59+ UserProfile userProfile = (UserProfile ) getEnv ().get (EngineConstants .ENV_USER_PROFILE );
8960 IModelAccessModality accessModality = getEngineInstance ().getDataSource ().getModelAccessModality ();
9061 Query filteredQuery = accessModality .getFilteredStatement (query , this .getEngineInstance ().getDataSource (), userProfile .getUserAttributes ());
9162
9263 getEngineInstance ().setActiveQuery (filteredQuery );
93-
94-
95- if (replaceParametersWithQuestion ) {
96- // promptable filters values may come with request (read-only
97- // user modality)
98- ExecuteQueryAction .updatePromptableFiltersValue (filteredQuery , this , true );
99- } else {
100- // promptable filters values may come with request (read-only
101- // user modality)
102- ExecuteQueryAction .updatePromptableFiltersValue (filteredQuery , this );
64+ JSONObject promptFileds = null ;
65+
66+ if (promptableFilters != null && !promptableFilters .isEmpty ()) {
67+ try {
68+ if (replaceParametersWithQuestion ) {
69+ // promptable filters values may come with request (read-only
70+ // user modality)
71+ QbeQueryResource .updatePromptableFiltersValue (filteredQuery , true , promptableFilters );
72+ } else {
73+ // promptable filters values may come with request (read-only
74+ // user modality)
75+ QbeQueryResource .updatePromptableFiltersValue (filteredQuery , promptableFilters );
76+ }
77+ } catch (Exception e ) {
78+ logger .error ("Errore nel parsing dei promptableFilters" , e );
79+ promptFileds = new JSONObject ();
80+ }
10381 }
10482
10583 statement = getEngineInstance ().getStatment ();
@@ -112,7 +90,7 @@ public void service(SourceBean request, SourceBean response) {
11290 String sqlQuery = statement .getSqlQueryString ();
11391 String sqlQueryFormatted = formatQueryString (sqlQuery );
11492
115- LOGGER .debug ("Executable query (SQL): [" + sqlQuery + "]" );
93+ logger .debug ("Executable query (SQL): [" + sqlQuery + "]" );
11694
11795 toReturn .put ("sql" , sqlQuery );
11896 toReturn .put ("sqlFormatted" , sqlQueryFormatted );
@@ -122,28 +100,22 @@ public void service(SourceBean request, SourceBean response) {
122100 String jpaQueryStrFormatted = formatQueryString (addAliasInJqpl (query , jpaQueryStr ));
123101 String sqlQueryFormatted = formatQueryString (addAliasInSql (query , sqlQuery ));
124102
125- LOGGER .debug ("Executable query (HQL/JPQL): [" + jpaQueryStr + "]" );
126- LOGGER .debug ("Executable query (SQL): [" + sqlQuery + "]" );
103+ logger .debug ("Executable query (HQL/JPQL): [" + jpaQueryStr + "]" );
104+ logger .debug ("Executable query (SQL): [" + sqlQuery + "]" );
127105
128106 toReturn .put ("sql" , sqlQuery );
129107 toReturn .put ("jpqlFormatted" , jpaQueryStrFormatted );
130108 toReturn .put ("sqlFormatted" , sqlQueryFormatted );
131109 }
132110
133- try {
134- writeBackToClient (new JSONSuccess (toReturn ));
135- } catch (IOException e ) {
136- String message = "Impossible to write back the responce to the client" ;
137- throw new SpagoBIEngineServiceException (getActionName (), message , e );
138- }
111+ return Response .status (200 ).entity (toReturn .toString ()).build ();
139112
140113 } catch (Throwable t ) {
141- throw SpagoBIEngineServiceExceptionHandler .getInstance ().getWrappedException (getActionName (), getEngineInstance (), t );
114+ throw SpagoBIEngineServiceExceptionHandler .getInstance ().getWrappedException (this . getClass (). getName (), getEngineInstance (), t );
142115 } finally {
143- LOGGER .debug ("OUT" );
116+ logger .debug ("OUT" );
144117 }
145-
146- }
118+ }
147119
148120 /**
149121 * Get the query string of the passed Query. The returned query is formatted
@@ -157,7 +129,7 @@ public String formatQueryString(String queryString) {
157129 BasicFormatterImpl fromatter ;
158130
159131 if (queryString == null || queryString .equals ("" )) {
160- LOGGER .error ("Impossible to get the query string because the query is null" );
132+ logger .error ("Impossible to get the query string because the query is null" );
161133 return "" ;
162134 }
163135
@@ -218,4 +190,5 @@ public String addAliasInJqpl(Query query, String queryString) {
218190 return queryString ;
219191 }
220192
221- }
193+
194+ }
0 commit comments