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

102 lines
3.9 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;
2014-08-17 14:16:30 +02:00
import javax.inject.Inject;
import javax.inject.Singleton;
2014-08-09 13:25:53 +02:00
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
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.backend.service.UserService;
2014-08-09 13:25:53 +02:00
import com.commafeed.frontend.resource.CategoryREST;
import com.google.common.base.Optional;
2014-08-09 13:25:53 +02:00
import com.google.common.collect.Iterables;
@SuppressWarnings("serial")
2014-08-17 14:16:30 +02:00
@RequiredArgsConstructor(onConstructor = @__({ @Inject }))
@Singleton
2014-08-09 13:25:53 +02:00
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 UserService userService;
2014-08-09 13:25:53 +02:00
private final CommaFeedConfiguration config;
@Override
2014-08-15 15:20:21 +02:00
protected void doGet(final 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-15 15:20:21 +02:00
final Optional<User> user = new UnitOfWork<Optional<User>>(sessionFactory) {
@Override
protected Optional<User> runInSession() throws Exception {
return userService.login(req.getSession());
}
}.run();
if (!user.isPresent()) {
2014-08-09 19:15:11 +02:00
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.get());
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user.get(), subs, true, null, null, 0, 1,
order, true, false, null);
2014-08-09 19:15:11 +02:00
status = Iterables.getFirst(statuses, null);
} else {
FeedCategory category = feedCategoryDAO.findById(user.get(), Long.valueOf(categoryId));
2014-08-09 19:15:11 +02:00
if (category != null) {
List<FeedCategory> children = feedCategoryDAO.findAllChildrenCategories(user.get(), category);
List<FeedSubscription> subscriptions = feedSubscriptionDAO.findByCategories(user.get(), children);
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user.get(), subscriptions, true, null,
null, 0, 1, order, true, false, null);
2014-08-09 19:15:11 +02:00
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));
}
}
}