Files
Athou_commafeed/src/main/java/com/commafeed/frontend/servlet/NextUnreadServlet.java

92 lines
3.6 KiB
Java
Raw Normal View History

2014-08-09 13:25:53 +02:00
package com.commafeed.frontend.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
2014-08-09 19:15:11 +02:00
import org.hibernate.SessionFactory;
2014-08-09 13:25:53 +02:00
2014-08-09 19:15:11 +02:00
import com.commafeed.CommaFeedApplication;
2014-08-09 13:25:53 +02:00
import com.commafeed.CommaFeedConfiguration;
import com.commafeed.backend.dao.FeedCategoryDAO;
import com.commafeed.backend.dao.FeedEntryStatusDAO;
import com.commafeed.backend.dao.FeedSubscriptionDAO;
2014-08-09 19:15:11 +02:00
import com.commafeed.backend.dao.UnitOfWork;
2014-08-09 13:25:53 +02:00
import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.User;
import com.commafeed.backend.model.UserSettings.ReadingOrder;
import com.commafeed.frontend.resource.CategoryREST;
import com.google.common.collect.Iterables;
@SuppressWarnings("serial")
@RequiredArgsConstructor
public class NextUnreadServlet extends HttpServlet {
2014-08-14 11:40:30 +02:00
private static final String PARAM_CATEGORYID = "category";
private static final String PARAM_READINGORDER = "order";
2014-08-09 13:25:53 +02:00
2014-08-09 19:15:11 +02:00
private final SessionFactory sessionFactory;
2014-08-09 13:25:53 +02:00
private final FeedSubscriptionDAO feedSubscriptionDAO;
private final FeedEntryStatusDAO feedEntryStatusDAO;
private final FeedCategoryDAO feedCategoryDAO;
private final CommaFeedConfiguration config;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
2014-08-09 19:15:11 +02:00
final String categoryId = req.getParameter(PARAM_CATEGORYID);
2014-08-09 13:25:53 +02:00
String orderParam = req.getParameter(PARAM_READINGORDER);
2014-08-09 19:15:11 +02:00
final User user = (User) req.getSession().getAttribute(CommaFeedApplication.SESSION_USER);
if (user == null) {
resp.sendRedirect(resp.encodeRedirectURL(config.getApplicationSettings().getPublicUrl()));
2014-08-09 13:25:53 +02:00
return;
}
2014-08-09 19:15:11 +02:00
final ReadingOrder order = StringUtils.equals(orderParam, "asc") ? ReadingOrder.asc : ReadingOrder.desc;
FeedEntryStatus status = new UnitOfWork<FeedEntryStatus>(sessionFactory) {
@Override
protected FeedEntryStatus runInSession() throws Exception {
FeedEntryStatus status = null;
if (StringUtils.isBlank(categoryId) || CategoryREST.ALL.equals(categoryId)) {
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user);
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user, subs, true, null, null, 0, 1, order,
true, false, null);
status = Iterables.getFirst(statuses, null);
} else {
FeedCategory category = feedCategoryDAO.findById(user, Long.valueOf(categoryId));
if (category != null) {
List<FeedCategory> children = feedCategoryDAO.findAllChildrenCategories(user, category);
List<FeedSubscription> subscriptions = feedSubscriptionDAO.findByCategories(user, children);
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user, subscriptions, true, null, null, 0,
1, order, true, false, null);
status = Iterables.getFirst(statuses, null);
}
}
if (status != null) {
status.setRead(true);
feedEntryStatusDAO.saveOrUpdate(status);
}
return status;
2014-08-09 13:25:53 +02:00
}
2014-08-09 19:15:11 +02:00
}.run();
2014-08-09 13:25:53 +02:00
2014-08-09 19:15:11 +02:00
if (status == null) {
2014-08-09 13:25:53 +02:00
resp.sendRedirect(resp.encodeRedirectURL(config.getApplicationSettings().getPublicUrl()));
} else {
String url = status.getEntry().getUrl();
resp.sendRedirect(resp.encodeRedirectURL(url));
}
}
}