1 20 package org.openi.chart; 21 22 import com.tonbeller.jpivot.olap.model.Dimension; 23 import com.tonbeller.jpivot.olap.model.Member; 24 import com.tonbeller.jpivot.olap.model.OlapException; 25 import com.tonbeller.jpivot.olap.model.OlapModel; 26 import com.tonbeller.jpivot.olap.model.Position; 27 import com.tonbeller.jpivot.xmla.XMLA_Hierarchy; 28 import com.tonbeller.jpivot.xmla.XMLA_Memento; 29 import org.apache.log4j.Logger; 30 import java.util.List ; 31 import java.util.regex.Matcher ; 32 import java.util.regex.Pattern ; 33 34 35 41 public class AxisLabelGenerator { 42 private static Logger logger = Logger.getLogger(AxisLabelGenerator.class); 43 private String vertAxisLabel; 44 private String horizAxisLabel; 45 46 public AxisLabelGenerator() { 47 logger.debug("ctor"); 48 } 49 50 54 public void buildAxisLabel(OlapModel olapModel) throws OlapException { 55 logger.debug("beginning buildAxisLabel"); 56 57 long startTime = System.currentTimeMillis(); 58 59 String mdxQuery = null; 60 String yLabel = null; 61 62 if (olapModel.getBookmarkState(0) instanceof XMLA_Memento) { 64 logger.debug("found an xmla memento"); 65 66 XMLA_Memento olapMem = (XMLA_Memento) olapModel.getBookmarkState(0); 67 mdxQuery = olapMem.getMdxQuery(); 68 } 69 70 if (mdxQuery == null) { 71 logger.debug("mdxQuery is null"); 72 73 return; 74 } 75 76 Pattern p = Pattern.compile("WHERE *( *.*Measures.*)", 77 Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); 78 Matcher m = p.matcher(mdxQuery); 79 80 if (m.find()) { 81 mdxQuery = m.group(); 82 83 String [] parts = mdxQuery.split("\\["); 84 85 for (int i = 0; i < parts.length; i++) { 86 if (parts[i].toUpperCase().indexOf("MEASURES") != -1) { 87 yLabel = ""; 88 89 String temp = parts[i + 1]; 90 91 for (int j = 0; j < temp.length(); j++) { 92 char c = temp.charAt(j); 93 94 if ((c == '(') || (c == ')') || (c == '[') 95 || (c == ']') || (c == '.') || (c == ',')) { 96 continue; 97 } 98 99 yLabel = yLabel + String.valueOf(c); 100 } 101 102 break; 103 } 104 } 105 } 106 107 if (yLabel == null) { 109 int dimIndex = 0; 110 int memberIndex = 0; 111 int count = 0; 112 113 List rowPositions = olapModel.getResult().getAxes()[1].getPositions(); 114 int rowCount = rowPositions.size(); 115 116 String lastMember = ""; 117 118 for (int i = 0; i < rowCount; i++) { 119 Position pos = (Position) rowPositions.get(i); 120 Member[] rowMembers = pos.getMembers(); 121 122 for (int j = 0; j < rowMembers.length; j++) { 123 try { 124 if (rowMembers[j].getLevel().getHierarchy() 125 .getDimension().isMeasure()) { 126 if (!lastMember.equalsIgnoreCase( 127 rowMembers[j].getLabel())) { 128 count++; 129 dimIndex = i; 130 memberIndex = j; 131 lastMember = rowMembers[j].getLabel(); 132 } 133 } 134 } catch (Exception ex) { 135 logger.error(ex); 136 } 137 } 138 } 139 140 if (count == 1) { 142 yLabel = ((Position) rowPositions.get(dimIndex)).getMembers()[memberIndex] 143 .getLabel(); 144 } else if (count == 0) { 145 yLabel = null; 146 } else { 147 yLabel = ""; 148 } 149 } 150 151 if (yLabel == null) { 153 int dimIndex = 0; 154 int memberIndex = 0; 155 int count = 0; 156 157 List columnPositions = olapModel.getResult().getAxes()[0] 158 .getPositions(); 159 160 int colCount = columnPositions.size(); 161 162 String lastMember = ""; 163 164 for (int i = 0; i < colCount; i++) { 165 Position pos = (Position) columnPositions.get(i); 166 Member[] colMembers = pos.getMembers(); 167 168 for (int j = 0; j < colMembers.length; j++) { 169 try { 170 if (colMembers[j].getLevel().getHierarchy() 171 .getDimension().isMeasure()) { 172 if (!lastMember.equalsIgnoreCase( 173 colMembers[j].getLabel())) { 174 count++; 175 dimIndex = i; 176 memberIndex = j; 177 lastMember = colMembers[j].getLabel(); 178 } 179 } 180 } catch (Exception ex) { 181 logger.error(ex); 182 } 183 } 184 } 185 186 if (count == 1) { yLabel = ((Position) columnPositions.get(dimIndex)) 189 .getMembers()[memberIndex].getLabel(); 190 } else if (count == 0) { 191 yLabel = null; 192 } else { 193 yLabel = ""; 194 } 195 } 196 197 if (yLabel == null) { 199 Dimension[] dims = olapModel.getDimensions(); 200 201 for (int i = 0; i < dims.length; i++) { 202 if (dims[i].isMeasure()) { 203 String temp = ((XMLA_Hierarchy) dims[i] 204 .getHierarchies()[0]).getDefaultMember(); 205 temp = temp.substring(temp.indexOf(".") + 1); 206 207 yLabel = ""; 208 209 for (int j = 0; j < temp.length(); j++) { 210 char c = temp.charAt(j); 211 212 if ((c == '(') || (c == ')') || (c == '[') 213 || (c == ']') || (c == '.')) { 214 continue; 215 } 216 217 yLabel = yLabel + String.valueOf(c); 218 } 219 220 break; 221 } 222 } 223 } 224 225 String xLabel = olapModel.getResult().getAxes()[1] 226 .getHierarchies()[0].getLabel(); 227 228 this.setVertAxisLabel(yLabel); 229 this.setHorizAxisLabel(xLabel); 230 231 logger.info("buildAxisLabel completed in " 232 + (System.currentTimeMillis() - startTime) + " ms"); 233 } 234 235 238 public String getHorizAxisLabel(OlapModel olapModel) 239 throws OlapException { 240 if (this.horizAxisLabel == null) { 241 buildAxisLabel(olapModel); 242 } 243 244 return horizAxisLabel; 245 } 246 247 250 public String getVertAxisLabel(OlapModel olapModel) 251 throws OlapException { 252 if (this.vertAxisLabel == null) { 253 buildAxisLabel(olapModel); 254 } 255 256 return vertAxisLabel; 257 } 258 259 262 private void setHorizAxisLabel(String horizAxisLabel) { 263 this.horizAxisLabel = horizAxisLabel; 264 } 265 266 269 private void setVertAxisLabel(String vertAxisLabel) { 270 this.vertAxisLabel = vertAxisLabel; 271 } 272 } 273 | Popular Tags |