コード例 #1
0
ファイル: TDQueryUtils.java プロジェクト: rodericj/TopDish
  /**
   * Searches for {@link Dish}es belonging to a {@link Restauarnt} using the supplied search terms
   *
   * @param queryWords terms to search with
   * @param restKey {@link Key} to the {@link Restaurant} to search within
   * @param maxResults maximum number of results to return
   * @return {@link Collection} of {@link Dish}es
   */
  @SuppressWarnings("unchecked")
  public static Set<Dish> searchDishesByRestaurant(
      String[] queryWords, Key restKey, int maxResults) {
    List<Object> paramList = null;
    String paramS = "";
    String queryS = "";
    final String queryString = "SELECT key FROM " + Dish.class.getName();
    final Query q = PMF.get().getPersistenceManager().newQuery(queryString);

    if (queryWords.length > 0) {
      paramList = new ArrayList<Object>();
      queryS += "restaurant == restParam";
      paramS = Key.class.getName() + " restParam";
      paramList.add(restKey);

      for (int i = 0; i < queryWords.length; i++) {
        queryS += " && searchTerms.contains(s" + i + ")";
        paramS += ", String s" + i;
        paramList.add(queryWords[i]);
      }

      q.setFilter(queryS);
      q.declareParameters(paramS);

      return Datastore.get(new HashSet<Key>((List<Key>) q.executeWithArray(paramList.toArray())));
    }

    return new HashSet<Dish>();
  }
コード例 #2
0
 @Nullable
 private Object execute(Query query) {
   Object rv;
   if (!orderedConstants.isEmpty()) {
     rv = query.executeWithArray(orderedConstants.toArray());
   } else {
     rv = query.execute();
   }
   if (isDetach()) {
     rv = detach(rv);
   }
   return rv;
 }
コード例 #3
0
 public static List<VariableEffectDefinitionJDO> getVariableEffectDefinitions(
     PersistenceManager pm, Long gameId, String name) {
   Query query = pm.newQuery(VariableEffectDefinitionJDO.class);
   Object args[] = {gameId, name};
   if (ManagerUtil.generateFilter(args, params, paramsNames).trim().equals("")) {
     //                query.setFilter("deleted == null");
     return (List<VariableEffectDefinitionJDO>) query.execute();
   }
   query.setFilter(ManagerUtil.generateFilter(args, params, paramsNames));
   query.declareParameters(
       ManagerUtil.generateDeclareParameters(args, types, params, paramsNames));
   return ((List<VariableEffectDefinitionJDO>)
       query.executeWithArray(ManagerUtil.filterOutNulls(args)));
 }
コード例 #4
0
ファイル: JdoDao.java プロジェクト: remtcs/dcache
 @Override
 @Transactional(readOnly = true)
 public boolean hasSharedSticky(Pin pin) {
   PersistenceManager pm = _pmf.getPersistenceManager();
   Query query =
       pm.newQuery(
           Pin.class,
           "_pnfsId == :pnfsId && _id != :id && _pool == :pool && _sticky == :sticky && _state != 'UNPINNING'");
   query.setResult("count(this)");
   return ((Long)
           query.executeWithArray(
               pin.getPnfsId().toString(), pin.getPinId(),
               pin.getPool(), pin.getSticky()))
       > 0;
 }
コード例 #5
0
 public <T> List<T> getQuery(
     Class<T> c, String queryString, String parameterDeclaration, Object... parameters) {
   PersistenceManager pm = pmf.getPersistenceManager();
   try {
     Query query = pm.newQuery(c, queryString);
     query.declareParameters(parameterDeclaration);
     List<T> list = (List<T>) query.executeWithArray(parameters);
     List<T> buffer = new ArrayList<T>();
     for (T t : list) {
       buffer.add(t);
     }
     query.closeAll();
     return buffer;
   } catch (Throwable e) {
     LOG.warning(e);
   } finally {
     pm.close();
   }
   return new ArrayList<T>();
 }
コード例 #6
0
  @Override
  public List<SignalMetadata> getSignalList(
      Double minLatitude,
      Double minLongitude,
      Double maxLatitude,
      Double maxLongitude,
      List<String> carrierParams,
      List<String> phoneTypes,
      String clientId) {
    PersistenceManager manager = PMF.getManager();

    // If no carrier is specified, show points from all carriers
    if (carrierParams == null) {
      String[] carrierList = {"att", "verizon", "tmobile", "sprint"};
      carrierParams = Arrays.asList(carrierList);
    }

    // If no phoneType is specified, show points from all phoneTypes
    if (phoneTypes == null) {
      String[] phoneList = {"0", "1", "2", "3"};
      phoneTypes = Arrays.asList(phoneList);
    }

    // If user requests data for a region of size larger than 1.0000 by 1.0000 (about 11km by 11km),
    // only the center 1.0000 by 1.0000 of data will be returned
    if (maxLatitude - minLatitude > 1.0) {
      Double center = (maxLatitude + minLatitude) / 2.0;
      maxLatitude = center + .5;
      minLatitude = center - .5;
    }
    if (maxLongitude - minLongitude > 1.0) {
      Double centerl = (maxLongitude + minLongitude) / 2.0;
      maxLongitude = centerl + .5;
      minLongitude = centerl - .5;
    }

    try {
      ArrayList<SignalMetadata> signalInfo = new ArrayList<SignalMetadata>();

      // If clientId is absent, returns data within the given geographic box
      // for the given phoneType and carrier from the consolidated datastore
      if (clientId == null) {
        Query q = manager.newQuery(SignalInfoAvg.class);
        q.declareImports("import java.util.List");
        Object[] parameters = {minLatitude, maxLatitude, carrierParams, phoneTypes};
        q.declareParameters(
            "Double minLatitude, Double maxLatitude, List carrierParams, List phoneTypes");
        q.setFilter(
            "latitude >= minLatitude && latitude <= maxLatitude && carrierParams.contains(carrier) && phoneTypes.contains(phoneType)");

        List<SignalInfoAvg> allSignalInfo = (List<SignalInfoAvg>) q.executeWithArray(parameters);
        // Extent<SignalInfo> allSignalInfo = manager.getExtent(SignalInfo.class);

        ArrayList<SignalInfoAvg> filtered = new ArrayList<SignalInfoAvg>();
        for (SignalInfoAvg savg : allSignalInfo) {
          if (savg.getLongitude() >= minLongitude && savg.getLongitude() <= maxLongitude) {
            filtered.add(savg);
          }
        }

        for (SignalInfoAvg s : filtered) {
          signalInfo.add(s.getSignalMetadata());
        }

      } else {
        // If clientId is present, returns raw data of the given clientId,
        // phoneType, and carrier within the given geographic box
        Query q = manager.newQuery(SignalInfo.class);
        q.declareImports("import java.util.List");
        Object[] parameters = {minLatitude, maxLatitude, carrierParams, phoneTypes, clientId};
        q.declareParameters(
            "Double minLatitude, Double maxLatitude, List<String> carrierParams, List<String> phoneTypes, String myClientId");
        q.setFilter(
            "clientId == myClientId && latitude >= minLatitude && latitude <= maxLatitude && carrier == carrierParams && phoneType == phoneTypes");

        List<SignalInfo> allSignalInfo = (List<SignalInfo>) q.executeWithArray(parameters);
        // Extent<SignalInfo> allSignalInfo = manager.getExtent(SignalInfo.class);

        ArrayList<SignalInfo> filtered = new ArrayList<SignalInfo>();
        for (SignalInfo s : allSignalInfo) {
          if (s.getLongitude() >= minLongitude && s.getLongitude() <= maxLongitude) {
            filtered.add(s);
          }
        }

        for (SignalInfo s : filtered) {
          signalInfo.add(s.getSignalMetadata());
        }
      }

      return signalInfo;
    } finally {
      manager.close();
    }
  }
コード例 #7
0
  @SuppressWarnings("unchecked")
  public ServiceResult<List<Page>> getListPageByCriteria(
      int maximum, int[] criterias, String username, String q, String... cat_keys) {
    ServiceResult<List<Page>> result = new ServiceResult<List<Page>>();

    StringBuilder where = new StringBuilder();
    StringBuilder orderBy = new StringBuilder();
    String query = "";
    List<Object> listParameters = new ArrayList<Object>();

    if (criterias != null) {
      for (int criteria : criterias) {
        switch (criteria) {
          case 0:
            orderBy.append("date_post asc ");
            break;

          case 1:
            orderBy.append("date_post desc ");
            break;

          case 2:
            orderBy.append("price asc ");
            break;

          case 3:
            orderBy.append("price desc ");
            break;

          case 4:
            orderBy.append("page_view asc ");
            break;

          case 5:
            orderBy.append("page_view desc ");
            break;

          case 6:
            orderBy.append("quantity asc ");
            break;

          case 7:
            orderBy.append("quantity desc ");
            break;

          default:
            break;
        }
      }
    } else {
      orderBy.append("date_post desc ");
    }

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Query queryObj = null;
    List<Page> listPages = null;

    if (StringUtils.isEmptyOrNull(q)) {
      if (!StringUtils.isEmptyOrNull(username)) {
        if (cat_keys != null) {
          if (!StringUtils.isEmptyOrNull(where.toString())) where.append(" && ");
          where.append("setCategoryKeys.contains(catKey) && username==us ");
          listParameters.add(Arrays.asList(cat_keys));
          listParameters.add(username);

          query =
              "select from "
                  + Page.class.getName()
                  + " where ("
                  + where.toString()
                  + ") order by "
                  + orderBy.toString()
                  + ((maximum == 0) ? "" : (" limit " + maximum));
          queryObj = pm.newQuery(query);
          queryObj.declareParameters("String catKey, String us");
          listPages = (List<Page>) queryObj.executeWithArray(listParameters.toArray());
        } else {
          if (!StringUtils.isEmptyOrNull(where.toString())) where.append(" && ");
          where.append("username==us ");
          listParameters.add(username);

          query =
              "select from "
                  + Page.class.getName()
                  + " where ("
                  + where.toString()
                  + ") order by "
                  + orderBy.toString()
                  + ((maximum == 0) ? "" : (" limit " + maximum));

          queryObj = pm.newQuery(query);
          queryObj.declareParameters("String us");

          listPages = (List<Page>) queryObj.executeWithArray(listParameters.toArray());
        }
      } else { // end if q>username
        // Duplicate
        if (cat_keys != null) {
          if (!StringUtils.isEmptyOrNull(where.toString())) where.append(" && ");
          where.append("setCategoryKeys.contains(catKey) ");
          query =
              "select from "
                  + Page.class.getName()
                  + " where ("
                  + where.toString()
                  + ") order by "
                  + orderBy.toString()
                  + ((maximum == 0) ? "" : (" limit " + maximum));
          queryObj = pm.newQuery(query);
          queryObj.declareParameters("String catKey");
          listParameters.add(Arrays.asList(cat_keys));
          listPages = (List<Page>) queryObj.executeWithArray(listParameters.toArray());
        } else {
          query =
              "select from "
                  + Page.class.getName()
                  + (StringUtils.isEmptyOrNull(where.toString())
                      ? ""
                      : " where (" + where.toString() + ")")
                  + " order by "
                  + orderBy.toString()
                  + ((maximum == 0) ? "" : (" limit " + maximum));
          queryObj = pm.newQuery(query);
          listPages = (List<Page>) queryObj.execute();
        }
      } // end else q>username
    } else { // end if q
      // Prepare to search
      StringBuffer queryBuffer = new StringBuffer();
      Set<String> queryTokens =
          SearchJanitorUtils.getTokensForIndexingOrQuery(
              q, Global.MAXIMUM_NUMBER_OF_WORDS_TO_SEARCH);
      List<Object> parametersForSearch = new ArrayList<Object>(queryTokens);
      StringBuffer declareParametersBuffer = new StringBuffer(", ");
      int parameterCounter = 0;

      while (parameterCounter < queryTokens.size()) {
        queryBuffer.append("fts == param" + parameterCounter);
        declareParametersBuffer.append("String param" + parameterCounter);

        if (parameterCounter + 1 < queryTokens.size()) {
          queryBuffer.append(" && ");
          declareParametersBuffer.append(", ");
        }
        parameterCounter++;
      }
      // //////
      if (!StringUtils.isEmptyOrNull(username)) {
        if (cat_keys != null) {
          if (!StringUtils.isEmptyOrNull(where.toString())) where.append(" && ");
          where.append("setCategoryKeys.contains(catKey) && username==us && ");
          where.append(queryBuffer.toString());

          listParameters.add(Arrays.asList(cat_keys));
          listParameters.add(username);
          // listParameters.add(parametersForSearch.toArray());
          for (Object str : parametersForSearch) listParameters.add(str);

          query =
              "select from "
                  + Page.class.getName()
                  + " where ("
                  + where.toString()
                  + ") order by "
                  + orderBy.toString()
                  + ((maximum == 0) ? "" : (" limit " + maximum));
          queryObj = pm.newQuery(query);
          queryObj.declareParameters(
              "String catKey, String us" + declareParametersBuffer.toString());

          listPages = (List<Page>) queryObj.executeWithArray(listParameters.toArray());

        } else {
          if (!StringUtils.isEmptyOrNull(where.toString())) where.append(" && ");
          where.append("username==us && ");
          where.append(queryBuffer.toString());

          listParameters.add(username);
          // listParameters.add(parametersForSearch.toArray());
          for (Object str : parametersForSearch) listParameters.add(str);

          query =
              "select from "
                  + Page.class.getName()
                  + " where ("
                  + where.toString()
                  + ") order by "
                  + orderBy.toString()
                  + ((maximum == 0) ? "" : (" limit " + maximum));

          queryObj = pm.newQuery(query);
          queryObj.declareParameters("String us" + declareParametersBuffer.toString());

          listPages = (List<Page>) queryObj.executeWithArray(listParameters.toArray());
        }
      } else {
        // Duplicate
        if (cat_keys != null) {
          if (!StringUtils.isEmptyOrNull(where.toString())) where.append(" && ");
          where.append("setCategoryKeys.contains(catKey) && ");
          where.append(queryBuffer.toString());

          listParameters.add(Arrays.asList(cat_keys));
          // listParameters.add(parametersForSearch.toArray());
          for (Object str : parametersForSearch) listParameters.add(str);

          query =
              "select from "
                  + Page.class.getName()
                  + " where ("
                  + where.toString()
                  + ") order by "
                  + orderBy.toString()
                  + ((maximum == 0) ? "" : (" limit " + maximum));
          queryObj = pm.newQuery(query);
          queryObj.declareParameters("String catKey" + declareParametersBuffer.toString());

          listPages = (List<Page>) queryObj.executeWithArray(listParameters.toArray());
        } else {
          if (!StringUtils.isEmptyOrNull(where.toString())) where.append(" && ");
          where.append(queryBuffer.toString());
          // listParameters.add(parametersForSearch.toArray());
          for (Object str : parametersForSearch) listParameters.add(str);

          query =
              "select from "
                  + Page.class.getName()
                  + " where ("
                  + where.toString()
                  + ") order by "
                  + orderBy.toString()
                  + ((maximum == 0) ? "" : (" limit " + maximum));

          queryObj = pm.newQuery(query);
          queryObj.declareParameters(declareParametersBuffer.toString());

          listPages = (List<Page>) queryObj.executeWithArray(listParameters.toArray());
        }
      }
    } // end else q

    if (listPages.size() > 0) {
      result.setOK(true);
      result.setMessage(Global.messages.getString("search_page_by_criteria_successfully"));
      result.setResult(listPages);
    } else {
      result.setOK(false);
      result.setMessage(Global.messages.getString("search_page_by_criteria_fail"));
    }

    return result;
  }