/**
   * @see org.kuali.rice.krad.dao.MaintenanceDocumentDao#getLockingDocumentNumber(java.lang.String,
   *     java.lang.String)
   */
  public String getLockingDocumentNumber(String lockingRepresentation, String documentNumber) {

    String lockingDocNumber = "";

    // build the query criteria
    Criteria criteria = new Criteria();
    criteria.addEqualTo("lockingRepresentation", lockingRepresentation);

    // if a docHeaderId is specified, then it will be excluded from the
    // locking representation test.
    if (StringUtils.isNotBlank(documentNumber)) {
      criteria.addNotEqualTo(KRADPropertyConstants.DOCUMENT_NUMBER, documentNumber);
    }

    // attempt to retrieve a document based off this criteria
    MaintenanceLock maintenanceLock =
        (MaintenanceLock)
            getPersistenceBrokerTemplate()
                .getObjectByQuery(QueryFactory.newQuery(MaintenanceLock.class, criteria));

    // if a document was found, then there's already one out there pending, and
    // we consider it 'locked' and we return the docnumber.
    if (maintenanceLock != null) {
      lockingDocNumber = maintenanceLock.getDocumentNumber();
    }
    return lockingDocNumber;
  }
 protected MaintenanceLock verifyAllLocksFromThisDocument(
     List<MaintenanceLock> maintenanceLocks, String documentNumber) {
   for (MaintenanceLock maintenanceLock : maintenanceLocks) {
     String lockingDocNumber =
         maintenanceDocumentDao.getLockingDocumentNumber(
             maintenanceLock.getLockingRepresentation(), documentNumber);
     if (StringUtils.isNotBlank(lockingDocNumber)) {
       return maintenanceLock;
     }
   }
   return null;
 }
  /**
   * This generates the appropriate maintenance locks for the {@link ObjectCode}
   *
   * @see org.kuali.rice.kns.maintenance.Maintainable#generateMaintenanceLocks()
   */
  @Override
  public List<MaintenanceLock> generateMaintenanceLocks() {
    ObjectCodeGlobal objectCodeGlobal = (ObjectCodeGlobal) getBusinessObject();
    List<MaintenanceLock> maintenanceLocks = new ArrayList();
    SubObjectTrickleDownInactivationService subObjectTrickleDownInactivationService =
        SpringContext.getBean(SubObjectTrickleDownInactivationService.class);

    for (ObjectCodeGlobalDetail detail : objectCodeGlobal.getObjectCodeGlobalDetails()) {
      MaintenanceLock maintenanceLock = new MaintenanceLock();
      StringBuffer lockrep = new StringBuffer();

      lockrep.append(ObjectCode.class.getName() + KFSConstants.Maintenance.AFTER_CLASS_DELIM);
      lockrep.append("universityFiscalYear" + KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
      lockrep.append(detail.getUniversityFiscalYear() + KFSConstants.Maintenance.AFTER_VALUE_DELIM);
      lockrep.append("chartOfAccountsCode" + KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
      lockrep.append(detail.getChartOfAccountsCode() + KFSConstants.Maintenance.AFTER_VALUE_DELIM);
      lockrep.append("financialObjectCode" + KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
      lockrep.append(detail.getFinancialObjectCode());

      maintenanceLock.setDocumentNumber(objectCodeGlobal.getDocumentNumber());
      maintenanceLock.setLockingRepresentation(lockrep.toString());
      maintenanceLocks.add(maintenanceLock);

      ObjectCode objectCode = new ObjectCode();
      objectCode.setUniversityFiscalYear(detail.getUniversityFiscalYear());
      objectCode.setChartOfAccountsCode(detail.getChartOfAccountsCode());
      objectCode.setFinancialObjectCode(detail.getFinancialObjectCode());
      objectCode.setActive(objectCodeGlobal.isFinancialObjectActiveIndicator());

      if (isInactivatingObjectCode(objectCode)) {
        // if it turns out that the object code does not have associated sub-objects (either because
        // the object code doesn't exist or doesn't have sub-objects)
        // then the generateTrickleDownMaintenanceLocks method returns an empty list
        maintenanceLocks.addAll(
            subObjectTrickleDownInactivationService.generateTrickleDownMaintenanceLocks(
                objectCode, getDocumentNumber()));
      }
    }
    return maintenanceLocks;
  }
  protected TrickleDownInactivationStatus trickleDownInactivate(
      Collection<SubObjectCode> subObjects, String documentNumber) {
    TrickleDownInactivationStatus trickleDownInactivationStatus =
        new TrickleDownInactivationStatus();

    if (subObjects != null && !subObjects.isEmpty()) {
      Maintainable subObjectMaintainable = getSubObjectMaintainable(documentNumber);
      for (Iterator<SubObjectCode> i = subObjects.iterator(); i.hasNext(); ) {
        SubObjectCode subObjCd = i.next();
        if (subObjCd.isActive()) {
          subObjectMaintainable.setBusinessObject(subObjCd);
          List<MaintenanceLock> subAccountLocks = subObjectMaintainable.generateMaintenanceLocks();

          MaintenanceLock failedLock =
              verifyAllLocksFromThisDocument(subAccountLocks, documentNumber);
          if (failedLock != null) {
            // another document has locked this sub account, so we don't try to inactivate the
            // account
            trickleDownInactivationStatus.alreadyLockedSubObjCds.put(
                subObjCd, failedLock.getDocumentNumber());
          } else {
            // no locks other than our own (but there may have been no locks at all), just go ahead
            // and try to update
            subObjCd.setActive(false);

            try {
              subObjectMaintainable.saveBusinessObject();
              trickleDownInactivationStatus.inactivatedSubObjCds.add(subObjCd);
            } catch (RuntimeException e) {
              LOG.error("Unable to trickle-down inactivate sub-account " + subObjCd.toString(), e);
              trickleDownInactivationStatus.errorPersistingSubObjCds.add(subObjCd);
            }
          }
        }
      }
    }

    return trickleDownInactivationStatus;
  }
  /**
   * @see org.kuali.rice.krad.dao.MaintenanceDocumentDao#getLockingDocumentNumber(java.lang.String,
   *     java.lang.String)
   */
  public String getLockingDocumentNumber(String lockingRepresentation, String documentNumber) {
    String lockingDocNumber = "";

    // build the query criteria
    Criteria criteria = new Criteria(MaintenanceLock.class.getName());
    criteria.eq("lockingRepresentation", lockingRepresentation);

    // if a docHeaderId is specified, then it will be excluded from the
    // locking representation test.
    if (StringUtils.isNotBlank(documentNumber)) {
      criteria.ne(KRADPropertyConstants.DOCUMENT_NUMBER, documentNumber);
    }

    // attempt to retrieve a document based off this criteria
    MaintenanceLock maintenanceLock =
        (MaintenanceLock) new QueryByCriteria(entityManager, criteria).toQuery().getSingleResult();

    // if a document was found, then there's already one out there pending,
    // and we consider it 'locked' and we return the docnumber.
    if (maintenanceLock != null) {
      lockingDocNumber = maintenanceLock.getDocumentNumber();
    }
    return lockingDocNumber;
  }