Package org.damour.base.client.exceptions

Examples of org.damour.base.client.exceptions.SimpleMessageException


    Logger.log("Ping received from: " + getThreadLocalRequest().getRemoteAddr());
  }

  public String executeHQL(String query, boolean executeUpdate) {
    if (StringUtils.isEmpty(query)) {
      throw new SimpleMessageException("Query not supplied.");
    }
    User authUser = getAuthenticatedUser(session.get());
    if (authUser == null || !authUser.isAdministrator()) {
      throw new SimpleMessageException("Insufficient authorization.");
    }
    Transaction tx = null;
    try {
      tx = session.get().beginTransaction();
      String result;
      if (executeUpdate) {
        result = "Rows affected: " + session.get().createQuery(query).executeUpdate();
      } else {
        List<?> list = session.get().createQuery(query).list();
        result = "Objects returned: " + list.size() + "\r\n";
        for (int i = 0; i < list.size(); i++) {
          result += "Obj[" + i + "]: " + list.get(i).toString() + "\r\n";
        }
      }

      tx.commit();
      return result;
    } catch (Throwable t) {
      Logger.log(t);
      try {
        tx.rollback();
      } catch (Throwable tt) {
      }
      throw new SimpleMessageException(t.getMessage());
    }
  }
View Full Code Here


    }
  }

  public UserRating getUserRating(PermissibleObject permissibleObject) throws SimpleMessageException {
    if (permissibleObject == null) {
      throw new SimpleMessageException("PermissibleObject not supplied.");
    }
    User authUser = getAuthenticatedUser(session.get());
    try {
      permissibleObject = (PermissibleObject) session.get().load(PermissibleObject.class, permissibleObject.getId());
      if (!SecurityHelper.doesUserHavePermission(session.get(), authUser, permissibleObject, PERM.READ)) {
        throw new SimpleMessageException("User is not authorized to get rating on this content.");
      }
      // find rating based on remote address if needed
      return RatingHelper.getUserRating(session.get(), permissibleObject, authUser, getVoterGUID());
    } catch (Throwable t) {
      Logger.log(t);
      throw new SimpleMessageException(t.getMessage());
    }
  }
View Full Code Here

    }
  }

  public UserRating setUserRating(PermissibleObject permissibleObject, int rating) throws SimpleMessageException {
    if (permissibleObject == null) {
      throw new SimpleMessageException("PermissibleObject not supplied.");
    }
    User authUser = getAuthenticatedUser(session.get());
    Transaction tx = session.get().beginTransaction();
    try {
      permissibleObject = (PermissibleObject) session.get().load(PermissibleObject.class, permissibleObject.getId());

      if (!SecurityHelper.doesUserHavePermission(session.get(), authUser, permissibleObject, PERM.READ)) {
        throw new SimpleMessageException("User is not authorized to set rating on this content.");
      }

      UserRating userRating = RatingHelper.getUserRating(session.get(), permissibleObject, authUser, getVoterGUID());
      // check if rating already exists
      if (userRating != null) {
        // TODO: consider changing the vote
        // simply subtract the previous amount and decrement the numRatingVotes and redivide
        throw new SimpleMessageException("Already voted.");
      }

      float totalRating = (float) permissibleObject.getNumRatingVotes() * permissibleObject.getAverageRating();
      totalRating += rating;
      permissibleObject.setNumRatingVotes(permissibleObject.getNumRatingVotes() + 1);
      float newAvg = totalRating / (float) permissibleObject.getNumRatingVotes();
      permissibleObject.setAverageRating(newAvg);
      session.get().save(permissibleObject);

      userRating = new UserRating();
      userRating.setPermissibleObject(permissibleObject);
      userRating.setRating(rating);
      userRating.setVoter(authUser);
      userRating.setVoterGUID(getVoterGUID());

      session.get().save(userRating);
      tx.commit();
      return userRating;
    } catch (Throwable t) {
      Logger.log(t);
      try {
        tx.rollback();
      } catch (Throwable tt) {
      }
      throw new SimpleMessageException(t.getMessage());
    }
  }
View Full Code Here

    }
  }

  public UserThumb getUserThumb(PermissibleObject permissibleObject) throws SimpleMessageException {
    if (permissibleObject == null) {
      throw new SimpleMessageException("PermissibleObject not supplied.");
    }
    User authUser = getAuthenticatedUser(session.get());
    try {
      permissibleObject = (PermissibleObject) session.get().load(PermissibleObject.class, permissibleObject.getId());
      if (!SecurityHelper.doesUserHavePermission(session.get(), authUser, permissibleObject, PERM.READ)) {
        throw new SimpleMessageException("User is not authorized to get thumbs on this content.");
      }
      // find thumb based on remote address if needed
      return ThumbHelper.getUserThumb(session.get(), permissibleObject, authUser, getVoterGUID());
    } catch (Throwable t) {
      Logger.log(t);
      throw new SimpleMessageException(t.getMessage());
    }
  }
View Full Code Here

    }
  }

  public UserThumb setUserThumb(PermissibleObject permissibleObject, boolean like) throws SimpleMessageException {
    if (permissibleObject == null) {
      throw new SimpleMessageException("PermissibleObject not supplied.");
    }
    User authUser = getAuthenticatedUser(session.get());
    Transaction tx = session.get().beginTransaction();
    try {
      permissibleObject = (PermissibleObject) session.get().load(PermissibleObject.class, permissibleObject.getId());

      if (!SecurityHelper.doesUserHavePermission(session.get(), authUser, permissibleObject, PERM.READ)) {
        throw new SimpleMessageException("User is not authorized to set thumbs on this content.");
      }

      UserThumb userThumb = ThumbHelper.getUserThumb(session.get(), permissibleObject, authUser, getVoterGUID());
      // check if thumb already exists
      if (userThumb != null) {
        // TODO: consider changing the vote
        // simply subtract the previous amount and decrement the numRatingVotes and redivide
        throw new SimpleMessageException("Already voted.");
      }

      if (like) {
        permissibleObject.setNumUpVotes(permissibleObject.getNumUpVotes() + 1);
      } else {
        permissibleObject.setNumDownVotes(permissibleObject.getNumDownVotes() + 1);
      }
      session.get().save(permissibleObject);

      userThumb = new UserThumb();
      userThumb.setPermissibleObject(permissibleObject);
      userThumb.setLikeThumb(like);
      userThumb.setVoter(authUser);
      userThumb.setVoterGUID(getVoterGUID());

      session.get().save(userThumb);
      tx.commit();
      return userThumb;
    } catch (Throwable t) {
      Logger.log(t);
      try {
        tx.rollback();
      } catch (Throwable tt) {
      }
      throw new SimpleMessageException(t.getMessage());
    }
  }
View Full Code Here

    }
  }

  public List<PermissibleObject> getMostRated(int maxResults, String classType) throws SimpleMessageException {
    if (classType == null) {
      throw new SimpleMessageException("classType not supplied.");
    }
    User authUser = getAuthenticatedUser(session.get());
    try {
      List<PermissibleObject> mostRated = new ArrayList<PermissibleObject>();
      String simpleClassName = Class.forName(classType).getSimpleName();
      List<PermissibleObject> list = session.get().createQuery("from " + simpleClassName + " where numRatingVotes > 0 order by numRatingVotes desc")
          .setMaxResults(maxResults).setCacheable(true).list();
      for (PermissibleObject permissibleObject : list) {
        if (SecurityHelper.doesUserHavePermission(session.get(), authUser, permissibleObject, PERM.READ)) {
          mostRated.add(permissibleObject);
        }
      }
      return mostRated;
    } catch (Throwable t) {
      Logger.log(t);
      throw new SimpleMessageException(t.getMessage());
    }
  }
View Full Code Here

TOP

Related Classes of org.damour.base.client.exceptions.SimpleMessageException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.