コード例 #1
0
  /*
   * (non-Javadoc)
   *
   * @see
   * com.toolsverse.etl.connector.DataSetConnector#persist(com.toolsverse.
   * etl.connector.DataSetConnectorParams, com.toolsverse.etl.common.DataSet,
   * com.toolsverse.etl.driver.Driver)
   */
  public ConnectorResult persist(ExcelConnectorParams params, DataSet dataSet, Driver driver)
      throws Exception {
    if (dataSet == null
        || params == null
        || (driver == null && dataSet.getDriver() == null)
        || Utils.isNothing(dataSet.getName())
        || dataSet.getFieldCount() == 0
        || (params.isFileNameRequired() && Utils.isNothing(params.getFileName()))) {
      ConnectorResult result = new ConnectorResult();
      result.setRetCode(ConnectorResult.VALIDATION_FAILED_CODE);

      if (dataSet == null)
        result.addResult(ConnectorResource.VALIDATION_ERROR_DATA_SET_NULL.getValue());
      if (driver == null && dataSet.getDriver() == null)
        result.addResult(ConnectorResource.VALIDATION_ERROR_DRIVER_NULL.getValue());
      if (params == null)
        result.addResult(ConnectorResource.VALIDATION_ERROR_PARAMS_NULL.getValue());
      if (dataSet != null && dataSet.getFieldCount() == 0)
        result.addResult(ConnectorResource.VALIDATION_ERROR_DATA_SET_NO_FIELDS.getValue());
      if (dataSet != null && Utils.isNothing(dataSet.getName()))
        result.addResult(ConnectorResource.VALIDATION_ERROR_DATA_SET_NO_NAME.getValue());
      if (params.isFileNameRequired() && Utils.isNothing(params.getFileName()))
        result.addResult(FileConnectorResource.VALIDATION_ERROR_FILE_NAME_NOT_SPECIFIED.getValue());

      return result;
    }

    if (!params.isSilent())
      Logger.log(
          Logger.INFO,
          EtlLogger.class,
          EtlResource.PERSISTING_DATASET_MSG.getValue() + dataSet.getName() + "...");

    try {
      prePersist(params, dataSet, driver);

      int records = dataSet.getRecordCount();

      // data
      for (int row = 0; row < records; row++) {
        DataSetRecord record = dataSet.getRecord(row);

        inlinePersist(params, dataSet, driver, record, row, records);
      }

      postPersist(params, dataSet, driver);

      ConnectorResult connectorResult = new ConnectorResult();

      connectorResult.addResult(
          Utils.format(
              FileConnectorResource.FILE_PERSISTED.getValue(),
              new String[] {FilenameUtils.getName(params.getRealFileName())}));

      return connectorResult;
    } finally {
      cleanUp(params, dataSet, driver);
    }
  }
コード例 #2
0
  /*
   * (non-Javadoc)
   *
   * @see
   * com.toolsverse.etl.connector.DataSetConnector#populate(com.toolsverse
   * .etl.connector.DataSetConnectorParams, com.toolsverse.etl.common.DataSet,
   * com.toolsverse.etl.driver.Driver)
   */
  public ConnectorResult populate(ExcelConnectorParams params, DataSet dataSet, Driver driver)
      throws Exception {
    if (dataSet == null
        || params == null
        || Utils.isNothing(dataSet.getName())
        || (driver == null && dataSet.getDriver() == null)) {
      ConnectorResult result = new ConnectorResult();
      result.setRetCode(ConnectorResult.VALIDATION_FAILED_CODE);

      if (dataSet == null)
        result.addResult(ConnectorResource.VALIDATION_ERROR_DATA_SET_NULL.getValue());
      if (driver == null && dataSet.getDriver() == null)
        result.addResult(ConnectorResource.VALIDATION_ERROR_DRIVER_NULL.getValue());
      if (params == null)
        result.addResult(ConnectorResource.VALIDATION_ERROR_PARAMS_NULL.getValue());
      if (dataSet != null && Utils.isNothing(dataSet.getName()))
        result.addResult(ConnectorResource.VALIDATION_ERROR_DATA_SET_NO_NAME.getValue());

      return result;
    }

    dataSet.clear();

    driver = driver != null ? driver : dataSet.getDriver();

    if (!params.isSilent())
      Logger.log(
          Logger.INFO,
          EtlLogger.class,
          EtlResource.LOADING_DATASET_MSG.getValue() + dataSet.getName() + "...");

    FileInputStream fin = null;
    POIFSFileSystem poifs = null;

    try {
      String fileName = null;

      if (params.getInputStream() == null) {
        fileName =
            SystemConfig.instance()
                .getPathUsingAppFolders(
                    params.getFileName(
                        dataSet.getOwnerName() != null ? dataSet.getOwnerName() : dataSet.getName(),
                        ".xls",
                        true));

        fin = new FileInputStream(fileName);

        poifs = new POIFSFileSystem(fin);
      } else poifs = new POIFSFileSystem(params.getInputStream());

      XlsProcessor xlsProcessor = new XlsProcessor(params, dataSet, driver);

      MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(xlsProcessor);
      FormatTrackingHSSFListener formatListener = new FormatTrackingHSSFListener(listener);

      xlsProcessor.setFormatListener(formatListener);

      HSSFRequest request = new HSSFRequest();
      request.addListenerForAllRecords(formatListener);

      HSSFEventFactory factory = new HSSFEventFactory();

      try {
        factory.processWorkbookEvents(request, poifs);
      } catch (Exception ex) {
        if (!params.isMaxRowsExceededException(ex) && !params.isSheetAlreadyExatractedException(ex))
          throw ex;
      }

      if (dataSet.getFieldCount() > 0
          && dataSet.getRecordCount() == 0
          && params.getAddRecordCallback() != null) {
        params.getAddRecordCallback().onAddRecord(dataSet, driver, null, 0);
      }

      ConnectorResult connectorResult = new ConnectorResult();

      connectorResult.addResult(
          Utils.format(
              FileConnectorResource.FILE_POPULATED.getValue(),
              new String[] {FilenameUtils.getName(fileName)}));

      return connectorResult;

    } finally {
      if (fin != null) fin.close();

      if (params.getInputStream() != null && params.isCloseInput()) params.getInputStream().close();

      if (params.getAfterCallback() != null) params.getAfterCallback().onAfter(dataSet, driver);
    }
  }