1 package com.eyeq.pivot4j.pentaho.datasource;
2
3 import java.util.Iterator;
4 import java.util.LinkedList;
5 import java.util.List;
6 import java.util.Properties;
7
8 import mondrian.olap.Util;
9 import mondrian.olap4j.MondrianOlap4jDriver;
10 import mondrian.util.Pair;
11
12 import org.apache.commons.configuration.HierarchicalConfiguration;
13 import org.apache.commons.lang.NullArgumentException;
14 import org.olap4j.OlapDataSource;
15 import org.pentaho.platform.api.engine.IPentahoSession;
16 import org.pentaho.platform.engine.core.system.PentahoSessionHolder;
17 import org.pentaho.platform.engine.core.system.PentahoSystem;
18 import org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService;
19 import org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog;
20 import org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCube;
21 import org.pentaho.platform.util.messages.LocaleHelper;
22 import org.slf4j.Logger;
23
24 import com.eyeq.pivot4j.analytics.datasource.AbstractDataSourceManager;
25 import com.eyeq.pivot4j.analytics.datasource.CatalogInfo;
26 import com.eyeq.pivot4j.analytics.datasource.ConnectionInfo;
27 import com.eyeq.pivot4j.analytics.datasource.CubeInfo;
28
29 public class PentahoDataSourceManager extends
30 AbstractDataSourceManager<PentahoDataSourceDefinition> {
31
32 private IPentahoSession session;
33
34 private IMondrianCatalogService catalogService;
35
36
37
38
39 @Override
40 protected void initialize() {
41 this.session = PentahoSessionHolder.getSession();
42 this.catalogService = PentahoSystem.get(IMondrianCatalogService.class,
43 session);
44
45 super.initialize();
46 }
47
48
49
50
51 @Override
52 protected void destroy() {
53 this.session = null;
54 this.catalogService = null;
55 }
56
57
58
59
60 protected IPentahoSession getSession() {
61 return session;
62 }
63
64
65
66
67 protected IMondrianCatalogService getCatalogService() {
68 return catalogService;
69 }
70
71
72
73
74 @Override
75 public List<CatalogInfo> getCatalogs() {
76 List<MondrianCatalog> catalogs = catalogService.listCatalogs(session,
77 false);
78
79 List<CatalogInfo> result = new LinkedList<CatalogInfo>();
80
81 for (MondrianCatalog catalog : catalogs) {
82 result.add(new CatalogInfo(catalog.getName(), catalog.getName(),
83 catalog.getDefinition()));
84 }
85
86 return result;
87 }
88
89
90
91
92
93 public MondrianCatalog getCatalog(String name) {
94 return catalogService.getCatalog(name, session);
95 }
96
97
98
99
100 @Override
101 public List<CubeInfo> getCubes(String catalogName) {
102 if (catalogName == null) {
103 throw new NullArgumentException("catalogName");
104 }
105
106 List<CubeInfo> cubes = new LinkedList<CubeInfo>();
107
108 MondrianCatalog catalog = getCatalog(catalogName);
109
110 if (catalog == null) {
111 throw new IllegalArgumentException(
112 "The catalog with the given name does not exist : "
113 + catalogName);
114 }
115
116 List<MondrianCube> mondrianCubes = catalog.getSchema().getCubes();
117
118 for (MondrianCube cube : mondrianCubes) {
119 cubes.add(new CubeInfo(cube.getId(), cube.getName(), cube.getName()));
120 }
121
122 return cubes;
123 }
124
125
126
127
128 @Override
129 protected void registerDefinitions() {
130 List<MondrianCatalog> catalogs = catalogService.listCatalogs(session,
131 false);
132
133 for (MondrianCatalog catalog : catalogs) {
134 registerDefinition(new PentahoDataSourceDefinition(catalog));
135 }
136 }
137
138
139
140
141 @Override
142 protected synchronized PentahoDataSourceDefinition getDefinition(
143 ConnectionInfo connectionInfo) {
144 PentahoDataSourceDefinition definition = super
145 .getDefinition(connectionInfo);
146
147 if (definition == null) {
148 MondrianCatalog catalog = catalogService.getCatalog(
149 connectionInfo.getCatalogName(), session);
150
151 if (catalog != null) {
152 definition = new PentahoDataSourceDefinition(catalog);
153 registerDefinition(definition);
154 }
155 }
156
157 return definition;
158 }
159
160
161
162
163 @Override
164 protected PentahoDataSourceDefinition createDataSourceDefinition(
165 HierarchicalConfiguration configuration) {
166 return null;
167 }
168
169
170
171
172 @Override
173 protected OlapDataSource createDataSource(
174 PentahoDataSourceDefinition definition) {
175 if (definition == null) {
176 return null;
177 }
178
179 MondrianCatalog catalog = getCatalog(definition.getName());
180
181 if (catalog == null) {
182 Logger logger = getLogger();
183 if (logger.isWarnEnabled()) {
184 logger.warn("Unable to find catalog with name : "
185 + definition.getName());
186 }
187
188 return null;
189 }
190
191 Util.PropertyList parsedProperties = Util.parseConnectString(catalog
192 .getDataSourceInfo());
193
194 StringBuilder builder = new StringBuilder();
195 builder.append("jdbc:mondrian:");
196 builder.append("Catalog=");
197 builder.append(catalog.getDefinition());
198 builder.append("; ");
199
200 Iterator<Pair<String, String>> it = parsedProperties.iterator();
201
202 while (it.hasNext()) {
203 Pair<String, String> pair = it.next();
204 builder.append(pair.getKey());
205 builder.append("=");
206 builder.append(pair.getValue());
207 builder.append("; ");
208 }
209
210 builder.append("PoolNeeded=false; ");
211 builder.append("Locale=");
212 builder.append(LocaleHelper.getLocale().toString());
213 builder.append(";");
214
215 String url = builder.toString();
216
217 Properties properties = new Properties();
218 properties.put("url", url);
219 properties.put("driver", MondrianOlap4jDriver.class.getName());
220
221 Logger logger = getLogger();
222 if (logger.isInfoEnabled()) {
223 logger.info("Using connection URL : " + url);
224 }
225
226 return new MdxOlap4JDataSource(session, properties);
227 }
228 }