mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
major classes refactoring
This commit is contained in:
@@ -11,11 +11,11 @@ import javax.inject.Inject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.commafeed.backend.dao.ApplicationSettingsService;
|
||||
import com.commafeed.backend.dao.FeedCategoryService;
|
||||
import com.commafeed.backend.dao.FeedService;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionService;
|
||||
import com.commafeed.backend.dao.UserService;
|
||||
import com.commafeed.backend.dao.ApplicationSettingsDAO;
|
||||
import com.commafeed.backend.dao.FeedCategoryDAO;
|
||||
import com.commafeed.backend.dao.FeedDAO;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
||||
import com.commafeed.backend.dao.UserDAO;
|
||||
import com.commafeed.backend.feeds.FeedRefreshWorker;
|
||||
import com.commafeed.backend.model.ApplicationSettings;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
@@ -23,7 +23,8 @@ import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.model.UserRole.Role;
|
||||
import com.commafeed.backend.security.PasswordEncryptionService;
|
||||
import com.commafeed.backend.services.PasswordEncryptionService;
|
||||
import com.commafeed.backend.services.UserService;
|
||||
|
||||
@Startup
|
||||
@Singleton
|
||||
@@ -33,13 +34,16 @@ public class StartupBean {
|
||||
public static final String ADMIN_NAME = "admin";
|
||||
|
||||
@Inject
|
||||
FeedService feedService;
|
||||
FeedDAO feedDAO;
|
||||
|
||||
@Inject
|
||||
FeedCategoryService feedCategoryService;
|
||||
FeedCategoryDAO feedCategoryDAO;
|
||||
|
||||
@Inject
|
||||
FeedSubscriptionService feedSubscriptionService;
|
||||
FeedSubscriptionDAO feedSubscriptionDAO;
|
||||
|
||||
@Inject
|
||||
UserDAO userDAO;
|
||||
|
||||
@Inject
|
||||
UserService userService;
|
||||
@@ -48,7 +52,7 @@ public class StartupBean {
|
||||
PasswordEncryptionService encryptionService;
|
||||
|
||||
@Inject
|
||||
ApplicationSettingsService applicationSettingsService;
|
||||
ApplicationSettingsDAO applicationSettingsDAO;
|
||||
|
||||
@Inject
|
||||
FeedRefreshWorker worker;
|
||||
@@ -58,7 +62,7 @@ public class StartupBean {
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
startupTime = Calendar.getInstance().getTimeInMillis();
|
||||
if (userService.getCount() == 0) {
|
||||
if (userDAO.getCount() == 0) {
|
||||
initialData();
|
||||
}
|
||||
|
||||
@@ -72,57 +76,57 @@ public class StartupBean {
|
||||
private void initialData() {
|
||||
log.info("Populating database with default values");
|
||||
|
||||
applicationSettingsService.save(new ApplicationSettings());
|
||||
applicationSettingsDAO.save(new ApplicationSettings());
|
||||
|
||||
User user = userService.register(ADMIN_NAME, "admin",
|
||||
Arrays.asList(Role.ADMIN, Role.USER));
|
||||
userService.register("test", "test", Arrays.asList(Role.USER));
|
||||
|
||||
Feed dilbert = new Feed("http://feed.dilbert.com/dilbert/daily_strip");
|
||||
feedService.save(dilbert);
|
||||
feedDAO.save(dilbert);
|
||||
|
||||
Feed engadget = new Feed("http://www.engadget.com/rss.xml");
|
||||
feedService.save(engadget);
|
||||
feedDAO.save(engadget);
|
||||
|
||||
Feed frandroid = new Feed("http://feeds.feedburner.com/frandroid");
|
||||
feedService.save(frandroid);
|
||||
feedDAO.save(frandroid);
|
||||
|
||||
FeedCategory newsCategory = new FeedCategory();
|
||||
newsCategory.setName("News");
|
||||
newsCategory.setUser(user);
|
||||
feedCategoryService.save(newsCategory);
|
||||
feedCategoryDAO.save(newsCategory);
|
||||
|
||||
FeedCategory comicsCategory = new FeedCategory();
|
||||
comicsCategory.setName("Comics");
|
||||
comicsCategory.setUser(user);
|
||||
comicsCategory.setParent(newsCategory);
|
||||
feedCategoryService.save(comicsCategory);
|
||||
feedCategoryDAO.save(comicsCategory);
|
||||
|
||||
FeedCategory techCategory = new FeedCategory();
|
||||
techCategory.setName("Tech");
|
||||
techCategory.setUser(user);
|
||||
techCategory.setParent(newsCategory);
|
||||
feedCategoryService.save(techCategory);
|
||||
feedCategoryDAO.save(techCategory);
|
||||
|
||||
FeedSubscription sub = new FeedSubscription();
|
||||
sub.setCategory(comicsCategory);
|
||||
sub.setFeed(dilbert);
|
||||
sub.setTitle("Dilbert - Strips");
|
||||
sub.setUser(user);
|
||||
feedSubscriptionService.save(sub);
|
||||
feedSubscriptionDAO.save(sub);
|
||||
|
||||
FeedSubscription sub2 = new FeedSubscription();
|
||||
sub2.setCategory(techCategory);
|
||||
sub2.setFeed(engadget);
|
||||
sub2.setTitle("Engadget");
|
||||
sub2.setUser(user);
|
||||
feedSubscriptionService.save(sub2);
|
||||
feedSubscriptionDAO.save(sub2);
|
||||
|
||||
FeedSubscription sub3 = new FeedSubscription();
|
||||
sub3.setFeed(frandroid);
|
||||
sub3.setTitle("Frandroid");
|
||||
sub3.setUser(user);
|
||||
feedSubscriptionService.save(sub3);
|
||||
feedSubscriptionDAO.save(sub3);
|
||||
}
|
||||
|
||||
public long getStartupTime() {
|
||||
|
||||
@@ -12,7 +12,7 @@ import com.uaihebert.factory.EasyCriteriaFactory;
|
||||
import com.uaihebert.model.EasyCriteria;
|
||||
|
||||
@Stateless
|
||||
public class ApplicationSettingsService {
|
||||
public class ApplicationSettingsDAO {
|
||||
|
||||
@PersistenceContext
|
||||
protected EntityManager em;
|
||||
@@ -16,7 +16,7 @@ import com.uaihebert.model.EasyCriteria;
|
||||
|
||||
@Stateless
|
||||
@SuppressWarnings("serial")
|
||||
public class FeedCategoryService extends GenericDAO<FeedCategory> {
|
||||
public class FeedCategoryDAO extends GenericDAO<FeedCategory> {
|
||||
|
||||
public List<FeedCategory> findAll(User user) {
|
||||
EasyCriteria<FeedCategory> criteria = createCriteria();
|
||||
@@ -20,7 +20,7 @@ import com.uaihebert.model.EasyCriteria;
|
||||
|
||||
@Stateless
|
||||
@SuppressWarnings("serial")
|
||||
public class FeedService extends GenericDAO<Feed> {
|
||||
public class FeedDAO extends GenericDAO<Feed> {
|
||||
|
||||
public List<Feed> findNextUpdatable(int count) {
|
||||
CriteriaQuery<Feed> query = builder.createQuery(getType());
|
||||
@@ -56,7 +56,7 @@ public class FeedService extends GenericDAO<Feed> {
|
||||
return Iterables.getFirst(feeds, null);
|
||||
}
|
||||
|
||||
public Feed getByIdWithEntries(Long feedId, int offset, int limit) {
|
||||
public Feed findByIdWithEntries(Long feedId, int offset, int limit) {
|
||||
EasyCriteria<Feed> criteria = createCriteria();
|
||||
criteria.andEquals(MF.i(proxy().getId()), feedId);
|
||||
criteria.leftJoinFetch(MF.i(proxy().getEntries()));
|
||||
38
src/main/java/com/commafeed/backend/dao/FeedEntryDAO.java
Normal file
38
src/main/java/com/commafeed/backend/dao/FeedEntryDAO.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.commafeed.backend.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntry_;
|
||||
import com.commafeed.frontend.utils.ModelFactory.MF;
|
||||
import com.uaihebert.model.EasyCriteria;
|
||||
|
||||
@Stateless
|
||||
@SuppressWarnings("serial")
|
||||
public class FeedEntryDAO extends GenericDAO<FeedEntry> {
|
||||
|
||||
public List<FeedEntry> findByGuids(List<String> guids) {
|
||||
EasyCriteria<FeedEntry> criteria = createCriteria();
|
||||
criteria.setDistinctTrue();
|
||||
criteria.andStringIn(MF.i(proxy().getGuid()), guids);
|
||||
criteria.leftJoinFetch(MF.i(proxy().getFeeds()));
|
||||
return criteria.getResultList();
|
||||
}
|
||||
|
||||
public List<FeedEntry> findByFeed(Feed feed, int offset, int limit) {
|
||||
CriteriaQuery<FeedEntry> query = builder.createQuery(getType());
|
||||
Root<FeedEntry> root = query.from(getType());
|
||||
query.where(builder.isMember(feed, root.get(FeedEntry_.feeds)));
|
||||
query.orderBy(builder.desc(root.get(FeedEntry_.updated)));
|
||||
TypedQuery<FeedEntry> q = em.createQuery(query);
|
||||
q.setFirstResult(offset);
|
||||
q.setMaxResults(limit);
|
||||
return q.getResultList();
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.TypedQuery;
|
||||
@@ -33,10 +32,7 @@ import com.uaihebert.model.EasyCriteria;
|
||||
|
||||
@Stateless
|
||||
@SuppressWarnings("serial")
|
||||
public class FeedEntryStatusService extends GenericDAO<FeedEntryStatus> {
|
||||
|
||||
@Inject
|
||||
FeedCategoryService feedCategoryService;
|
||||
public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
|
||||
|
||||
public FeedEntryStatus findById(User user, Long id) {
|
||||
|
||||
@@ -58,7 +54,7 @@ public class FeedEntryStatusService extends GenericDAO<FeedEntryStatus> {
|
||||
return status;
|
||||
}
|
||||
|
||||
public List<FeedEntryStatus> getStatusesByKeywords(User user,
|
||||
public List<FeedEntryStatus> findByKeywords(User user,
|
||||
String keywords, int offset, int limit, boolean includeContent) {
|
||||
|
||||
String joinedKeywords = StringUtils.join(
|
||||
@@ -93,12 +89,12 @@ public class FeedEntryStatusService extends GenericDAO<FeedEntryStatus> {
|
||||
return q.getResultList();
|
||||
}
|
||||
|
||||
public List<FeedEntryStatus> getStatuses(User user, boolean unreadOnly,
|
||||
public List<FeedEntryStatus> findAll(User user, boolean unreadOnly,
|
||||
ReadingOrder order, boolean includeContent) {
|
||||
return getStatuses(user, unreadOnly, -1, -1, order, includeContent);
|
||||
return findAll(user, unreadOnly, -1, -1, order, includeContent);
|
||||
}
|
||||
|
||||
public List<FeedEntryStatus> getStatuses(User user, boolean unreadOnly,
|
||||
public List<FeedEntryStatus> findAll(User user, boolean unreadOnly,
|
||||
int offset, int limit, ReadingOrder order, boolean includeContent) {
|
||||
CriteriaQuery<FeedEntryStatus> query = builder.createQuery(getType());
|
||||
Root<FeedEntryStatus> root = query.from(getType());
|
||||
@@ -138,13 +134,13 @@ public class FeedEntryStatusService extends GenericDAO<FeedEntryStatus> {
|
||||
return map;
|
||||
}
|
||||
|
||||
public List<FeedEntryStatus> getStatuses(Feed feed, User user,
|
||||
public List<FeedEntryStatus> findByFeed(Feed feed, User user,
|
||||
boolean unreadOnly, ReadingOrder order, boolean includeContent) {
|
||||
return getStatuses(feed, user, unreadOnly, -1, -1, order,
|
||||
return findByFeed(feed, user, unreadOnly, -1, -1, order,
|
||||
includeContent);
|
||||
}
|
||||
|
||||
public List<FeedEntryStatus> getStatuses(Feed feed, User user,
|
||||
public List<FeedEntryStatus> findByFeed(Feed feed, User user,
|
||||
boolean unreadOnly, int offset, int limit, ReadingOrder order,
|
||||
boolean includeContent) {
|
||||
|
||||
@@ -173,14 +169,14 @@ public class FeedEntryStatusService extends GenericDAO<FeedEntryStatus> {
|
||||
return q.getResultList();
|
||||
}
|
||||
|
||||
public List<FeedEntryStatus> getStatuses(List<FeedCategory> categories,
|
||||
public List<FeedEntryStatus> findByCategories(List<FeedCategory> categories,
|
||||
User user, boolean unreadOnly, ReadingOrder order,
|
||||
boolean includeContent) {
|
||||
return getStatuses(categories, user, unreadOnly, -1, -1, order,
|
||||
return findByCategories(categories, user, unreadOnly, -1, -1, order,
|
||||
includeContent);
|
||||
}
|
||||
|
||||
public List<FeedEntryStatus> getStatuses(List<FeedCategory> categories,
|
||||
public List<FeedEntryStatus> findByCategories(List<FeedCategory> categories,
|
||||
User user, boolean unreadOnly, int offset, int limit,
|
||||
ReadingOrder order, boolean includeContent) {
|
||||
|
||||
@@ -209,15 +205,6 @@ public class FeedEntryStatusService extends GenericDAO<FeedEntryStatus> {
|
||||
return q.getResultList();
|
||||
}
|
||||
|
||||
private void limit(TypedQuery<FeedEntryStatus> query, int offset, int limit) {
|
||||
if (offset > -1) {
|
||||
query.setFirstResult(offset);
|
||||
}
|
||||
if (limit > -1) {
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
}
|
||||
|
||||
private void orderBy(CriteriaQuery<FeedEntryStatus> query,
|
||||
Root<FeedEntryStatus> root, ReadingOrder order) {
|
||||
Path<Date> orderPath = root.get(FeedEntryStatus_.entry).get(
|
||||
@@ -231,20 +218,20 @@ public class FeedEntryStatusService extends GenericDAO<FeedEntryStatus> {
|
||||
}
|
||||
|
||||
public void markFeedEntries(User user, Feed feed, Date olderThan) {
|
||||
List<FeedEntryStatus> statuses = getStatuses(feed, user, true,
|
||||
List<FeedEntryStatus> statuses = findByFeed(feed, user, true,
|
||||
ReadingOrder.desc, false);
|
||||
update(markList(statuses, olderThan));
|
||||
}
|
||||
|
||||
public void markCategoryEntries(User user, List<FeedCategory> categories,
|
||||
Date olderThan) {
|
||||
List<FeedEntryStatus> statuses = getStatuses(categories, user, true,
|
||||
List<FeedEntryStatus> statuses = findByCategories(categories, user, true,
|
||||
ReadingOrder.desc, false);
|
||||
update(markList(statuses, olderThan));
|
||||
}
|
||||
|
||||
public void markAllEntries(User user, Date olderThan) {
|
||||
List<FeedEntryStatus> statuses = getStatuses(user, true,
|
||||
List<FeedEntryStatus> statuses = findAll(user, true,
|
||||
ReadingOrder.desc, false);
|
||||
update(markList(statuses, olderThan));
|
||||
}
|
||||
@@ -3,72 +3,21 @@ package com.commafeed.backend.dao;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntryStatus;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.frontend.utils.ModelFactory.MF;
|
||||
import com.google.api.client.util.Lists;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.uaihebert.factory.EasyCriteriaFactory;
|
||||
import com.uaihebert.model.EasyCriteria;
|
||||
|
||||
@Stateless
|
||||
@SuppressWarnings("serial")
|
||||
public class FeedSubscriptionService extends GenericDAO<FeedSubscription> {
|
||||
public class FeedSubscriptionDAO extends GenericDAO<FeedSubscription> {
|
||||
|
||||
@Inject
|
||||
FeedCategoryService feedCategoryService;
|
||||
|
||||
@Inject
|
||||
FeedService feedService;
|
||||
|
||||
@Inject
|
||||
FeedEntryService feedEntryService;
|
||||
|
||||
@Inject
|
||||
FeedEntryStatusService feedEntryStatusService;
|
||||
|
||||
public void subscribe(User user, String url, String title,
|
||||
FeedCategory category) {
|
||||
|
||||
Feed feed = feedService.findByUrl(url);
|
||||
if (feed == null) {
|
||||
feed = new Feed();
|
||||
feed.setUrl(url);
|
||||
feedService.save(feed);
|
||||
}
|
||||
|
||||
FeedSubscription sub = findByFeed(user, feed);
|
||||
boolean newSubscription = false;
|
||||
if (sub == null) {
|
||||
sub = new FeedSubscription();
|
||||
sub.setFeed(feed);
|
||||
sub.setUser(user);
|
||||
newSubscription = true;
|
||||
}
|
||||
sub.setCategory(category);
|
||||
sub.setTitle(title);
|
||||
saveOrUpdate(sub);
|
||||
|
||||
if (newSubscription) {
|
||||
List<FeedEntryStatus> statuses = Lists.newArrayList();
|
||||
List<FeedEntry> allEntries = feedEntryService.findByFeed(feed, 0,
|
||||
10);
|
||||
for (FeedEntry entry : allEntries) {
|
||||
FeedEntryStatus status = new FeedEntryStatus();
|
||||
status.setEntry(entry);
|
||||
status.setRead(true);
|
||||
status.setSubscription(sub);
|
||||
statuses.add(status);
|
||||
}
|
||||
feedEntryStatusService.save(statuses);
|
||||
}
|
||||
}
|
||||
|
||||
public FeedSubscription findById(User user, Long id) {
|
||||
EasyCriteria<FeedSubscription> criteria = createCriteria();
|
||||
@@ -7,11 +7,13 @@ import java.util.List;
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import com.commafeed.backend.model.AbstractModel;
|
||||
import com.commafeed.backend.model.FeedEntryStatus;
|
||||
import com.commafeed.frontend.utils.ModelFactory.MF;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.uaihebert.factory.EasyCriteriaFactory;
|
||||
@@ -128,6 +130,16 @@ public abstract class GenericDAO<T extends AbstractModel> implements
|
||||
return criteria.getResultList();
|
||||
}
|
||||
|
||||
protected void limit(TypedQuery<FeedEntryStatus> query, int offset,
|
||||
int limit) {
|
||||
if (offset > -1) {
|
||||
query.setFirstResult(offset);
|
||||
}
|
||||
if (limit > -1) {
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Class<T> getType() {
|
||||
return (Class<T>) type.getRawType();
|
||||
|
||||
31
src/main/java/com/commafeed/backend/dao/UserDAO.java
Normal file
31
src/main/java/com/commafeed/backend/dao/UserDAO.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.commafeed.backend.dao;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.services.PasswordEncryptionService;
|
||||
|
||||
@Stateless
|
||||
@SuppressWarnings("serial")
|
||||
public class UserDAO extends GenericDAO<User> {
|
||||
|
||||
@Inject
|
||||
PasswordEncryptionService encryptionService;
|
||||
|
||||
public User findByName(String name) {
|
||||
TypedQuery<User> query = em.createNamedQuery("User.byName", User.class);
|
||||
query.setParameter("name", name.toLowerCase());
|
||||
|
||||
User user = null;
|
||||
try {
|
||||
user = query.getSingleResult();
|
||||
} catch (NoResultException e) {
|
||||
user = null;
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,13 +13,13 @@ import com.google.common.collect.Sets;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@Stateless
|
||||
public class UserRoleService extends GenericDAO<UserRole> {
|
||||
public class UserRoleDAO extends GenericDAO<UserRole> {
|
||||
|
||||
public List<UserRole> findAll(User user) {
|
||||
return findByField(MF.i(MF.p(UserRole.class).getUser()), user);
|
||||
}
|
||||
|
||||
public Set<Role> getRoles(User user) {
|
||||
public Set<Role> findRoles(User user) {
|
||||
Set<Role> list = Sets.newHashSet();
|
||||
for (UserRole role : findByField(MF.i(proxy().getUser()), user)) {
|
||||
list.add(role.getRole());
|
||||
@@ -11,7 +11,7 @@ import com.uaihebert.model.EasyCriteria;
|
||||
|
||||
@Stateless
|
||||
@SuppressWarnings("serial")
|
||||
public class UserSettingsService extends GenericDAO<UserSettings> {
|
||||
public class UserSettingsDAO extends GenericDAO<UserSettings> {
|
||||
|
||||
public UserSettings findByUser(User user) {
|
||||
|
||||
@@ -22,9 +22,10 @@ import org.apache.commons.lang.time.DateUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.commafeed.backend.dao.FeedEntryService;
|
||||
import com.commafeed.backend.dao.FeedService;
|
||||
import com.commafeed.backend.dao.FeedDAO;
|
||||
import com.commafeed.backend.dao.FeedEntryDAO;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.services.FeedUpdateService;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
@Stateless
|
||||
@@ -40,10 +41,13 @@ public class FeedRefreshWorker {
|
||||
FeedFetcher fetcher;
|
||||
|
||||
@Inject
|
||||
FeedService feedService;
|
||||
FeedDAO feedDAO;
|
||||
|
||||
@Inject
|
||||
FeedEntryService feedEntryService;
|
||||
FeedEntryDAO feedEntryDAO;
|
||||
|
||||
@Inject
|
||||
FeedUpdateService feedUpdateService;
|
||||
|
||||
@Resource
|
||||
private UserTransaction transaction;
|
||||
@@ -102,13 +106,13 @@ public class FeedRefreshWorker {
|
||||
transaction.begin();
|
||||
|
||||
if (fetchedFeed != null) {
|
||||
feedEntryService.updateEntries(feed.getUrl(),
|
||||
feedUpdateService.updateEntries(feed.getUrl(),
|
||||
fetchedFeed.getEntries());
|
||||
if (feed.getLink() == null) {
|
||||
feed.setLink(fetchedFeed.getLink());
|
||||
}
|
||||
}
|
||||
feedService.update(feed);
|
||||
feedDAO.update(feed);
|
||||
|
||||
transaction.commit();
|
||||
|
||||
@@ -121,10 +125,10 @@ public class FeedRefreshWorker {
|
||||
Feed feed = null;
|
||||
lock.lock();
|
||||
try {
|
||||
feed = Iterables.getFirst(feedService.findNextUpdatable(1), null);
|
||||
feed = Iterables.getFirst(feedDAO.findNextUpdatable(1), null);
|
||||
if (feed != null) {
|
||||
feed.setLastUpdated(Calendar.getInstance().getTime());
|
||||
feedService.update(feed);
|
||||
feedDAO.update(feed);
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
|
||||
@@ -7,11 +7,11 @@ import javax.inject.Inject;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.commafeed.backend.dao.FeedCategoryService;
|
||||
import com.commafeed.backend.dao.FeedService;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionService;
|
||||
import com.commafeed.backend.dao.FeedCategoryDAO;
|
||||
import com.commafeed.backend.dao.FeedDAO;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.services.FeedSubscriptionService;
|
||||
import com.sun.syndication.feed.opml.Opml;
|
||||
import com.sun.syndication.feed.opml.Outline;
|
||||
import com.sun.syndication.io.FeedException;
|
||||
@@ -20,13 +20,13 @@ import com.sun.syndication.io.WireFeedInput;
|
||||
public class OPMLImporter {
|
||||
|
||||
@Inject
|
||||
FeedService feedService;
|
||||
FeedDAO feedDAO;
|
||||
|
||||
@Inject
|
||||
FeedSubscriptionService feedSubscriptionService;
|
||||
|
||||
@Inject
|
||||
FeedCategoryService feedCategoryService;
|
||||
FeedCategoryDAO feedCategoryDAO;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void importOpml(User user, String xml) throws FeedException {
|
||||
@@ -43,14 +43,14 @@ public class OPMLImporter {
|
||||
private void handleOutline(User user, Outline outline, FeedCategory parent) {
|
||||
|
||||
if (StringUtils.isEmpty(outline.getType())) {
|
||||
FeedCategory category = feedCategoryService.findByName(user,
|
||||
FeedCategory category = feedCategoryDAO.findByName(user,
|
||||
outline.getText(), parent);
|
||||
if (category == null) {
|
||||
category = new FeedCategory();
|
||||
category.setName(outline.getText());
|
||||
category.setParent(parent);
|
||||
category.setUser(user);
|
||||
feedCategoryService.save(category);
|
||||
feedCategoryDAO.save(category);
|
||||
}
|
||||
|
||||
List<Outline> children = outline.getChildren();
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.commafeed.backend.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import com.commafeed.backend.dao.FeedCategoryDAO;
|
||||
import com.commafeed.backend.dao.FeedDAO;
|
||||
import com.commafeed.backend.dao.FeedEntryDAO;
|
||||
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntryStatus;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.google.api.client.util.Lists;
|
||||
|
||||
@Stateless
|
||||
public class FeedSubscriptionService {
|
||||
|
||||
@Inject
|
||||
FeedCategoryDAO feedCategoryDAO;
|
||||
|
||||
@Inject
|
||||
FeedDAO feedDAO;
|
||||
|
||||
@Inject
|
||||
FeedEntryDAO feedEntryDAO;
|
||||
|
||||
@Inject
|
||||
FeedEntryStatusDAO feedEntryStatusDAO;
|
||||
|
||||
@Inject
|
||||
FeedSubscriptionDAO feedSubscriptionDAO;
|
||||
|
||||
public void subscribe(User user, String url, String title,
|
||||
FeedCategory category) {
|
||||
|
||||
Feed feed = feedDAO.findByUrl(url);
|
||||
if (feed == null) {
|
||||
feed = new Feed();
|
||||
feed.setUrl(url);
|
||||
feedDAO.save(feed);
|
||||
}
|
||||
|
||||
FeedSubscription sub = feedSubscriptionDAO.findByFeed(user, feed);
|
||||
boolean newSubscription = false;
|
||||
if (sub == null) {
|
||||
sub = new FeedSubscription();
|
||||
sub.setFeed(feed);
|
||||
sub.setUser(user);
|
||||
newSubscription = true;
|
||||
}
|
||||
sub.setCategory(category);
|
||||
sub.setTitle(title);
|
||||
feedSubscriptionDAO.saveOrUpdate(sub);
|
||||
|
||||
if (newSubscription) {
|
||||
List<FeedEntryStatus> statuses = Lists.newArrayList();
|
||||
List<FeedEntry> allEntries = feedEntryDAO.findByFeed(feed, 0,
|
||||
10);
|
||||
for (FeedEntry entry : allEntries) {
|
||||
FeedEntryStatus status = new FeedEntryStatus();
|
||||
status.setEntry(entry);
|
||||
status.setRead(true);
|
||||
status.setSubscription(sub);
|
||||
statuses.add(status);
|
||||
}
|
||||
feedEntryStatusDAO.save(statuses);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.commafeed.backend.dao;
|
||||
package com.commafeed.backend.services;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
@@ -7,41 +7,44 @@ import java.util.List;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.apache.commons.lang.ObjectUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.commafeed.backend.dao.FeedDAO;
|
||||
import com.commafeed.backend.dao.FeedEntryDAO;
|
||||
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntryStatus;
|
||||
import com.commafeed.backend.model.FeedEntry_;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.frontend.utils.ModelFactory.MF;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.uaihebert.model.EasyCriteria;
|
||||
|
||||
@Stateless
|
||||
@SuppressWarnings("serial")
|
||||
public class FeedEntryService extends GenericDAO<FeedEntry> {
|
||||
public class FeedUpdateService {
|
||||
|
||||
@Inject
|
||||
FeedService feedService;
|
||||
FeedDAO feedDAO;
|
||||
|
||||
@Inject
|
||||
FeedSubscriptionService feedSubscriptionService;
|
||||
FeedSubscriptionDAO feedSubscriptionDAO;
|
||||
|
||||
@Inject
|
||||
FeedEntryDAO feedEntryDAO;
|
||||
|
||||
@Inject
|
||||
FeedEntryStatusDAO feedEntryStatusDAO;
|
||||
|
||||
public void updateEntries(String url, Collection<FeedEntry> entries) {
|
||||
Feed feed = feedService.findByUrl(url);
|
||||
Feed feed = feedDAO.findByUrl(url);
|
||||
List<String> guids = Lists.newArrayList();
|
||||
for (FeedEntry entry : entries) {
|
||||
guids.add(entry.getGuid());
|
||||
}
|
||||
|
||||
List<FeedEntry> existingEntries = guids.isEmpty() ? new ArrayList<FeedEntry>()
|
||||
: findByGuids(guids);
|
||||
: feedEntryDAO.findByGuids(guids);
|
||||
for (FeedEntry entry : entries) {
|
||||
FeedEntry foundEntry = null;
|
||||
for (FeedEntry existingEntry : existingEntries) {
|
||||
@@ -72,34 +75,15 @@ public class FeedEntryService extends GenericDAO<FeedEntry> {
|
||||
|
||||
private void addFeedToEntry(FeedEntry entry, Feed feed) {
|
||||
entry.getFeeds().add(feed);
|
||||
saveOrUpdate(entry);
|
||||
List<FeedSubscription> subscriptions = feedSubscriptionService
|
||||
feedEntryDAO.saveOrUpdate(entry);
|
||||
List<FeedSubscription> subscriptions = feedSubscriptionDAO
|
||||
.findByFeed(feed);
|
||||
for (FeedSubscription sub : subscriptions) {
|
||||
FeedEntryStatus status = new FeedEntryStatus();
|
||||
status.setEntry(entry);
|
||||
status.setSubscription(sub);
|
||||
em.persist(status);
|
||||
feedEntryStatusDAO.save(status);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public List<FeedEntry> findByGuids(List<String> guids) {
|
||||
EasyCriteria<FeedEntry> criteria = createCriteria();
|
||||
criteria.setDistinctTrue();
|
||||
criteria.andStringIn(MF.i(proxy().getGuid()), guids);
|
||||
criteria.leftJoinFetch(MF.i(proxy().getFeeds()));
|
||||
return criteria.getResultList();
|
||||
}
|
||||
|
||||
public List<FeedEntry> findByFeed(Feed feed, int offset, int limit) {
|
||||
CriteriaQuery<FeedEntry> query = builder.createQuery(getType());
|
||||
Root<FeedEntry> root = query.from(getType());
|
||||
query.where(builder.isMember(feed, root.get(FeedEntry_.feeds)));
|
||||
query.orderBy(builder.desc(root.get(FeedEntry_.updated)));
|
||||
TypedQuery<FeedEntry> q = em.createQuery(query);
|
||||
q.setFirstResult(offset);
|
||||
q.setMaxResults(limit);
|
||||
return q.getResultList();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.commafeed.backend.security;
|
||||
package com.commafeed.backend.services;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
@@ -13,14 +13,14 @@ import javax.ejb.Singleton;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.commafeed.backend.dao.UserService;
|
||||
import com.commafeed.backend.dao.UserDAO;
|
||||
|
||||
// http://www.javacodegeeks.com/2012/05/secure-password-storage-donts-dos-and.html
|
||||
@Singleton
|
||||
public class PasswordEncryptionService {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(UserService.class);
|
||||
.getLogger(UserDAO.class);
|
||||
|
||||
public boolean authenticate(String attemptedPassword,
|
||||
byte[] encryptedPassword, byte[] salt) {
|
||||
@@ -1,39 +1,26 @@
|
||||
package com.commafeed.backend.dao;
|
||||
package com.commafeed.backend.services;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
import com.commafeed.backend.dao.UserDAO;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.model.UserRole;
|
||||
import com.commafeed.backend.model.UserRole.Role;
|
||||
import com.commafeed.backend.security.PasswordEncryptionService;
|
||||
|
||||
@Stateless
|
||||
@SuppressWarnings("serial")
|
||||
public class UserService extends GenericDAO<User> {
|
||||
public class UserService {
|
||||
|
||||
@Inject
|
||||
UserDAO userDAO;
|
||||
|
||||
@Inject
|
||||
PasswordEncryptionService encryptionService;
|
||||
|
||||
public User findByName(String name) {
|
||||
TypedQuery<User> query = em.createNamedQuery("User.byName", User.class);
|
||||
query.setParameter("name", name.toLowerCase());
|
||||
|
||||
User user = null;
|
||||
try {
|
||||
user = query.getSingleResult();
|
||||
} catch (NoResultException e) {
|
||||
user = null;
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
public User login(String name, String password) {
|
||||
User user = findByName(name);
|
||||
User user = userDAO.findByName(name);
|
||||
if (user != null && !user.isDisabled()) {
|
||||
boolean authenticated = encryptionService.authenticate(password,
|
||||
user.getPassword(), user.getSalt());
|
||||
@@ -51,7 +38,7 @@ public class UserService extends GenericDAO<User> {
|
||||
|
||||
public User register(String name, String password, String email,
|
||||
Collection<Role> roles) {
|
||||
if (findByName(name) != null) {
|
||||
if (userDAO.findByName(name) != null) {
|
||||
return null;
|
||||
}
|
||||
User user = new User();
|
||||
@@ -65,7 +52,7 @@ public class UserService extends GenericDAO<User> {
|
||||
user.getRoles().add(new UserRole(user, role));
|
||||
user.getRoles().add(new UserRole(user, role));
|
||||
}
|
||||
save(user);
|
||||
userDAO.save(user);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user