4747public class MdxQuery implements IQuery {
4848
4949 private static final Logger log = LoggerFactory .getLogger (MdxQuery .class );
50-
50+
5151 private Properties properties = new Properties ();
5252 private String mdx ;
5353 private SaikuCube cube ;
@@ -56,14 +56,14 @@ public class MdxQuery implements IQuery {
5656 private Scenario scenario ;
5757 private CellSet cellset ;
5858 private OlapStatement statement ;
59-
59+
6060 public MdxQuery (OlapConnection con , SaikuCube cube , String name , String mdx ) {
6161 this .cube = cube ;
6262 this .connection = con ;
6363 this .name = name ;
6464 this .mdx = mdx ;
6565 }
66-
66+
6767 public String getName () {
6868 return name ;
6969 }
@@ -87,60 +87,69 @@ public SaikuCube getSaikuCube() {
8787 return cube ;
8888 }
8989
90- public String getMdx () {
91- return mdx ;
92- }
93-
94- public void setMdx (String mdx ) {
95- this .mdx = mdx ;
96- }
90+ public String getMdx () {
91+ return mdx ;
92+ }
93+
94+ public void setMdx (String mdx ) {
95+ this .mdx = mdx ;
96+ }
9797
9898 public void resetQuery () {
9999 this .mdx = "" ;
100100 }
101-
102- public void setProperties (Properties props ) {
103- this .properties .putAll (props );
104- }
105-
106- public Properties getProperties () {
107- Properties props = new Properties ( this .properties ) ;
108- props .put (QueryProperties .KEY_IS_DRILLTHROUGH , isDrillThroughEnabled ().toString ());
109- props .put ("org.saiku.connection.scenario" , Boolean .toString (false ));
110- try {
111- props .put ("org.saiku.query.explain" , Boolean .toString (connection .isWrapperFor (RolapConnection .class )));
112- } catch (Exception e ) {
113- props .put ("org.saiku.query.explain" , Boolean .toString (false ));
114- }
115- return props ;
116- }
117-
118- public String toXml () {
119- QuerySerializer qs = new QuerySerializer (this );
120- return qs .createXML ();
121- }
122-
123- public Boolean isDrillThroughEnabled () {
124- try {
125- Cube cube = getCube ();
126- return (cube != null && cube .isDrillThroughEnabled ());
127- } catch (Exception e ) {
128- e .printStackTrace ();
129- };
130- return false ;
131- }
101+
102+ public void setProperties (Properties props ) {
103+ this .properties .putAll (props );
104+ }
105+
106+ public Properties getProperties () {
107+ Properties props = this .properties ;
108+ props .put (QueryProperties .KEY_IS_DRILLTHROUGH , isDrillThroughEnabled ().toString ());
109+ props .put ("org.saiku.connection.scenario" , Boolean .toString (false ));
110+ try {
111+ props .put ("org.saiku.query.explain" , Boolean .toString (connection .isWrapperFor (RolapConnection .class )));
112+ } catch (Exception e ) {
113+ props .put ("org.saiku.query.explain" , Boolean .toString (false ));
114+ }
115+ return props ;
116+ }
117+
118+ public String toXml () {
119+ QuerySerializer qs = new QuerySerializer (this );
120+ return qs .createXML ();
121+ }
122+
123+ public Boolean isDrillThroughEnabled () {
124+ try {
125+ Cube cube = getCube ();
126+ return (cube != null && cube .isDrillThroughEnabled ());
127+ } catch (Exception e ) {
128+ e .printStackTrace ();
129+ };
130+ return false ;
131+ }
132132
133133 public CellSet execute () throws Exception {
134- OlapConnection con = connection ;
135- con .setCatalog (getSaikuCube ().getCatalogName ());
136- OlapStatement stmt = con .createStatement ();
137- this .statement = stmt ;
138- CellSet cs = stmt .executeOlapQuery (mdx );
139- if (statement != null ) {
140- statement .close ();
134+ try {
135+ if (statement != null ) {
136+ statement .close ();
137+ statement = null ;
138+ }
139+
140+ OlapConnection con = connection ;
141+ con .setCatalog (getSaikuCube ().getCatalogName ());
142+ OlapStatement stmt = con .createStatement ();
143+ this .statement = stmt ;
144+ CellSet cs = stmt .executeOlapQuery (mdx );
145+
146+ return cs ;
147+ } finally {
148+ if (statement != null ) {
149+ statement .close ();
150+ statement = null ;
151+ }
141152 }
142- this .statement = null ;
143- return cs ;
144153 }
145154
146155 public QueryType getType () {
@@ -164,47 +173,49 @@ public QueryAxis getAxis(String name) throws SaikuOlapException {
164173 }
165174
166175 public Cube getCube () {
167- final MdxParserFactory parserFactory =
168- connection .getParserFactory ();
169- MdxParser mdxParser =
170- parserFactory .createMdxParser (connection );
171- MdxValidator mdxValidator =
172- parserFactory .createMdxValidator (connection );
173-
174- String mdx = getMdx ();
175- try {
176-
177- if (mdx != null && mdx .length () > 0 && mdx .toUpperCase ().contains ("FROM" )) {
178- SelectNode select =
179- mdxParser .parseSelect (getMdx ());
180- select = mdxValidator .validateSelect (select );
181- CubeType cubeType = (CubeType ) select .getFrom ().getType ();
182- return cubeType .getCube ();
183- }
184- } catch (Exception e ) {
185- log .debug ("Parsing MDX to get the Cube failed. Using fallback scenario." , e );
186- }
187- try {
188- // ok seems like we failed to get the cube, lets try it differently
189- if (connection != null && mdx != null && mdx .length () > 0 ) {
190- for (Database db : connection .getOlapDatabases ()) {
191- Catalog cat = db .getCatalogs ().get (cube .getCatalogName ());
192- if (cat != null ) {
193- for (Schema schema : cat .getSchemas ()) {
194- for (Cube cub : schema .getCubes ()) {
195- if (cub .getName ().equals (cube .getName ()) || cub .getUniqueName ().equals (cube .getName ())) {
196- return cub ;
197- }
198- }
199- }
200- }
201- }
202- }
203- } catch (OlapException e ) {
204- e .printStackTrace ();
176+ final MdxParserFactory parserFactory =
177+ connection .getParserFactory ();
178+ MdxParser mdxParser =
179+ parserFactory .createMdxParser (connection );
180+ MdxValidator mdxValidator =
181+ parserFactory .createMdxValidator (connection );
182+
183+ String mdx = getMdx ();
184+ try {
185+
186+ if (mdx != null && mdx .length () > 0 && mdx .toUpperCase ().contains ("FROM" )) {
187+ SelectNode select =
188+ mdxParser .parseSelect (getMdx ());
189+ select = mdxValidator .validateSelect (select );
190+ CubeType cubeType = (CubeType ) select .getFrom ().getType ();
191+ return cubeType .getCube ();
192+ }
193+ } catch (Exception e ) {
194+ log .debug ("Parsing MDX to get the Cube failed. Using fallback scenario." , e );
195+ } finally {
196+ mdxValidator = null ;
197+ }
198+ try {
199+ // ok seems like we failed to get the cube, lets try it differently
200+ if (connection != null && mdx != null && mdx .length () > 0 ) {
201+ for (Database db : connection .getOlapDatabases ()) {
202+ Catalog cat = db .getCatalogs ().get (cube .getCatalogName ());
203+ if (cat != null ) {
204+ for (Schema schema : cat .getSchemas ()) {
205+ for (Cube cub : schema .getCubes ()) {
206+ if (cub .getName ().equals (cube .getName ()) || cub .getUniqueName ().equals (cube .getName ())) {
207+ return cub ;
208+ }
209+ }
210+ }
211+ }
212+ }
213+ }
214+ } catch (OlapException e ) {
215+ e .printStackTrace ();
205216 }
206217
207-
218+
208219 return null ;
209220 }
210221
@@ -231,15 +242,15 @@ public void resetAxisSelections(QueryAxis axis) {
231242 public void clearAllQuerySelections () {
232243 throw new UnsupportedOperationException ();
233244 }
234-
245+
235246 public void clearAxis (String axisName ) throws SaikuOlapException {
236247 throw new UnsupportedOperationException ();
237248 }
238249
239250 public void setScenario (Scenario scenario ) {
240251 this .scenario = scenario ;
241252 }
242-
253+
243254 public Scenario getScenario () {
244255 return scenario ;
245256 }
@@ -257,7 +268,7 @@ public void removeTag() {
257268
258269 public void storeCellset (CellSet cs ) {
259270 this .cellset = cs ;
260-
271+
261272 }
262273
263274 public CellSet getCellset () {
@@ -266,13 +277,13 @@ public CellSet getCellset() {
266277
267278 public void setStatement (OlapStatement os ) {
268279 this .statement = os ;
269-
280+
270281 }
271282
272283 public OlapStatement getStatement () {
273284 return this .statement ;
274285 }
275-
286+
276287 public void cancel () throws Exception {
277288 if (this .statement != null && !this .statement .isClosed ()) {
278289 statement .close ();
0 commit comments