Program: 106290
Project: 106290
Buggy source:
@Override public Set<Pair<Long,Long>> searchTemplates(String name,String keyword,TemplateFilter templateFilter,boolean isIso,List<HypervisorType> hypers,Boolean bootable,DomainVO domain,Long pageSize,Long startIndex,Long zoneId,HypervisorType hyperType,boolean onlyReady,boolean showDomr,List<Account> permittedAccounts,Account caller,ListProjectResourcesCriteria listProjectResourcesCriteria,Map<String,String> tags,String zoneType){
  StringBuilder builder=new StringBuilder();
  if (!permittedAccounts.isEmpty()) {
    for (    Account permittedAccount : permittedAccounts) {
      builder.append(permittedAccount.getAccountId() + "String_Node_Str");
    }
  }
  String permittedAccountsStr=builder.toString();
  if (permittedAccountsStr.length() > 0) {
    permittedAccountsStr=permittedAccountsStr.substring(0,permittedAccountsStr.length() - 1);
  }
  Transaction txn=Transaction.currentTxn();
  txn.start();
  Set<Pair<Long,Long>> templateZonePairList=new LinkedHashSet<Pair<Long,Long>>();
  PreparedStatement pstmt=null;
  ResultSet rs=null;
  StringBuilder relatedDomainIds=new StringBuilder();
  String sql=SELECT_TEMPLATE_ZONE_REF;
  String groupByClause="String_Node_Str";
  try {
    String guestOSJoin="String_Node_Str";
    StringBuilder templateHostRefJoin=new StringBuilder();
    String dataCenterJoin="String_Node_Str", lpjoin="String_Node_Str";
    String tagsJoin="String_Node_Str";
    if (isIso && !hyperType.equals(HypervisorType.None)) {
      guestOSJoin="String_Node_Str";
    }
    if (onlyReady) {
      templateHostRefJoin.append("String_Node_Str");
      sql=SELECT_TEMPLATE_HOST_REF;
      groupByClause="String_Node_Str";
    }
    if (((templateFilter == TemplateFilter.featured) || (templateFilter == TemplateFilter.community)) || (zoneType != null && zoneId != null)) {
      dataCenterJoin="String_Node_Str";
    }
    if (templateFilter == TemplateFilter.sharedexecutable || templateFilter == TemplateFilter.shared) {
      lpjoin="String_Node_Str";
    }
    if (tags != null && !tags.isEmpty()) {
      tagsJoin="String_Node_Str";
    }
    sql+=guestOSJoin + templateHostRefJoin + dataCenterJoin+ lpjoin+ tagsJoin;
    String whereClause="String_Node_Str";
    if ((listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources || (!permittedAccounts.isEmpty() && !(templateFilter == TemplateFilter.community || templateFilter == TemplateFilter.featured))) && !(caller.getType() != Account.ACCOUNT_TYPE_NORMAL && templateFilter == TemplateFilter.all)) {
      whereClause+="String_Node_Str";
      if ((templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) && (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN)) {
        whereClause+="String_Node_Str" + domain.getPath() + "String_Node_Str";
        if (listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources) {
          whereClause+="String_Node_Str" + Account.ACCOUNT_TYPE_PROJECT;
        }
      }
 else       if (listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources) {
        whereClause+="String_Node_Str" + Account.ACCOUNT_TYPE_PROJECT;
      }
    }
    if (!permittedAccounts.isEmpty()) {
      for (      Account account : permittedAccounts) {
        DomainVO accountDomain=_domainDao.findById(account.getDomainId());
        DomainVO domainTreeNode=accountDomain;
        while (true) {
          relatedDomainIds.append(domainTreeNode.getId());
          relatedDomainIds.append("String_Node_Str");
          if (domainTreeNode.getParent() != null) {
            domainTreeNode=_domainDao.findById(domainTreeNode.getParent());
          }
 else {
            break;
          }
        }
        if (isAdmin(account.getType())) {
          List<DomainVO> allChildDomains=_domainDao.findAllChildren(accountDomain.getPath(),accountDomain.getId());
          for (          DomainVO childDomain : allChildDomains) {
            relatedDomainIds.append(childDomain.getId());
            relatedDomainIds.append("String_Node_Str");
          }
        }
        relatedDomainIds.setLength(relatedDomainIds.length() - 1);
      }
    }
    String attr="String_Node_Str";
    if (whereClause.endsWith("String_Node_Str")) {
      attr+="String_Node_Str";
    }
    if (!isIso) {
      if (hypers.isEmpty()) {
        return templateZonePairList;
      }
 else {
        StringBuilder relatedHypers=new StringBuilder();
        for (        HypervisorType hyper : hypers) {
          relatedHypers.append("String_Node_Str");
          relatedHypers.append(hyper.toString());
          relatedHypers.append("String_Node_Str");
          relatedHypers.append("String_Node_Str");
        }
        relatedHypers.setLength(relatedHypers.length() - 1);
        whereClause+=attr + "String_Node_Str" + relatedHypers+ "String_Node_Str";
      }
    }
    if (!permittedAccounts.isEmpty() && !(templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community || templateFilter == TemplateFilter.executable || templateFilter == TemplateFilter.shared || templateFilter == TemplateFilter.sharedexecutable) && !isAdmin(caller.getType())) {
      whereClause+=attr + "String_Node_Str" + permittedAccountsStr+ "String_Node_Str";
    }
    if (templateFilter == TemplateFilter.featured) {
      whereClause+=attr + "String_Node_Str";
      if (!permittedAccounts.isEmpty()) {
        whereClause+=attr + "String_Node_Str" + relatedDomainIds+ "String_Node_Str";
      }
    }
 else     if (templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) {
      whereClause+="String_Node_Str" + permittedAccountsStr + "String_Node_Str";
    }
 else     if (templateFilter == TemplateFilter.sharedexecutable || templateFilter == TemplateFilter.shared) {
      whereClause+="String_Node_Str" + "String_Node_Str" + permittedAccountsStr + "String_Node_Str"+ "String_Node_Str"+ permittedAccountsStr+ "String_Node_Str";
    }
 else     if (templateFilter == TemplateFilter.executable && !permittedAccounts.isEmpty()) {
      whereClause+=attr + "String_Node_Str" + permittedAccountsStr+ "String_Node_Str";
    }
 else     if (templateFilter == TemplateFilter.community) {
      whereClause+=attr + "String_Node_Str";
      if (!permittedAccounts.isEmpty()) {
        whereClause+=attr + "String_Node_Str" + relatedDomainIds+ "String_Node_Str";
      }
    }
 else     if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN && !isIso) {
      return templateZonePairList;
    }
    if (tags != null && !tags.isEmpty()) {
      whereClause+="String_Node_Str";
      boolean first=true;
      for (      String key : tags.keySet()) {
        if (!first) {
          whereClause+="String_Node_Str";
        }
        whereClause+="String_Node_Str" + key + "String_Node_Str"+ tags.get(key)+ "String_Node_Str";
        first=false;
      }
      whereClause+="String_Node_Str";
    }
    if (whereClause.equals("String_Node_Str")) {
      whereClause+="String_Node_Str";
    }
 else     if (!whereClause.equals("String_Node_Str")) {
      whereClause+="String_Node_Str";
    }
    sql+=whereClause + getExtrasWhere(templateFilter,name,keyword,isIso,bootable,hyperType,zoneId,onlyReady,showDomr,zoneType) + groupByClause+ getOrderByLimit(pageSize,startIndex);
    pstmt=txn.prepareStatement(sql);
    rs=pstmt.executeQuery();
    while (rs.next()) {
      Pair<Long,Long> templateZonePair=new Pair<Long,Long>(rs.getLong(1),rs.getLong(2));
      templateZonePairList.add(templateZonePair);
    }
    if (isIso && templateZonePairList.size() < (pageSize != null ? pageSize : 500) && templateFilter != TemplateFilter.community && !(templateFilter == TemplateFilter.self && !BaseCmd.isRootAdmin(caller.getType()))) {
      List<VMTemplateVO> publicIsos=publicIsoSearch(bootable,false,tags);
      List<VMTemplateVO> userIsos=userIsoSearch(false);
      int i=0;
      if (startIndex > userIsos.size()) {
        i=(int)(startIndex - userIsos.size());
      }
      for (; i < publicIsos.size(); i++) {
        if (templateZonePairList.size() >= pageSize) {
          break;
        }
 else {
          if (keyword != null && publicIsos.get(i).getName().contains(keyword)) {
            templateZonePairList.add(new Pair<Long,Long>(publicIsos.get(i).getId(),null));
            continue;
          }
 else           if (name != null && publicIsos.get(i).getName().contains(name)) {
            templateZonePairList.add(new Pair<Long,Long>(publicIsos.get(i).getId(),null));
            continue;
          }
 else           if (keyword == null && name == null) {
            templateZonePairList.add(new Pair<Long,Long>(publicIsos.get(i).getId(),null));
          }
        }
      }
    }
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
  }
 finally {
    try {
      if (rs != null) {
        rs.close();
      }
      if (pstmt != null) {
        pstmt.close();
      }
      txn.commit();
    }
 catch (    SQLException sqle) {
      s_logger.warn("String_Node_Str",sqle);
    }
  }
  return templateZonePairList;
}

Fixed version (ground truth):
@Override public Set<Pair<Long,Long>> searchTemplates(String name,String keyword,TemplateFilter templateFilter,boolean isIso,List<HypervisorType> hypers,Boolean bootable,DomainVO domain,Long pageSize,Long startIndex,Long zoneId,HypervisorType hyperType,boolean onlyReady,boolean showDomr,List<Account> permittedAccounts,Account caller,ListProjectResourcesCriteria listProjectResourcesCriteria,Map<String,String> tags,String zoneType){
  StringBuilder builder=new StringBuilder();
  if (!permittedAccounts.isEmpty()) {
    for (    Account permittedAccount : permittedAccounts) {
      builder.append(permittedAccount.getAccountId() + "String_Node_Str");
    }
  }
  String permittedAccountsStr=builder.toString();
  if (permittedAccountsStr.length() > 0) {
    permittedAccountsStr=permittedAccountsStr.substring(0,permittedAccountsStr.length() - 1);
  }
  Transaction txn=Transaction.currentTxn();
  txn.start();
  Set<Pair<Long,Long>> templateZonePairList=new LinkedHashSet<Pair<Long,Long>>();
  PreparedStatement pstmt=null;
  ResultSet rs=null;
  StringBuilder relatedDomainIds=new StringBuilder();
  String sql=SELECT_TEMPLATE_ZONE_REF;
  String groupByClause="String_Node_Str";
  try {
    String guestOSJoin="String_Node_Str";
    StringBuilder templateHostRefJoin=new StringBuilder();
    String dataCenterJoin="String_Node_Str", lpjoin="String_Node_Str";
    String tagsJoin="String_Node_Str";
    if (isIso && !hyperType.equals(HypervisorType.None)) {
      guestOSJoin="String_Node_Str";
    }
    if (onlyReady) {
      templateHostRefJoin.append("String_Node_Str");
      sql=SELECT_TEMPLATE_HOST_REF;
      groupByClause="String_Node_Str";
    }
    if ((templateFilter == TemplateFilter.featured) || (templateFilter == TemplateFilter.community)) {
      dataCenterJoin="String_Node_Str";
    }
    if (zoneType != null) {
      dataCenterJoin="String_Node_Str";
      dataCenterJoin+="String_Node_Str";
    }
    if (templateFilter == TemplateFilter.sharedexecutable || templateFilter == TemplateFilter.shared) {
      lpjoin="String_Node_Str";
    }
    if (tags != null && !tags.isEmpty()) {
      tagsJoin="String_Node_Str";
    }
    sql+=guestOSJoin + templateHostRefJoin + dataCenterJoin+ lpjoin+ tagsJoin;
    String whereClause="String_Node_Str";
    if ((listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources || (!permittedAccounts.isEmpty() && !(templateFilter == TemplateFilter.community || templateFilter == TemplateFilter.featured))) && !(caller.getType() != Account.ACCOUNT_TYPE_NORMAL && templateFilter == TemplateFilter.all)) {
      whereClause+="String_Node_Str";
      if ((templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) && (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN)) {
        whereClause+="String_Node_Str" + domain.getPath() + "String_Node_Str";
        if (listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources) {
          whereClause+="String_Node_Str" + Account.ACCOUNT_TYPE_PROJECT;
        }
      }
 else       if (listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources) {
        whereClause+="String_Node_Str" + Account.ACCOUNT_TYPE_PROJECT;
      }
    }
    if (!permittedAccounts.isEmpty()) {
      for (      Account account : permittedAccounts) {
        DomainVO accountDomain=_domainDao.findById(account.getDomainId());
        DomainVO domainTreeNode=accountDomain;
        while (true) {
          relatedDomainIds.append(domainTreeNode.getId());
          relatedDomainIds.append("String_Node_Str");
          if (domainTreeNode.getParent() != null) {
            domainTreeNode=_domainDao.findById(domainTreeNode.getParent());
          }
 else {
            break;
          }
        }
        if (isAdmin(account.getType())) {
          List<DomainVO> allChildDomains=_domainDao.findAllChildren(accountDomain.getPath(),accountDomain.getId());
          for (          DomainVO childDomain : allChildDomains) {
            relatedDomainIds.append(childDomain.getId());
            relatedDomainIds.append("String_Node_Str");
          }
        }
        relatedDomainIds.setLength(relatedDomainIds.length() - 1);
      }
    }
    String attr="String_Node_Str";
    if (whereClause.endsWith("String_Node_Str")) {
      attr+="String_Node_Str";
    }
    if (!isIso) {
      if (hypers.isEmpty()) {
        return templateZonePairList;
      }
 else {
        StringBuilder relatedHypers=new StringBuilder();
        for (        HypervisorType hyper : hypers) {
          relatedHypers.append("String_Node_Str");
          relatedHypers.append(hyper.toString());
          relatedHypers.append("String_Node_Str");
          relatedHypers.append("String_Node_Str");
        }
        relatedHypers.setLength(relatedHypers.length() - 1);
        whereClause+=attr + "String_Node_Str" + relatedHypers+ "String_Node_Str";
      }
    }
    if (!permittedAccounts.isEmpty() && !(templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community || templateFilter == TemplateFilter.executable || templateFilter == TemplateFilter.shared || templateFilter == TemplateFilter.sharedexecutable) && !isAdmin(caller.getType())) {
      whereClause+=attr + "String_Node_Str" + permittedAccountsStr+ "String_Node_Str";
    }
    if (templateFilter == TemplateFilter.featured) {
      whereClause+=attr + "String_Node_Str";
      if (!permittedAccounts.isEmpty()) {
        whereClause+=attr + "String_Node_Str" + relatedDomainIds+ "String_Node_Str";
      }
    }
 else     if (templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) {
      whereClause+="String_Node_Str" + permittedAccountsStr + "String_Node_Str";
    }
 else     if (templateFilter == TemplateFilter.sharedexecutable || templateFilter == TemplateFilter.shared) {
      whereClause+="String_Node_Str" + "String_Node_Str" + permittedAccountsStr + "String_Node_Str"+ "String_Node_Str"+ permittedAccountsStr+ "String_Node_Str";
    }
 else     if (templateFilter == TemplateFilter.executable && !permittedAccounts.isEmpty()) {
      whereClause+=attr + "String_Node_Str" + permittedAccountsStr+ "String_Node_Str";
    }
 else     if (templateFilter == TemplateFilter.community) {
      whereClause+=attr + "String_Node_Str";
      if (!permittedAccounts.isEmpty()) {
        whereClause+=attr + "String_Node_Str" + relatedDomainIds+ "String_Node_Str";
      }
    }
 else     if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN && !isIso) {
      return templateZonePairList;
    }
    if (tags != null && !tags.isEmpty()) {
      whereClause+="String_Node_Str";
      boolean first=true;
      for (      String key : tags.keySet()) {
        if (!first) {
          whereClause+="String_Node_Str";
        }
        whereClause+="String_Node_Str" + key + "String_Node_Str"+ tags.get(key)+ "String_Node_Str";
        first=false;
      }
      whereClause+="String_Node_Str";
    }
    if (whereClause.equals("String_Node_Str")) {
      whereClause+="String_Node_Str";
    }
 else     if (!whereClause.equals("String_Node_Str")) {
      whereClause+="String_Node_Str";
    }
    sql+=whereClause + getExtrasWhere(templateFilter,name,keyword,isIso,bootable,hyperType,zoneId,onlyReady,showDomr,zoneType) + groupByClause+ getOrderByLimit(pageSize,startIndex);
    pstmt=txn.prepareStatement(sql);
    rs=pstmt.executeQuery();
    while (rs.next()) {
      Pair<Long,Long> templateZonePair=new Pair<Long,Long>(rs.getLong(1),rs.getLong(2));
      templateZonePairList.add(templateZonePair);
    }
    if (isIso && templateZonePairList.size() < (pageSize != null ? pageSize : 500) && templateFilter != TemplateFilter.community && !(templateFilter == TemplateFilter.self && !BaseCmd.isRootAdmin(caller.getType()))) {
      List<VMTemplateVO> publicIsos=publicIsoSearch(bootable,false,tags);
      List<VMTemplateVO> userIsos=userIsoSearch(false);
      int i=0;
      if (startIndex > userIsos.size()) {
        i=(int)(startIndex - userIsos.size());
      }
      for (; i < publicIsos.size(); i++) {
        if (templateZonePairList.size() >= pageSize) {
          break;
        }
 else {
          if (keyword != null && publicIsos.get(i).getName().contains(keyword)) {
            templateZonePairList.add(new Pair<Long,Long>(publicIsos.get(i).getId(),null));
            continue;
          }
 else           if (name != null && publicIsos.get(i).getName().contains(name)) {
            templateZonePairList.add(new Pair<Long,Long>(publicIsos.get(i).getId(),null));
            continue;
          }
 else           if (keyword == null && name == null) {
            templateZonePairList.add(new Pair<Long,Long>(publicIsos.get(i).getId(),null));
          }
        }
      }
    }
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
  }
 finally {
    try {
      if (rs != null) {
        rs.close();
      }
      if (pstmt != null) {
        pstmt.close();
      }
      txn.commit();
    }
 catch (    SQLException sqle) {
      s_logger.warn("String_Node_Str",sqle);
    }
  }
  return templateZonePairList;
}