Program: 105942
Project: 105942
Buggy source:
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_DEDICATE_RESOURCE,eventDescription="String_Node_Str") public List<DedicatedResourceVO> dedicateZone(final Long zoneId,final Long domainId,final String accountName){
  Long accountId=null;
  List<HostVO> hosts=null;
  if (accountName != null) {
    Account caller=CallContext.current().getCallingAccount();
    Account owner=_accountMgr.finalizeOwner(caller,accountName,domainId,null);
    accountId=owner.getId();
  }
  List<Long> childDomainIds=getDomainChildIds(domainId);
  childDomainIds.add(domainId);
  checkAccountAndDomain(accountId,domainId);
  final DataCenterVO dc=_zoneDao.findById(zoneId);
  if (dc == null) {
    throw new InvalidParameterValueException("String_Node_Str" + zoneId);
  }
 else {
    DedicatedResourceVO dedicatedZone=_dedicatedDao.findByZoneId(zoneId);
    if (dedicatedZone != null) {
      s_logger.error("String_Node_Str" + dc.getName() + "String_Node_Str");
      throw new CloudRuntimeException("String_Node_Str" + dc.getName() + "String_Node_Str");
    }
    List<HostPodVO> pods=_podDao.listByDataCenterId(dc.getId());
    List<DedicatedResourceVO> podsToRelease=new ArrayList<DedicatedResourceVO>();
    List<DedicatedResourceVO> clustersToRelease=new ArrayList<DedicatedResourceVO>();
    List<DedicatedResourceVO> hostsToRelease=new ArrayList<DedicatedResourceVO>();
    for (    HostPodVO pod : pods) {
      DedicatedResourceVO dPod=_dedicatedDao.findByPodId(pod.getId());
      if (dPod != null) {
        if (!(childDomainIds.contains(dPod.getDomainId()))) {
          throw new CloudRuntimeException("String_Node_Str" + pod.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
        }
        if (accountId != null) {
          if (dPod.getAccountId() == accountId) {
            podsToRelease.add(dPod);
          }
 else {
            s_logger.error("String_Node_Str" + pod.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
            throw new CloudRuntimeException("String_Node_Str" + pod.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
          }
        }
 else {
          if (dPod.getAccountId() == null && dPod.getDomainId() == domainId) {
            podsToRelease.add(dPod);
          }
        }
      }
    }
    for (    DedicatedResourceVO dr : podsToRelease) {
      releaseDedicatedResource(null,dr.getPodId(),null,null);
    }
    List<ClusterVO> clusters=_clusterDao.listClustersByDcId(dc.getId());
    for (    ClusterVO cluster : clusters) {
      DedicatedResourceVO dCluster=_dedicatedDao.findByClusterId(cluster.getId());
      if (dCluster != null) {
        if (!(childDomainIds.contains(dCluster.getDomainId()))) {
          throw new CloudRuntimeException("String_Node_Str" + cluster.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
        }
        if (accountId != null) {
          if (dCluster.getAccountId() == accountId) {
            clustersToRelease.add(dCluster);
          }
 else {
            s_logger.error("String_Node_Str" + cluster.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
            throw new CloudRuntimeException("String_Node_Str" + cluster.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
          }
        }
 else {
          if (dCluster.getAccountId() == null && dCluster.getDomainId() == domainId) {
            clustersToRelease.add(dCluster);
          }
        }
      }
    }
    for (    DedicatedResourceVO dr : clustersToRelease) {
      releaseDedicatedResource(null,null,dr.getClusterId(),null);
    }
    hosts=_hostDao.listByDataCenterId(dc.getId());
    for (    HostVO host : hosts) {
      DedicatedResourceVO dHost=_dedicatedDao.findByHostId(host.getId());
      if (dHost != null) {
        if (!(childDomainIds.contains(dHost.getDomainId()))) {
          throw new CloudRuntimeException("String_Node_Str" + host.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
        }
        if (accountId != null) {
          if (dHost.getAccountId() == accountId) {
            hostsToRelease.add(dHost);
          }
 else {
            s_logger.error("String_Node_Str" + host.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
            throw new CloudRuntimeException("String_Node_Str" + host.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
          }
        }
 else {
          if (dHost.getAccountId() == null && dHost.getDomainId() == domainId) {
            hostsToRelease.add(dHost);
          }
        }
      }
    }
    for (    DedicatedResourceVO dr : hostsToRelease) {
      releaseDedicatedResource(null,null,null,dr.getHostId());
    }
  }
  checkHostsSuitabilityForExplicitDedication(accountId,childDomainIds,hosts);
  final Long accountIdFinal=accountId;
  return Transaction.execute(new TransactionCallback<List<DedicatedResourceVO>>(){
    @Override public List<DedicatedResourceVO> doInTransaction(    TransactionStatus status){
      AffinityGroup group=findOrCreateDedicatedAffinityGroup(domainId,accountIdFinal);
      if (group == null) {
        s_logger.error("String_Node_Str");
        throw new CloudRuntimeException("String_Node_Str");
      }
      DedicatedResourceVO dedicatedResource=new DedicatedResourceVO(zoneId,null,null,null,null,null,group.getId());
      try {
        dedicatedResource.setDomainId(domainId);
        if (accountIdFinal != null) {
          dedicatedResource.setAccountId(accountIdFinal);
        }
        dedicatedResource=_dedicatedDao.persist(dedicatedResource);
        dc.setDomainId(domainId);
        if (!_zoneDao.update(zoneId,dc)) {
          throw new CloudRuntimeException("String_Node_Str");
        }
      }
 catch (      Exception e) {
        s_logger.error("String_Node_Str" + e.getMessage(),e);
        throw new CloudRuntimeException("String_Node_Str");
      }
      List<DedicatedResourceVO> result=new ArrayList<DedicatedResourceVO>();
      result.add(dedicatedResource);
      return result;
    }
  }
);
}

Fixed version (ground truth):
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_DEDICATE_RESOURCE,eventDescription="String_Node_Str") public List<DedicatedResourceVO> dedicateZone(final Long zoneId,final Long domainId,final String accountName){
  Long accountId=null;
  List<HostVO> hosts=null;
  if (accountName != null) {
    Account caller=CallContext.current().getCallingAccount();
    Account owner=_accountMgr.finalizeOwner(caller,accountName,domainId,null);
    accountId=owner.getId();
  }
  List<Long> childDomainIds=getDomainChildIds(domainId);
  childDomainIds.add(domainId);
  checkAccountAndDomain(accountId,domainId);
  final DataCenterVO dc=_zoneDao.findById(zoneId);
  if (dc == null) {
    throw new InvalidParameterValueException("String_Node_Str" + zoneId);
  }
 else {
    DedicatedResourceVO dedicatedZone=_dedicatedDao.findByZoneId(zoneId);
    if (dedicatedZone != null) {
      s_logger.error("String_Node_Str" + dc.getName() + "String_Node_Str");
      throw new CloudRuntimeException("String_Node_Str" + dc.getName() + "String_Node_Str");
    }
    List<HostPodVO> pods=_podDao.listByDataCenterId(dc.getId());
    List<DedicatedResourceVO> podsToRelease=new ArrayList<DedicatedResourceVO>();
    List<DedicatedResourceVO> clustersToRelease=new ArrayList<DedicatedResourceVO>();
    List<DedicatedResourceVO> hostsToRelease=new ArrayList<DedicatedResourceVO>();
    for (    HostPodVO pod : pods) {
      DedicatedResourceVO dPod=_dedicatedDao.findByPodId(pod.getId());
      if (dPod != null) {
        if (!(childDomainIds.contains(dPod.getDomainId()))) {
          throw new CloudRuntimeException("String_Node_Str" + pod.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
        }
        if (accountId != null) {
          if (dPod.getAccountId().equals(accountId)) {
            podsToRelease.add(dPod);
          }
 else {
            s_logger.error("String_Node_Str" + pod.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
            throw new CloudRuntimeException("String_Node_Str" + pod.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
          }
        }
 else {
          if (dPod.getAccountId() == null && dPod.getDomainId().equals(domainId)) {
            podsToRelease.add(dPod);
          }
        }
      }
    }
    for (    DedicatedResourceVO dr : podsToRelease) {
      releaseDedicatedResource(null,dr.getPodId(),null,null);
    }
    List<ClusterVO> clusters=_clusterDao.listClustersByDcId(dc.getId());
    for (    ClusterVO cluster : clusters) {
      DedicatedResourceVO dCluster=_dedicatedDao.findByClusterId(cluster.getId());
      if (dCluster != null) {
        if (!(childDomainIds.contains(dCluster.getDomainId()))) {
          throw new CloudRuntimeException("String_Node_Str" + cluster.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
        }
        if (accountId != null) {
          if (dCluster.getAccountId().equals(accountId)) {
            clustersToRelease.add(dCluster);
          }
 else {
            s_logger.error("String_Node_Str" + cluster.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
            throw new CloudRuntimeException("String_Node_Str" + cluster.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
          }
        }
 else {
          if (dCluster.getAccountId() == null && dCluster.getDomainId().equals(domainId)) {
            clustersToRelease.add(dCluster);
          }
        }
      }
    }
    for (    DedicatedResourceVO dr : clustersToRelease) {
      releaseDedicatedResource(null,null,dr.getClusterId(),null);
    }
    hosts=_hostDao.listByDataCenterId(dc.getId());
    for (    HostVO host : hosts) {
      DedicatedResourceVO dHost=_dedicatedDao.findByHostId(host.getId());
      if (dHost != null) {
        if (!(childDomainIds.contains(dHost.getDomainId()))) {
          throw new CloudRuntimeException("String_Node_Str" + host.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
        }
        if (accountId != null) {
          if (dHost.getAccountId().equals(accountId)) {
            hostsToRelease.add(dHost);
          }
 else {
            s_logger.error("String_Node_Str" + host.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
            throw new CloudRuntimeException("String_Node_Str" + host.getName() + "String_Node_Str"+ dc.getName()+ "String_Node_Str");
          }
        }
 else {
          if (dHost.getAccountId() == null && dHost.getDomainId().equals(domainId)) {
            hostsToRelease.add(dHost);
          }
        }
      }
    }
    for (    DedicatedResourceVO dr : hostsToRelease) {
      releaseDedicatedResource(null,null,null,dr.getHostId());
    }
  }
  checkHostsSuitabilityForExplicitDedication(accountId,childDomainIds,hosts);
  final Long accountIdFinal=accountId;
  return Transaction.execute(new TransactionCallback<List<DedicatedResourceVO>>(){
    @Override public List<DedicatedResourceVO> doInTransaction(    TransactionStatus status){
      AffinityGroup group=findOrCreateDedicatedAffinityGroup(domainId,accountIdFinal);
      if (group == null) {
        s_logger.error("String_Node_Str");
        throw new CloudRuntimeException("String_Node_Str");
      }
      DedicatedResourceVO dedicatedResource=new DedicatedResourceVO(zoneId,null,null,null,null,null,group.getId());
      try {
        dedicatedResource.setDomainId(domainId);
        if (accountIdFinal != null) {
          dedicatedResource.setAccountId(accountIdFinal);
        }
        dedicatedResource=_dedicatedDao.persist(dedicatedResource);
        dc.setDomainId(domainId);
        if (!_zoneDao.update(zoneId,dc)) {
          throw new CloudRuntimeException("String_Node_Str");
        }
      }
 catch (      Exception e) {
        s_logger.error("String_Node_Str" + e.getMessage(),e);
        throw new CloudRuntimeException("String_Node_Str");
      }
      List<DedicatedResourceVO> result=new ArrayList<DedicatedResourceVO>();
      result.add(dedicatedResource);
      return result;
    }
  }
);
}