KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > xmla > XmlaConnector


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.xmla;
21
22 import com.tonbeller.jpivot.core.ModelFactory;
23 import com.tonbeller.jpivot.olap.model.Dimension;
24 import com.tonbeller.jpivot.olap.model.OlapException;
25 import com.tonbeller.jpivot.olap.model.OlapItem;
26 import com.tonbeller.jpivot.xmla.XMLA_Dimension;
27 import com.tonbeller.jpivot.xmla.XMLA_Model;
28 import com.tonbeller.jpivot.xmla.XMLA_SOAP;
29
30 import org.apache.log4j.Logger;
31 import org.openi.analysis.Datasource;
32 import org.openi.application.Application;
33 import org.xml.sax.SAXException JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.LinkedList JavaDoc;
39 import java.util.List JavaDoc;
40
41
42 /**
43  *
44  * does the work of connecting to an xmla server, queries, parses results.
45  * Goal is to isolate from jpivot xmla_soap
46  */

47 public class XmlaConnector {
48     private static Logger logger = Logger.getLogger(XmlaConnector.class);
49
50     /**
51      * Constructor
52      * @param arg0
53      */

54     public XmlaConnector() {
55         // super(arg0);
56
// Util.setupLog4j();
57
}
58
59     public XMLA_Model query(String JavaDoc xmlaUri, String JavaDoc catalog, String JavaDoc mdxQuery)
60         throws IOException JavaDoc, OlapException, SAXException JavaDoc {
61         long start = System.currentTimeMillis();
62         URL JavaDoc confUrl = XMLA_Model.class.getResource("config.xml");
63         XMLA_Model model = (XMLA_Model) ModelFactory.instance(confUrl);
64
65         model.setMdxQuery(mdxQuery);
66         model.setUri(xmlaUri);
67         model.setCatalog(catalog);
68         
69         // initialize executes a query to the xmla provider
70
model.initialize();
71
72         return model;
73     }
74
75     /**
76      * returns cube list from the olap server using XMLA_SOAP's discoverCube method
77      * @param server String
78      * @param catalog String
79      * @param user String
80      * @param pwd String
81      * @return List
82      * @throws OlapException
83      * @throws Exception
84      */

85     public List JavaDoc getCubeList(String JavaDoc uri, String JavaDoc catalog, String JavaDoc user,
86         String JavaDoc pwd) throws OlapException {
87         XMLA_SOAP olap;
88
89         List JavaDoc cubeList = null;
90
91         if (Application.getInstance().isBasicAuthentication()) {
92             olap = new XMLA_SOAP(uri, user, pwd);
93         } else {
94             olap = new XMLA_SOAP(uri, "", "");
95         }
96
97         // filter perspectives from cube list
98
cubeList = getCubesWithoutPerspectives(catalog, olap);
99
100         return cubeList;
101     }
102     
103     /**
104      * Returns a List of dimension names as Strings
105      * Includes the Measures!
106      *
107      * @param uri
108      * @param catalog
109      * @param cubeName
110      * @param user
111      * @param pwd
112      * @return
113      * @throws OlapException
114      */

115     public List JavaDoc getDimensionList(String JavaDoc uri, String JavaDoc catalog, String JavaDoc cubeName, String JavaDoc user, String JavaDoc pwd) throws OlapException{
116         List JavaDoc dimensions = new ArrayList JavaDoc();
117         XMLA_SOAP olap = new XMLA_SOAP(uri, user, pwd);
118         Iterator JavaDoc olapItems = olap.discoverDim(catalog, cubeName).iterator();
119         while(olapItems.hasNext()){
120             OlapItem item = (OlapItem)olapItems.next();
121             dimensions.add(item.getName());
122         }
123         
124         return dimensions;
125     }
126     
127     /**
128      * SSAS 2005 uses concept of Perspectives
129      * (http://msdn2.microsoft.com/en-us/library/ms167223.aspx) for a cube.
130      * Discover Cube SOAP request retrieves perspectives along with cubes.
131      * Since dimensions used in a cube may not available in a perspective for the cube,
132      * when a perspective is selected from the list of cubes in new analysis,
133      * some members/levels are not found and exception is thrown. This method
134      * filter perspectives.
135      *
136      * @param catalog String
137      * @param olap XMLA_SOAP
138      * @return List
139      */

140     private List JavaDoc getCubesWithoutPerspectives(String JavaDoc catalog, XMLA_SOAP olap)
141             throws OlapException {
142
143         List JavaDoc cubesWithoutPerspectives = null;
144
145         if (olap != null) {
146             Iterator JavaDoc iterator = olap.discoverCube(catalog).iterator();
147             cubesWithoutPerspectives = new ArrayList JavaDoc();
148
149             while (iterator.hasNext()) {
150                 OlapItem item = (OlapItem) iterator.next();
151                 
152                 // perspective has BASE_CUBE_NAME property
153
if (item.getProperty("BASE_CUBE_NAME") != null
154                         && ! "".equalsIgnoreCase(item
155                                 .getProperty("BASE_CUBE_NAME"))) {
156                     continue;
157                 }
158
159                 cubesWithoutPerspectives.add(item.getName());
160             }
161         }
162
163         return cubesWithoutPerspectives;
164     }
165     
166     public List JavaDoc discoverHier(String JavaDoc uri, String JavaDoc catalog, String JavaDoc user, String JavaDoc pwd, String JavaDoc cube, String JavaDoc dimension) throws OlapException {
167         XMLA_SOAP xmla = new XMLA_SOAP(uri,
168                 user, pwd);
169         
170         List JavaDoc hierarchies = new LinkedList JavaDoc();
171         Iterator JavaDoc hiers = xmla.discoverHier(catalog, cube, "[" + dimension + "]").iterator();
172
173         while ((hiers != null) && hiers.hasNext()) {
174             OlapItem currentHierarchy = (OlapItem) hiers.next();
175             String JavaDoc parsedName = currentHierarchy.getName().replaceAll("\\[", "");
176             parsedName = parsedName.replaceAll("\\[", "");
177             hierarchies.add(parsedName);
178         }
179         return hierarchies;
180
181     }
182     
183     public String JavaDoc createDefaultMdx(String JavaDoc uri, String JavaDoc catalog, String JavaDoc user, String JavaDoc pwd, String JavaDoc cube, String JavaDoc dimension) throws OlapException{
184         XmlaConnector xmla = new XmlaConnector();
185         String JavaDoc firstHier = "";
186         String JavaDoc mdxQuery = "";
187         if (!"Measures".equalsIgnoreCase(dimension)) {
188             List JavaDoc hiers = xmla.discoverHier(uri, catalog, user, pwd, cube, dimension);
189             
190             if ((hiers != null) && (hiers.size() > 1)) {
191                 firstHier = (String JavaDoc) hiers.get(0);
192
193                 if (firstHier.startsWith("[") && firstHier.endsWith("]")) {
194                     firstHier = "." + firstHier;
195                 } else {
196                     firstHier = ".[" + firstHier + "]";
197                 }
198             }
199
200             mdxQuery = "SELECT {[Measures].DefaultMember} on columns, {[" +
201                 dimension + "]" + firstHier + ".Children} on rows FROM [" +
202                 cube + "]";
203             logger.debug("MDX generated as :" + mdxQuery);
204         }
205         return mdxQuery;
206     }
207
208
209     
210 }
211
Popular Tags