KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > chart > AxisLabelGenerator


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.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 JavaDoc;
31 import java.util.regex.Matcher JavaDoc;
32 import java.util.regex.Pattern JavaDoc;
33
34
35 /**
36  * @author plucas
37  *
38  * TODO To change the template for this generated type comment go to
39  * Window - Preferences - Java - Code Style - Code Templates
40  */

41 public class AxisLabelGenerator {
42     private static Logger logger = Logger.getLogger(AxisLabelGenerator.class);
43     private String JavaDoc vertAxisLabel;
44     private String JavaDoc horizAxisLabel;
45
46     public AxisLabelGenerator() {
47         logger.debug("ctor");
48     }
49
50     /**
51      * @throws OlapException
52      *
53      */

54     public void buildAxisLabel(OlapModel olapModel) throws OlapException {
55         logger.debug("beginning buildAxisLabel");
56
57         long startTime = System.currentTimeMillis();
58
59         String JavaDoc mdxQuery = null;
60         String JavaDoc yLabel = null;
61
62         //search measure in filter
63
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 JavaDoc p = Pattern.compile("WHERE *( *.*Measures.*)",
77                 Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
78         Matcher JavaDoc m = p.matcher(mdxQuery);
79
80         if (m.find()) {
81             mdxQuery = m.group();
82
83             String JavaDoc[] 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 JavaDoc 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         //Search measure in rows
108
if (yLabel == null) {
109             int dimIndex = 0;
110             int memberIndex = 0;
111             int count = 0;
112
113             List JavaDoc rowPositions = olapModel.getResult().getAxes()[1].getPositions();
114             int rowCount = rowPositions.size();
115
116             String JavaDoc 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 JavaDoc ex) {
135                         logger.error(ex);
136                     }
137                 }
138             }
139
140             // build the label name
141
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         //Search measure in cols
152
if (yLabel == null) {
153             int dimIndex = 0;
154             int memberIndex = 0;
155             int count = 0;
156
157             List JavaDoc columnPositions = olapModel.getResult().getAxes()[0]
158                     .getPositions();
159
160                 int colCount = columnPositions.size();
161
162                 String JavaDoc 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 JavaDoc ex) {
181                             logger.error(ex);
182                         }
183                     }
184                 }
185
186                 // build the label name
187
if (count == 1) { // && errorFlag==false) {
188
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                 //use default
198
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 JavaDoc 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 JavaDoc 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                     /**
236                      * @return Returns the horizAxisLabel.
237                      */

238                     public String JavaDoc getHorizAxisLabel(OlapModel olapModel)
239                         throws OlapException {
240                         if (this.horizAxisLabel == null) {
241                             buildAxisLabel(olapModel);
242                         }
243
244                         return horizAxisLabel;
245                     }
246
247                     /**
248                      * @return Returns the vertAxisLabel.
249                      */

250                     public String JavaDoc getVertAxisLabel(OlapModel olapModel)
251                         throws OlapException {
252                         if (this.vertAxisLabel == null) {
253                             buildAxisLabel(olapModel);
254                         }
255
256                         return vertAxisLabel;
257                     }
258
259                     /**
260                      * @param horizAxisLabel The horizAxisLabel to set.
261                      */

262                     private void setHorizAxisLabel(String JavaDoc horizAxisLabel) {
263                         this.horizAxisLabel = horizAxisLabel;
264                     }
265
266                     /**
267                      * @param vertAxisLabel The vertAxisLabel to set.
268                      */

269                     private void setVertAxisLabel(String JavaDoc vertAxisLabel) {
270                         this.vertAxisLabel = vertAxisLabel;
271                     }
272                 }
273
Popular Tags