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");
}
}
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;
}
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;
}
}
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);
}
}
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);
}
}
Aggregations