KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > core > ResultSetCrosstabComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12 */

13 package org.pentaho.plugin.core;
14
15 import java.text.DecimalFormat JavaDoc;
16 import java.text.Format JavaDoc;
17 import java.text.SimpleDateFormat JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.pentaho.core.connection.IPentahoResultSet;
23 import org.pentaho.core.connection.PentahoDataTransmuter;
24 import org.pentaho.messages.Messages;
25 import org.pentaho.plugin.ComponentBase;
26
27 public class ResultSetCrosstabComponent extends ComponentBase {
28
29   private static final long serialVersionUID = -868492439427599791L;
30   private static final String JavaDoc RESULT_SET = "result_set"; //$NON-NLS-1$
31
private static final String JavaDoc PIVOT_COLUMN = "pivot_column"; //$NON-NLS-1$
32
private static final String JavaDoc MEASURES_COLUMN = "measures_column"; //$NON-NLS-1$
33
private static final String JavaDoc FORMAT_TYPE = "format_type"; //$NON-NLS-1$
34
private static final String JavaDoc FORMAT_STRING = "format_string"; //$NON-NLS-1$
35
private static final String JavaDoc ORDERED_MAPS = "ordered_maps"; //$NON-NLS-1$
36
private static final String JavaDoc TRANSFORM_SORTBYCOL = "sort_by_col"; //$NON-NLS-1$
37
private static final String JavaDoc SORT_FORMAT_TYPE = "sort_format_type"; //$NON-NLS-1$
38
private static final String JavaDoc SORT_FORMAT_STRING = "sort_format_string"; //$NON-NLS-1$
39
private static final String JavaDoc OLD_STYLE_CROSSTAB = "non_ordered"; //$NON-NLS-1$
40
private static final String JavaDoc UNIQUE_ROW_IDENTIFIER_COLUMN = "unique_row_identifier_column"; //$NON-NLS-1$
41

42   public void done() {
43     // TODO Auto-generated method stub
44

45   }
46
47   protected boolean validateAction() {
48     if (!isDefinedInput(PIVOT_COLUMN)) {
49       error(Messages.getErrorString("ResultSetCrosstabComponent.ERROR_0001_PIVOT_COLUMN_IS_REQUIRED")); //$NON-NLS-1$
50
return false;
51     }
52     if (!isDefinedInput(MEASURES_COLUMN)) {
53       error(Messages.getErrorString("ResultSetCrosstabComponent.ERROR_0002_MEASURES_COLUMN_IS_REQUIRED")); //$NON-NLS-1$
54
return false;
55     }
56     if (isDefinedInput(FORMAT_TYPE)) {
57       if (!isDefinedInput(FORMAT_STRING)) {
58         error(Messages.getErrorString("ResultSetCrosstabComponent.ERROR_0003_FORMAT_PARAMETERS_BAD")); //$NON-NLS-1$
59
return false;
60       }
61     }
62     if (isDefinedInput(SORT_FORMAT_TYPE)) {
63       if (!isDefinedInput(SORT_FORMAT_STRING)) {
64         error(Messages.getErrorString("ResultSetCrosstabComponent.ERROR_0004_SORT_FORMAT_PARAMETERS_BAD")); //$NON-NLS-1$
65
return false;
66       }
67     }
68     return true;
69   }
70
71   protected boolean executeAction() throws Throwable JavaDoc {
72     Object JavaDoc resultSetObject = getInputValue(RESULT_SET);
73     String JavaDoc outputName = getResultOutputName();
74     if (outputName == null) {
75       return false;
76     }
77     if (resultSetObject instanceof IPentahoResultSet) {
78       int columnToPivot = Integer.parseInt(getInputStringValue(PIVOT_COLUMN));
79       int measuresColumn = Integer.parseInt(getInputStringValue(MEASURES_COLUMN));
80       String JavaDoc formatType = null;
81       String JavaDoc formatString = null;
82       if (isDefinedInput(FORMAT_TYPE)) {
83         formatType = getInputStringValue(FORMAT_TYPE);
84         formatString = getInputStringValue(FORMAT_STRING);
85       }
86       Format JavaDoc format = null;
87
88       // this is that rSet is now the valid result Create the format if there is one
89
if (formatType != null && formatType.length() > 0) {
90           if (StandardSettings.DECIMAL_FORMAT_TYPE.equalsIgnoreCase(formatType)) {
91               format = new DecimalFormat JavaDoc(formatString);
92           } else if (StandardSettings.DATE_FORMAT_TYPE.equalsIgnoreCase(formatType)) {
93               format = new SimpleDateFormat JavaDoc(formatString);
94           }
95       }
96       // transform rSet here
97
String JavaDoc orderedMaps = getInputStringValue(ORDERED_MAPS);
98       boolean orderOutputColumns = "true".equalsIgnoreCase(orderedMaps); //$NON-NLS-1$
99

100       // Sort-by column information
101
int transformSortByColumn = 0;
102       String JavaDoc sortColumn = getInputStringValue(TRANSFORM_SORTBYCOL);
103       if (sortColumn != null) {
104           transformSortByColumn = Integer.parseInt(sortColumn);
105       }
106       
107       //
108
// Column that uniquely identifies a row
109
// If supplied, will allow input rows to be un-ordered.
110
//
111
int uniqueRowIdentifierColumn = -1;
112       if (isDefinedInput(UNIQUE_ROW_IDENTIFIER_COLUMN)) {
113         String JavaDoc tmp = getInputStringValue(UNIQUE_ROW_IDENTIFIER_COLUMN);
114         uniqueRowIdentifierColumn = Integer.parseInt(tmp);
115       }
116       
117       String JavaDoc sortFormatType = null;
118       String JavaDoc sortFormatString = null;
119       if (isDefinedInput(SORT_FORMAT_TYPE)) {
120         sortFormatString = getInputStringValue(SORT_FORMAT_STRING);
121         sortFormatType = getInputStringValue(SORT_FORMAT_TYPE);
122       }
123       Format JavaDoc sortFormat = null;
124       if (sortFormatType != null && sortFormatType.length() > 0) {
125           if (StandardSettings.DECIMAL_FORMAT_TYPE.equalsIgnoreCase(sortFormatString)) {
126               sortFormat = new DecimalFormat JavaDoc(sortFormatString);
127           } else if (StandardSettings.DATE_FORMAT_TYPE.equalsIgnoreCase(sortFormatType)) {
128               sortFormat = new SimpleDateFormat JavaDoc(sortFormatString);
129           }
130       }
131
132       IPentahoResultSet rSet = null;
133       
134       if (isDefinedInput(OLD_STYLE_CROSSTAB)) {
135         warn(Messages.getString("ResultSetCrosstabComponent.WARN_DEPRECATED")); //$NON-NLS-1$
136
rSet = PentahoDataTransmuter.crossTab((IPentahoResultSet)resultSetObject, columnToPivot - 1, measuresColumn - 1, transformSortByColumn - 1, format, sortFormat, orderOutputColumns);
137       } else {
138         rSet = PentahoDataTransmuter.crossTabOrdered((IPentahoResultSet)resultSetObject, columnToPivot - 1, measuresColumn - 1, transformSortByColumn - 1, format, sortFormat, orderOutputColumns, uniqueRowIdentifierColumn - 1);
139       }
140       // then set the outputResult
141
setOutputValue(outputName, rSet);
142       
143       return true;
144     
145     }
146     return false;
147   }
148
149   public boolean init() {
150     // TODO Auto-generated method stub
151
return true;
152   }
153
154   protected boolean validateSystemSettings() {
155     return true; // nothing here...
156
}
157
158   public Log getLogger() {
159     return LogFactory.getLog(ResultSetCrosstabComponent.class);
160   }
161
162   public String JavaDoc getResultOutputName() {
163     Set JavaDoc outputs = getOutputNames();
164     if ((outputs == null) || (outputs.size() != 1)) {
165         error(Messages.getString("Template.ERROR_0002_OUTPUT_COUNT_WRONG")); //$NON-NLS-1$
166
return null;
167     }
168     String JavaDoc outputName = null;
169     try {
170         outputName = getInputStringValue(StandardSettings.OUTPUT_NAME);
171     } catch (Exception JavaDoc e) {
172     }
173     if (outputName == null) { // Drop back to the old behavior
174
outputName = (String JavaDoc) outputs.iterator().next();
175     }
176     return outputName;
177 }
178   
179   
180 }
181
Popular Tags