Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* This Source Code Form is subject to the terms of the Mozilla Public
   * License, v. 2.0. If a copy of the MPL was not distributed with this file,
   * You can obtain one at http://mozilla.org/MPL/2.0/. */
  
  package pt.webdetails.cda.utils.kettle;
  
  import java.util.List;
 import  org.apache.commons.logging.Log;
 import  org.apache.commons.logging.LogFactory;
 import  org.pentaho.reporting.libraries.base.config.Configuration;
 import  org.pentaho.reporting.libraries.base.util.StringUtils;
 
 import  plugins.org.pentaho.di.robochef.kettle.DynamicTransConfig;
 import  plugins.org.pentaho.di.robochef.kettle.DynamicTransConfig.EntryType;
 import  plugins.org.pentaho.di.robochef.kettle.DynamicTransMetaConfig;
 import  plugins.org.pentaho.di.robochef.kettle.DynamicTransMetaConfig.Type;
 import  plugins.org.pentaho.di.robochef.kettle.DynamicTransformation;
 import  plugins.org.pentaho.di.robochef.kettle.RowProductionManager;
 import  plugins.org.pentaho.di.robochef.kettle.TableModelInput;

Author(s):
pedro
 
 public class SortTableModel implements RowProductionManager
 {
 
   private static final Log logger = LogFactory.getLog(SortTableModel.class);
   private ExecutorService executorService = Executors.newCachedThreadPool();
   private static final long DEFAULT_ROW_PRODUCTION_TIMEOUT = 120;
 
 
   public SortTableModel()
   {
   }
 
 
   public TableModel doSort(TableModel unsortedArrayList<StringsortBythrows SortException
   {
     Configuration config = CdaBoot.getInstance().getGlobalConfig();
     String sortType = config.getConfigProperty("pt.webdetails.cda.SortingType");
     if (sortType.equals("DEFAULT"))
     {
       return defaultSort(unsortedsortBy);
     }
     else
     {
       return customSort(unsortedsortBysortType);
     }
   }
 
 
   public TableModel customSort(TableModel unsortedArrayList<StringsortByString comparatorClassthrows SortException
   {
     try
     {
       @SuppressWarnings("unchecked")
       Class<? extends Comparator<Integer>> comp = (Class<? extends Comparator<Integer>>) Class.forName(comparatorClass);
       SortableTableModel sortable = new SortableTableModel(unsorted);
       sortable.sort(compsortBy);
       return sortable;
     } catch (Exception e){
       throw new SortException("Exception during sorting "e);
     }
   }
 
 
   public TableModel defaultSort(TableModel unsortedArrayList<StringsortBythrows SortException
   {
 
     if (unsorted == null || unsorted.getRowCount() == 0)
     {
       return unsorted;
     }
     else
     {
       TableModel output = null;
       .clear();
 
 
       try
       {
 
         String sort = getSortXmlStep(unsortedsortBy);
        DynamicTransMetaConfig transMetaConfig = new DynamicTransMetaConfig(Type.EMPTY, "JoinCompoundData"nullnull);
        DynamicTransConfig transConfig = new DynamicTransConfig();
        transConfig.addConfigEntry(EntryType.STEP, "input""<step><name>input</name><type>Injector</type><copies>1</copies></step>");
        transConfig.addConfigEntry(EntryType.STEP, "sort"sort);
        transConfig.addConfigEntry(EntryType.HOP, "input""sort");
        TableModelInput input = new TableModelInput();
        transConfig.addInput("input"input);
        .add(input.getCallableRowProducer(unsortedtrue));
        RowMetaToTableModel outputListener = new RowMetaToTableModel(falsetruefalse);
        transConfig.addOutput("sort"outputListener);
        DynamicTransformation trans = new DynamicTransformation(transConfigtransMetaConfig);
        trans.executeCheckedSuccess(nullnullthis);
        .info(trans.getReadWriteThroughput());
        output = outputListener.getRowsWritten();
        return output;
      }
      catch (Exception e)
      {
        throw new SortException("Exception during sorting "e);
      }
    }
  }
  public void startRowProduction()
  {
    String timeoutStr = CdaBoot.getInstance().getGlobalConfig().getConfigProperty("pt.webdetails.cda.DefaultRowProductionTimeout");
    long timeout =  StringUtils.isEmpty(timeoutStr) ?  : Long.parseLong(timeoutStr);
    String unitStr = CdaBoot.getInstance().getGlobalConfig().getConfigProperty("pt.webdetails.cda.DefaultRowProductionTimeoutTimeUnit");
    TimeUnit unit = StringUtils.isEmpty(unitStr) ?  : TimeUnit.valueOf(unitStr);
    startRowProduction(timeoutunit);
  }
  public void startRowProduction(long timeoutTimeUnit unit)
  {
    try
    {
      List<Future<Boolean>> results = .invokeAll(timeoutunit);
      for (Future<Booleanresult : results)
      {
        result.get();
      }
    }
    catch (InterruptedException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (ExecutionException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  private String getSortXmlStep(TableModel unsortedArrayList<StringsortBythrows SortException
  {
    StringBuilder sortXML = new StringBuilder(
            "  <step>\n"
            + "    <name>sort</name>\n"
            + "    <type>SortRows</type>\n"
            + "    <description/>\n"
            + "    <distribute>Y</distribute>\n"
            + "    <copies>1</copies>\n"
            + "         <partitioning>\n"
            + "           <method>none</method>\n"
            + "           <schema_name/>\n"
            + "           </partitioning>\n"
            + "      <directory>%%java.io.tmpdir%%</directory>\n"
            + "      <prefix>out</prefix>\n"
            + "      <sort_size>1000000</sort_size>\n"
            + "      <free_memory>25</free_memory>\n"
            + "      <compress>N</compress>\n"
            + "      <compress_variable/>\n"
            + "      <unique_rows>N</unique_rows>\n"
            + "    <fields>\n");
    for (String s : sortBy)
    {
      SortDescriptor sort = new SortDescriptor((s));
      sortXML.append("      <field>\n"
              + "        <name>" + unsorted.getColumnName(sort.getIndex()) + "</name>\n"
              + "        <ascending>" + sort.getIsAscendingString() + "</ascending>\n"
              + "        <case_sensitive>N</case_sensitive>\n"
              + "      </field>\n");
    }
    sortXML.append("    </fields>\n"
            + "     <cluster_schema/>\n"
            + " <remotesteps>   <input>   </input>   <output>   </output> </remotesteps>    <GUI>\n"
            + "      <xloc>615</xloc>\n"
            + "      <yloc>188</yloc>\n"
            + "      <draw>Y</draw>\n"
            + "      </GUI>\n"
            + "    </step>\n");
    return sortXML.toString();
  }
  private class SortDescriptor
  {
    private Integer index;
    private String direction;
    private static final String REGEXP = "^(\\d+)([AD]?)$";
    Pattern p = Pattern.compile();
    public SortDescriptor(String sortBythrows SortException
    {
      Matcher m = .matcher(sortBy);
      if (m.matches())
      {
        // valid one
         = Integer.parseInt(m.group(1));
        if (m.group(2).equals("D"))
        {
          setDirection("DESC");
        }
        else
        {
          setDirection("ASC");
        }
      }
      else
      {
        throw new SortException("Invalid searchBy option: " + sortBynull);
      }
    }
    public String getIsAscendingString()
    {
      if (getDirection().equals("ASC"))
      {
        return "Y";
      }
      else
      {
        return "N";
      }
    }
    public String getDirection()
    {
      return ;
    }
    public void setDirection(String direction)
    {
      this. = direction;
    }
    public Integer getIndex()
    {
      return ;
    }
    public void setIndex(Integer index)
    {
      this. = index;
    }
  }
New to GrepCode? Check out our FAQ X