Program: 107435
Project: 107435
Buggy source:
@Override public ResourceLimitVO updateResourceLimit(UpdateResourceLimitCmd cmd) throws InvalidParameterValueException {
  Account account=(Account)UserContext.current().getAccount();
  Long domainId=cmd.getDomainId();
  Long max=cmd.getMax();
  Integer type=cmd.getResourceType();
  Long accountId=null;
  if (max == null) {
    max=new Long(-1);
  }
 else   if (max < -1) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
  }
  ResourceType resourceType;
  try {
    resourceType=ResourceType.values()[type];
  }
 catch (  ArrayIndexOutOfBoundsException e) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
  }
  if (account != null) {
    if (domainId != null) {
      if (!_domainDao.isChildDomain(account.getDomainId(),domainId)) {
        throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + ((account.getAccountName() == null) ? "String_Node_Str" : "String_Node_Str" + account.getAccountName() + "String_Node_Str") + "String_Node_Str"+ domainId+ "String_Node_Str");
      }
    }
 else     if (account.getType() == Account.ACCOUNT_TYPE_ADMIN) {
      domainId=DomainVO.ROOT_DOMAIN;
    }
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
      if ((domainId != null) && (account.getAccountName() == null) && domainId.equals(account.getDomainId())) {
        throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + ((account.getAccountName() == null) ? "String_Node_Str" : "String_Node_Str" + account.getAccountName() + "String_Node_Str") + "String_Node_Str"+ domainId+ "String_Node_Str");
      }
      Criteria c=new Criteria();
      c.addCriteria(Criteria.DOMAINID,DomainVO.ROOT_DOMAIN);
      c.addCriteria(Criteria.TYPE,resourceType);
      List<ResourceLimitVO> currentRootDomainLimits=searchForLimits(c);
      ResourceLimitVO currentRootDomainLimit=(currentRootDomainLimits.size() == 0) ? null : currentRootDomainLimits.get(0);
      if (currentRootDomainLimit != null) {
        long currentRootDomainMax=currentRootDomainLimits.get(0).getMax();
        if ((max == -1 && currentRootDomainMax != -1) || max > currentRootDomainMax) {
          throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + resourceType + "String_Node_Str"+ currentRootDomainMax+ "String_Node_Str");
        }
      }
    }
  }
 else   if (domainId == null) {
    domainId=DomainVO.ROOT_DOMAIN;
  }
  if (domainId == null) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
  }
 else   if (account != null) {
    if (account.getAccountName() != null) {
      Account userAccount=_accountDao.findActiveAccount(account.getAccountName(),domainId);
      if (userAccount == null) {
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + account.getAccountName() + "String_Node_Str"+ domainId);
      }
      accountId=userAccount.getId();
      domainId=userAccount.getDomainId();
    }
  }
  if (accountId != null)   domainId=null;
  if ((domainId == null) && (accountId == null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (accountId != null) {
    AccountVO accountHandle=_accountDao.findById(accountId);
    if (accountHandle == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else     if (accountHandle.getRemoved() != null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else     if (account.getType() == Account.ACCOUNT_TYPE_ADMIN || accountHandle.getType() == Account.ACCOUNT_ID_SYSTEM) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DomainVO domain=_domainDao.findById(account.getDomainId());
    long parentMaximum=findCorrectResourceLimit(domain,resourceType);
    if ((parentMaximum >= 0) && ((max.longValue() == -1) || (max.longValue() > parentMaximum))) {
      throw new InvalidParameterValueException("String_Node_Str" + account.getAccountName() + "String_Node_Str"+ accountId+ "String_Node_Str"+ parentMaximum+ "String_Node_Str"+ type+ "String_Node_Str"+ parentMaximum);
    }
  }
 else   if (domainId != null) {
    DomainVO domain=_domainDao.findById(domainId);
    if (domain == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else     if (domain.getRemoved() != null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    Long parentDomainId=domain.getParent();
    if (parentDomainId != null) {
      DomainVO parentDomain=_domainDao.findById(parentDomainId);
      long parentMaximum=findCorrectResourceLimit(parentDomain,resourceType);
      if ((parentMaximum >= 0) && (max.longValue() > parentMaximum)) {
        throw new InvalidParameterValueException("String_Node_Str" + domain.getName() + "String_Node_Str"+ domainId+ "String_Node_Str"+ parentMaximum+ "String_Node_Str"+ type+ "String_Node_Str"+ parentMaximum);
      }
    }
  }
  if (type == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  Filter searchFilter=new Filter(ResourceLimitVO.class,null,false,null,null);
  SearchCriteria<ResourceLimitVO> sc=_resourceLimitDao.createSearchCriteria();
  if (domainId != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,domainId);
  }
  if (accountId != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,accountId);
  }
  if (type != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,type);
  }
  List<ResourceLimitVO> limits=_resourceLimitDao.search(sc,searchFilter);
  if (limits.size() == 1) {
    ResourceLimitVO limit=limits.get(0);
    _resourceLimitDao.update(limit.getId(),max);
    return _resourceLimitDao.findById(limit.getId());
  }
 else {
    return _resourceLimitDao.persist(new ResourceLimitVO(domainId,accountId,resourceType,max));
  }
}

Fixed version (ground truth):
@Override public ResourceLimitVO updateResourceLimit(UpdateResourceLimitCmd cmd) throws InvalidParameterValueException {
  Account account=(Account)UserContext.current().getAccount();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  Long max=cmd.getMax();
  Integer type=cmd.getResourceType();
  Long accountId=null;
  if (max == null) {
    max=new Long(-1);
  }
 else   if (max < -1) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
  }
  ResourceType resourceType;
  try {
    resourceType=ResourceType.values()[type];
  }
 catch (  ArrayIndexOutOfBoundsException e) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
  }
  if (account != null) {
    if (domainId != null) {
      if (!_domainDao.isChildDomain(account.getDomainId(),domainId)) {
        throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + ((account.getAccountName() == null) ? "String_Node_Str" : "String_Node_Str" + account.getAccountName() + "String_Node_Str") + "String_Node_Str"+ domainId+ "String_Node_Str");
      }
    }
 else     if (account.getType() == Account.ACCOUNT_TYPE_ADMIN) {
      domainId=DomainVO.ROOT_DOMAIN;
    }
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
      if ((domainId != null) && (accountName == null) && domainId.equals(account.getDomainId())) {
        throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + domainId + "String_Node_Str");
      }
      Criteria c=new Criteria();
      c.addCriteria(Criteria.DOMAINID,DomainVO.ROOT_DOMAIN);
      c.addCriteria(Criteria.TYPE,resourceType);
      List<ResourceLimitVO> currentRootDomainLimits=searchForLimits(c);
      ResourceLimitVO currentRootDomainLimit=(currentRootDomainLimits.size() == 0) ? null : currentRootDomainLimits.get(0);
      if (currentRootDomainLimit != null) {
        long currentRootDomainMax=currentRootDomainLimits.get(0).getMax();
        if ((max == -1 && currentRootDomainMax != -1) || max > currentRootDomainMax) {
          throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + resourceType + "String_Node_Str"+ currentRootDomainMax+ "String_Node_Str");
        }
      }
    }
  }
 else   if (domainId == null) {
    domainId=DomainVO.ROOT_DOMAIN;
  }
  if (domainId == null) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str");
  }
 else   if (accountName != null) {
    Account userAccount=_accountDao.findActiveAccount(accountName,domainId);
    if (userAccount == null) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + account.getAccountName() + "String_Node_Str"+ domainId);
    }
    accountId=userAccount.getId();
  }
  if (accountId != null)   domainId=null;
  if ((domainId == null) && (accountId == null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (accountId != null) {
    AccountVO accountHandle=_accountDao.findById(accountId);
    if (accountHandle == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else     if (accountHandle.getRemoved() != null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else     if (account.getType() == Account.ACCOUNT_TYPE_ADMIN || accountHandle.getType() == Account.ACCOUNT_ID_SYSTEM) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DomainVO domain=_domainDao.findById(account.getDomainId());
    long parentMaximum=findCorrectResourceLimit(domain,resourceType);
    if ((parentMaximum >= 0) && ((max.longValue() == -1) || (max.longValue() > parentMaximum))) {
      throw new InvalidParameterValueException("String_Node_Str" + account.getAccountName() + "String_Node_Str"+ accountId+ "String_Node_Str"+ parentMaximum+ "String_Node_Str"+ type+ "String_Node_Str"+ parentMaximum);
    }
  }
 else   if (domainId != null) {
    DomainVO domain=_domainDao.findById(domainId);
    if (domain == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else     if (domain.getRemoved() != null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    Long parentDomainId=domain.getParent();
    if (parentDomainId != null) {
      DomainVO parentDomain=_domainDao.findById(parentDomainId);
      long parentMaximum=findCorrectResourceLimit(parentDomain,resourceType);
      if ((parentMaximum >= 0) && (max.longValue() > parentMaximum)) {
        throw new InvalidParameterValueException("String_Node_Str" + domain.getName() + "String_Node_Str"+ domainId+ "String_Node_Str"+ parentMaximum+ "String_Node_Str"+ type+ "String_Node_Str"+ parentMaximum);
      }
    }
  }
  if (type == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  Filter searchFilter=new Filter(ResourceLimitVO.class,null,false,null,null);
  SearchCriteria<ResourceLimitVO> sc=_resourceLimitDao.createSearchCriteria();
  if (domainId != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,domainId);
  }
  if (accountId != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,accountId);
  }
  if (type != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,type);
  }
  List<ResourceLimitVO> limits=_resourceLimitDao.search(sc,searchFilter);
  if (limits.size() == 1) {
    ResourceLimitVO limit=limits.get(0);
    _resourceLimitDao.update(limit.getId(),max);
    return _resourceLimitDao.findById(limit.getId());
  }
 else {
    return _resourceLimitDao.persist(new ResourceLimitVO(domainId,accountId,resourceType,max));
  }
}