Program: 105935
Project: 105935
Buggy source:
private Pair<List<TemplateJoinVO>,Integer> searchForTemplatesInternal(Long templateId,String name,String keyword,TemplateFilter templateFilter,boolean isIso,Boolean bootable,Long pageSize,Long startIndex,Long zoneId,HypervisorType hyperType,boolean showDomr,boolean onlyReady,List<Long> permittedDomains,List<Long> permittedAccounts,List<Long> permittedResources,boolean isRecursive,Account caller,ListProjectResourcesCriteria listProjectResourcesCriteria,Map<String,String> tags,boolean showRemovedTmpl){
  List<HypervisorType> hypers=null;
  if (!isIso) {
    hypers=_resourceMgr.listAvailHypervisorInZone(null,null);
    if (hypers == null || hypers.isEmpty()) {
      return new Pair<List<TemplateJoinVO>,Integer>(new ArrayList<TemplateJoinVO>(),0);
    }
  }
  VMTemplateVO template=null;
  Boolean isAscending=Boolean.parseBoolean(_configDao.getValue("String_Node_Str"));
  isAscending=(isAscending == null ? true : isAscending);
  Filter searchFilter=new Filter(TemplateJoinVO.class,"String_Node_Str",isAscending,startIndex,pageSize);
  SearchBuilder<TemplateJoinVO> sb=_templateJoinDao.createSearchBuilder();
  sb.select(null,Func.DISTINCT,sb.entity().getTempZonePair());
  SearchCriteria<TemplateJoinVO> sc=sb.create();
  if (templateId != null) {
    template=_templateDao.findByIdIncludingRemoved(templateId);
    if (template == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (isIso && template.getFormat() != ImageFormat.ISO) {
      s_logger.error("String_Node_Str" + templateId + "String_Node_Str");
      InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
      ex.addProxyObject(template.getUuid(),"String_Node_Str");
      throw ex;
    }
    if (!isIso && template.getFormat() == ImageFormat.ISO) {
      s_logger.error("String_Node_Str" + templateId);
      InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str" + template.getFormat() + "String_Node_Str");
      ex.addProxyObject(template.getUuid(),"String_Node_Str");
      throw ex;
    }
    if (!template.isPublicTemplate() && !_accountMgr.isRootAdmin(caller.getId())) {
      Account owner=_accountMgr.getAccount(template.getAccountId());
      _accountMgr.checkAccess(caller,null,true,owner);
    }
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,templateId);
  }
 else {
    if (!isIso) {
      if (hypers != null && !hypers.isEmpty()) {
        String[] relatedHypers=new String[hypers.size()];
        for (int i=0; i < hypers.size(); i++) {
          relatedHypers[i]=hypers.get(i).toString();
        }
        sc.addAnd("String_Node_Str",SearchCriteria.Op.IN,relatedHypers);
      }
    }
    DomainVO callerDomain=_domainDao.findById(caller.getDomainId());
    if (templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,true);
      if (templateFilter == TemplateFilter.featured) {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,true);
      }
 else {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,false);
      }
    }
 else     if (templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) {
      if (permittedDomains.contains(caller.getDomainId())) {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.LIKE,callerDomain.getPath() + "String_Node_Str");
      }
 else {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,caller.getAccountId());
      }
    }
 else     if (templateFilter == TemplateFilter.sharedexecutable || templateFilter == TemplateFilter.shared) {
      permittedDomains.remove(caller.getDomainId());
      permittedAccounts.remove(caller.getAccountId());
      SearchCriteria<TemplateJoinVO> aclSc=_templateJoinDao.createSearchCriteria();
      _accountMgr.buildACLViewSearchCriteria(sc,aclSc,isRecursive,permittedDomains,permittedAccounts,permittedResources,listProjectResourcesCriteria);
    }
 else     if (templateFilter == TemplateFilter.executable) {
      SearchCriteria<TemplateJoinVO> scc=_templateJoinDao.createSearchCriteria();
      scc.addOr("String_Node_Str",SearchCriteria.Op.EQ,true);
      if (permittedDomains.contains(caller.getDomainId())) {
        sc.addOr("String_Node_Str",SearchCriteria.Op.LIKE,callerDomain.getPath() + "String_Node_Str");
      }
 else {
        sc.addOr("String_Node_Str",SearchCriteria.Op.EQ,caller.getAccountId());
      }
      sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,scc);
    }
    if (tags != null && !tags.isEmpty()) {
      SearchCriteria<TemplateJoinVO> scc=_templateJoinDao.createSearchCriteria();
      for (      String key : tags.keySet()) {
        SearchCriteria<TemplateJoinVO> scTag=_templateJoinDao.createSearchCriteria();
        scTag.addAnd("String_Node_Str",SearchCriteria.Op.EQ,key);
        scTag.addAnd("String_Node_Str",SearchCriteria.Op.EQ,tags.get(key));
        if (isIso) {
          scTag.addAnd("String_Node_Str",SearchCriteria.Op.EQ,ResourceObjectType.ISO);
        }
 else {
          scTag.addAnd("String_Node_Str",SearchCriteria.Op.EQ,ResourceObjectType.Template);
        }
        scc.addOr("String_Node_Str",SearchCriteria.Op.SC,scTag);
      }
      sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,scc);
    }
    if (keyword != null) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.LIKE,"String_Node_Str" + keyword + "String_Node_Str");
    }
 else     if (name != null) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,name);
    }
    if (isIso) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,"String_Node_Str");
    }
 else {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.NEQ,"String_Node_Str");
    }
    if (!hyperType.equals(HypervisorType.None)) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,hyperType);
    }
    if (bootable != null) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,bootable);
    }
    if (onlyReady) {
      SearchCriteria<TemplateJoinVO> readySc=_templateJoinDao.createSearchCriteria();
      readySc.addOr("String_Node_Str",SearchCriteria.Op.EQ,TemplateState.Ready);
      readySc.addOr("String_Node_Str",SearchCriteria.Op.EQ,ImageFormat.BAREMETAL);
      SearchCriteria<TemplateJoinVO> isoPerhostSc=_templateJoinDao.createSearchCriteria();
      isoPerhostSc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,ImageFormat.ISO);
      isoPerhostSc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,TemplateType.PERHOST);
      readySc.addOr("String_Node_Str",SearchCriteria.Op.SC,isoPerhostSc);
      sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,readySc);
    }
    if (!showDomr) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.NEQ,Storage.TemplateType.SYSTEM);
    }
  }
  if (zoneId != null) {
    SearchCriteria<TemplateJoinVO> zoneSc=_templateJoinDao.createSearchCriteria();
    zoneSc.addOr("String_Node_Str",SearchCriteria.Op.EQ,zoneId);
    zoneSc.addOr("String_Node_Str",SearchCriteria.Op.EQ,ScopeType.REGION);
    SearchCriteria<TemplateJoinVO> isoPerhostSc=_templateJoinDao.createSearchCriteria();
    isoPerhostSc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,ImageFormat.ISO);
    isoPerhostSc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,TemplateType.PERHOST);
    zoneSc.addOr("String_Node_Str",SearchCriteria.Op.SC,isoPerhostSc);
    sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,zoneSc);
  }
  Pair<List<TemplateJoinVO>,Integer> uniqueTmplPair=null;
  if (showRemovedTmpl) {
    uniqueTmplPair=_templateJoinDao.searchIncludingRemovedAndCount(sc,searchFilter);
  }
 else {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,State.Active);
    uniqueTmplPair=_templateJoinDao.searchAndCount(sc,searchFilter);
  }
  Integer count=uniqueTmplPair.second();
  if (count.intValue() == 0) {
    return uniqueTmplPair;
  }
  List<TemplateJoinVO> uniqueTmpls=uniqueTmplPair.first();
  String[] tzIds=new String[uniqueTmpls.size()];
  int i=0;
  for (  TemplateJoinVO v : uniqueTmpls) {
    tzIds[i++]=v.getTempZonePair();
  }
  List<TemplateJoinVO> vrs=_templateJoinDao.searchByTemplateZonePair(showRemovedTmpl,tzIds);
  return new Pair<List<TemplateJoinVO>,Integer>(vrs,count);
}

Fixed version (ground truth):
private Pair<List<TemplateJoinVO>,Integer> searchForTemplatesInternal(Long templateId,String name,String keyword,TemplateFilter templateFilter,boolean isIso,Boolean bootable,Long pageSize,Long startIndex,Long zoneId,HypervisorType hyperType,boolean showDomr,boolean onlyReady,List<Long> permittedDomains,List<Long> permittedAccounts,List<Long> permittedResources,boolean isRecursive,Account caller,ListProjectResourcesCriteria listProjectResourcesCriteria,Map<String,String> tags,boolean showRemovedTmpl){
  List<HypervisorType> hypers=null;
  if (!isIso) {
    hypers=_resourceMgr.listAvailHypervisorInZone(null,null);
    if (hypers == null || hypers.isEmpty()) {
      return new Pair<List<TemplateJoinVO>,Integer>(new ArrayList<TemplateJoinVO>(),0);
    }
  }
  VMTemplateVO template=null;
  Boolean isAscending=Boolean.parseBoolean(_configDao.getValue("String_Node_Str"));
  isAscending=(isAscending == null ? true : isAscending);
  Filter searchFilter=new Filter(TemplateJoinVO.class,"String_Node_Str",isAscending,startIndex,pageSize);
  SearchBuilder<TemplateJoinVO> sb=_templateJoinDao.createSearchBuilder();
  sb.select(null,Func.DISTINCT,sb.entity().getTempZonePair());
  SearchCriteria<TemplateJoinVO> sc=sb.create();
  if (templateId != null) {
    template=_templateDao.findByIdIncludingRemoved(templateId);
    if (template == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (isIso && template.getFormat() != ImageFormat.ISO) {
      s_logger.error("String_Node_Str" + templateId + "String_Node_Str");
      InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
      ex.addProxyObject(template.getUuid(),"String_Node_Str");
      throw ex;
    }
    if (!isIso && template.getFormat() == ImageFormat.ISO) {
      s_logger.error("String_Node_Str" + templateId);
      InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str" + template.getFormat() + "String_Node_Str");
      ex.addProxyObject(template.getUuid(),"String_Node_Str");
      throw ex;
    }
    if (!template.isPublicTemplate() && !_accountMgr.isRootAdmin(caller.getId())) {
      Account owner=_accountMgr.getAccount(template.getAccountId());
      _accountMgr.checkAccess(caller,null,true,owner);
    }
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,templateId);
  }
 else {
    if (!isIso) {
      if (hypers != null && !hypers.isEmpty()) {
        String[] relatedHypers=new String[hypers.size()];
        for (int i=0; i < hypers.size(); i++) {
          relatedHypers[i]=hypers.get(i).toString();
        }
        sc.addAnd("String_Node_Str",SearchCriteria.Op.IN,relatedHypers);
      }
    }
    DomainVO callerDomain=_domainDao.findById(caller.getDomainId());
    if (templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,true);
      if (templateFilter == TemplateFilter.featured) {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,true);
      }
 else {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,false);
      }
    }
 else     if (templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) {
      if (permittedDomains.contains(caller.getDomainId())) {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.LIKE,callerDomain.getPath() + "String_Node_Str");
      }
 else {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,caller.getAccountId());
      }
    }
 else     if (templateFilter == TemplateFilter.sharedexecutable || templateFilter == TemplateFilter.shared) {
      permittedDomains.remove(caller.getDomainId());
      permittedAccounts.remove(caller.getAccountId());
      for (      Long tid : permittedResources) {
        VMTemplateVO tmpl=_templateDao.findById(tid);
        if (tmpl != null && tmpl.getAccountId() == caller.getAccountId()) {
          permittedResources.remove(tid);
        }
      }
      SearchCriteria<TemplateJoinVO> aclSc=_templateJoinDao.createSearchCriteria();
      _accountMgr.buildACLViewSearchCriteria(sc,aclSc,isRecursive,permittedDomains,permittedAccounts,permittedResources,listProjectResourcesCriteria);
    }
 else     if (templateFilter == TemplateFilter.executable) {
      SearchCriteria<TemplateJoinVO> scc=_templateJoinDao.createSearchCriteria();
      scc.addOr("String_Node_Str",SearchCriteria.Op.EQ,true);
      if (permittedDomains.contains(caller.getDomainId())) {
        sc.addOr("String_Node_Str",SearchCriteria.Op.LIKE,callerDomain.getPath() + "String_Node_Str");
      }
 else {
        sc.addOr("String_Node_Str",SearchCriteria.Op.EQ,caller.getAccountId());
      }
      sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,scc);
    }
    if (tags != null && !tags.isEmpty()) {
      SearchCriteria<TemplateJoinVO> scc=_templateJoinDao.createSearchCriteria();
      for (      String key : tags.keySet()) {
        SearchCriteria<TemplateJoinVO> scTag=_templateJoinDao.createSearchCriteria();
        scTag.addAnd("String_Node_Str",SearchCriteria.Op.EQ,key);
        scTag.addAnd("String_Node_Str",SearchCriteria.Op.EQ,tags.get(key));
        if (isIso) {
          scTag.addAnd("String_Node_Str",SearchCriteria.Op.EQ,ResourceObjectType.ISO);
        }
 else {
          scTag.addAnd("String_Node_Str",SearchCriteria.Op.EQ,ResourceObjectType.Template);
        }
        scc.addOr("String_Node_Str",SearchCriteria.Op.SC,scTag);
      }
      sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,scc);
    }
    if (keyword != null) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.LIKE,"String_Node_Str" + keyword + "String_Node_Str");
    }
 else     if (name != null) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,name);
    }
    if (isIso) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,"String_Node_Str");
    }
 else {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.NEQ,"String_Node_Str");
    }
    if (!hyperType.equals(HypervisorType.None)) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,hyperType);
    }
    if (bootable != null) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,bootable);
    }
    if (onlyReady) {
      SearchCriteria<TemplateJoinVO> readySc=_templateJoinDao.createSearchCriteria();
      readySc.addOr("String_Node_Str",SearchCriteria.Op.EQ,TemplateState.Ready);
      readySc.addOr("String_Node_Str",SearchCriteria.Op.EQ,ImageFormat.BAREMETAL);
      SearchCriteria<TemplateJoinVO> isoPerhostSc=_templateJoinDao.createSearchCriteria();
      isoPerhostSc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,ImageFormat.ISO);
      isoPerhostSc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,TemplateType.PERHOST);
      readySc.addOr("String_Node_Str",SearchCriteria.Op.SC,isoPerhostSc);
      sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,readySc);
    }
    if (!showDomr) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.NEQ,Storage.TemplateType.SYSTEM);
    }
  }
  if (zoneId != null) {
    SearchCriteria<TemplateJoinVO> zoneSc=_templateJoinDao.createSearchCriteria();
    zoneSc.addOr("String_Node_Str",SearchCriteria.Op.EQ,zoneId);
    zoneSc.addOr("String_Node_Str",SearchCriteria.Op.EQ,ScopeType.REGION);
    SearchCriteria<TemplateJoinVO> isoPerhostSc=_templateJoinDao.createSearchCriteria();
    isoPerhostSc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,ImageFormat.ISO);
    isoPerhostSc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,TemplateType.PERHOST);
    zoneSc.addOr("String_Node_Str",SearchCriteria.Op.SC,isoPerhostSc);
    sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,zoneSc);
  }
  Pair<List<TemplateJoinVO>,Integer> uniqueTmplPair=null;
  if (showRemovedTmpl) {
    uniqueTmplPair=_templateJoinDao.searchIncludingRemovedAndCount(sc,searchFilter);
  }
 else {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,State.Active);
    uniqueTmplPair=_templateJoinDao.searchAndCount(sc,searchFilter);
  }
  Integer count=uniqueTmplPair.second();
  if (count.intValue() == 0) {
    return uniqueTmplPair;
  }
  List<TemplateJoinVO> uniqueTmpls=uniqueTmplPair.first();
  String[] tzIds=new String[uniqueTmpls.size()];
  int i=0;
  for (  TemplateJoinVO v : uniqueTmpls) {
    tzIds[i++]=v.getTempZonePair();
  }
  List<TemplateJoinVO> vrs=_templateJoinDao.searchByTemplateZonePair(showRemovedTmpl,tzIds);
  return new Pair<List<TemplateJoinVO>,Integer>(vrs,count);
}