Example #1
0
  /** execute with output filter */
  private OmniDTO executeKeepConnection(
      UserDatabaseConnection udc,
      Map inputs,
      String processorType,
      String processorName,
      Map outputFilters)
      throws BaseSQLException {
    if (udc == null) throw new IllegalArgumentException("UserDatabaseConnection object is null.");

    if (processorType == null || processorName == null)
      throw new IllegalArgumentException("processorType or processorName is null.");

    cleanUpInputs(inputs);
    if (inputs == null) inputs = new HashMap();

    OmniDTO returnTO = null;

    try {
      DataProcessor dp =
          DataProcessorFactory.getInstance().getDataProcessor(udc, processorType, processorName);
      returnTO = dp.execute(udc, convertKeyCase(inputs), outputFilters);
      if (returnTO != null) {
        returnTO.setProcessorType(processorType);
        returnTO.setProcessorName(processorName);
      }
    } catch (UnsupportedDataProcessorTypeException udptEx) {
      throw new BaseSQLException("Unsupported DataProcessor Type: " + processorType);
    } catch (UnsupportedDataProcessorNameException udpnEx) {
      throw new BaseSQLException("Unsupported DataProcessor Name: " + processorName);
    } catch (UnsupportedStoredProcedureAPINameException udpnEx) {
      throw new BaseSQLException("Unsupported DataProcessor Name: " + processorName);
    }

    return returnTO;
  }
Example #2
0
  /** execute a collection of InputInfo objects in one transaction */
  public Collection execute(Collection inputInfoList) throws BaseSQLException {
    if (inputInfoList == null) throw new IllegalArgumentException("inputs list is null.");

    ImplicitTransactionManager tm = TransactionManagerUtil.getImplicitTransactionManager();
    Collection returnTOList = new ArrayList();

    try {
      tm.beginTransactionImplicit();

      Iterator it = inputInfoList.iterator();
      while (it.hasNext()) {
        InputInfo ip = (InputInfo) it.next();
        UserDatabaseConnection connection = findOrCreateConnection(ip);
        OmniDTO returnTO =
            executeKeepConnection(
                connection,
                ip.getInputs(),
                ip.getProcessorType(),
                ip.getProcessorName(),
                ip.getOutputFilters());
        returnTOList.add(returnTO);

        // now execute child InputInfo
        Collection childList = ip.getChildInputInfoObjects();
        Iterator childIt = childList.iterator();
        while (childIt.hasNext()) {
          InputInfo childIp = (InputInfo) childIt.next();
          OmniDTO returnTO2 =
              executeKeepConnection(
                  connection,
                  childIp.getInputs(),
                  childIp.getProcessorType(),
                  childIp.getProcessorName(),
                  childIp.getOutputFilters());
          returnTO.addChildrenOmniDTOToList(returnTO2);
        }
      }

      tm.commitTransactionImplicit();
    } catch (BaseSQLException bdex) {
      tm.rollbackTransactionImplicit();
      throw bdex;
    } finally {
      tm.releaseResourcesImplicit();
    }

    return returnTOList;
  }
Example #3
0
  /**
   * Retrieve a list of rows from database with a certain limit range. If the number of returned
   * records is more than the preset limit range, an UnexpectedDataException will be thrown.
   *
   * <p>If DataProcessor.input_key_records_fixed key has value "true" in inputs, absolute fixed
   * number of records is required. An UnexpectedDataException will be thrown if the number of
   * retrieved records is not equal to limitOrFixed.
   *
   * <p>If the limitOrFixed = -1, all records are retrieved.
   *
   * <p>offset defaults to 0.
   *
   * @param inputs Map of input data
   * @param processorType A named sql or direct sql or stored procedure
   * @param processorName Sql name or sql itself or stored procedure name
   * @param limitOrFixed Number of desired (limit) or fixed records to retrieve
   * @param offset int for offset
   * @return TableData The row data
   * @throws com.scooterframework.orm.sqldataexpress.exception.BaseSQLException
   */
  public TableData retrieveRows(
      Map inputs, String processorType, String processorName, int limitOrFixed, int offset)
      throws BaseSQLException {
    if (processorType == null || processorName == null)
      throw new IllegalArgumentException("processorType or processorName is null.");

    if (inputs == null) inputs = new HashMap();

    inputs.put(DataProcessor.input_key_records_offset, new Integer(offset));
    inputs.put(DataProcessor.input_key_records_limit, new Integer(limitOrFixed));

    OmniDTO dto = execute(inputs, processorType, processorName);
    TableData td = dto.getTableData(processorName);

    if (limitOrFixed != DataProcessor.NO_ROW_LIMIT) {
      if (td.getTableSize() > limitOrFixed) {
        throw new UnexpectedDataException(
            "Failed to retrieveRows: required "
                + limitOrFixed
                + " but retrieved "
                + td.getTableSize()
                + ".");
      } else {
        boolean requireFixed =
            Util.getBooleanValue(inputs, DataProcessor.input_key_records_fixed, false);
        if (requireFixed) {
          if (td.getTableSize() != limitOrFixed) {
            throw new UnexpectedDataException(
                "Failed to retrieveRows: required only "
                    + limitOrFixed
                    + " but retrieved "
                    + td.getTableSize()
                    + ".");
          }
        }
      }
    }
    return td;
  }
Example #4
0
 /**
  * Update data in database.
  *
  * @param inputs Map of input data
  * @param processorType A named sql or direct sql or stored procedure
  * @param processorName Sql name or sql itself or stored procedure name
  * @return int number of rows updated
  * @throws com.scooterframework.orm.sqldataexpress.exception.BaseSQLException
  */
 public int update(Map inputs, String processorType, String processorName)
     throws BaseSQLException {
   OmniDTO dto = execute(inputs, processorType, processorName);
   return dto.getUpdatedRowCount();
 }
Example #5
0
  /** execute a collection of InputInfo objects in one transaction */
  public OmniDTO retrieveMasterDetails(InputInfo inputInfo) throws BaseSQLException {
    if (inputInfo == null) throw new IllegalArgumentException("inputInfo is null.");

    ImplicitTransactionManager tm = TransactionManagerUtil.getImplicitTransactionManager();
    OmniDTO returnTO = null;

    try {
      tm.beginTransactionImplicit();

      InputInfo ip = inputInfo;
      UserDatabaseConnection udc = findOrCreateConnection(ip);
      udc.getConnection().setReadOnly(true);

      returnTO =
          executeKeepConnection(
              udc,
              ip.getInputs(),
              ip.getProcessorType(),
              ip.getProcessorName(),
              ip.getOutputFilters());
      log.debug("parent: " + returnTO);

      // now execute child InputInfo
      Collection childList = ip.getChildInputInfoObjects();
      Iterator childIt = childList.iterator();
      while (childIt.hasNext()) {
        InputInfo childIp = (InputInfo) childIt.next();

        // find all input parameters in childIp that need data from parent
        List connectorList = new ArrayList();
        Map childInputs = childIp.getInputs();
        childInputs = convertKeyCase(childInputs);
        Iterator it = childInputs.keySet().iterator();
        while (it.hasNext()) {
          String key = (String) it.next();
          String value = (String) childInputs.get(key);
          if (key.startsWith("&")) connectorList.add(value);
        }

        // create a select union query
        String query = null;
        if (DataProcessorTypes.NAMED_SQL_STATEMENT_PROCESSOR.equals(childIp.getProcessorType())) {
          query = SqlConfig.getInstance().getSql(childIp.getProcessorName());
        } else if (DataProcessorTypes.DIRECT_SQL_STATEMENT_PROCESSOR.equals(
            childIp.getProcessorType())) {
          query = childIp.getProcessorName();
        }

        log.debug("child query1: " + query);

        // check if parent has data
        boolean parentHasData = false;
        TableData parentRt = null;
        if (returnTO != null) {
          parentRt = returnTO.getTableData(ip.getProcessorName());
          if (parentRt != null) {
            int size = parentRt.getAllRows().size();
            if (size > 0) parentHasData = true;
          }
        }

        // construct child query
        String childQuery = "";
        if (query != null && connectorList.size() > 0 && parentHasData) {
          childQuery = getNewChildQuery(query, childIp, parentRt.getAllRows());
        } else {
          childQuery = query;
        }

        log.debug("child query2: " + childQuery);

        if (parentHasData) {
          udc = findOrCreateConnection(childIp);

          OmniDTO returnTO2 =
              executeKeepConnection(
                  udc,
                  childIp.getInputs(),
                  DataProcessorTypes.DIRECT_SQL_STATEMENT_PROCESSOR,
                  childQuery,
                  childIp.getOutputFilters());

          // merge child records with corresponding parent record
          if (returnTO2 != null) {
            linkParentWithChild(
                parentRt,
                returnTO2.getTableData(childQuery),
                childIp.getProcessorName(),
                connectorList);
          }

          log.debug("returnTO2: " + returnTO2);
        }
      }
    } catch (SQLException ex) {
      throw new BaseSQLException(ex);
    } catch (BaseSQLException bdex) {
      throw bdex;
    } finally {
      tm.releaseResourcesImplicit();
    }

    return returnTO;
  }