Advertisement
talkingtree

DataProviderManager.cfc

Mar 19th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. component  hint="Encapsulates access to the test data in Excel spreadsheets using the JExcel API.  Wraps a Java access layer to that API." output="false"
  2. {
  3.     property name="dpm" type="DataproviderMgr" hint="An instance of a Java object";
  4.    
  5.     public void function setDataProvider(required string argXlFilePath, required string argSheetName, required string argTableName)
  6.      hint="Sets the excel file, sheet name, and table name for access to desired data" output="false"
  7.     {
  8.         local.dpm = createObject("java","DataproviderMgr");
  9.         local.dpm.setDataprovider(argXlFilePath,argSheetName,argTableName);
  10.         this.dpm = local.dpm;
  11.     }
  12.    
  13.     public Query function getData()
  14.     hint="Returns an query object of all data in the desired table from the Excel sheet"  output="false"  
  15.     {
  16.         local.result = TwoDimArrayWithHeadersToQuery(stripEmptyRows(this.dpm.getData()));
  17.         return local.result;
  18.     }
  19.        
  20.     public Query function getDataAtRow(required numeric rowNum)
  21.     hint="Returns an query object of one row of data  in the desired table from the Excel sheet"  output="false"  
  22.     {
  23.         local.qryDpm = getData();
  24.         if (isQuery(local.qryDpm)){
  25.             local.qryService = new Query();
  26.             qryService.setDbtype("query");
  27.             qryService.setAttributes(sourceQuery=local.qryDpm);          
  28.             qryService.setSQL("SELECT * FROM sourceQuery WHERE id = #rowNum#");
  29.             qryResult = qryService.execute();
  30.             if(isQuery(qryResult.getResult())){
  31.                 return qryResult.getResult();
  32.             }
  33.             return QueryNew("");
  34.         }
  35.         return QueryNew("");
  36.     }
  37.        
  38.     public Query function getDataByRange(required numeric startRowNum, required numeric endRowNum)
  39.     hint="Returns an array of data between the specified rows inclusively from the desired table from the Excel sheet"  output="false"  
  40.     {
  41.         local.qryDpm = getData();
  42.         if (isQuery(local.qryDpm)){
  43.             local.qryService = new Query();
  44.             qryService.setDbtype("query");
  45.             qryService.setAttributes(sourceQuery=local.qryDpm);          
  46.             qryService.setSQL("SELECT * FROM sourceQuery WHERE id >= #startRowNum# AND id <= #endRowNum#");
  47.             qryResult = qryService.execute();
  48.             if(isQuery(qryResult.getResult())){
  49.                 return qryResult.getResult();
  50.             }
  51.             return QueryNew("");
  52.         }
  53.         return QueryNew("");
  54.     }
  55.    
  56.     private Array function stripEmptyRows(required Array dp) {
  57.         returnDP = ArrayNew(2);
  58.         // 2 dimensional array.  Loop over the inner array which represents a data row
  59.         for(local.i = 1; i <= arraylen(dp); local.i++){
  60.             local.bIsEmptyRow = true;
  61.             for(local.j = 1; j <= arraylen(dp[local.i]); local.j++){
  62.                 if (local.j != 1 && dp[local.i][local.j] IS NOT ""){
  63.                     local.bIsEmptyRow = false;  // if any row other than the id column has a value, then this is not an empty row, don't delete
  64.                 }
  65.             }                  
  66.             if (!local.bIsEmptyRow) {
  67.                 for(local.j = 1; j <= arraylen(dp[local.i]); local.j++){
  68.                     returnDP[i][j]  = dp[i][j];
  69.                 }
  70.             }
  71.         }
  72.         return returnDP;
  73.     }
  74.    
  75.     private Query function TwoDimArrayWithHeadersToQuery(required Array argArray){
  76.         local.result = "";
  77.         for(local.i=1;i<=arraylen(argArray);i++){
  78.            
  79.             if(i EQ 1){  // First row is headers, so use to set query column names
  80.                 local.result = QueryNew("");
  81.                 local.seed = arrayNew(1);
  82.                 // all queries must have "id" column, and "id" must be first column in query
  83.                 QueryAddColumn(local.result,"id","integer",local.seed);
  84.                 for(local.k=1;k<=arraylen(argArray[i]);k++){
  85.                     if (argArray[i][k] IS NOT "id") {
  86.                         QueryAddColumn(local.result,argArray[i][k],"varchar",local.seed);
  87.                     }
  88.                 }
  89.             } else {
  90.                 QueryAddRow(local.result,1);
  91.             }
  92.            
  93.             for(local.j=1;j<=arraylen(argArray[i]);j++){
  94.                 if(i GT 1) {
  95.                     QuerySetCell(local.result,argArray[1][j],argArray[i][j],i-1); // on all other rows for inner array, values are row values
  96.                 }
  97.             }
  98.             if(i GT 1){
  99.                 QuerySetCell(local.result,"id",i-1,i-1);
  100.             }
  101.         }
  102.         return local.result;
  103.     }
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement