Program: 106271
Project: 106271
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){
  VMTemplateVO template=null;
  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,templateId,"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,templateId,"String_Node_Str");
      throw ex;
    }
    if (!template.isPublicTemplate() && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
      Account owner=_accountMgr.getAccount(template.getAccountId());
      _accountMgr.checkAccess(caller,null,true,owner);
    }
  }
  DomainVO domain=null;
  if (!permittedAccounts.isEmpty()) {
    domain=_domainDao.findById(permittedAccounts.get(0).getDomainId());
  }
 else {
    domain=_domainDao.findById(DomainVO.ROOT_DOMAIN);
  }
  List<HypervisorType> hypers=null;
  if (!isIso) {
    hypers=_resourceMgr.listAvailHypervisorInZone(null,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);
  SearchCriteria<TemplateJoinVO> sc=_templateJoinDao.createSearchCriteria();
  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) && (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || 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())) {
        List<DomainVO> allChildDomains=_domainDao.findAllChildren(accountDomain.getPath(),accountDomain.getId());
        for (        DomainVO childDomain : allChildDomains) {
          relatedDomainIds.add(childDomain.getId());
        }
      }
    }
  }
  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);
      if (!_accountMgr.isAdmin(caller.getType())) {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.IN,permittedAccountIds.toArray());
      }
    }
  }
 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,TaggedResourceType.ISO);
      }
 else {
        scTag.addAnd("String_Node_Str",SearchCriteria.Op.EQ,TaggedResourceType.Template);
      }
      scc.addOr("String_Node_Str",SearchCriteria.Op.SC,scTag);
      count++;
    }
    sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,scc);
  }
  if (templateId != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,templateId);
  }
 else   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) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,Status.DOWNLOADED);
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,false);
  }
  if (zoneId != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,zoneId);
  }
  if (!showDomr) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.NEQ,Storage.TemplateType.SYSTEM);
  }
  Pair<List<TemplateJoinVO>,Integer> uniqueTmplPair=_templateJoinDao.searchAndCount(sc,searchFilter);
  Integer count=uniqueTmplPair.second();
  if (count.intValue() == 0) {
    return uniqueTmplPair;
  }
  List<TemplateJoinVO> uniqueTmpls=uniqueTmplPair.first();
  Long[] vrIds=new Long[uniqueTmpls.size()];
  int i=0;
  for (  TemplateJoinVO v : uniqueTmpls) {
    vrIds[i++]=v.getId();
  }
  List<TemplateJoinVO> vrs=_templateJoinDao.searchByIds(vrIds);
  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<Account> permittedAccounts,Account caller,ListProjectResourcesCriteria listProjectResourcesCriteria,Map<String,String> tags){
  VMTemplateVO template=null;
  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,templateId,"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,templateId,"String_Node_Str");
      throw ex;
    }
    if (!template.isPublicTemplate() && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
      Account owner=_accountMgr.getAccount(template.getAccountId());
      _accountMgr.checkAccess(caller,null,true,owner);
    }
  }
  DomainVO domain=null;
  if (!permittedAccounts.isEmpty()) {
    domain=_domainDao.findById(permittedAccounts.get(0).getDomainId());
  }
 else {
    domain=_domainDao.findById(DomainVO.ROOT_DOMAIN);
  }
  List<HypervisorType> hypers=null;
  if (!isIso) {
    hypers=_resourceMgr.listAvailHypervisorInZone(null,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);
  SearchCriteria<TemplateJoinVO> sc=_templateJoinDao.createSearchCriteria();
  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) && (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || 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())) {
        List<DomainVO> allChildDomains=_domainDao.findAllChildren(accountDomain.getPath(),accountDomain.getId());
        for (        DomainVO childDomain : allChildDomains) {
          relatedDomainIds.add(childDomain.getId());
        }
      }
    }
  }
  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);
      if (!_accountMgr.isAdmin(caller.getType())) {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.IN,permittedAccountIds.toArray());
      }
    }
  }
 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,TaggedResourceType.ISO);
      }
 else {
        scTag.addAnd("String_Node_Str",SearchCriteria.Op.EQ,TaggedResourceType.Template);
      }
      scc.addOr("String_Node_Str",SearchCriteria.Op.SC,scTag);
      count++;
    }
    sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,scc);
  }
  if (templateId != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,templateId);
  }
 else   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) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,Status.DOWNLOADED);
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,false);
  }
  if (zoneId != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,zoneId);
  }
  if (!showDomr) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.NEQ,Storage.TemplateType.SYSTEM);
  }
  sc.addAnd("String_Node_Str",SearchCriteria.Op.NULL);
  Pair<List<TemplateJoinVO>,Integer> uniqueTmplPair=_templateJoinDao.searchAndCount(sc,searchFilter);
  Integer count=uniqueTmplPair.second();
  if (count.intValue() == 0) {
    return uniqueTmplPair;
  }
  List<TemplateJoinVO> uniqueTmpls=uniqueTmplPair.first();
  Long[] vrIds=new Long[uniqueTmpls.size()];
  int i=0;
  for (  TemplateJoinVO v : uniqueTmpls) {
    vrIds[i++]=v.getId();
  }
  List<TemplateJoinVO> vrs=_templateJoinDao.searchByIds(vrIds);
  return new Pair<List<TemplateJoinVO>,Integer>(vrs,count);
}