1 4 package com.tonbeller.jpivot.xmla; 5 6 9 10 11 12 import java.util.ArrayList ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 import org.apache.log4j.Logger; 20 import com.tonbeller.wcf.table.AbstractTableModel; 21 import com.tonbeller.wcf.table.DefaultTableRow; 22 import com.tonbeller.wcf.table.TableRow; 23 import com.tonbeller.jpivot.olap.model.Axis; 24 import com.tonbeller.jpivot.olap.model.*; 25 29 30 public class XMLA_DrillThroughTableModel extends AbstractTableModel { 31 private static Logger logger = Logger.getLogger(XMLA_DrillThroughTableModel.class); 32 private String title = "Drill Through Table"; 33 private String caption = ""; 34 private String dataSourceName; 35 private int cellOrdinal; 36 39 public int getCellOrdinal() { 40 return cellOrdinal; 41 } 42 45 public void setCellOrdinal(int cellOrdinal) { 46 this.cellOrdinal = cellOrdinal; 47 } 48 XMLA_Model model; 49 XMLA_Model drillModel; 50 XMLA_Result drillResult; 51 54 public XMLA_Model getModel() { 55 return model; 56 } 57 60 public void setModel(XMLA_Model model) { 61 this.model = model; 62 } 63 65 private boolean ready = false; 66 67 private TableRow[] rows = new TableRow[0]; 68 private String [] columnTitles = new String [0]; 69 70 public XMLA_DrillThroughTableModel() { 71 } 72 73 public int getRowCount() { 74 if ( !ready ) { 75 executeQuery(); 76 } 77 return rows.length; 78 } 79 80 public TableRow getRow(int rowIndex) { 81 if ( !ready ) { 82 executeQuery(); 83 } 84 return rows[rowIndex]; 85 } 86 87 public String getTitle() { 88 return title; 89 } 90 91 94 public void setTitle(String title) { 95 this.title = title; 96 } 97 98 103 public int getColumnCount() { 104 if ( !ready ) { 105 executeQuery(); 106 } 107 return columnTitles.length; 108 } 109 110 public String getColumnTitle(int columnIndex) { 111 if ( !ready ) { 112 executeQuery(); 113 } 114 return columnTitles[columnIndex]; 115 } 116 117 private String buildDrillThroughMdx(XMLA_Result res) { 119 Axis[] axes = res.getAxes(); 120 StringBuffer mdxBuf = new StringBuffer (" Drillthrough Select "); 121 int nXPositions = axes[0].getPositions().size(); 122 int posIdx = 0; 123 for (int i = 0; i < axes.length; i++) { 124 125 if (i==0) { 128 posIdx = cellOrdinal - ((cellOrdinal / nXPositions) * nXPositions); 129 } else { 130 posIdx = cellOrdinal / nXPositions; 131 } 132 133 XMLA_Position position = (XMLA_Position) axes[i].getPositions().get(posIdx); 134 Member[] positionMembers = position.getMembers(); 135 if (i ==1) { 136 mdxBuf.append(","); 137 } 138 mdxBuf.append("{("); 139 for (int j = 0; j < positionMembers.length; j++) { 140 XMLA_Member member = (XMLA_Member)positionMembers[j]; 141 if (j>0) { 142 mdxBuf.append(","); 143 } 144 mdxBuf.append(member.getUniqueName()); 145 } 146 mdxBuf.append(")}"); 147 if (i == 0) { 148 mdxBuf.append(" on Columns"); 149 } else { 150 mdxBuf.append(" on Rows"); 151 152 } 153 } 154 155 mdxBuf.append(" from [" + this.model.getCube() +"]"); 156 157 Axis slicer = res.getSlicer(); 158 List slicerList = slicer.getPositions(); 160 XMLA_Position slicerPosition = (XMLA_Position) slicerList.get(0); 161 Member[] slicerMembers = slicerPosition.getMembers(); 162 for (int j = 0; j < slicerMembers.length; j++) { 163 XMLA_Member member = (XMLA_Member)slicerMembers[j]; 164 if (j == 0) { 165 mdxBuf.append(" Where ( "); 166 } else if (j>0) { 167 mdxBuf.append(" , "); 168 } 169 mdxBuf.append(member.getUniqueName()); 170 171 } 172 173 if (slicerMembers.length > 0 ) { 174 mdxBuf.append(")"); 175 176 } 177 return mdxBuf.toString(); 178 179 180 } 181 182 186 private void executeQuery() { 187 188 XMLA_Result res = null; 189 try { 190 res = (XMLA_Result) model.getResult(); 191 192 String mdx = buildDrillThroughMdx(res); 193 if (drillModel == null) { 194 drillModel = new XMLA_Model(); 195 drillModel.setCatalog(model.getCatalog()); 196 drillModel.setDataSource(model.getDataSource()); 197 drillModel.setMdxQuery(mdx); 198 drillModel.setID("Drill" + model.getID()); 199 drillModel.setUri(model.getUri()); 200 drillModel.setUser(model.getUser()); 201 drillModel.setPassword(model.getPassword()); 202 drillModel.initialize(); 204 } 205 206 207 drillResult = (XMLA_Result) drillModel.getDrillResult(); 208 209 Map headerMap = drillResult.getDrillHeader(); 213 int numCols = headerMap.size(); 214 columnTitles = new String [numCols]; 215 Set headerSet = headerMap.entrySet(); 216 Iterator headerSetIt = headerSet.iterator(); 217 while (headerSetIt.hasNext()) { 218 Map.Entry e = (Map.Entry ) headerSetIt.next();; 219 columnTitles[((Integer )e.getValue()).intValue()] = e.getKey().toString(); 220 } 221 222 List dataRows = drillResult.getDrillRows(); 224 List tempRows = new ArrayList (); 225 Iterator dataRowIt = dataRows.iterator(); 226 while (dataRowIt.hasNext()) { 227 Object [] row = new Object [numCols]; 228 Map columnMap = (HashMap )dataRowIt.next(); 229 Set columnSet = columnMap.entrySet(); 230 Iterator colSetIt = columnSet.iterator(); 231 while (colSetIt.hasNext()) { 232 Map.Entry e = (Map.Entry ) colSetIt.next(); 233 String value = e.getValue().toString(); 234 String colName = e.getKey().toString(); 235 int colNo = ((Integer )headerMap.get(colName)).intValue(); 236 row[colNo] = value; 237 } 238 tempRows.add(new DefaultTableRow(row)); 239 } 240 rows = (TableRow[]) tempRows.toArray(new TableRow[0]); 241 } catch (Exception e) { 242 e.printStackTrace(); 243 logger.error("?", e); 244 rows = new TableRow[1]; 245 columnTitles = new String [1]; 246 columnTitles[0] = "An error occured"; 247 Object [] row = new Object [1]; 248 row[0] = e.toString(); 249 rows[0] = new DefaultTableRow(row); 250 ready=false; 251 return; 252 } finally { 253 try { 254 } catch (Exception e1) { 255 } 257 } 258 ready = true; 259 } 260 261 264 public String getCaption() { 265 return caption; 266 } 267 268 271 public void setCaption(String caption) { 272 this.caption = caption; 273 } 274 275 278 public String getDataSourceName() { 279 return dataSourceName; 280 } 281 282 285 public void setDataSourceName(String string) { 286 dataSourceName = string; 287 } 288 289 } 290 291 | Popular Tags |