Program: 89876
Project: 89876
Buggy source:
@Override @DB public VMTemplateVO createPrivateTemplate(CreateTemplateCmd command) throws CloudRuntimeException {
  Long userId=UserContext.current().getCallerUserId();
  if (userId == null) {
    userId=User.UID_SYSTEM;
  }
  long templateId=command.getEntityId();
  Long volumeId=command.getVolumeId();
  Long snapshotId=command.getSnapshotId();
  SnapshotVO snapshot=null;
  if (snapshotId != null) {
    snapshot=_snapshotDao.findById(snapshotId);
    volumeId=snapshot.getVolumeId();
  }
  VolumeVO volume=_volsDao.findById(volumeId);
  String vmName=_storageMgr.getVmNameOnVolume(volume);
  if (snapshotId == null && !_storageMgr.volumeInactive(volume)) {
    String msg="String_Node_Str" + volume.getName() + "String_Node_Str";
    if (s_logger.isInfoEnabled()) {
      s_logger.info(msg);
    }
    throw new CloudRuntimeException(msg);
  }
  SnapshotCommand cmd=null;
  VMTemplateVO privateTemplate=null;
  long zoneId=volume.getDataCenterId();
  String uniqueName=getRandomPrivateTemplateName();
  HostVO secondaryStorageHost=_storageMgr.getSecondaryStorageHost(zoneId);
  String secondaryStorageURL=_storageMgr.getSecondaryStorageURL(zoneId);
  if (secondaryStorageHost == null || secondaryStorageURL == null) {
    throw new CloudRuntimeException("String_Node_Str" + zoneId);
  }
  if (snapshotId != null) {
    volume=_volsDao.findById(volumeId);
    StringBuilder userFolder=new StringBuilder();
    Formatter userFolderFormat=new Formatter(userFolder);
    userFolderFormat.format("String_Node_Str",snapshot.getAccountId());
    String name=command.getTemplateName();
    String backupSnapshotUUID=snapshot.getBackupSnapshotId();
    if (backupSnapshotUUID == null) {
      throw new CloudRuntimeException("String_Node_Str" + snapshotId + "String_Node_Str");
    }
    Long dcId=volume.getDataCenterId();
    Long accountId=volume.getAccountId();
    String origTemplateInstallPath=null;
    cmd=new CreatePrivateTemplateFromSnapshotCommand(_storageMgr.getPrimaryStorageNameLabel(volume),secondaryStorageURL,dcId,accountId,snapshot.getVolumeId(),backupSnapshotUUID,snapshot.getName(),origTemplateInstallPath,templateId,name);
  }
 else   if (volumeId != null) {
    volume=_volsDao.findById(volumeId);
    if (volume == null) {
      throw new CloudRuntimeException("String_Node_Str" + volumeId);
    }
    if (volume.getPoolId() == null) {
      _templateDao.remove(templateId);
      throw new CloudRuntimeException("String_Node_Str" + volumeId + "String_Node_Str");
    }
    Long instanceId=volume.getInstanceId();
    if (instanceId != null) {
      VMInstanceVO vm=_vmDao.findById(instanceId);
      State vmState=vm.getState();
      if (!vmState.equals(State.Stopped) && !vmState.equals(State.Destroyed)) {
        throw new CloudRuntimeException("String_Node_Str" + vm.getName() + "String_Node_Str");
      }
    }
    cmd=new CreatePrivateTemplateFromVolumeCommand(secondaryStorageURL,templateId,volume.getAccountId(),command.getTemplateName(),uniqueName,volume.getPath(),vmName);
  }
 else {
    throw new CloudRuntimeException("String_Node_Str");
  }
  CreatePrivateTemplateAnswer answer=(CreatePrivateTemplateAnswer)_storageMgr.sendToHostsOnStoragePool(volume.getPoolId(),cmd,null);
  if ((answer != null) && answer.getResult()) {
    privateTemplate=_templateDao.findById(templateId);
    Long origTemplateId=volume.getTemplateId();
    VMTemplateVO origTemplate=null;
    if (origTemplateId != null) {
      origTemplate=_templateDao.findById(origTemplateId);
    }
    if ((origTemplate != null) && !Storage.ImageFormat.ISO.equals(origTemplate.getFormat())) {
      privateTemplate.setRequiresHvm(origTemplate.requiresHvm());
      privateTemplate.setBits(origTemplate.getBits());
    }
 else {
      privateTemplate.setRequiresHvm(true);
      privateTemplate.setBits(64);
    }
    String answerUniqueName=answer.getUniqueName();
    if (answerUniqueName != null) {
      privateTemplate.setUniqueName(answerUniqueName);
    }
 else {
      privateTemplate.setUniqueName(uniqueName);
    }
    ImageFormat format=answer.getImageFormat();
    if (format != null) {
      privateTemplate.setFormat(format);
    }
 else {
      privateTemplate.setFormat(ImageFormat.RAW);
    }
    _templateDao.update(templateId,privateTemplate);
    _templateDao.addTemplateToZone(privateTemplate,zoneId);
    VMTemplateHostVO templateHostVO=new VMTemplateHostVO(secondaryStorageHost.getId(),templateId);
    templateHostVO.setDownloadPercent(100);
    templateHostVO.setDownloadState(Status.DOWNLOADED);
    templateHostVO.setInstallPath(answer.getPath());
    templateHostVO.setLastUpdated(new Date());
    templateHostVO.setSize(answer.getVirtualSize());
    templateHostVO.setPhysicalSize(answer.getphysicalSize());
    _templateHostDao.persist(templateHostVO);
    _accountMgr.incrementResourceCount(volume.getAccountId(),ResourceType.template);
  }
 else {
    _templateDao.remove(templateId);
    throw new CloudRuntimeException("String_Node_Str" + answer.getDetails());
  }
  return privateTemplate;
}

Fixed version (ground truth):
@Override @DB public VMTemplateVO createPrivateTemplate(CreateTemplateCmd command) throws CloudRuntimeException {
  Long userId=UserContext.current().getCallerUserId();
  if (userId == null) {
    userId=User.UID_SYSTEM;
  }
  long templateId=command.getEntityId();
  Long volumeId=command.getVolumeId();
  Long snapshotId=command.getSnapshotId();
  SnapshotVO snapshot=null;
  if (snapshotId != null) {
    snapshot=_snapshotDao.findById(snapshotId);
    volumeId=snapshot.getVolumeId();
  }
  VolumeVO volume=_volsDao.findById(volumeId);
  String vmName=_storageMgr.getVmNameOnVolume(volume);
  if (snapshotId == null && !_storageMgr.volumeInactive(volume)) {
    String msg="String_Node_Str" + volume.getName() + "String_Node_Str";
    if (s_logger.isInfoEnabled()) {
      s_logger.info(msg);
    }
    throw new CloudRuntimeException(msg);
  }
  SnapshotCommand cmd=null;
  VMTemplateVO privateTemplate=null;
  long zoneId=volume.getDataCenterId();
  String uniqueName=getRandomPrivateTemplateName();
  HostVO secondaryStorageHost=_storageMgr.getSecondaryStorageHost(zoneId);
  String secondaryStorageURL=_storageMgr.getSecondaryStorageURL(zoneId);
  if (secondaryStorageHost == null || secondaryStorageURL == null) {
    throw new CloudRuntimeException("String_Node_Str" + zoneId);
  }
  if (snapshotId != null) {
    volume=_volsDao.findById(volumeId);
    StringBuilder userFolder=new StringBuilder();
    Formatter userFolderFormat=new Formatter(userFolder);
    userFolderFormat.format("String_Node_Str",snapshot.getAccountId());
    String name=command.getTemplateName();
    String backupSnapshotUUID=snapshot.getBackupSnapshotId();
    if (backupSnapshotUUID == null) {
      throw new CloudRuntimeException("String_Node_Str" + snapshotId + "String_Node_Str");
    }
    Long dcId=volume.getDataCenterId();
    Long accountId=volume.getAccountId();
    String origTemplateInstallPath=null;
    cmd=new CreatePrivateTemplateFromSnapshotCommand(_storageMgr.getPrimaryStorageNameLabel(volume),secondaryStorageURL,dcId,accountId,snapshot.getVolumeId(),backupSnapshotUUID,snapshot.getName(),origTemplateInstallPath,templateId,name);
  }
 else   if (volumeId != null) {
    volume=_volsDao.findById(volumeId);
    if (volume == null) {
      throw new CloudRuntimeException("String_Node_Str" + volumeId);
    }
    if (volume.getPoolId() == null) {
      _templateDao.remove(templateId);
      throw new CloudRuntimeException("String_Node_Str" + volumeId + "String_Node_Str");
    }
    Long instanceId=volume.getInstanceId();
    if (instanceId != null) {
      VMInstanceVO vm=_vmDao.findById(instanceId);
      State vmState=vm.getState();
      if (!vmState.equals(State.Stopped) && !vmState.equals(State.Destroyed)) {
        throw new CloudRuntimeException("String_Node_Str" + vm.getName() + "String_Node_Str");
      }
    }
    cmd=new CreatePrivateTemplateFromVolumeCommand(secondaryStorageURL,templateId,volume.getAccountId(),command.getTemplateName(),uniqueName,volume.getPath(),vmName);
  }
 else {
    throw new CloudRuntimeException("String_Node_Str");
  }
  CreatePrivateTemplateAnswer answer=(CreatePrivateTemplateAnswer)_storageMgr.sendToHostsOnStoragePool(volume.getPoolId(),cmd,null);
  if ((answer != null) && answer.getResult()) {
    privateTemplate=_templateDao.findById(templateId);
    Long origTemplateId=volume.getTemplateId();
    VMTemplateVO origTemplate=null;
    if (origTemplateId != null) {
      origTemplate=_templateDao.findById(origTemplateId);
    }
    if ((origTemplate != null) && !Storage.ImageFormat.ISO.equals(origTemplate.getFormat())) {
      privateTemplate.setRequiresHvm(origTemplate.requiresHvm());
      privateTemplate.setBits(origTemplate.getBits());
    }
 else {
      privateTemplate.setRequiresHvm(true);
      privateTemplate.setBits(64);
    }
    String answerUniqueName=answer.getUniqueName();
    if (answerUniqueName != null) {
      privateTemplate.setUniqueName(answerUniqueName);
    }
 else {
      privateTemplate.setUniqueName(uniqueName);
    }
    ImageFormat format=answer.getImageFormat();
    if (format != null) {
      privateTemplate.setFormat(format);
    }
 else {
      privateTemplate.setFormat(ImageFormat.RAW);
    }
    if (snapshot != null)     privateTemplate.setHypervisorType(snapshot.getHypervisorType());
    _templateDao.update(templateId,privateTemplate);
    _templateDao.addTemplateToZone(privateTemplate,zoneId);
    VMTemplateHostVO templateHostVO=new VMTemplateHostVO(secondaryStorageHost.getId(),templateId);
    templateHostVO.setDownloadPercent(100);
    templateHostVO.setDownloadState(Status.DOWNLOADED);
    templateHostVO.setInstallPath(answer.getPath());
    templateHostVO.setLastUpdated(new Date());
    templateHostVO.setSize(answer.getVirtualSize());
    templateHostVO.setPhysicalSize(answer.getphysicalSize());
    _templateHostDao.persist(templateHostVO);
    _accountMgr.incrementResourceCount(volume.getAccountId(),ResourceType.template);
  }
 else {
    _templateDao.remove(templateId);
    throw new CloudRuntimeException("String_Node_Str" + answer.getDetails());
  }
  return privateTemplate;
}