Search in sources :

Example 1 with ModuleException

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

the class SplitTopic method splitAfter.

// ---------- UI Actions supported by this bean ----------------------------
/**
 * This user interface action is spliting topic after post selected by user.
 *
 * @return the name of the operation
 */
@SecureActionForum
@Interceptors(AuthorizationListener.class)
public String splitAfter() {
    // Checking whether topic has only one post, so it can't be splitted
    if (posts.size() == 1) {
        setWarnBundleMessage("ERR_SPLIT_ONE_POST_TOPIC");
        return "";
    }
    // Removing all not slected posts
    Iterator<Integer> selectIt = checkboxes.keySet().iterator();
    while (selectIt.hasNext()) {
        Boolean postFlag = checkboxes.get(selectIt.next());
        if (!postFlag.booleanValue()) {
            selectIt.remove();
        }
    }
    // Checking if user selected anything.
    if (checkboxes.size() == 0) {
        setWarnBundleMessage("ERR_NO_POST_SELECTED");
        return "";
    }
    // User can't select more than one post for this action.
    if (checkboxes.size() != 1) {
        setWarnBundleMessage("Too_many_error");
        return "";
    }
    // check if user selected first post
    try {
        posts = forumsModule.findPostsByTopicId(topic);
        if (posts.get(0).getId().equals(checkboxes.keySet().iterator().next())) {
            setWarnBundleMessage("ERR_SPLIT_ALL");
            return "";
        }
    } catch (ModuleException e1) {
        setWarnBundleMessage("ERR_SPLIT_ALL");
        return "";
    } catch (Exception e1) {
        setWarnBundleMessage("ERR_SPLIT_ALL");
        return "";
    }
    // Trying to get destination forum for new topic.
    String toForumId = getParameter(p_forum_to_id);
    if (toForumId == null || toForumId.trim().compareToIgnoreCase("-1") == 0 || toForumId.trim().length() == 0) {
        setWarnBundleMessage("ERR_DEST_FORUM");
        return "";
    }
    // Checking if user gave subject for new topic.
    if (newTopicTitle == null || newTopicTitle.trim().compareToIgnoreCase("-1") == 0 || newTopicTitle.trim().length() == 0) {
        setWarnBundleMessage("ERR_NO_SUBJECT_GIVEN");
        return "";
    }
    try {
        Forum destForum = forumsModule.findForumById(new Integer(toForumId));
        // Creating new topic in destination forum.
        Topic newTopic = forumsModule.createTopic(destForum, getUser(userModule).getId().toString(), newTopicTitle, topic.getType());
        // Getting post id after which the topic must be splitted.
        Integer selectedPostId = (Integer) checkboxes.keySet().iterator().next();
        // Searching for the split pointing post in topic.
        Iterator<Post> it = posts.iterator();
        Post tempPost = null;
        while (it.hasNext()) {
            tempPost = it.next();
            // searching for post to split after
            if (tempPost.getId().equals(selectedPostId)) {
                break;
            }
        }
        List<Post> postsToRemove = new ArrayList<Post>();
        // Adding splitting post and all which are after him to new topic.
        if (tempPost != null) {
            tempPost.setTopic(newTopic);
            forumsModule.update(tempPost);
            postsToRemove.add(tempPost);
        }
        while (it.hasNext()) {
            Post post = it.next();
            post.setTopic(newTopic);
            forumsModule.update(post);
            postsToRemove.add(post);
        }
        newTopic = forumsModule.findTopicById(newTopic.getId());
        List<Post> postsNewTopic = forumsModule.findPostsByTopicId(newTopic);
        newTopic.setReplies(postsNewTopic.size() - 1);
        newTopic.setLastPostDate(postsNewTopic.get(postsNewTopic.size() - 1).getCreateDate());
        Forum fromForum = topic.getForum();
        topic.setReplies(topic.getReplies() - newTopic.getReplies() - 1);
        fromForum.setPostCount(fromForum.getPostCount() - newTopic.getReplies() - 1);
        posts.removeAll(postsToRemove);
        topic.setLastPostDate(posts.get(posts.size() - 1).getCreateDate());
        destForum.addTopicSize();
        destForum.setPostCount(destForum.getPostCount() + newTopic.getReplies() + 1);
        forumsModule.update(newTopic);
        forumsModule.update(topic);
        forumsModule.update(fromForum);
        forumsModule.update(destForum);
    } catch (Exception e) {
        log.error(e);
        setWarnBundleMessage("ERR_INTERNAL");
        return "";
    }
    // Setting message that everything went smooth.
    setInfoBundleMessage("SUCC_TOPIC_SPLITTED");
    return "";
}
Also used : Post(it.vige.rubia.model.Post) ArrayList(java.util.ArrayList) ModuleException(it.vige.rubia.ModuleException) Topic(it.vige.rubia.model.Topic) ModuleException(it.vige.rubia.ModuleException) Forum(it.vige.rubia.model.Forum) SecureActionForum(it.vige.rubia.auth.SecureActionForum) Interceptors(javax.interceptor.Interceptors) SecureActionForum(it.vige.rubia.auth.SecureActionForum)

Example 2 with ModuleException

use of it.vige.rubia.ModuleException 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 3 with ModuleException

use of it.vige.rubia.ModuleException 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 4 with ModuleException

use of it.vige.rubia.ModuleException 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 5 with ModuleException

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

the class ForumsSearchModuleImpl method findTopics.

@SuppressWarnings("unchecked")
public ResultPage<Topic> findTopics(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());
            SortBy sortBy = valueOf(criteria.getSortBy());
            fullTextQuery.setSort(getSort(sortBy, sortOrder));
            fullTextQuery.setProjection("topic.id");
            LinkedHashSet<Integer> topicIds = new LinkedHashSet<Integer>();
            LinkedHashSet<Integer> topicToDispIds = new LinkedHashSet<Integer>();
            int start = criteria.getPageSize() * criteria.getPageNumber();
            int end = start + criteria.getPageSize();
            int index = 0;
            for (Object o : fullTextQuery.list()) {
                Integer id = (Integer) ((Object[]) o)[0];
                if (topicIds.add(id)) {
                    if (index >= start && index < end) {
                        topicToDispIds.add(id);
                    }
                    index++;
                }
            }
            List<Topic> topics = null;
            if (topicToDispIds.size() > 0) {
                Query q = session.createQuery("from Topic as t join fetch t.poster where t.id IN ( :topicIds )");
                q.setParameter("topicIds", topicToDispIds);
                List<Topic> results = q.getResultList();
                topics = new LinkedList<Topic>();
                for (Integer id : topicToDispIds) {
                    for (Topic topic : results) {
                        if (id.equals(topic.getId())) {
                            topics.add(topic);
                            break;
                        }
                    }
                }
            }
            ResultPage<Topic> resultPage = new ResultPage<Topic>();
            resultPage.setPage(topics);
            resultPage.setResultSize(topicIds.size());
            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 : LinkedHashSet(java.util.LinkedHashSet) WildcardQuery(org.apache.lucene.search.WildcardQuery) FullTextSession(org.hibernate.search.FullTextSession) Search.getFullTextSession(org.hibernate.search.Search.getFullTextSession) FullTextQuery(org.hibernate.search.FullTextQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(javax.persistence.Query) TermQuery(org.apache.lucene.search.TermQuery) SortBy(it.vige.rubia.search.SortBy) Builder(org.apache.lucene.search.BooleanQuery.Builder) ResultPage(it.vige.rubia.search.ResultPage) Topic(it.vige.rubia.model.Topic) ModuleException(it.vige.rubia.ModuleException) TermQuery(org.apache.lucene.search.TermQuery) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) 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) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ParseException(org.apache.lucene.queryparser.classic.ParseException) FullTextQuery(org.hibernate.search.FullTextQuery)

Aggregations

ModuleException (it.vige.rubia.ModuleException)13 ResultPage (it.vige.rubia.search.ResultPage)8 Searching (it.vige.rubia.search.Searching)8 SortBy (it.vige.rubia.search.SortBy)8 SortOrder (it.vige.rubia.search.SortOrder)8 EntityManager (javax.persistence.EntityManager)8 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)8 Term (org.apache.lucene.index.Term)8 MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)8 ParseException (org.apache.lucene.queryparser.classic.ParseException)8 Builder (org.apache.lucene.search.BooleanQuery.Builder)8 TermQuery (org.apache.lucene.search.TermQuery)8 WildcardQuery (org.apache.lucene.search.WildcardQuery)8 FullTextQuery (org.hibernate.search.FullTextQuery)8 FullTextSession (org.hibernate.search.FullTextSession)8 Search.getFullTextSession (org.hibernate.search.Search.getFullTextSession)8 Topic (it.vige.rubia.model.Topic)7 Post (it.vige.rubia.model.Post)6 LinkedHashSet (java.util.LinkedHashSet)4 Query (javax.persistence.Query)4