Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package pt.webdetails.cda.utils.kettle;
  
  import java.util.List;
  
  
  import  org.pentaho.di.core.row.RowMetaInterface;
 import  org.pentaho.di.core.row.ValueMetaInterface;
 import  org.pentaho.di.trans.step.RowListener;
 import  org.pentaho.reporting.engine.classic.core.util.TypedTableModel;

Bridge class between Kettle's RowMeta and CDA's TableModel

Author(s):
Daniel Einspanjer
 
 public class RowMetaToTableModel implements RowListener
 {
   private boolean                                         recordRowsRead;
   private final AtomicMarkableReference<RowMetaInterface> rowsReadMeta    = new AtomicMarkableReference<RowMetaInterface>(nullfalse);
   private List<Object[]>                                  rowsRead;
 
   private boolean                                         recordRowsWritten;
   private final AtomicMarkableReference<RowMetaInterface> rowsWrittenMeta = new AtomicMarkableReference<RowMetaInterface>(nullfalse); ;
   private List<Object[]>                                  rowsWritten;
 
   private boolean                                         recordRowsError;
   private final AtomicMarkableReference<RowMetaInterface> rowsErrorMeta   = new AtomicMarkableReference<RowMetaInterface>(nullfalse); ;
   private List<Object[]>                                  rowsError;
 
   public RowMetaToTableModel(final boolean recordRowsReadfinal boolean recordRowsWrittenfinal boolean recordRowsError)
   {
     if (!(recordRowsWritten || recordRowsRead || recordRowsError)) throw new IllegalArgumentException("Not recording any output. Must listen to something.");
     if (recordRowsRead) {
       this. = true;
        = new ArrayList<Object[]>();
     }
     if (recordRowsWritten) {
       this. = true;
        = new ArrayList<Object[]>();
     }
     if (recordRowsError) {
       this. = true;
        = new ArrayList<Object[]>();
     }
   }
 
   public void rowReadEvent(final RowMetaInterface rowMetafinal Object[] row)
   {
     if () {
       .weakCompareAndSet(nullrowMetafalsetrue);
       .add(row);
     }
   }
 
   public void rowWrittenEvent(final RowMetaInterface rowMetafinal Object[] row)
   {
     if () {
       .weakCompareAndSet(nullrowMetafalsetrue);
       .add(row);
     }
   }
 
   public void errorRowWrittenEvent(final RowMetaInterface rowMetafinal Object[] row)
   {
     if () {
       .weakCompareAndSet(nullrowMetafalsetrue);
       .add(row);
     }
   }
 
   public TableModel getRowsRead()
   {
     return asTableModel(.getReference(), );
   }
 
   public TableModel getRowsWritten()
   {
     return asTableModel(.getReference(), );
   }
 
   public TableModel getRowsError()
   {
     return asTableModel(.getReference(), );
   }
 
   private TableModel asTableModel(final RowMetaInterface rowMetafinal List<Object[]> rows)
   {
     if (rowMeta == nullreturn null;
 
     final TypedTableModel output = new TypedTableModel(rowMeta.getFieldNames(), getClassesForFields(rowMeta), rows.size());
     for (int i = 0; i < rows.size(); i++) {
       final Object[] row = rows.get(i);
       for (int j = 0; j < row.lengthj++) {
         output.setValueAt(row[j], ij);
       }
     }
    return output;
  }
  private Class<?>[] getClassesForFields(final RowMetaInterface rowMetathrows IllegalArgumentException
  {
    final List<ValueMetaInterface> valueMetas = rowMeta.getValueMetaList();
    final Class<?>[] types = new Class[valueMetas.size()];
    for (int i = 0; i < valueMetas.size(); i++) {
      final ValueMetaInterface valueMeta = valueMetas.get(i);
      switch (valueMeta.getType()) {
        case ValueMetaInterface.TYPE_STRING:
          types[i] = String.class;
          break;
        case ValueMetaInterface.TYPE_NUMBER:
          types[i] = Double.class;
          break;
        case ValueMetaInterface.TYPE_INTEGER:
          types[i] = Long.class;
          break;
        case ValueMetaInterface.TYPE_DATE:
          types[i] = java.util.Date.class;
          break;
        case ValueMetaInterface.TYPE_BIGNUMBER:
          types[i] = java.math.BigDecimal.class;
          break;
        case ValueMetaInterface.TYPE_BOOLEAN:
          types[i] = Boolean.class;
          break;
        case ValueMetaInterface.TYPE_BINARY:
          types[i] = byte[].class;
          break;
        case ValueMetaInterface.TYPE_SERIALIZABLE:
        case ValueMetaInterface.TYPE_NONE:
        default:
          throw new IllegalArgumentException(String.format("No type conversion found for Field %d %s"ivalueMeta.toString()));
      }
    }
    return types;
  }
New to GrepCode? Check out our FAQ X