run in session

This commit is contained in:
Athou
2014-08-09 19:15:11 +02:00
parent 0329c7d876
commit cf96a0a84e
2 changed files with 37 additions and 38 deletions

View File

@@ -189,8 +189,8 @@ public class CommaFeedApplication extends Application<CommaFeedConfiguration> {
environment.jersey().register(new UserREST(userDAO, userRoleDAO, userSettingsDAO, userService, encryptionService)); environment.jersey().register(new UserREST(userDAO, userRoleDAO, userSettingsDAO, userService, encryptionService));
// Servlets // Servlets
NextUnreadServlet nextUnreadServlet = new NextUnreadServlet(feedSubscriptionDAO, feedEntryStatusDAO, feedCategoryDAO, userService, NextUnreadServlet nextUnreadServlet = new NextUnreadServlet(sessionFactory, feedSubscriptionDAO, feedEntryStatusDAO,
config); feedCategoryDAO, config);
LogoutServlet logoutServlet = new LogoutServlet(config); LogoutServlet logoutServlet = new LogoutServlet(config);
environment.servlets().addServlet("next", nextUnreadServlet).addMapping("/next"); environment.servlets().addServlet("next", nextUnreadServlet).addMapping("/next");
environment.servlets().addServlet("logout", logoutServlet).addMapping("/logout"); environment.servlets().addServlet("logout", logoutServlet).addMapping("/logout");

View File

@@ -10,21 +10,21 @@ import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.hibernate.SessionFactory;
import com.commafeed.CommaFeedApplication;
import com.commafeed.CommaFeedConfiguration; import com.commafeed.CommaFeedConfiguration;
import com.commafeed.backend.dao.FeedCategoryDAO; import com.commafeed.backend.dao.FeedCategoryDAO;
import com.commafeed.backend.dao.FeedEntryStatusDAO; import com.commafeed.backend.dao.FeedEntryStatusDAO;
import com.commafeed.backend.dao.FeedSubscriptionDAO; import com.commafeed.backend.dao.FeedSubscriptionDAO;
import com.commafeed.backend.dao.UnitOfWork;
import com.commafeed.backend.model.FeedCategory; import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedEntryStatus; import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedSubscription; import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.User; import com.commafeed.backend.model.User;
import com.commafeed.backend.model.UserSettings.ReadingOrder; import com.commafeed.backend.model.UserSettings.ReadingOrder;
import com.commafeed.backend.service.UserService;
import com.commafeed.frontend.resource.CategoryREST; import com.commafeed.frontend.resource.CategoryREST;
import com.google.common.base.Optional;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@SuppressWarnings("serial") @SuppressWarnings("serial")
@@ -33,58 +33,57 @@ public class NextUnreadServlet extends HttpServlet {
public static final String PARAM_CATEGORYID = "category"; public static final String PARAM_CATEGORYID = "category";
public static final String PARAM_READINGORDER = "order"; public static final String PARAM_READINGORDER = "order";
public static final String PARAM_APIKEY = "apiKey";
private final SessionFactory sessionFactory;
private final FeedSubscriptionDAO feedSubscriptionDAO; private final FeedSubscriptionDAO feedSubscriptionDAO;
private final FeedEntryStatusDAO feedEntryStatusDAO; private final FeedEntryStatusDAO feedEntryStatusDAO;
private final FeedCategoryDAO feedCategoryDAO; private final FeedCategoryDAO feedCategoryDAO;
private final UserService userService;
private final CommaFeedConfiguration config; private final CommaFeedConfiguration config;
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String categoryId = req.getParameter(PARAM_CATEGORYID); final String categoryId = req.getParameter(PARAM_CATEGORYID);
String orderParam = req.getParameter(PARAM_READINGORDER); String orderParam = req.getParameter(PARAM_READINGORDER);
String apiKey = req.getParameter(PARAM_APIKEY);
if (apiKey == null) { final User user = (User) req.getSession().getAttribute(CommaFeedApplication.SESSION_USER);
resp.getWriter().write("api key is required"); if (user == null) {
resp.sendRedirect(resp.encodeRedirectURL(config.getApplicationSettings().getPublicUrl()));
return; return;
} }
Optional<User> user = userService.login(apiKey); final ReadingOrder order = StringUtils.equals(orderParam, "asc") ? ReadingOrder.asc : ReadingOrder.desc;
if (!user.isPresent()) {
resp.getWriter().write("unknown user or api key not found");
return;
}
ReadingOrder order = ReadingOrder.desc; FeedEntryStatus status = new UnitOfWork<FeedEntryStatus>(sessionFactory) {
@Override
if (StringUtils.equals(orderParam, "asc")) { protected FeedEntryStatus runInSession() throws Exception {
order = ReadingOrder.asc; FeedEntryStatus status = null;
} if (StringUtils.isBlank(categoryId) || CategoryREST.ALL.equals(categoryId)) {
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user);
List<FeedEntryStatus> statuses = null; List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user, subs, true, null, null, 0, 1, order,
if (StringUtils.isBlank(categoryId) || CategoryREST.ALL.equals(categoryId)) { true, false, null);
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user.get()); status = Iterables.getFirst(statuses, null);
statuses = feedEntryStatusDAO.findBySubscriptions(user.get(), subs, true, null, null, 0, 1, order, true, false, null); } else {
} else { FeedCategory category = feedCategoryDAO.findById(user, Long.valueOf(categoryId));
FeedCategory category = feedCategoryDAO.findById(user.get(), Long.valueOf(categoryId)); if (category != null) {
if (category != null) { List<FeedCategory> children = feedCategoryDAO.findAllChildrenCategories(user, category);
List<FeedCategory> children = feedCategoryDAO.findAllChildrenCategories(user.get(), category); List<FeedSubscription> subscriptions = feedSubscriptionDAO.findByCategories(user, children);
List<FeedSubscription> subscriptions = feedSubscriptionDAO.findByCategories(user.get(), children); List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user, subscriptions, true, null, null, 0,
statuses = feedEntryStatusDAO.findBySubscriptions(user.get(), subscriptions, true, null, null, 0, 1, order, true, false, 1, order, true, false, null);
null); status = Iterables.getFirst(statuses, null);
}
}
if (status != null) {
status.setRead(true);
feedEntryStatusDAO.saveOrUpdate(status);
}
return status;
} }
} }.run();
if (CollectionUtils.isEmpty(statuses)) { if (status == null) {
resp.sendRedirect(resp.encodeRedirectURL(config.getApplicationSettings().getPublicUrl())); resp.sendRedirect(resp.encodeRedirectURL(config.getApplicationSettings().getPublicUrl()));
} else { } else {
FeedEntryStatus status = Iterables.getFirst(statuses, null);
String url = status.getEntry().getUrl(); String url = status.getEntry().getUrl();
status.setRead(true);
feedEntryStatusDAO.saveOrUpdate(status);
resp.sendRedirect(resp.encodeRedirectURL(url)); resp.sendRedirect(resp.encodeRedirectURL(url));
} }
} }