@Override public void updateComment(Comment comment) throws NonExistingEntityException, IllegalArgumentException { if (comment == null) { throw new IllegalArgumentException("Comment is null."); } if (comment.getId() == null) { throw new IllegalArgumentException("Comment id is null."); } commentCache = provider.getCacheContainer().getCache("commentcache"); if (!commentCache.containsKey(comment.getId())) { throw new NonExistingEntityException("Comment does not exist in cache."); } try { userTransaction.begin(); commentCache.put(comment.getId(), comment); userTransaction.commit(); logger.info("Comment with id: " + comment.getId() + " was updated in cache store."); } catch (Exception e) { if (userTransaction != null) { try { userTransaction.rollback(); } catch (Exception e1) { e1.printStackTrace(); } } logger.error("Error while updating comment.", e); throw new CacheException(e); } }
@Override public Comment getCommentById(String id) { if (id == null) { throw new IllegalArgumentException("Comment id is null."); } commentCache = provider.getCacheContainer().getCache("commentcache"); if (commentCache.containsKey(id)) { return (Comment) commentCache.get(id); } else { return null; } }
@Override public List<Comment> getCommentsBySongId(String songId) { if (songId == null) { throw new IllegalArgumentException("Song id is null."); } ArrayList<Comment> comments = new ArrayList<Comment>(); commentCache = provider.getCacheContainer().getCache("commentcache"); SearchManager sm = Search.getSearchManager((Cache<String, Object>) commentCache); QueryBuilder queryBuilder = sm.buildQueryBuilderForClass(Comment.class).get(); Query q = queryBuilder.keyword().onField("songId").matching(songId).createQuery(); logger.debug("Lucene query: " + q); CacheQuery cq = sm.getQuery(q, Comment.class); for (Object o : cq.list()) { if (o instanceof Comment) { comments.add(((Comment) o)); } } return comments; }
@Override public void createComment(Comment comment) throws IllegalEntityException, IllegalArgumentException, CacheException { if (comment == null) { throw new IllegalArgumentException("Comment is null."); } if (comment.getId() != null) { throw new IllegalEntityException( "Comment id is not null, Comment entity cannot be put into cache."); } commentCache = provider.getCacheContainer().getCache("commentcache"); comment.setId(UUIDStringGenerator.generateCommentId()); try { userTransaction.begin(); commentCache.put(comment.getId(), comment); userTransaction.commit(); logger.info( "Comment with id: " + comment.getId() + ", songID" + comment.getSongId() + " was inserted to cache store."); } catch (Exception e) { if (userTransaction != null) { try { userTransaction.rollback(); } catch (Exception e1) { e1.printStackTrace(); } } logger.error("Error while putting comment to the comment cache.", e); throw new CacheException(e); } }