Exemplo n.º 1
0
  /**
   * Validate and update the provided list of AdDcCoopBeans in the database in transactional
   * fashion. If validation on any coop, an ApplicationException is thrown and no data is changed on
   * any entry in the database. If all entries are validated, their data is updated in the database
   * and workflow is performed. If <code>divisionUpdate</code> is <code>true</code>, then workflow
   * is sent a <code>open</code>, <code>reject</code>, or <code>accept</code> depending if the
   * <code>divisionTaken</code> attribute is -1, 1, or 0, respectively. If <code>divisionUpdate
   * </code> is <code>false</code> (i.e. an accounting update), then workflow is send <code>billed
   * </code> (bill date or signature is provided), <code>reject</code> (an accounting reject reason
   * is provided), or <code>open</code> (otherwise). Also, for all accounting updates,
   * #updateDivisionCoopMonies(int) is invoked.
   *
   * @param dcCoops the list of division coops to process
   * @param divisionUpdate indicates if this is an update from the division phase
   * @param username the user submitting the workflow request
   * @throws ApplicationException if a database, validation, or workflow error occurs
   */
  public void updateCoopsWithWorkflow(
      AdDcCoopBean[] dcCoops, boolean divisionUpdate, String username) throws ApplicationException {

    if (dcCoops == null || dcCoops.length == 0) {
      return;
    }

    boolean success = false;
    try {
      //	        DBHelper.startTransaction(NFCCoreVariables.DATASOURCE_WAG_JNDI, false);
      for (int i = 0; i < dcCoops.length; i++) {

        AdDcCoopBean dcCoop = dcCoops[i];
        if (dcCoop != null) {
          validateCoop(dcCoop);

          String outcome = null;
          if (divisionUpdate) {
            AdCoopBean coop = AdCoopManager.getCoopById(dcCoop.getCoopId());
            if (coop == null) {
              throw new ApplicationException(
                  "Unable to locate coop having id: " + dcCoop.getCoopId());
            }

            if (coop.getType() == ContractsUtils.getCoopIdCaseRate()) {
              int divisionTaken = 0;
              if (dcCoop.getEstimatedCases() > 0) {
                divisionTaken = 1;
              } else if (dcCoop.getEstimatedCases() == 0) {
                divisionTaken = -1;
              }
              dcCoop.setDivisionTaken(divisionTaken);
            }
            outcome =
                dcCoop.getDivisionTaken() == 1
                    ? "accept"
                    : dcCoop.getDivisionTaken() == -1 ? "reject" : "open";
          } else {
            if (username.equals(AdCoopExportCommand.WF_USERID)) outcome = "export";
            else
              outcome =
                  Utils.hasContent(dcCoop.getBillSign()) || dcCoop.getBillDate() != null
                      ? "billed"
                      : dcCoop.getAccountRejectReason() > 0 ? "reject" : "open";
          }
          WorkflowTransactionPhase newPhase =
              ContractsUtils.getWorkflowCommand()
                  .processTransition(
                      dcCoop.getWorkFlowPhaseId(), outcome, username, dcCoop.toString());
          dcCoop.setWorkFlowPhaseId(newPhase.getId());

          this.storeAdDcCoop(dcCoop);
          if (!divisionUpdate) {
            this.updateDivisionCoopMonies(
                dcCoop.getCoopId()); // this is an accounting update, so we will affect the billed
            // amounts.
          }
        }
      }

      success = true;
    } finally {
      //	        DBHelper.finalizeActiveTransaction(NFCCoreVariables.DATASOURCE_WAG_JNDI, success);
    }
  }