@Test public void oneToOneToMany_projection() { Map<String, User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).as(Projections.constructor(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment))))); assertEquals(2, results.size()); User user = results.get("Jane"); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void transform_results() { List<Post> results = POST_W_COMMENTS.transform( groupBy(postId).list(Projections.constructor(Post.class, postId, postName, set(qComment)))); assertEquals(4, results.size()); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void oneToOneToMany_projection() { List<User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).list(Projections.constructor(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment))))); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean() { List<User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).list(Projections.bean(User.class, userName, Projections.bean(Post.class, postId, postName, set(qComment).as("comments")).as("latestPost")))); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean_and_constructor() { Map<String, User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).as(Projections.bean(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment)).as("latestPost")))); assertEquals(2, results.size()); User user = results.get("Jane"); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void transform_results() { CloseableIterator<Post> resultsIt = POST_W_COMMENTS.transform( groupBy(postId).iterate(Projections.constructor(Post.class, postId, postName, set(qComment)))); List<Post> results = IteratorAdapter.asList(resultsIt); assertEquals(4, results.size()); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void transform_as_bean() { List<Post> results = POST_W_COMMENTS.transform( groupBy(postId).list(Projections.bean(Post.class, postId, postName, set(qComment).as("comments")))); assertEquals(4, results.size()); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean_and_constructor() { List<User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).list(Projections.bean(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment)).as("latestPost")))); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean() { Map<String, User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).as(Projections.bean(User.class, userName, Projections.bean(Post.class, postId, postName, set(qComment).as("comments")).as("latestPost")))); assertEquals(2, results.size()); User user = results.get("Jane"); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void transform_as_bean() { CloseableIterator<Post> resultsIt = POST_W_COMMENTS.transform( groupBy(postId).iterate(Projections.bean(Post.class, postId, postName, set(qComment).as("comments")))); List<Post> results = IteratorAdapter.asList(resultsIt); assertEquals(4, results.size()); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void oneToOneToMany_projection() { CloseableIterator<User> resultsIt = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).iterate(Projections.constructor(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment))))); List<User> results = IteratorAdapter.asList(resultsIt); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean_and_constructor() { CloseableIterator<User> resultsIt = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).iterate(Projections.bean(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment)).as("latestPost")))); List<User> results = IteratorAdapter.asList(resultsIt); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean() { CloseableIterator<User> resultsIt = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).iterate(Projections.bean(User.class, userName, Projections.bean(Post.class, postId, postName, set(qComment).as("comments")).as("latestPost")))); List<User> results = IteratorAdapter.asList(resultsIt); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void transform_via_groupByProjection() { Map<Integer, Post> results = POST_W_COMMENTS2.transform( new GroupByProjection<Integer, Post>(postId, postName, set(qComment)) { @Override protected Post transform(Group group) { return new Post( group.getOne(postId), group.getOne(postName), group.getSet(qComment)); } }); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void transform_results() { Map<Integer, Post> results = POST_W_COMMENTS.transform( groupBy(postId).as(Projections.constructor(Post.class, postId, postName, set(qComment)))); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void transform_as_bean() { Map<Integer, Post> results = POST_W_COMMENTS.transform( groupBy(postId).as(Projections.bean(Post.class, postId, postName, set(qComment).as("comments")))); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
FaceBook faceBook = new FaceBookTemplate(accessToken); FeedOperations feeds=faceBook.feedOperations(); List<Post> posts=feeds.getPosts(); List<Comment> comments=new ArrayList<>(); for(Post post:posts){ List<Comment> commentsList = post.getComments(); for(Comment comment:commentsList){ comments.add(comment); } }
public List<Comment> getComments(Post post) { List<Comment> fullComments = new ArrayList<>(); try { // get first few comments using getComments from post PagableList<Comment> comments = post.getComments(); Paging<Comment> paging; do { for (Comment comment: comments) fullComments.add(comment); // get next page // NOTE: somehow few comments will not be included. // however, this won't affect much on our research paging = comments.getPaging(); } while ((paging != null) && ((comments = fb_.fetchNext(paging)) != null)); } catch (FacebookException ex) { Logger.getLogger(Facebook.class.getName()) .log(Level.SEVERE, null, ex); } return fullComments; }
//post_id and commentData received from view Post post=Post.findByPostId(post_id); //find post of that comment where findByPostId() is a function in model Comment comment=new Comment(commentData,null,post); //Create a new Comment Object Comment cm=Comment.save(comment); //where save() saves the Comment object in data base and return the saved object List <Comments> allCommentsOnPost=post.getComments(); //get all comments on that post allComments.add(cm); //add new comment to list post.setComments(allCommentsOnPost); //set the new list in Post object post.update(post_id); //update post entity
@ApiMethod(name = "getPostComments") public List<CommentResults> getPostComments(@Named("postId") Long postId) { EntityManager mgr = getEntityManager(); List<CommentResults> results = new ArrayList<Comment>(); try { Post p = mgr.find(Post.class, postId); if (p == null) { throw new EntityNotFoundException("Post does not exist"); } else { for(Comment c:p.getComments()){ //copying the data CommentResults cr = new CommentResults(); cr.setMessage(c.getMessage); cr.setUserId(c.getUserId); results.add(cr); } } } finally { mgr.close(); } return results; }