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: * @created January 3rd, 2006
014: * @author Michael D'Amour
015: **/package org.pentaho.plugin.core;
016:
017: import java.util.Set;
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.pentaho.commons.connection.IPentahoResultSet;
021: import org.pentaho.commons.connection.PentahoDataTransmuter;
022: import org.pentaho.messages.Messages;
023: import org.pentaho.plugin.ComponentBase;
024:
025: /**
026: *
027: * This class flattens an IPentahoResultSet by looking at a particular column.
028: *
029: *
030: * The flattening is based on a particular column, as we build our consolidated
031: * row, we look at the this columns value to create a new consolidated row when
032: * it changes.
033: *
034: * eg)
035: *
036: * Dec 20, 2005 3432 Dec 20, 2005 235 Dec 20, 2005 8568 Dec 20, 2005 5685 Dec
037: * 20, 2005 9873 Dec 29, 2005 24685 Dec 29, 2005 12345 Dec 29, 2005 13151 Dec
038: * 29, 2005 12302 Dec 29, 2005 34772
039: *
040: * Dec 20, 2005 3432 235 8568 5685 9873 Dec 29, 2005 24685 12345 13151 12302
041: * 34772
042: */
043: public class ResultSetFlattenerComponent extends ComponentBase {
044: /**
045: *
046: */
047: private static final String RESULT_SET = "result-set"; //$NON-NLS-1$
048:
049: private static final String FLATTEN_COLUMN = "flatten-column"; //$NON-NLS-1$
050:
051: private static final long serialVersionUID = 5969716585776621813L;
052:
053: public Log getLogger() {
054: return LogFactory.getLog(ResultSetFlattenerComponent.class);
055: }
056:
057: public boolean init() {
058: // TODO Auto-generated method stub
059: return true;
060: }
061:
062: protected boolean validateSystemSettings() {
063: // This component does not have any system settings to validate
064: return true;
065: }
066:
067: protected boolean validateAction() {
068: if (!isDefinedInput(RESULT_SET)) {
069: error(Messages
070: .getString("ResultSetFlattenerComponent.ERROR_0001_DATA_INPUT_INVALID_OBJECT")); //$NON-NLS-1$
071: return false;
072: }
073: if (getResultOutputName() == null) {
074: error(Messages
075: .getString("ResultSetFlattenerComponent.ERROR_0002_INVALID_OUTPUT")); //$NON-NLS-1$
076: return false;
077: }
078: if (!isDefinedInput(FLATTEN_COLUMN)) {
079: error(Messages
080: .getString("ResultSetFlattenerComponent.ERROR_0003_INVALID_FLATTEN_COLUMN")); //$NON-NLS-1$
081: return false;
082: }
083: return true;
084: }
085:
086: protected boolean executeAction() {
087: Object resultSetObject = getInputValue(RESULT_SET);
088: if (resultSetObject instanceof IPentahoResultSet) {
089: IPentahoResultSet resultset = (IPentahoResultSet) resultSetObject;
090: int column = (int) getInputLongValue(FLATTEN_COLUMN, 0);
091: column--;
092: if (getResultOutputName() != null) {
093: setOutputValue(getResultOutputName(),
094: PentahoDataTransmuter.flattenResultSet(
095: resultset, column));
096: }
097: }
098: return true;
099: }
100:
101: public void done() {
102: }
103:
104: public String getResultOutputName() {
105: Set outputs = getOutputNames();
106: if ((outputs == null) || (outputs.size() == 0)) {
107: error(Messages
108: .getString("Template.ERROR_0002_OUTPUT_COUNT_WRONG")); //$NON-NLS-1$
109: return null;
110: }
111: String outputName = null;
112: try {
113: outputName = getInputStringValue(StandardSettings.OUTPUT_NAME);
114: } catch (Exception e) {
115: }
116: if (outputName == null) { // Drop back to the old behavior
117: outputName = (String) outputs.iterator().next();
118: }
119: return outputName;
120: }
121: }
|