001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.plugin.core;
014:
015: import java.text.DecimalFormat;
016: import java.text.Format;
017: import java.text.SimpleDateFormat;
018: import java.util.Set;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.pentaho.commons.connection.IPentahoResultSet;
023: import org.pentaho.commons.connection.PentahoDataTransmuter;
024: import org.pentaho.messages.Messages;
025: import org.pentaho.plugin.ComponentBase;
026:
027: public class ResultSetCrosstabComponent extends ComponentBase {
028:
029: private static final long serialVersionUID = -868492439427599791L;
030:
031: private static final String RESULT_SET = "result_set"; //$NON-NLS-1$
032:
033: private static final String PIVOT_COLUMN = "pivot_column"; //$NON-NLS-1$
034:
035: private static final String MEASURES_COLUMN = "measures_column"; //$NON-NLS-1$
036:
037: private static final String FORMAT_TYPE = "format_type"; //$NON-NLS-1$
038:
039: private static final String FORMAT_STRING = "format_string"; //$NON-NLS-1$
040:
041: private static final String ORDERED_MAPS = "ordered_maps"; //$NON-NLS-1$
042:
043: private static final String TRANSFORM_SORTBYCOL = "sort_by_col"; //$NON-NLS-1$
044:
045: private static final String SORT_FORMAT_TYPE = "sort_format_type"; //$NON-NLS-1$
046:
047: private static final String SORT_FORMAT_STRING = "sort_format_string"; //$NON-NLS-1$
048:
049: private static final String OLD_STYLE_CROSSTAB = "non_ordered"; //$NON-NLS-1$
050:
051: private static final String UNIQUE_ROW_IDENTIFIER_COLUMN = "unique_row_identifier_column"; //$NON-NLS-1$
052:
053: public void done() {
054: // TODO Auto-generated method stub
055:
056: }
057:
058: protected boolean validateAction() {
059: if (!isDefinedInput(PIVOT_COLUMN)) {
060: error(Messages
061: .getErrorString("ResultSetCrosstabComponent.ERROR_0001_PIVOT_COLUMN_IS_REQUIRED")); //$NON-NLS-1$
062: return false;
063: }
064: if (!isDefinedInput(MEASURES_COLUMN)) {
065: error(Messages
066: .getErrorString("ResultSetCrosstabComponent.ERROR_0002_MEASURES_COLUMN_IS_REQUIRED")); //$NON-NLS-1$
067: return false;
068: }
069: if (isDefinedInput(FORMAT_TYPE)) {
070: if (!isDefinedInput(FORMAT_STRING)) {
071: error(Messages
072: .getErrorString("ResultSetCrosstabComponent.ERROR_0003_FORMAT_PARAMETERS_BAD")); //$NON-NLS-1$
073: return false;
074: }
075: }
076: if (isDefinedInput(SORT_FORMAT_TYPE)) {
077: if (!isDefinedInput(SORT_FORMAT_STRING)) {
078: error(Messages
079: .getErrorString("ResultSetCrosstabComponent.ERROR_0004_SORT_FORMAT_PARAMETERS_BAD")); //$NON-NLS-1$
080: return false;
081: }
082: }
083: return true;
084: }
085:
086: protected boolean executeAction() throws Throwable {
087: Object resultSetObject = getInputValue(RESULT_SET);
088: String outputName = getResultOutputName();
089: if (outputName == null) {
090: return false;
091: }
092: if (resultSetObject instanceof IPentahoResultSet) {
093: int columnToPivot = Integer
094: .parseInt(getInputStringValue(PIVOT_COLUMN));
095: int measuresColumn = Integer
096: .parseInt(getInputStringValue(MEASURES_COLUMN));
097: String formatType = null;
098: String formatString = null;
099: if (isDefinedInput(FORMAT_TYPE)) {
100: formatType = getInputStringValue(FORMAT_TYPE);
101: formatString = getInputStringValue(FORMAT_STRING);
102: }
103: Format format = null;
104:
105: // this is that rSet is now the valid result Create the format if there is one
106: if (formatType != null && formatType.length() > 0) {
107: if (StandardSettings.DECIMAL_FORMAT_TYPE
108: .equalsIgnoreCase(formatType)) {
109: format = new DecimalFormat(formatString);
110: } else if (StandardSettings.DATE_FORMAT_TYPE
111: .equalsIgnoreCase(formatType)) {
112: format = new SimpleDateFormat(formatString);
113: }
114: }
115: // transform rSet here
116: String orderedMaps = getInputStringValue(ORDERED_MAPS);
117: boolean orderOutputColumns = "true".equalsIgnoreCase(orderedMaps); //$NON-NLS-1$
118:
119: // Sort-by column information
120: int transformSortByColumn = 0;
121: String sortColumn = getInputStringValue(TRANSFORM_SORTBYCOL);
122: if (sortColumn != null) {
123: transformSortByColumn = Integer.parseInt(sortColumn);
124: }
125:
126: //
127: // Column that uniquely identifies a row
128: // If supplied, will allow input rows to be un-ordered.
129: //
130: int uniqueRowIdentifierColumn = -1;
131: if (isDefinedInput(UNIQUE_ROW_IDENTIFIER_COLUMN)) {
132: String tmp = getInputStringValue(UNIQUE_ROW_IDENTIFIER_COLUMN);
133: uniqueRowIdentifierColumn = Integer.parseInt(tmp);
134: }
135:
136: String sortFormatType = null;
137: String sortFormatString = null;
138: if (isDefinedInput(SORT_FORMAT_TYPE)) {
139: sortFormatString = getInputStringValue(SORT_FORMAT_STRING);
140: sortFormatType = getInputStringValue(SORT_FORMAT_TYPE);
141: }
142: Format sortFormat = null;
143: if (sortFormatType != null && sortFormatType.length() > 0) {
144: if (StandardSettings.DECIMAL_FORMAT_TYPE
145: .equalsIgnoreCase(sortFormatString)) {
146: sortFormat = new DecimalFormat(sortFormatString);
147: } else if (StandardSettings.DATE_FORMAT_TYPE
148: .equalsIgnoreCase(sortFormatType)) {
149: sortFormat = new SimpleDateFormat(sortFormatString);
150: }
151: }
152:
153: IPentahoResultSet rSet = null;
154:
155: if (isDefinedInput(OLD_STYLE_CROSSTAB)) {
156: warn(Messages
157: .getString("ResultSetCrosstabComponent.WARN_DEPRECATED")); //$NON-NLS-1$
158: rSet = PentahoDataTransmuter.crossTab(
159: (IPentahoResultSet) resultSetObject,
160: columnToPivot - 1, measuresColumn - 1,
161: transformSortByColumn - 1, format, sortFormat,
162: orderOutputColumns);
163: } else {
164: rSet = PentahoDataTransmuter.crossTabOrdered(
165: (IPentahoResultSet) resultSetObject,
166: columnToPivot - 1, measuresColumn - 1,
167: transformSortByColumn - 1, format, sortFormat,
168: orderOutputColumns,
169: uniqueRowIdentifierColumn - 1);
170: }
171: // then set the outputResult
172: setOutputValue(outputName, rSet);
173:
174: return true;
175:
176: }
177: return false;
178: }
179:
180: public boolean init() {
181: // TODO Auto-generated method stub
182: return true;
183: }
184:
185: protected boolean validateSystemSettings() {
186: return true; // nothing here...
187: }
188:
189: public Log getLogger() {
190: return LogFactory.getLog(ResultSetCrosstabComponent.class);
191: }
192:
193: public String getResultOutputName() {
194: Set outputs = getOutputNames();
195: if ((outputs == null) || (outputs.size() != 1)) {
196: error(Messages
197: .getString("Template.ERROR_0002_OUTPUT_COUNT_WRONG")); //$NON-NLS-1$
198: return null;
199: }
200: String outputName = null;
201: try {
202: outputName = getInputStringValue(StandardSettings.OUTPUT_NAME);
203: } catch (Exception e) {
204: }
205: if (outputName == null) { // Drop back to the old behavior
206: outputName = (String) outputs.iterator().next();
207: }
208: return outputName;
209: }
210:
211: }
|