private boolean OpenFile() throws Exception {
    data.oneFileOpened = true;
    String realFilename = environmentSubstitute(meta.getFilename());
    if (log.isBasic()) {
      logBasic(BaseMessages.getString(PKG, "AccessOutput.log.WritingToFile", realFilename));
    }
    FileObject fileObject = KettleVFS.getFileObject(realFilename, getTransMeta());
    File file = FileUtils.toFile(fileObject.getURL());

    // First open or create the access file
    if (!file.exists()) {
      if (meta.isFileCreated()) {
        data.db = Database.create(file);
      } else {
        logError(
            BaseMessages.getString(PKG, "AccessOutput.InitError.FileDoesNotExist", realFilename));
        return false;
      }
    } else {
      data.db = Database.open(file);
    }

    // Add the filename to the result object...
    //
    if (meta.isAddToResultFiles()) {
      ResultFile resultFile =
          new ResultFile(
              ResultFile.FILE_TYPE_GENERAL, fileObject, getTransMeta().getName(), toString());
      resultFile.setComment("This file was created with an access output step");
      addResultFile(resultFile);
    }

    return true;
  }
  /**
   * Updates survey table with weights
   *
   * @param tableSetup The hashtable that defines the database tables, fields, etc.
   * @param FinalWeights The object holding the final weight values to be written
   */
  public static void updateSurveyTableWeights(
      Hashtable<String, String> tableSetup, List<WeightData> FinalWeights) {
    File dFile = new File((String) tableSetup.get("dataFile"));
    // Logger logger=IPFMain.logger;
    try {
      Table table = Database.open(dFile).getTable((String) tableSetup.get("surveyTable"));
      Cursor cur = Cursor.createCursor(table);

      cur.reset();
      while (cur.moveToNextRow()) {
        Map<String, Object> row = cur.getCurrentRow();
        for (WeightData wd : FinalWeights) {
          Map<String, Object> newRow = new HashMap<String, Object>();
          // logger.debug("row\t"+row.get(tableSetup.get("routeField")).toString()+"\t"+row.get(tableSetup.get("directionField")).toString()+"\t"+row.get(tableSetup.get("timeField")).toString()+"\t"+CInt(row.get(tableSetup.get("BoardingLocationCode")))+"\t"+CInt(row.get(tableSetup.get("AlightingLocationCode"))));
          // logger.debug("wd
          // \t"+wd.RouteName+"\t"+wd.Direction+"\t"+wd.TimePeriod+"\t"+wd.BoardLocation+"\t"+wd.AlightLocation);
          if (row.get(tableSetup.get("routeField")).toString().equalsIgnoreCase(wd.RouteName)
              && row.get(tableSetup.get("directionField")).toString().equalsIgnoreCase(wd.Direction)
              && row.get(tableSetup.get("timeField")).toString().equalsIgnoreCase(wd.TimePeriod)
              && CInt(row.get(tableSetup.get("BoardingLocationCode"))) == wd.BoardLocation
              && CInt(row.get(tableSetup.get("AlightingLocationCode"))) == wd.AlightLocation) {
            // FIXME: Somehow in the last round of changes, execution never gets to this point.
            newRow.put("ODWeight", wd.ODWeightValue); // TODO: Hash
            if (wd.Direction.equalsIgnoreCase("Inbound")
                && wd.TimePeriod.equalsIgnoreCase("AM Peak")) {
              if (CInt(row.get(tableSetup.get("OriginAccess"))) <= 2) {
                newRow.put(tableSetup.get("StationWeightField"), wd.StationWalkWeight);
              } else if (CInt(row.get(tableSetup.get("OriginAccess"))) == 5) {
                newRow.put(tableSetup.get("StationWeightField"), wd.StationKNRWeight);
              } else {
                newRow.put(tableSetup.get("StationWeightField"), wd.StationPNRWeight);
              }

            } else if (wd.Direction.equalsIgnoreCase("Outbound")
                && wd.TimePeriod.equalsIgnoreCase("PM Peak")) {
              if (CInt(row.get(tableSetup.get("DestinationEgress"))) <= 2) {
                newRow.put(tableSetup.get("StationWeightField"), wd.StationWalkWeight);
              } else if (CInt(row.get(tableSetup.get("DestinationEgress"))) == 5) {
                newRow.put(tableSetup.get("StationWeightField"), wd.StationKNRWeight);
              } else {
                newRow.put(tableSetup.get("StationWeightField"), wd.StationPNRWeight);
              }
            }
            Column col = table.getColumn(tableSetup.get("StationWeightField"));
            cur.setCurrentRowValue(col, newRow.get(tableSetup.get("StationWeightField")));
            col = table.getColumn("ODWeight"); // TODO: Hash
            cur.setCurrentRowValue(col, newRow.get("ODWeight"));
            // break;
          }
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    }
  }
  public RowMetaInterface getRequiredFields(VariableSpace space) throws KettleException {
    String realFilename = space.environmentSubstitute(filename);
    File file = new File(realFilename);
    Database db = null;
    try {
      if (!file.exists() || !file.isFile()) {
        throw new KettleException(
            BaseMessages.getString(
                PKG, "AccessOutputMeta.Exception.FileDoesNotExist", realFilename));
      }

      // open the database and get the table
      db = Database.open(file);
      String realTablename = space.environmentSubstitute(tablename);
      Table table = db.getTable(realTablename);
      if (table == null) {
        throw new KettleException(
            BaseMessages.getString(
                PKG, "AccessOutputMeta.Exception.TableDoesNotExist", realTablename));
      }

      RowMetaInterface layout = getLayout(table);
      return layout;
    } catch (Exception e) {
      throw new KettleException(
          BaseMessages.getString(PKG, "AccessOutputMeta.Exception.ErrorGettingFields"), e);
    } finally {
      try {
        if (db != null) {
          db.close();
        }
      } catch (IOException e) {
        throw new KettleException(
            BaseMessages.getString(PKG, "AccessOutputMeta.Exception.ErrorClosingDatabase"), e);
      }
    }
  }
Example #4
0
 /*
  * Open the mdbFile with a specified charset
  */
 public AccessWrapper(File mdbFile, Charset ch) throws IOException {
   db = Database.open(mdbFile, true, false, ch, null);
 }
Example #5
0
 /*
  * Open the mdbFile using sensible default
  */
 public AccessWrapper(File mdbFile) throws IOException {
   db = Database.open(mdbFile, true);
 }