Files
Athou_commafeed/src/main/java/com/commafeed/backend/dao/FeedSubscriptionService.java

69 lines
1.9 KiB
Java
Raw Normal View History

2013-03-20 20:33:42 +01:00
package com.commafeed.backend.dao;
import java.util.List;
import javax.ejb.Stateless;
2013-03-22 22:11:40 +01:00
import javax.inject.Inject;
import javax.persistence.TypedQuery;
import org.apache.commons.lang.ObjectUtils;
2013-03-20 20:33:42 +01:00
import com.commafeed.frontend.utils.ModelFactory.MF;
import com.commafeed.model.FeedCategory;
import com.commafeed.model.FeedSubscription;
import com.commafeed.model.User;
2013-03-22 22:11:40 +01:00
import com.google.common.collect.Lists;
2013-03-22 11:42:25 +01:00
import com.uaihebert.factory.EasyCriteriaFactory;
import com.uaihebert.model.EasyCriteria;
2013-03-20 20:33:42 +01:00
@Stateless
public class FeedSubscriptionService extends GenericDAO<FeedSubscription, Long> {
2013-03-22 22:11:40 +01:00
@Inject
FeedCategoryService feedCategoryService;
2013-03-20 20:33:42 +01:00
public List<FeedSubscription> findAll(User user) {
return findByField(MF.i(MF.p(FeedCategory.class).getUser()), user);
}
2013-03-22 22:11:40 +01:00
2013-03-22 11:42:25 +01:00
public List<FeedSubscription> findWithoutCategories(User user) {
2013-03-22 22:11:40 +01:00
EasyCriteria<FeedSubscription> criteria = EasyCriteriaFactory
.createQueryCriteria(em, getType());
2013-03-22 11:42:25 +01:00
criteria.andEquals("user", user);
2013-03-23 00:09:57 +01:00
criteria.andIsNull("category");
2013-03-22 11:42:25 +01:00
return criteria.getResultList();
2013-03-22 22:11:40 +01:00
2013-03-22 11:42:25 +01:00
}
2013-03-22 22:11:40 +01:00
public List<FeedSubscription> findWithCategory(User user,
FeedCategory category) {
List<FeedCategory> categories = Lists.newArrayList();
for (FeedCategory c : feedCategoryService.findAll(user)) {
if (isChild(c, category)) {
categories.add(c);
}
}
String query = "select s from FeedSubscription s where s.user=:user and s.category in :categories";
TypedQuery<FeedSubscription> typedQuery = em.createQuery(query,
FeedSubscription.class);
typedQuery.setParameter("user", user);
typedQuery.setParameter("categories", categories);
return typedQuery.getResultList();
}
private boolean isChild(FeedCategory c, FeedCategory category) {
boolean isChild = false;
while (c != null) {
if (ObjectUtils.equals(c.getId(), category.getId())) {
isChild = true;
break;
}
c = c.getParent();
}
return isChild;
}
2013-03-20 20:33:42 +01:00
}