Program: 106466
Project: 106466
Buggy source:
@Override public List<? extends Network> searchForNetworks(ListNetworksCmd cmd){
  Long id=cmd.getId();
  String keyword=cmd.getKeyword();
  Long zoneId=cmd.getZoneId();
  Account caller=UserContext.current().getCaller();
  Long domainId=cmd.getDomainId();
  String accountName=cmd.getAccountName();
  String guestIpType=cmd.getGuestIpType();
  String trafficType=cmd.getTrafficType();
  Boolean isSystem=cmd.getIsSystem();
  String aclType=cmd.getAclType();
  Long projectId=cmd.getProjectId();
  List<Long> permittedAccounts=new ArrayList<Long>();
  String path=null;
  Long physicalNetworkId=cmd.getPhysicalNetworkId();
  List<String> supportedServicesStr=cmd.getSupportedServices();
  Boolean restartRequired=cmd.getRestartRequired();
  boolean listAll=cmd.listAll();
  boolean isRecursive=cmd.isRecursive();
  Boolean specifyIpRanges=cmd.getSpecifyIpRanges();
  Boolean canUseForDeploy=cmd.canUseForDeploy();
  Map<String,String> tags=cmd.getTags();
  if ((isSystem == null || caller.getType() == Account.ACCOUNT_TYPE_NORMAL) && id == null) {
    isSystem=false;
  }
  if (isSystem != null && isSystem && (accountName != null || domainId != null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (domainId != null) {
    DomainVO domain=_domainDao.findById(domainId);
    if (domain == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    _accountMgr.checkAccess(caller,domain);
    if (accountName != null) {
      Account owner=_accountMgr.getActiveAccountByName(accountName,domainId);
      if (owner == null) {
        throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str");
      }
      _accountMgr.checkAccess(caller,null,true,owner);
      permittedAccounts.add(owner.getId());
    }
  }
  if (!_accountMgr.isAdmin(caller.getType()) || !listAll) {
    permittedAccounts.add(caller.getId());
    domainId=caller.getDomainId();
  }
  if (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
    domainId=caller.getDomainId();
  }
  boolean skipProjectNetworks=true;
  if (projectId != null) {
    if (projectId == -1) {
      permittedAccounts.addAll(_projectMgr.listPermittedProjectAccounts(caller.getId()));
    }
 else {
      permittedAccounts.clear();
      Project project=_projectMgr.getProject(projectId);
      if (project == null) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
      if (!_projectMgr.canAccessProjectAccount(caller,project.getProjectAccountId())) {
        InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str" + caller + "String_Node_Str");
        ex.addProxyObject(project,projectId,"String_Node_Str");
        throw ex;
      }
      permittedAccounts.add(project.getProjectAccountId());
    }
    skipProjectNetworks=false;
  }
  path=_domainDao.findById(caller.getDomainId()).getPath();
  if (listAll) {
    isRecursive=true;
  }
  Filter searchFilter=new Filter(NetworkVO.class,"String_Node_Str",false,cmd.getStartIndex(),cmd.getPageSizeVal());
  SearchBuilder<NetworkVO> sb=_networksDao.createSearchBuilder();
  SearchBuilder<NetworkOfferingVO> networkOfferingSearch=_networkOfferingDao.createSearchBuilder();
  networkOfferingSearch.and("String_Node_Str",networkOfferingSearch.entity().isSystemOnly(),SearchCriteria.Op.EQ);
  if (isSystem != null && isSystem) {
    networkOfferingSearch.and("String_Node_Str",networkOfferingSearch.entity().getTrafficType(),SearchCriteria.Op.EQ);
  }
  sb.join("String_Node_Str",networkOfferingSearch,sb.entity().getNetworkOfferingId(),networkOfferingSearch.entity().getId(),JoinBuilder.JoinType.INNER);
  SearchBuilder<DataCenterVO> zoneSearch=_dcDao.createSearchBuilder();
  zoneSearch.and("String_Node_Str",zoneSearch.entity().getNetworkType(),SearchCriteria.Op.EQ);
  sb.join("String_Node_Str",zoneSearch,sb.entity().getDataCenterId(),zoneSearch.entity().getId(),JoinBuilder.JoinType.INNER);
  sb.and("String_Node_Str",sb.entity().getRemoved(),Op.NULL);
  if (tags != null && !tags.isEmpty()) {
    SearchBuilder<ResourceTagVO> tagSearch=_resourceTagDao.createSearchBuilder();
    for (int count=0; count < tags.size(); count++) {
      tagSearch.or().op("String_Node_Str" + String.valueOf(count),tagSearch.entity().getKey(),SearchCriteria.Op.EQ);
      tagSearch.and("String_Node_Str" + String.valueOf(count),tagSearch.entity().getValue(),SearchCriteria.Op.EQ);
      tagSearch.cp();
    }
    tagSearch.and("String_Node_Str",tagSearch.entity().getResourceType(),SearchCriteria.Op.EQ);
    sb.groupBy(sb.entity().getId());
    sb.join("String_Node_Str",tagSearch,sb.entity().getId(),tagSearch.entity().getResourceId(),JoinBuilder.JoinType.INNER);
  }
  if (permittedAccounts.isEmpty()) {
    SearchBuilder<DomainVO> domainSearch=_domainDao.createSearchBuilder();
    domainSearch.and("String_Node_Str",domainSearch.entity().getPath(),SearchCriteria.Op.LIKE);
    sb.join("String_Node_Str",domainSearch,sb.entity().getDomainId(),domainSearch.entity().getId(),JoinBuilder.JoinType.INNER);
  }
  if (skipProjectNetworks) {
    SearchBuilder<AccountVO> accountSearch=_accountDao.createSearchBuilder();
    accountSearch.and("String_Node_Str",accountSearch.entity().getType(),SearchCriteria.Op.NEQ);
    sb.join("String_Node_Str",accountSearch,sb.entity().getAccountId(),accountSearch.entity().getId(),JoinBuilder.JoinType.INNER);
  }
  List<NetworkVO> networksToReturn=new ArrayList<NetworkVO>();
  if (isSystem == null || !isSystem) {
    if (domainId != null) {
      networksToReturn.addAll(listDomainLevelNetworks(buildNetworkSearchCriteria(sb,keyword,id,isSystem,zoneId,guestIpType,trafficType,physicalNetworkId,aclType,skipProjectNetworks,restartRequired,specifyIpRanges,tags),searchFilter,domainId));
    }
    if (!permittedAccounts.isEmpty()) {
      networksToReturn.addAll(listAccountSpecificNetworks(buildNetworkSearchCriteria(sb,keyword,id,isSystem,zoneId,guestIpType,trafficType,physicalNetworkId,aclType,skipProjectNetworks,restartRequired,specifyIpRanges,tags),searchFilter,permittedAccounts));
    }
 else     if (domainId == null) {
      networksToReturn.addAll(listAccountSpecificNetworksByDomainPath(buildNetworkSearchCriteria(sb,keyword,id,isSystem,zoneId,guestIpType,trafficType,physicalNetworkId,aclType,skipProjectNetworks,restartRequired,specifyIpRanges,tags),searchFilter,path,isRecursive));
      networksToReturn.addAll(listDomainSpecificNetworksByDomainPath(buildNetworkSearchCriteria(sb,keyword,id,isSystem,zoneId,guestIpType,trafficType,physicalNetworkId,aclType,skipProjectNetworks,restartRequired,specifyIpRanges,tags),searchFilter,path,isRecursive));
    }
  }
 else {
    networksToReturn=_networksDao.search(buildNetworkSearchCriteria(sb,keyword,id,isSystem,zoneId,guestIpType,trafficType,physicalNetworkId,null,skipProjectNetworks,restartRequired,specifyIpRanges,tags),searchFilter);
  }
  if (supportedServicesStr != null && !supportedServicesStr.isEmpty() && !networksToReturn.isEmpty()) {
    List<NetworkVO> supportedNetworks=new ArrayList<NetworkVO>();
    Service[] suppportedServices=new Service[supportedServicesStr.size()];
    int i=0;
    for (    String supportedServiceStr : supportedServicesStr) {
      Service service=Service.getService(supportedServiceStr);
      if (service == null) {
        throw new InvalidParameterValueException("String_Node_Str" + supportedServiceStr);
      }
 else {
        suppportedServices[i]=service;
      }
      i++;
    }
    for (    NetworkVO network : networksToReturn) {
      if (areServicesSupportedInNetwork(network.getId(),suppportedServices)) {
        supportedNetworks.add(network);
      }
    }
    networksToReturn=supportedNetworks;
  }
  if (canUseForDeploy != null) {
    List<NetworkVO> networksForDeploy=new ArrayList<NetworkVO>();
    for (    NetworkVO network : networksToReturn) {
      if (canUseForDeploy(network) == canUseForDeploy) {
        networksForDeploy.add(network);
      }
    }
    networksToReturn=networksForDeploy;
  }
  return networksToReturn;
}

Fixed version (ground truth):
@Override public List<? extends Network> searchForNetworks(ListNetworksCmd cmd){
  Long id=cmd.getId();
  String keyword=cmd.getKeyword();
  Long zoneId=cmd.getZoneId();
  Account caller=UserContext.current().getCaller();
  Long domainId=cmd.getDomainId();
  String accountName=cmd.getAccountName();
  String guestIpType=cmd.getGuestIpType();
  String trafficType=cmd.getTrafficType();
  Boolean isSystem=cmd.getIsSystem();
  String aclType=cmd.getAclType();
  Long projectId=cmd.getProjectId();
  List<Long> permittedAccounts=new ArrayList<Long>();
  String path=null;
  Long physicalNetworkId=cmd.getPhysicalNetworkId();
  List<String> supportedServicesStr=cmd.getSupportedServices();
  Boolean restartRequired=cmd.getRestartRequired();
  boolean listAll=cmd.listAll();
  boolean isRecursive=cmd.isRecursive();
  Boolean specifyIpRanges=cmd.getSpecifyIpRanges();
  Boolean canUseForDeploy=cmd.canUseForDeploy();
  Map<String,String> tags=cmd.getTags();
  if ((isSystem == null || caller.getType() == Account.ACCOUNT_TYPE_NORMAL) && id == null) {
    isSystem=false;
  }
  if (isSystem != null && isSystem && (accountName != null || domainId != null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (domainId != null) {
    DomainVO domain=_domainDao.findById(domainId);
    if (domain == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    _accountMgr.checkAccess(caller,domain);
    if (accountName != null) {
      Account owner=_accountMgr.getActiveAccountByName(accountName,domainId);
      if (owner == null) {
        throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str");
      }
      _accountMgr.checkAccess(caller,null,true,owner);
      permittedAccounts.add(owner.getId());
    }
  }
  if (!_accountMgr.isAdmin(caller.getType()) || (!listAll && (projectId != null && projectId != -1 && domainId == null))) {
    permittedAccounts.add(caller.getId());
    domainId=caller.getDomainId();
  }
  boolean skipProjectNetworks=true;
  if (projectId != null) {
    if (projectId == -1) {
      permittedAccounts.addAll(_projectMgr.listPermittedProjectAccounts(caller.getId()));
    }
 else {
      permittedAccounts.clear();
      Project project=_projectMgr.getProject(projectId);
      if (project == null) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
      if (!_projectMgr.canAccessProjectAccount(caller,project.getProjectAccountId())) {
        InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str" + caller + "String_Node_Str");
        ex.addProxyObject(project,projectId,"String_Node_Str");
        throw ex;
      }
      permittedAccounts.add(project.getProjectAccountId());
    }
    skipProjectNetworks=false;
  }
  if (domainId != null) {
    path=_domainDao.findById(domainId).getPath();
  }
 else {
    path=_domainDao.findById(caller.getDomainId()).getPath();
  }
  if (listAll && domainId == null) {
    isRecursive=true;
  }
  Filter searchFilter=new Filter(NetworkVO.class,"String_Node_Str",false,cmd.getStartIndex(),cmd.getPageSizeVal());
  SearchBuilder<NetworkVO> sb=_networksDao.createSearchBuilder();
  SearchBuilder<NetworkOfferingVO> networkOfferingSearch=_networkOfferingDao.createSearchBuilder();
  networkOfferingSearch.and("String_Node_Str",networkOfferingSearch.entity().isSystemOnly(),SearchCriteria.Op.EQ);
  if (isSystem != null && isSystem) {
    networkOfferingSearch.and("String_Node_Str",networkOfferingSearch.entity().getTrafficType(),SearchCriteria.Op.EQ);
  }
  sb.join("String_Node_Str",networkOfferingSearch,sb.entity().getNetworkOfferingId(),networkOfferingSearch.entity().getId(),JoinBuilder.JoinType.INNER);
  SearchBuilder<DataCenterVO> zoneSearch=_dcDao.createSearchBuilder();
  zoneSearch.and("String_Node_Str",zoneSearch.entity().getNetworkType(),SearchCriteria.Op.EQ);
  sb.join("String_Node_Str",zoneSearch,sb.entity().getDataCenterId(),zoneSearch.entity().getId(),JoinBuilder.JoinType.INNER);
  sb.and("String_Node_Str",sb.entity().getRemoved(),Op.NULL);
  if (tags != null && !tags.isEmpty()) {
    SearchBuilder<ResourceTagVO> tagSearch=_resourceTagDao.createSearchBuilder();
    for (int count=0; count < tags.size(); count++) {
      tagSearch.or().op("String_Node_Str" + String.valueOf(count),tagSearch.entity().getKey(),SearchCriteria.Op.EQ);
      tagSearch.and("String_Node_Str" + String.valueOf(count),tagSearch.entity().getValue(),SearchCriteria.Op.EQ);
      tagSearch.cp();
    }
    tagSearch.and("String_Node_Str",tagSearch.entity().getResourceType(),SearchCriteria.Op.EQ);
    sb.groupBy(sb.entity().getId());
    sb.join("String_Node_Str",tagSearch,sb.entity().getId(),tagSearch.entity().getResourceId(),JoinBuilder.JoinType.INNER);
  }
  if (permittedAccounts.isEmpty()) {
    SearchBuilder<DomainVO> domainSearch=_domainDao.createSearchBuilder();
    domainSearch.and("String_Node_Str",domainSearch.entity().getPath(),SearchCriteria.Op.LIKE);
    sb.join("String_Node_Str",domainSearch,sb.entity().getDomainId(),domainSearch.entity().getId(),JoinBuilder.JoinType.INNER);
  }
  SearchBuilder<AccountVO> accountSearch=_accountDao.createSearchBuilder();
  accountSearch.and("String_Node_Str",accountSearch.entity().getType(),SearchCriteria.Op.NEQ);
  accountSearch.and("String_Node_Str",accountSearch.entity().getType(),SearchCriteria.Op.EQ);
  sb.join("String_Node_Str",accountSearch,sb.entity().getAccountId(),accountSearch.entity().getId(),JoinBuilder.JoinType.INNER);
  List<NetworkVO> networksToReturn=new ArrayList<NetworkVO>();
  if (isSystem == null || !isSystem) {
    if (!permittedAccounts.isEmpty()) {
      networksToReturn.addAll(listAccountSpecificNetworks(buildNetworkSearchCriteria(sb,keyword,id,isSystem,zoneId,guestIpType,trafficType,physicalNetworkId,aclType,skipProjectNetworks,restartRequired,specifyIpRanges,tags),searchFilter,permittedAccounts));
      if (domainId != null) {
        networksToReturn.addAll(listDomainLevelNetworks(buildNetworkSearchCriteria(sb,keyword,id,isSystem,zoneId,guestIpType,trafficType,physicalNetworkId,aclType,true,restartRequired,specifyIpRanges,tags),searchFilter,domainId,false));
      }
    }
 else {
      networksToReturn.addAll(listAccountSpecificNetworksByDomainPath(buildNetworkSearchCriteria(sb,keyword,id,isSystem,zoneId,guestIpType,trafficType,physicalNetworkId,aclType,skipProjectNetworks,restartRequired,specifyIpRanges,tags),searchFilter,path,isRecursive));
      networksToReturn.addAll(listDomainSpecificNetworksByDomainPath(buildNetworkSearchCriteria(sb,keyword,id,isSystem,zoneId,guestIpType,trafficType,physicalNetworkId,aclType,true,restartRequired,specifyIpRanges,tags),searchFilter,path,isRecursive));
      if (domainId == null) {
        networksToReturn.addAll(listDomainLevelNetworks(buildNetworkSearchCriteria(sb,keyword,id,isSystem,zoneId,guestIpType,trafficType,physicalNetworkId,aclType,true,restartRequired,specifyIpRanges,tags),searchFilter,caller.getDomainId(),true));
      }
    }
  }
 else {
    networksToReturn=_networksDao.search(buildNetworkSearchCriteria(sb,keyword,id,isSystem,zoneId,guestIpType,trafficType,physicalNetworkId,null,skipProjectNetworks,restartRequired,specifyIpRanges,tags),searchFilter);
  }
  if (supportedServicesStr != null && !supportedServicesStr.isEmpty() && !networksToReturn.isEmpty()) {
    List<NetworkVO> supportedNetworks=new ArrayList<NetworkVO>();
    Service[] suppportedServices=new Service[supportedServicesStr.size()];
    int i=0;
    for (    String supportedServiceStr : supportedServicesStr) {
      Service service=Service.getService(supportedServiceStr);
      if (service == null) {
        throw new InvalidParameterValueException("String_Node_Str" + supportedServiceStr);
      }
 else {
        suppportedServices[i]=service;
      }
      i++;
    }
    for (    NetworkVO network : networksToReturn) {
      if (areServicesSupportedInNetwork(network.getId(),suppportedServices)) {
        supportedNetworks.add(network);
      }
    }
    networksToReturn=supportedNetworks;
  }
  if (canUseForDeploy != null) {
    List<NetworkVO> networksForDeploy=new ArrayList<NetworkVO>();
    for (    NetworkVO network : networksToReturn) {
      if (canUseForDeploy(network) == canUseForDeploy) {
        networksForDeploy.add(network);
      }
    }
    networksToReturn=networksForDeploy;
  }
  return networksToReturn;
}