Example #1
0
  /**
   * Returns whether user is allowed to edit the entity if he created the entity
   *
   * @param <T> any object that extends TDPersistable
   * @param tdUser logged in user
   * @param t instance of T
   * @return
   */
  @SuppressWarnings("unchecked")
  public static <T extends TDPersistable> boolean isAccessible(Long id, T t) {
    boolean isAccessAllowed = true;
    try {
      TDUser tdUser = TDUserService.getUser();
      if (id != null && null != tdUser && tdUser.getRole() == TDUserRole.ROLE_STANDARD) {
        try {
          T returnValT = (T) Datastore.get(KeyFactory.createKey(t.getClass().getSimpleName(), id));
          if (returnValT != null) {
            if (returnValT.getCreator().getId() != Long.valueOf(tdUser.getKey().getId())) {
              isAccessAllowed = false;
            }
          }
        } catch (JDOObjectNotFoundException jdoe) {
          isAccessAllowed = false;
        } catch (Exception e) {
          isAccessAllowed = false;
        }
      }
      return isAccessAllowed;

    } catch (UserNotLoggedInException e) {
      return false;
    } catch (UserNotFoundException e) {
      return false;
    }
  }
Example #2
0
  /**
   * 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>();
  }
Example #3
0
 /**
  * Get the latest {@link Review} written for a {@link Dish}.
  *
  * @param dishKey {@link Key} of the {@link Dish}
  * @return the {@link Review}
  */
 public static Review getLatestReviewByDish(Key dishKey) {
   final Key reviewKey = getLatestReviewKeyByDish(dishKey);
   if (null != reviewKey) {
     return Datastore.get(getLatestReviewKeyByDish(dishKey));
   } else {
     return null;
   }
 }
Example #4
0
 /**
  * Gets the latest {@link Review} submitted by a {@link TDUser}
  *
  * @param userKey {@link Key} of the {@link TDUser}
  * @return the {@link Review}
  */
 public static Review getLatestReviewByUser(Key userKey) {
   final Key reviewKey = getLatestReviewKeyByUser(userKey);
   if (null != reviewKey) {
     return Datastore.get(reviewKey);
   } else {
     return null;
   }
 }
Example #5
0
 /**
  * Get the first review written about a dish.
  *
  * @param dishKey {@link Key} of the {@link Dish}
  * @return the {@link Review}
  */
 public static Review getFirstReviewByDish(Key dishKey) {
   final Key reviewKey = getFirstReviewKeyByDish(dishKey);
   if (null != reviewKey) {
     return (Review) Datastore.get(reviewKey);
   } else {
     return null;
   }
 }
Example #6
0
  /**
   * Returns the most recent <code>numDishes</code> {@link Dish}es added
   *
   * @param numDishes number of {@link Dish}es to return
   * @return {@link List} of {@link Dish}es ordered from newest to oldest
   */
  @SuppressWarnings("unchecked")
  public static Set<Dish> getNewestDishes(int numDishes) {
    final String query = "SELECT key FROM " + Dish.class.getName();
    Query q = PMF.get().getPersistenceManager().newQuery(query);
    q.setOrdering("dateCreated desc");
    q.setRange("0, " + numDishes);

    return Datastore.get((Collection<Key>) q.execute());
  }
Example #7
0
  /**
   * Find {@link Source}s by name with a limit
   *
   * @param name - the source's name
   * @param limit - limit results
   * @return a {@link Set} of related {@link Source}s
   */
  @SuppressWarnings("unchecked")
  public static Set<Source> searchSourcebyName(String name, int limit) {

    final String queryString = "SELECT key FROM " + Source.class.getName();
    final Query q = PMF.get().getPersistenceManager().newQuery(queryString);
    q.setFilter("name == :p");
    q.setRange("0, " + limit);

    return Datastore.get(new HashSet<Key>((List<Key>) q.execute(name.toLowerCase())));
  }
Example #8
0
  /**
   * Search tags by name and types
   *
   * @param stringTag name of a single tag
   * @param types {@link Set} of tag types
   * @param limit maximum number of results
   * @return {@link Set} of {@link Tag}s
   */
  @SuppressWarnings("unchecked")
  public static Set<Tag> searchTagsByNameType(String stringTag, Set<Integer> types, int limit) {

    final String queryString = "SELECT key FROM " + Tag.class.getName();
    final Query q = PMF.get().getPersistenceManager().newQuery(queryString);
    q.setFilter("searchTerms.contains(:searchTerm) && :typeParam.contains(type)");
    q.setRange("0, " + limit);

    return Datastore.get(
        new HashSet<Key>((Collection<Key>) q.execute(stringTag.toLowerCase(), types)));
  }
Example #9
0
 /**
  * Gets the direction of the latest {@link Review} by a given {@link TDUser} for a given {@link
  * Dish}.
  *
  * @param userKey {@link Key} of the {@link TDUser}
  * @param dishKey {@link Key} of the {@link Dish}
  * @return {@link Review#POSITIVE_DIRECTION}, {@link Review#NEGATIVE_DIRECTION}, or 0 if none
  *     found
  */
 public static int getLatestUserVoteByDish(Key userKey, Key dishKey) {
   final Key revKey = getLatestReviewKeyByUserDish(userKey, dishKey);
   if (null != revKey) {
     final Review review = Datastore.get(revKey);
     if (null != review) {
       return review.getDirection();
     } else {
       return 0;
     }
   } else {
     return 0;
   }
 }
Example #10
0
  /**
   * Gets the top <code>numUsers</code> sorted by most to least ratings.
   *
   * @param numUsers number of {@link TDUser}s to return
   * @return {@link List} of {@link TDUser}s
   */
  @SuppressWarnings("unchecked")
  public static List<TDUser> getTopUsers(int numUsers) {
    final String query = "SELECT key FROM " + TDUser.class.getName();
    Query q = PMF.get().getPersistenceManager().newQuery(query);
    q.setOrdering("numReviews desc");
    q.setRange("0, " + numUsers);

    final Set<TDUser> results = Datastore.get((Collection<Key>) q.execute());
    final List<TDUser> toReturn = new ArrayList<TDUser>(results);
    // Collections.sort(toReturn, )

    return toReturn;
  }
Example #11
0
  /**
   * Gets the top <code>numDishes</code> {@link Dish}es ranked from most to least positive reviews
   *
   * @param numDishes number of {@link Dish}es to return
   * @return {@link List} of {@link Dish}es
   */
  @SuppressWarnings("unchecked")
  public static List<Dish> getTopDishes(int numDishes) {
    final String query = "SELECT key FROM " + Dish.class.getName();
    Query q = PMF.get().getPersistenceManager().newQuery(query);
    q.setOrdering("posReviews desc");
    q.setRange("0," + numDishes);

    final Set<Dish> results = Datastore.get((Collection<Key>) q.execute());
    final List<Dish> toReturn = new ArrayList<Dish>(results);
    Collections.sort(toReturn, new DishPosReviewsComparator());

    return toReturn;
  }
Example #12
0
  /**
   * Gets the top <code>numRestaurantss</code> {@link Restaurant}s ranked from most to least
   * positive reviews <br>
   * <b>Note: this does not actually return the top X restaurants, just a random X restaurants!</b>
   *
   * @param numRestaurants number of {@link Restaurant}s to return
   * @return {@link List} of {@link Restaurant}s
   */
  @SuppressWarnings("unchecked")
  public static List<Restaurant> getTopRestaurants(int numRestaurants) {
    final String query = "SELECT key FROM " + Restaurant.class.getName();
    Query q = PMF.get().getPersistenceManager().newQuery(query);
    // TODO: return a query that actually chooses the X highest rated
    // restaurants
    // q.setOrdering("posReviews desc");
    q.setRange("0," + numRestaurants);

    final Set<Restaurant> results = Datastore.get((Collection<Key>) q.execute());
    final List<Restaurant> toReturn = new ArrayList<Restaurant>(results);
    Collections.sort(toReturn, new RestaurantPosReviewsComparator());

    return toReturn;
  }
Example #13
0
  /**
   * Search through all {@link Tag}s given an array of {@link String} search terms
   *
   * @param queryWords array of {@link String} terms to search with
   * @param maxResults maximum number of results
   * @return list of Tags matching the search terms
   */
  @SuppressWarnings("unchecked")
  public static Set<Tag> searchTags(String[] queryWords, int maxResults) {
    // TODO: can this be replaced by a generic searchItems somehow?
    // Using a TDSearchable Interface would work

    final String queryString = "SELECT key FROM " + Tag.class.getName();
    final Query query = PMF.get().getPersistenceManager().newQuery(queryString);
    query.setFilter("searchTerms.contains(:searchParam)");
    query.setRange(0, maxResults);

    if (queryWords.length > 0) {
      return Datastore.get((Collection<Key>) query.execute(Arrays.asList(queryWords)));
    }

    return new HashSet<Tag>();
  }
Example #14
0
 /**
  * Returns a model.
  *
  * @param key the key
  * @return a model
  */
 public T get(Key key) {
   return Datastore.get(modelClass, key);
 }
Example #15
0
 /**
  * Returns models.
  *
  * @param keys the keys
  * @return models
  */
 public List<T> get(List<Key> keys) {
   return Datastore.get(modelClass, keys);
 }
Example #16
0
 /**
  * Gets all of the {@link Review}s created by a {@link TDUser}
  *
  * @param userKey {@link Key} of the {@link TDUser}
  * @return {@link List} of {@link Review} objects sorted from newest to oldest
  */
 public static Set<Review> getReviewsByUser(Key userKey) {
   return Datastore.get(getReviewKeysByUser(userKey));
 }
Example #17
0
 /**
  * Gets all of the review objects for a dish
  *
  * @param dishKey {@link Key} of the {@link Dish}
  * @return {@link List} of {@link Dish} objects sorted from newest to oldest
  */
 public static Set<Review> getReviewsByDish(Key dishKey) {
   return Datastore.get(getReviewKeysByDish(dishKey));
 }
Example #18
0
 /**
  * Gets all of the {@link Dish}es at a {@link Restaurant}
  *
  * @param restKey {@link Key} of the {@link Restaurant}
  * @return {@link Collection} of {@link Dish} objects
  */
 public static Set<Dish> getDishesByRestaurant(Key restKey) {
   return Datastore.get(getDishKeysByRestaurant(restKey));
 }