Program: 105509
Project: 105509
Buggy source:
@Override @DB public boolean copy(long userId,VMTemplateVO template,DataStore srcSecStore,DataCenterVO dstZone) throws StorageUnavailableException, ResourceAllocationException {
  long tmpltId=template.getId();
  long dstZoneId=dstZone.getId();
  List<DataStore> dstSecStores=_dataStoreMgr.getImageStoresByScope(new ZoneScope(dstZoneId));
  if (dstSecStores == null || dstSecStores.isEmpty()) {
    throw new StorageUnavailableException("String_Node_Str",DataCenter.class,dstZone.getId());
  }
  AccountVO account=_accountDao.findById(template.getAccountId());
  TemplateDataStoreVO srcTmpltStore=_tmplStoreDao.findByStoreTemplate(srcSecStore.getId(),tmpltId);
  _resourceLimitMgr.checkResourceLimit(account,ResourceType.template);
  _resourceLimitMgr.checkResourceLimit(account,ResourceType.secondary_storage,new Long(srcTmpltStore.getSize()).longValue());
  String copyEventType;
  String createEventType;
  if (template.getFormat().equals(ImageFormat.ISO)) {
    copyEventType=EventTypes.EVENT_ISO_COPY;
    createEventType=EventTypes.EVENT_ISO_CREATE;
  }
 else {
    copyEventType=EventTypes.EVENT_TEMPLATE_COPY;
    createEventType=EventTypes.EVENT_TEMPLATE_CREATE;
  }
  TemplateInfo srcTemplate=_tmplFactory.getTemplate(template.getId(),srcSecStore);
  for (  DataStore dstSecStore : dstSecStores) {
    TemplateDataStoreVO dstTmpltStore=_tmplStoreDao.findByStoreTemplate(dstSecStore.getId(),tmpltId);
    if (dstTmpltStore != null && dstTmpltStore.getDownloadState() == Status.DOWNLOADED) {
      return true;
    }
    if (dstTmpltStore != null && dstTmpltStore.getDownloadState() != Status.DOWNLOAD_IN_PROGRESS) {
      _tmplStoreDao.removeByTemplateStore(tmpltId,dstSecStore.getId());
    }
    AsyncCallFuture<TemplateApiResult> future=_tmpltSvr.copyTemplate(srcTemplate,dstSecStore);
    try {
      TemplateApiResult result=future.get();
      if (result.isFailed()) {
        s_logger.debug("String_Node_Str" + dstSecStore.getName() + "String_Node_Str"+ result.getResult());
        continue;
      }
      _tmpltDao.addTemplateToZone(template,dstZoneId);
      if (account.getId() != Account.ACCOUNT_ID_SYSTEM) {
        UsageEventUtils.publishUsageEvent(copyEventType,account.getId(),dstZoneId,tmpltId,null,null,null,srcTmpltStore.getPhysicalSize(),srcTmpltStore.getSize(),template.getClass().getName(),template.getUuid());
      }
      return true;
    }
 catch (    Exception ex) {
      s_logger.debug("String_Node_Str" + dstSecStore.getName() + "String_Node_Str");
    }
  }
  return false;
}

Fixed version (ground truth):
@Override @DB public boolean copy(long userId,VMTemplateVO template,DataStore srcSecStore,DataCenterVO dstZone) throws StorageUnavailableException, ResourceAllocationException {
  long tmpltId=template.getId();
  long dstZoneId=dstZone.getId();
  List<DataStore> dstSecStores=_dataStoreMgr.getImageStoresByScope(new ZoneScope(dstZoneId));
  if (dstSecStores == null || dstSecStores.isEmpty()) {
    throw new StorageUnavailableException("String_Node_Str",DataCenter.class,dstZone.getId());
  }
  AccountVO account=_accountDao.findById(template.getAccountId());
  TemplateDataStoreVO srcTmpltStore=_tmplStoreDao.findByStoreTemplate(srcSecStore.getId(),tmpltId);
  _resourceLimitMgr.checkResourceLimit(account,ResourceType.template);
  _resourceLimitMgr.checkResourceLimit(account,ResourceType.secondary_storage,new Long(srcTmpltStore.getSize()).longValue());
  String copyEventType;
  if (template.getFormat().equals(ImageFormat.ISO)) {
    copyEventType=EventTypes.EVENT_ISO_COPY;
  }
 else {
    copyEventType=EventTypes.EVENT_TEMPLATE_COPY;
  }
  TemplateInfo srcTemplate=_tmplFactory.getTemplate(template.getId(),srcSecStore);
  for (  DataStore dstSecStore : dstSecStores) {
    TemplateDataStoreVO dstTmpltStore=_tmplStoreDao.findByStoreTemplate(dstSecStore.getId(),tmpltId);
    if (dstTmpltStore != null && dstTmpltStore.getDownloadState() == Status.DOWNLOADED) {
      return true;
    }
    if (dstTmpltStore != null && dstTmpltStore.getDownloadState() != Status.DOWNLOAD_IN_PROGRESS) {
      _tmplStoreDao.removeByTemplateStore(tmpltId,dstSecStore.getId());
    }
    AsyncCallFuture<TemplateApiResult> future=_tmpltSvr.copyTemplate(srcTemplate,dstSecStore);
    try {
      TemplateApiResult result=future.get();
      if (result.isFailed()) {
        s_logger.debug("String_Node_Str" + dstSecStore.getName() + "String_Node_Str"+ result.getResult());
        continue;
      }
      _tmpltDao.addTemplateToZone(template,dstZoneId);
      if (account.getId() != Account.ACCOUNT_ID_SYSTEM) {
        UsageEventUtils.publishUsageEvent(copyEventType,account.getId(),dstZoneId,tmpltId,null,null,null,srcTmpltStore.getPhysicalSize(),srcTmpltStore.getSize(),template.getClass().getName(),template.getUuid());
      }
      return true;
    }
 catch (    Exception ex) {
      s_logger.debug("String_Node_Str" + dstSecStore.getName() + "String_Node_Str");
    }
  }
  return false;
}