Program: 107187
Project: 107187
Buggy source:
@Override @ActionEvent(eventType=EventTypes.EVENT_VOLUME_CREATE,eventDescription="String_Node_Str",create=true) public VolumeVO allocVolume(CreateVolumeCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, ResourceAllocationException {
  Account account=UserContext.current().getCaller();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  Account targetAccount=null;
  if ((account == null) || isAdmin(account.getType())) {
    if ((domainId != null) && (accountName != null)) {
      if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(),domainId)) {
        throw new PermissionDeniedException("String_Node_Str" + domainId + "String_Node_Str");
      }
      targetAccount=_accountDao.findActiveAccount(accountName,domainId);
    }
 else {
      targetAccount=account;
    }
    if (targetAccount == null) {
      throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
    }
  }
 else {
    targetAccount=account;
  }
  if (_accountMgr.resourceLimitExceeded(targetAccount,ResourceType.volume)) {
    ResourceAllocationException rae=new ResourceAllocationException("String_Node_Str" + targetAccount.getAccountName() + "String_Node_Str");
    rae.setResourceType("String_Node_Str");
    throw rae;
  }
  Long zoneId=null;
  Long diskOfferingId=null;
  Long size=null;
  if (cmd.getSnapshotId() == null && cmd.getDiskOfferingId() == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (cmd.getSnapshotId() == null) {
    zoneId=cmd.getZoneId();
    if ((zoneId == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    diskOfferingId=cmd.getDiskOfferingId();
    size=cmd.getSize();
    if (diskOfferingId == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DiskOfferingVO diskOffering=_diskOfferingDao.findById(diskOfferingId);
    if ((diskOffering == null) || !DiskOfferingVO.Type.Disk.equals(diskOffering.getType())) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if ((diskOffering.isCustomized() && size == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (diskOffering.isCustomized() && size != null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (diskOffering.getDomainId() == null) {
    }
 else {
      _configMgr.checkDiskOfferingAccess(account,diskOffering);
    }
    if (!validateVolumeSizeRange(diskOffering.getDiskSize() / 1024)) {
      throw new InvalidParameterValueException("String_Node_Str" + size + "String_Node_Str"+ _maxVolumeSizeInGb);
    }
    if (diskOffering.getDiskSize() > 0) {
      size=(diskOffering.getDiskSize() * 1024 * 1024);
    }
 else {
      if (!validateVolumeSizeRange(size)) {
        throw new InvalidParameterValueException("String_Node_Str" + size + "String_Node_Str"+ _maxVolumeSizeInGb);
      }
      size=(size * 1024 * 1024* 1024);
    }
  }
 else {
    Long snapshotId=cmd.getSnapshotId();
    Snapshot snapshotCheck=_snapshotDao.findById(snapshotId);
    if (snapshotCheck == null) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + snapshotId);
    }
    VolumeVO vol=_volsDao.findById(snapshotCheck.getVolumeId());
    zoneId=vol.getDataCenterId();
    diskOfferingId=vol.getDiskOfferingId();
    size=vol.getSize();
    if (account != null) {
      if (isAdmin(account.getType())) {
        Account snapshotOwner=_accountDao.findById(snapshotCheck.getAccountId());
        if (!_domainDao.isChildDomain(account.getDomainId(),snapshotOwner.getDomainId())) {
          throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + snapshotId + "String_Node_Str");
        }
      }
 else       if (account.getId() != snapshotCheck.getAccountId()) {
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + snapshotId + "String_Node_Str");
      }
    }
  }
  List<StoragePoolVO> storagePools=_storagePoolDao.listByDataCenterId(zoneId);
  boolean sharedPoolExists=false;
  for (  StoragePoolVO storagePool : storagePools) {
    if (storagePool.isShared()) {
      sharedPoolExists=true;
    }
  }
  List<HostVO> hosts=_hostDao.listByDataCenter(zoneId);
  if (hosts.isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (!sharedPoolExists) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String userSpecifiedName=cmd.getVolumeName();
  if (userSpecifiedName == null) {
    userSpecifiedName=getRandomVolumeName();
  }
  VolumeVO volume=new VolumeVO(userSpecifiedName,-1,-1,-1,-1,new Long(-1),null,null,0,Volume.VolumeType.DATADISK);
  volume.setPoolId(null);
  volume.setDataCenterId(zoneId);
  volume.setPodId(null);
  volume.setAccountId(targetAccount.getId());
  volume.setDomainId(((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId()));
  volume.setDiskOfferingId(diskOfferingId);
  volume.setSize(size);
  volume.setStorageResourceType(StorageResourceType.STORAGE_POOL);
  volume.setInstanceId(null);
  volume.setUpdated(new Date());
  volume.setStatus(AsyncInstanceCreateStatus.Creating);
  volume.setDomainId((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId());
  volume.setState(Volume.State.Allocated);
  volume=_volsDao.persist(volume);
  UserContext.current().setEventDetails("String_Node_Str" + volume.getId());
  return volume;
}

Fixed version (ground truth):
@Override @ActionEvent(eventType=EventTypes.EVENT_VOLUME_CREATE,eventDescription="String_Node_Str",create=true) public VolumeVO allocVolume(CreateVolumeCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, ResourceAllocationException {
  Account account=UserContext.current().getCaller();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  Account targetAccount=null;
  if ((account == null) || isAdmin(account.getType())) {
    if ((domainId != null) && (accountName != null)) {
      if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(),domainId)) {
        throw new PermissionDeniedException("String_Node_Str" + domainId + "String_Node_Str");
      }
      targetAccount=_accountDao.findActiveAccount(accountName,domainId);
    }
 else {
      targetAccount=account;
    }
    if (targetAccount == null) {
      throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
    }
  }
 else {
    targetAccount=account;
  }
  if (_accountMgr.resourceLimitExceeded(targetAccount,ResourceType.volume)) {
    ResourceAllocationException rae=new ResourceAllocationException("String_Node_Str" + targetAccount.getAccountName() + "String_Node_Str");
    rae.setResourceType("String_Node_Str");
    throw rae;
  }
  Long zoneId=null;
  Long diskOfferingId=null;
  Long size=null;
  if (cmd.getSnapshotId() == null && cmd.getDiskOfferingId() == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (cmd.getSnapshotId() == null) {
    zoneId=cmd.getZoneId();
    if ((zoneId == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    diskOfferingId=cmd.getDiskOfferingId();
    size=cmd.getSize();
    if (diskOfferingId == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DiskOfferingVO diskOffering=_diskOfferingDao.findById(diskOfferingId);
    if ((diskOffering == null) || !DiskOfferingVO.Type.Disk.equals(diskOffering.getType())) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if ((diskOffering.isCustomized() && size == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (!diskOffering.isCustomized() && size != null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (diskOffering.getDomainId() == null) {
    }
 else {
      _configMgr.checkDiskOfferingAccess(account,diskOffering);
    }
    if (!validateVolumeSizeRange(diskOffering.getDiskSize() / 1024)) {
      throw new InvalidParameterValueException("String_Node_Str" + size + "String_Node_Str"+ _maxVolumeSizeInGb);
    }
    if (diskOffering.getDiskSize() > 0) {
      size=(diskOffering.getDiskSize() * 1024 * 1024);
    }
 else {
      if (!validateVolumeSizeRange(size)) {
        throw new InvalidParameterValueException("String_Node_Str" + size + "String_Node_Str"+ _maxVolumeSizeInGb);
      }
      size=(size * 1024 * 1024* 1024);
    }
  }
 else {
    Long snapshotId=cmd.getSnapshotId();
    Snapshot snapshotCheck=_snapshotDao.findById(snapshotId);
    if (snapshotCheck == null) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + snapshotId);
    }
    VolumeVO vol=_volsDao.findById(snapshotCheck.getVolumeId());
    zoneId=vol.getDataCenterId();
    diskOfferingId=vol.getDiskOfferingId();
    size=vol.getSize();
    if (account != null) {
      if (isAdmin(account.getType())) {
        Account snapshotOwner=_accountDao.findById(snapshotCheck.getAccountId());
        if (!_domainDao.isChildDomain(account.getDomainId(),snapshotOwner.getDomainId())) {
          throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + snapshotId + "String_Node_Str");
        }
      }
 else       if (account.getId() != snapshotCheck.getAccountId()) {
        throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + snapshotId + "String_Node_Str");
      }
    }
  }
  List<StoragePoolVO> storagePools=_storagePoolDao.listByDataCenterId(zoneId);
  boolean sharedPoolExists=false;
  for (  StoragePoolVO storagePool : storagePools) {
    if (storagePool.isShared()) {
      sharedPoolExists=true;
    }
  }
  List<HostVO> hosts=_hostDao.listByDataCenter(zoneId);
  if (hosts.isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (!sharedPoolExists) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String userSpecifiedName=cmd.getVolumeName();
  if (userSpecifiedName == null) {
    userSpecifiedName=getRandomVolumeName();
  }
  VolumeVO volume=new VolumeVO(userSpecifiedName,-1,-1,-1,-1,new Long(-1),null,null,0,Volume.VolumeType.DATADISK);
  volume.setPoolId(null);
  volume.setDataCenterId(zoneId);
  volume.setPodId(null);
  volume.setAccountId(targetAccount.getId());
  volume.setDomainId(((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId()));
  volume.setDiskOfferingId(diskOfferingId);
  volume.setSize(size);
  volume.setStorageResourceType(StorageResourceType.STORAGE_POOL);
  volume.setInstanceId(null);
  volume.setUpdated(new Date());
  volume.setStatus(AsyncInstanceCreateStatus.Creating);
  volume.setDomainId((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId());
  volume.setState(Volume.State.Allocated);
  volume=_volsDao.persist(volume);
  UserContext.current().setEventDetails("String_Node_Str" + volume.getId());
  return volume;
}