Program: 89691
Project: 89691
Buggy source:
@Override public List<? extends Network> searchForNetworks(ListNetworksCmd cmd){
  Object id=cmd.getId();
  Object keyword=cmd.getKeyword();
  Long zoneId=cmd.getZoneId();
  Account account=UserContext.current().getCaller();
  Long domainId=cmd.getDomainId();
  String accountName=cmd.getAccountName();
  String type=cmd.getType();
  String trafficType=cmd.getTrafficType();
  Boolean isSystem=cmd.getIsSystem();
  Boolean isShared=cmd.getIsShared();
  Boolean isDefault=cmd.isDefault();
  Long accountId=null;
  String path=null;
  List<Long> avoidNetworks=new ArrayList<Long>();
  List<Long> allowedSharedNetworks=new ArrayList<Long>();
  if (isSystem == null) {
    isSystem=false;
  }
  if (isSystem && (accountName != null || domainId != null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (_accountMgr.isAdmin(account.getType())) {
    if (domainId != null) {
      if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(),domainId)) {
        throw new PermissionDeniedException("String_Node_Str" + domainId + "String_Node_Str");
      }
      if (accountName != null) {
        account=_accountMgr.getActiveAccount(accountName,domainId);
        if (account == null) {
          throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
        }
        accountId=account.getId();
      }
    }
 else {
      accountId=account.getId();
    }
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
      DomainVO domain=_domainDao.findById(account.getDomainId());
      if (domain != null) {
        path=domain.getPath();
      }
    }
  }
 else {
    accountName=account.getAccountName();
    domainId=account.getDomainId();
    accountId=account.getId();
  }
  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) {
    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);
  if (path != null) {
    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);
  }
  sb.and("String_Node_Str",sb.entity().getRemoved(),Op.NULL);
  SearchCriteria<NetworkVO> sc=sb.create();
  if (isSystem != null) {
    sc.setJoinParameters("String_Node_Str","String_Node_Str",isSystem);
  }
  if (keyword != null) {
    SearchCriteria<NetworkVO> ssc=_networksDao.createSearchCriteria();
    ssc.addOr("String_Node_Str",SearchCriteria.Op.LIKE,"String_Node_Str" + keyword + "String_Node_Str");
    sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,ssc);
  }
  if (id != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,id);
  }
  if (zoneId != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,zoneId);
  }
  if (type != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,type);
  }
  if (!isSystem) {
    if (accountName != null && domainId != null) {
      if (isShared == null) {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,accountId);
      }
 else       if (!isShared) {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,accountId);
      }
 else {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,true);
      }
      if (isShared == null || isShared) {
        List<NetworkVO> allNetworks=_networksDao.listNetworksBy(true);
        for (        NetworkVO network : allNetworks) {
          if (!isNetworkAvailableInDomain(network.getId(),domainId)) {
            avoidNetworks.add(network.getId());
          }
 else {
            allowedSharedNetworks.add(network.getId());
          }
        }
      }
    }
 else     if (isShared != null) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,isShared);
    }
  }
  if (domainId != null && accountName == null) {
    List<NetworkVO> allNetworks=_networksDao.listNetworksBy(true);
    for (    NetworkVO network : allNetworks) {
      if (!isNetworkAvailableInDomain(network.getId(),domainId)) {
        avoidNetworks.add(network.getId());
      }
    }
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,true);
  }
  for (  Long avoidNetwork : avoidNetworks) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.NOTIN,avoidNetwork);
  }
  for (  Long allowerdSharedNetwork : allowedSharedNetworks) {
    sc.addOr("String_Node_Str",SearchCriteria.Op.IN,allowerdSharedNetwork);
  }
  if (isDefault != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,isDefault);
  }
  if (trafficType != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,trafficType);
  }
  if (!isSystem && path != null && (isShared == null || !isShared)) {
    sc.setJoinParameters("String_Node_Str","String_Node_Str",path + "String_Node_Str");
  }
  List<NetworkVO> networks=_networksDao.search(sc,searchFilter);
  return networks;
}

Fixed version (ground truth):
@Override public List<? extends Network> searchForNetworks(ListNetworksCmd cmd){
  Object id=cmd.getId();
  Object keyword=cmd.getKeyword();
  Long zoneId=cmd.getZoneId();
  Account account=UserContext.current().getCaller();
  Long domainId=cmd.getDomainId();
  String accountName=cmd.getAccountName();
  String type=cmd.getType();
  String trafficType=cmd.getTrafficType();
  Boolean isSystem=cmd.getIsSystem();
  Boolean isShared=cmd.getIsShared();
  Boolean isDefault=cmd.isDefault();
  Long accountId=null;
  String path=null;
  List<Long> avoidNetworks=new ArrayList<Long>();
  List<Long> allowedSharedNetworks=new ArrayList<Long>();
  if (isSystem == null) {
    isSystem=false;
  }
  if (isSystem && (accountName != null || domainId != null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (_accountMgr.isAdmin(account.getType())) {
    if (domainId != null) {
      if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(),domainId)) {
        throw new PermissionDeniedException("String_Node_Str" + domainId + "String_Node_Str");
      }
      if (accountName != null) {
        account=_accountMgr.getActiveAccount(accountName,domainId);
        if (account == null) {
          throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
        }
        accountId=account.getId();
      }
    }
 else {
      accountId=account.getId();
    }
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
      DomainVO domain=_domainDao.findById(account.getDomainId());
      if (domain != null) {
        path=domain.getPath();
      }
    }
  }
 else {
    accountName=account.getAccountName();
    domainId=account.getDomainId();
    accountId=account.getId();
  }
  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) {
    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);
  if (path != null) {
    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);
  }
  sb.and("String_Node_Str",sb.entity().getRemoved(),Op.NULL);
  SearchCriteria<NetworkVO> sc=sb.create();
  if (isSystem != null) {
    sc.setJoinParameters("String_Node_Str","String_Node_Str",isSystem);
  }
  if (keyword != null) {
    SearchCriteria<NetworkVO> ssc=_networksDao.createSearchCriteria();
    ssc.addOr("String_Node_Str",SearchCriteria.Op.LIKE,"String_Node_Str" + keyword + "String_Node_Str");
    sc.addAnd("String_Node_Str",SearchCriteria.Op.SC,ssc);
  }
  if (id != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,id);
  }
  if (zoneId != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,zoneId);
  }
  if (type != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,type);
  }
  if (!isSystem) {
    if (accountName != null && domainId != null) {
      if (isShared == null) {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,accountId);
      }
 else       if (!isShared) {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,accountId);
      }
 else {
        sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,true);
      }
      if (isShared == null || isShared) {
        List<NetworkVO> allNetworks=_networksDao.listNetworksBy(true);
        for (        NetworkVO network : allNetworks) {
          NetworkOffering offering=_configMgr.getNetworkOffering(network.getNetworkOfferingId());
          if (!isNetworkAvailableInDomain(network.getId(),domainId) || offering.isSystemOnly()) {
            avoidNetworks.add(network.getId());
          }
 else {
            allowedSharedNetworks.add(network.getId());
          }
        }
      }
    }
 else     if (isShared != null) {
      sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,isShared);
    }
  }
  if (domainId != null && accountName == null) {
    List<NetworkVO> allNetworks=_networksDao.listNetworksBy(true);
    for (    NetworkVO network : allNetworks) {
      if (!isNetworkAvailableInDomain(network.getId(),domainId)) {
        avoidNetworks.add(network.getId());
      }
    }
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,true);
  }
  for (  Long avoidNetwork : avoidNetworks) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.NOTIN,avoidNetwork);
  }
  for (  Long allowerdSharedNetwork : allowedSharedNetworks) {
    sc.addOr("String_Node_Str",SearchCriteria.Op.IN,allowerdSharedNetwork);
  }
  if (isDefault != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,isDefault);
  }
  if (trafficType != null) {
    sc.addAnd("String_Node_Str",SearchCriteria.Op.EQ,trafficType);
  }
  if (!isSystem && path != null && (isShared == null || !isShared)) {
    sc.setJoinParameters("String_Node_Str","String_Node_Str",path + "String_Node_Str");
  }
  List<NetworkVO> networks=_networksDao.search(sc,searchFilter);
  return networks;
}