Program: 106068
Project: 106068
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<Account> permittedAccounts,Account caller,ListProjectResourcesCriteria listProjectResourcesCriteria,Map<String,String> tags){
  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.findById(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 {
    DomainVO domain=null;
    if (!permittedAccounts.isEmpty()) {
      domain=_domainDao.findById(permittedAccounts.get(0).getDomainId());
    }
 else {
      domain=_domainDao.findById(DomainVO.ROOT_DOMAIN);
    }
    if (listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.NEQ,Account.ACCOUNT_TYPE_PROJECT);
    }
 else     if (listProjectResourcesCriteria == ListProjectResourcesCriteria.ListProjectResourcesOnly) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,Account.ACCOUNT_TYPE_PROJECT);
    }
    if ((templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) && (_accountMgr.isDomainAdmin(caller.getId()) || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN)) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.LIKE,domain.getPath() + "String_Node_Str");
    }
    List<Long> relatedDomainIds=new ArrayList<Long>();
    List<Long> permittedAccountIds=new ArrayList<Long>();
    if (!permittedAccounts.isEmpty()) {
      for (      Account account : permittedAccounts) {
        permittedAccountIds.add(account.getId());
        DomainVO accountDomain=_domainDao.findById(account.getDomainId());
        DomainVO domainTreeNode=accountDomain;
        relatedDomainIds.add(domainTreeNode.getId());
        while (domainTreeNode.getParent() != null) {
          domainTreeNode=_domainDao.findById(domainTreeNode.getParent());
          relatedDomainIds.add(domainTreeNode.getId());
        }
        if (_accountMgr.isAdmin(account.getType()) || (templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community)) {
          List<DomainVO> allChildDomains=_domainDao.findAllChildren(accountDomain.getPath(),accountDomain.getId());
          for (          DomainVO childDomain : allChildDomains) {
            relatedDomainIds.add(childDomain.getId());
          }
        }
      }
    }
    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);
      }
    }
    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);
      }
      if (!permittedAccounts.isEmpty()) {
        SearchCriteria<TemplateJoinVO> scc=_templateJoinDao.createSearchCriteria();
        scc.addOr("String_Node_Str",SearchCriteria.Op.IN,relatedDomainIds.toArray());
        scc.addOr("String_Node_Str",SearchCriteria.Op.NULL);
        sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,scc);
      }
    }
 else     if (templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) {
      if (!permittedAccounts.isEmpty()) {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.IN,permittedAccountIds.toArray());
      }
    }
 else     if (templateFilter == TemplateFilter.sharedexecutable || templateFilter == TemplateFilter.shared) {
      SearchCriteria<TemplateJoinVO> scc=_templateJoinDao.createSearchCriteria();
      scc.addOr("String_Node_Str",SearchCriteria.Op.IN,permittedAccountIds.toArray());
      scc.addOr("String_Node_Str",SearchCriteria.Op.IN,permittedAccountIds.toArray());
      sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,scc);
    }
 else     if (templateFilter == TemplateFilter.executable) {
      SearchCriteria<TemplateJoinVO> scc=_templateJoinDao.createSearchCriteria();
      scc.addOr("String_Node_Str",SearchCriteria.Op.EQ,true);
      if (!permittedAccounts.isEmpty()) {
        scc.addOr("String_Node_Str",SearchCriteria.Op.IN,permittedAccountIds.toArray());
      }
      sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,scc);
    }
    if (tags != null && !tags.isEmpty()) {
      SearchCriteria<TemplateJoinVO> scc=_templateJoinDao.createSearchCriteria();
      int count=0;
      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);
        count++;
      }
      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=_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(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){
  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.findById(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);
      }
      List<Long> domainTree=new ArrayList<Long>();
      DomainVO domainTreeNode=callerDomain;
      domainTree.add(domainTreeNode.getId());
      while (domainTreeNode.getParent() != null) {
        domainTreeNode=_domainDao.findById(domainTreeNode.getParent());
        domainTree.add(domainTreeNode.getId());
      }
      List<DomainVO> allChildDomains=_domainDao.findAllChildren(callerDomain.getPath(),callerDomain.getId());
      for (      DomainVO childDomain : allChildDomains) {
        domainTree.add(childDomain.getId());
      }
      SearchCriteria<TemplateJoinVO> scc=_templateJoinDao.createSearchCriteria();
      scc.addOr("String_Node_Str",SearchCriteria.Op.IN,domainTree.toArray());
      scc.addOr("String_Node_Str",SearchCriteria.Op.NULL);
      sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,scc);
    }
 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();
      int count=0;
      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);
        count++;
      }
      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=_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(tzIds);
  return new Pair<List<TemplateJoinVO>,Integer>(vrs,count);
}