package com.commafeed.backend.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.NoResultException;

import org.apache.commons.lang.ObjectUtils;

import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedCategory_;
import com.commafeed.backend.model.User;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.uaihebert.model.EasyCriteria;

@Stateless
public class FeedCategoryDAO extends GenericDAO<FeedCategory> {

	public List<FeedCategory> findAll(User user) {
		EasyCriteria<FeedCategory> criteria = createCriteria();
		criteria.andEquals(FeedCategory_.user.getName(), user);

		criteria.innerJoinFetch(FeedCategory_.user.getName());
		return criteria.getResultList();
	}

	public FeedCategory findById(User user, Long id) {
		EasyCriteria<FeedCategory> criteria = createCriteria();
		criteria.andEquals(FeedCategory_.user.getName(), user);
		criteria.andEquals(FeedCategory_.id.getName(), id);
		return Iterables.getFirst(criteria.getResultList(), null);
	}

	public FeedCategory findByName(User user, String name, FeedCategory parent) {
		EasyCriteria<FeedCategory> criteria = createCriteria();
		criteria.andEquals(FeedCategory_.user.getName(), user);
		criteria.andEquals(FeedCategory_.name.getName(), name);
		if (parent == null) {
			criteria.andIsNull(FeedCategory_.parent.getName());
		} else {
			criteria.andEquals(FeedCategory_.parent.getName(), parent);
		}

		FeedCategory category = null;
		try {
			category = criteria.getSingleResult();
		} catch (NoResultException e) {
			category = null;
		}
		return category;
	}

	public List<FeedCategory> findByParent(User user, FeedCategory parent) {
		EasyCriteria<FeedCategory> criteria = createCriteria();
		criteria.andEquals(FeedCategory_.user.getName(), user);
		criteria.andEquals(FeedCategory_.parent.getName(), parent);
		return criteria.getResultList();
	}

	public List<FeedCategory> findAllChildrenCategories(User user,
			FeedCategory parent) {
		List<FeedCategory> list = Lists.newArrayList();
		List<FeedCategory> all = findAll(user);
		for (FeedCategory cat : all) {
			if (isChild(cat, parent)) {
				list.add(cat);
			}
		}
		return list;
	}

	public boolean isChild(FeedCategory child, FeedCategory parent) {
		if (parent == null) {
			return true;
		}
		boolean isChild = false;
		while (child != null) {
			if (ObjectUtils.equals(child.getId(), parent.getId())) {
				isChild = true;
				break;
			}
			child = child.getParent();
		}
		return isChild;
	}

}

@@ -1873,32 +1873,126 @@
 Name(), user);%0D%0A
+%09%09if (parent == null) %7B%0D%0A%09%09%09criteria.andIsNull(FeedCategory_.parent.getName());%0D%0A%09%09%7D else %7B%0D%0A%09
 %09%09criteria.andEq
@@ -2034,24 +2034,31 @@
 , parent);%0D%0A
+%09%09%7D%0D%0A%0D%0A
 %09%09return cri

fix query for null parent