001: /*********************************************************************************
002: * The contents of this file are subject to the OpenI Public License Version 1.0
003: * ("License"); You may not use this file except in compliance with the
004: * License. You may obtain a copy of the License at
005: * http://www.openi.org/docs/LICENSE.txt
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is: OpenI Open Source
012: *
013: * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
014: * Portions created by Loyalty Matrix, Inc. are
015: * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
016: *
017: * Contributor(s): ______________________________________.
018: *
019: ********************************************************************************/package org.openi.chart;
020:
021: import com.tonbeller.jpivot.olap.model.Dimension;
022: import com.tonbeller.jpivot.olap.model.Member;
023: import com.tonbeller.jpivot.olap.model.OlapException;
024: import com.tonbeller.jpivot.olap.model.OlapModel;
025: import com.tonbeller.jpivot.olap.model.Position;
026: import com.tonbeller.jpivot.xmla.XMLA_Hierarchy;
027: import com.tonbeller.jpivot.xmla.XMLA_Memento;
028: import org.apache.log4j.Logger;
029: import java.util.List;
030: import java.util.regex.Matcher;
031: import java.util.regex.Pattern;
032:
033: /**
034: * @author plucas
035: *
036: * TODO To change the template for this generated type comment go to
037: * Window - Preferences - Java - Code Style - Code Templates
038: */
039: public class AxisLabelGenerator {
040: private static Logger logger = Logger
041: .getLogger(AxisLabelGenerator.class);
042: private String vertAxisLabel;
043: private String horizAxisLabel;
044:
045: public AxisLabelGenerator() {
046: logger.debug("ctor");
047: }
048:
049: /**
050: * @throws OlapException
051: *
052: */
053: public void buildAxisLabel(OlapModel olapModel)
054: throws OlapException {
055: logger.debug("beginning buildAxisLabel");
056:
057: long startTime = System.currentTimeMillis();
058:
059: String mdxQuery = null;
060: String yLabel = null;
061:
062: //search measure in filter
063: if (olapModel.getBookmarkState(0) instanceof XMLA_Memento) {
064: logger.debug("found an xmla memento");
065:
066: XMLA_Memento olapMem = (XMLA_Memento) olapModel
067: .getBookmarkState(0);
068: mdxQuery = olapMem.getMdxQuery();
069: }
070:
071: if (mdxQuery == null) {
072: logger.debug("mdxQuery is null");
073:
074: return;
075: }
076:
077: Pattern p = Pattern.compile("WHERE *( *.*Measures.*)",
078: Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
079: Matcher m = p.matcher(mdxQuery);
080:
081: if (m.find()) {
082: mdxQuery = m.group();
083:
084: String[] parts = mdxQuery.split("\\[");
085:
086: for (int i = 0; i < parts.length; i++) {
087: if (parts[i].toUpperCase().indexOf("MEASURES") != -1) {
088: yLabel = "";
089:
090: String temp = parts[i + 1];
091:
092: for (int j = 0; j < temp.length(); j++) {
093: char c = temp.charAt(j);
094:
095: if ((c == '(') || (c == ')') || (c == '[')
096: || (c == ']') || (c == '.')
097: || (c == ',')) {
098: continue;
099: }
100:
101: yLabel = yLabel + String.valueOf(c);
102: }
103:
104: break;
105: }
106: }
107: }
108:
109: //Search measure in rows
110: if (yLabel == null) {
111: int dimIndex = 0;
112: int memberIndex = 0;
113: int count = 0;
114:
115: List rowPositions = olapModel.getResult().getAxes()[1]
116: .getPositions();
117: int rowCount = rowPositions.size();
118:
119: String lastMember = "";
120:
121: for (int i = 0; i < rowCount; i++) {
122: Position pos = (Position) rowPositions.get(i);
123: Member[] rowMembers = pos.getMembers();
124:
125: for (int j = 0; j < rowMembers.length; j++) {
126: try {
127: if (rowMembers[j].getLevel().getHierarchy()
128: .getDimension().isMeasure()) {
129: if (!lastMember
130: .equalsIgnoreCase(rowMembers[j]
131: .getLabel())) {
132: count++;
133: dimIndex = i;
134: memberIndex = j;
135: lastMember = rowMembers[j].getLabel();
136: }
137: }
138: } catch (Exception ex) {
139: logger.error(ex);
140: }
141: }
142: }
143:
144: // build the label name
145: if (count == 1) {
146: yLabel = ((Position) rowPositions.get(dimIndex))
147: .getMembers()[memberIndex].getLabel();
148: } else if (count == 0) {
149: yLabel = null;
150: } else {
151: yLabel = "";
152: }
153: }
154:
155: //Search measure in cols
156: if (yLabel == null) {
157: int dimIndex = 0;
158: int memberIndex = 0;
159: int count = 0;
160:
161: List columnPositions = olapModel.getResult().getAxes()[0]
162: .getPositions();
163:
164: int colCount = columnPositions.size();
165:
166: String lastMember = "";
167:
168: for (int i = 0; i < colCount; i++) {
169: Position pos = (Position) columnPositions.get(i);
170: Member[] colMembers = pos.getMembers();
171:
172: for (int j = 0; j < colMembers.length; j++) {
173: try {
174: if (colMembers[j].getLevel().getHierarchy()
175: .getDimension().isMeasure()) {
176: if (!lastMember
177: .equalsIgnoreCase(colMembers[j]
178: .getLabel())) {
179: count++;
180: dimIndex = i;
181: memberIndex = j;
182: lastMember = colMembers[j].getLabel();
183: }
184: }
185: } catch (Exception ex) {
186: logger.error(ex);
187: }
188: }
189: }
190:
191: // build the label name
192: if (count == 1) { // && errorFlag==false) {
193: yLabel = ((Position) columnPositions.get(dimIndex))
194: .getMembers()[memberIndex].getLabel();
195: } else if (count == 0) {
196: yLabel = null;
197: } else {
198: yLabel = "";
199: }
200: }
201:
202: //use default
203: if (yLabel == null) {
204: Dimension[] dims = olapModel.getDimensions();
205:
206: for (int i = 0; i < dims.length; i++) {
207: if (dims[i].isMeasure()) {
208: String temp = ((XMLA_Hierarchy) dims[i]
209: .getHierarchies()[0]).getDefaultMember();
210: temp = temp.substring(temp.indexOf(".") + 1);
211:
212: yLabel = "";
213:
214: for (int j = 0; j < temp.length(); j++) {
215: char c = temp.charAt(j);
216:
217: if ((c == '(') || (c == ')') || (c == '[')
218: || (c == ']') || (c == '.')) {
219: continue;
220: }
221:
222: yLabel = yLabel + String.valueOf(c);
223: }
224:
225: break;
226: }
227: }
228: }
229:
230: String xLabel = olapModel.getResult().getAxes()[1]
231: .getHierarchies()[0].getLabel();
232:
233: this .setVertAxisLabel(yLabel);
234: this .setHorizAxisLabel(xLabel);
235:
236: logger.info("buildAxisLabel completed in "
237: + (System.currentTimeMillis() - startTime) + " ms");
238: }
239:
240: /**
241: * @return Returns the horizAxisLabel.
242: */
243: public String getHorizAxisLabel(OlapModel olapModel)
244: throws OlapException {
245: if (this .horizAxisLabel == null) {
246: buildAxisLabel(olapModel);
247: }
248:
249: return horizAxisLabel;
250: }
251:
252: /**
253: * @return Returns the vertAxisLabel.
254: */
255: public String getVertAxisLabel(OlapModel olapModel)
256: throws OlapException {
257: if (this .vertAxisLabel == null) {
258: buildAxisLabel(olapModel);
259: }
260:
261: return vertAxisLabel;
262: }
263:
264: /**
265: * @param horizAxisLabel The horizAxisLabel to set.
266: */
267: private void setHorizAxisLabel(String horizAxisLabel) {
268: this .horizAxisLabel = horizAxisLabel;
269: }
270:
271: /**
272: * @param vertAxisLabel The vertAxisLabel to set.
273: */
274: private void setVertAxisLabel(String vertAxisLabel) {
275: this.vertAxisLabel = vertAxisLabel;
276: }
277: }
|