Program: 89222
Project: 89222
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,boolean skipProjectTemplates){
  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";
    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";
    }
    sql+=guestOSJoin + templateHostRefJoin + dataCenterJoin;
    String whereClause="String_Node_Str";
    boolean joinedWithAccounts=false;
    if (skipProjectTemplates || (!permittedAccounts.isEmpty() && !(templateFilter == TemplateFilter.community || templateFilter == TemplateFilter.featured))) {
      whereClause+="String_Node_Str";
      joinedWithAccounts=true;
      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 (skipProjectTemplates) {
          whereClause+="String_Node_Str" + Account.ACCOUNT_TYPE_PROJECT;
        }
      }
 else       if (skipProjectTemplates) {
        whereClause+="String_Node_Str" + Account.ACCOUNT_TYPE_PROJECT;
      }
    }
    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+="String_Node_Str" + relatedHypers + "String_Node_Str";
      }
    }
    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 (!permittedAccounts.isEmpty() && !(templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community) && !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.sharedexecutable && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
      if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
        whereClause+="String_Node_Str" + "String_Node_Str" + permittedAccountsStr + "String_Node_Str"+ "String_Node_Str"+ permittedAccountsStr+ "String_Node_Str";
      }
 else {
        if (!joinedWithAccounts) {
          whereClause+="String_Node_Str";
        }
        whereClause+="String_Node_Str" + domain.getPath() + "String_Node_Str";
      }
    }
 else     if ((templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
    }
 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 (templateFilter == TemplateFilter.all && caller.getType() == Account.ACCOUNT_TYPE_ADMIN) {
      whereClause+=attr;
    }
 else     if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
      return templateZonePairList;
    }
    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) + 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);
      for (int i=0; i < publicIsos.size(); i++) {
        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,boolean skipProjectTemplates){
  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";
    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";
    }
    sql+=guestOSJoin + templateHostRefJoin + dataCenterJoin;
    String whereClause="String_Node_Str";
    boolean joinedWithAccounts=false;
    if (skipProjectTemplates || (!permittedAccounts.isEmpty() && !(templateFilter == TemplateFilter.community || templateFilter == TemplateFilter.featured))) {
      whereClause+="String_Node_Str";
      joinedWithAccounts=true;
      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 (skipProjectTemplates) {
          whereClause+="String_Node_Str" + Account.ACCOUNT_TYPE_PROJECT;
        }
      }
 else       if (skipProjectTemplates) {
        whereClause+="String_Node_Str" + Account.ACCOUNT_TYPE_PROJECT;
      }
    }
    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+="String_Node_Str" + relatedHypers + "String_Node_Str";
      }
    }
    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 (!permittedAccounts.isEmpty() && !(templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community) && !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.sharedexecutable && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
      if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
        whereClause+="String_Node_Str" + "String_Node_Str" + permittedAccountsStr + "String_Node_Str"+ "String_Node_Str"+ permittedAccountsStr+ "String_Node_Str";
      }
 else {
        if (!joinedWithAccounts) {
          whereClause+="String_Node_Str";
        }
        whereClause+="String_Node_Str" + domain.getPath() + "String_Node_Str";
      }
    }
 else     if ((templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
    }
 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 (templateFilter == TemplateFilter.all && caller.getType() == Account.ACCOUNT_TYPE_ADMIN) {
      whereClause+=attr;
    }
 else     if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN && !isIso) {
      return templateZonePairList;
    }
    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) + 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);
      for (int i=0; i < publicIsos.size(); i++) {
        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;
}