Search in sources :

Example 46 with Post

use of it.vige.rubia.model.Post in project rubia-forums by flashboss.

the class ForumsSearchModuleImpl method findPosts.

@SuppressWarnings("unchecked")
public ResultPage<Post> findPosts(SearchCriteria criteria) throws ModuleException {
    if (criteria != null) {
        try {
            EntityManager session = getSession();
            FullTextSession fullTextSession = getFullTextSession((Session) session.getDelegate());
            Builder builder = new Builder();
            String keywords = criteria.getKeywords();
            if (keywords != null && keywords.length() != 0) {
                String[] fields = null;
                Searching searching = Searching.valueOf(criteria.getSearching());
                switch(searching) {
                    case TITLE_MSG:
                        fields = new String[] { "message.text", "topic.subject" };
                        break;
                    case MSG:
                        fields = new String[] { "message.text" };
                        break;
                }
                MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
                builder.add(parser.parse(keywords), MUST);
            }
            String forumId = criteria.getForum();
            if (forumId != null && forumId.length() != 0) {
                builder.add(new TermQuery(new Term("topic.forum.id", forumId)), MUST);
            }
            String categoryId = criteria.getCategory();
            if (categoryId != null && categoryId.length() != 0) {
                builder.add(new TermQuery(new Term("topic.forum.category.id", categoryId)), MUST);
            }
            String userName = criteria.getAuthor();
            if (userName != null && userName.length() != 0) {
                builder.add(new WildcardQuery(new Term("poster.userId", userName)), MUST);
            }
            String timePeriod = criteria.getTimePeriod();
            if (timePeriod != null && timePeriod.length() != 0) {
                addPostTimeQuery(builder, TimePeriod.valueOf(timePeriod));
            }
            FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(builder.build(), Post.class);
            SortOrder sortOrder = SortOrder.valueOf(criteria.getSortOrder());
            String sortByStr = criteria.getSortBy();
            SortBy sortBy = null;
            if (sortByStr != null)
                sortBy = valueOf(sortByStr);
            fullTextQuery.setSort(getSort(sortBy, sortOrder));
            fullTextQuery.setFirstResult(criteria.getPageSize() * criteria.getPageNumber());
            fullTextQuery.setMaxResults(criteria.getPageSize());
            ResultPage<Post> resultPage = new ResultPage<Post>();
            resultPage.setPage(fullTextQuery.list());
            resultPage.setResultSize(fullTextQuery.getResultSize());
            return resultPage;
        } catch (ParseException e) {
            return null;
        } catch (Exception e) {
            throw new ModuleException(e.getMessage(), e);
        }
    } else {
        throw new IllegalArgumentException("criteria cannot be null");
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) FullTextSession(org.hibernate.search.FullTextSession) Search.getFullTextSession(org.hibernate.search.Search.getFullTextSession) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) SortBy(it.vige.rubia.search.SortBy) Post(it.vige.rubia.model.Post) Builder(org.apache.lucene.search.BooleanQuery.Builder) SortOrder(it.vige.rubia.search.SortOrder) Term(org.apache.lucene.index.Term) ParseException(org.apache.lucene.queryparser.classic.ParseException) ModuleException(it.vige.rubia.ModuleException) EntityManager(javax.persistence.EntityManager) Searching(it.vige.rubia.search.Searching) ResultPage(it.vige.rubia.search.ResultPage) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ParseException(org.apache.lucene.queryparser.classic.ParseException) FullTextQuery(org.hibernate.search.FullTextQuery) ModuleException(it.vige.rubia.ModuleException)

Example 47 with Post

use of it.vige.rubia.model.Post in project rubia-forums by flashboss.

the class ForumsModuleImpl method addAttachments.

@Override
public Post addAttachments(Collection<Attachment> attachments, Post post) {
    if (attachments != null) {
        for (Attachment attachment : attachments) {
            attachment.setId(null);
            attachment.setPost(post);
            em.persist(attachment);
        }
        em.flush();
    }
    post = em.find(Post.class, post.getId());
    em.refresh(post);
    return post;
}
Also used : Post(it.vige.rubia.model.Post) Attachment(it.vige.rubia.model.Attachment)

Example 48 with Post

use of it.vige.rubia.model.Post in project rubia-forums by flashboss.

the class ForumsModuleImpl method findLastPost.

@Override
public Post findLastPost(Topic topic) throws ModuleException {
    try {
        TypedQuery<Post> query = em.createNamedQuery("findLastPostOrder", Post.class);
        query.setParameter("topicId", "" + topic.getId());
        query.setFirstResult(0);
        query.setMaxResults(1);
        Post lastPost = (Post) uniqueElement(query.getResultList());
        return lastPost;
    } catch (Exception e) {
        log.error(e);
        return null;
    }
}
Also used : Post(it.vige.rubia.model.Post) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException)

Example 49 with Post

use of it.vige.rubia.model.Post in project rubia-forums by flashboss.

the class ForumsModuleImpl method removePost.

@Override
public void removePost(int postId, boolean isLastPost) throws ModuleException {
    try {
        Post post = em.find(Post.class, postId);
        Topic topic = post.getTopic();
        Forum forum = topic.getForum();
        em.remove(post);
        topic.setReplies(topic.getReplies() - 1);
        forum.setPostCount(forum.getPostCount() - 1);
        List<Post> posts = findPostsByTopicId(topic);
        Post lastPost = posts.get(posts.size() - 1);
        if (isLastPost) {
            topic.setLastPostDate(lastPost.getCreateDate());
        }
        em.merge(topic);
        em.merge(forum);
        em.flush();
    } catch (Exception e) {
        String errorMessage = "Cannot delete post";
        throw new ModuleException(errorMessage, e);
    }
}
Also used : Post(it.vige.rubia.model.Post) Topic(it.vige.rubia.model.Topic) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException) Forum(it.vige.rubia.model.Forum)

Example 50 with Post

use of it.vige.rubia.model.Post in project rubia-forums by flashboss.

the class ForumsModuleImpl method findPostsByIdsFetchAttachmentsAndPosters.

private List<Post> findPostsByIdsFetchAttachmentsAndPosters(Collection<Integer> posts, String order) throws ModuleException {
    if (posts == null || posts.size() == 0) {
        return new LinkedList<Post>();
    }
    try {
        TypedQuery<Post> query = em.createNamedQuery("findPostsByIdsFetchAttachmentsAndPosters" + order, Post.class);
        query.setParameter("postIds", posts);
        List<Post> it = query.getResultList();
        List<Post> list = new LinkedList<Post>();
        for (Post post : it) {
            if (!list.contains(post)) {
                list.add(post);
            }
        }
        return list;
    } catch (Exception e) {
        String message = "Cannot find posts";
        throw new ModuleException(message, e);
    }
}
Also used : Post(it.vige.rubia.model.Post) LinkedList(java.util.LinkedList) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException)

Aggregations

Post (it.vige.rubia.model.Post)98 Topic (it.vige.rubia.model.Topic)62 Forum (it.vige.rubia.model.Forum)52 CreateTopic.createTopic (it.vige.rubia.selenium.forum.action.CreateTopic.createTopic)43 CreateForum.createForum (it.vige.rubia.selenium.adminpanel.action.CreateForum.createForum)38 Category (it.vige.rubia.model.Category)34 RemoveTopic.removeTopic (it.vige.rubia.selenium.forum.action.RemoveTopic.removeTopic)32 RemoveForum.removeForum (it.vige.rubia.selenium.adminpanel.action.RemoveForum.removeForum)31 CreateCategory.createCategory (it.vige.rubia.selenium.adminpanel.action.CreateCategory.createCategory)28 RemoveCategory.removeCategory (it.vige.rubia.selenium.adminpanel.action.RemoveCategory.removeCategory)28 Attachment (it.vige.rubia.model.Attachment)25 Test (org.junit.Test)25 Poll (it.vige.rubia.model.Poll)21 Poster (it.vige.rubia.model.Poster)20 PollOption (it.vige.rubia.model.PollOption)19 CreatePost.createPost (it.vige.rubia.selenium.forum.action.CreatePost.createPost)17 Message (it.vige.rubia.model.Message)14 Date (java.util.Date)14 BeforeClass (org.junit.BeforeClass)12 WebElement (org.openqa.selenium.WebElement)12