This should help (not tested)

public class HibernateCategoryDao implements CategoryDao {
        // this will be called by ValueEncoder.toValue(categoryId)
        public CategoryNode findById(Long categoryId) {
                return findByCriterion(Restrictions.eq("categoryId",
categoryId)).iterator().next();
        }

        // this will be called by TreeModelAdapter.getChildren(CategoryNode
node)
        public List<CategoryNode> getChildren(CategoryNode node) {
                return findByCriterion(Restrictions.eq("parentCategoryId",
node.getCategory()));
        }

        // this will be called by your page
        public List<CategoryNode> findRoots() {
                return
findByCriterion(Restrictions.isNull("parentCategoryId"));
        }

        @SuppressWarnings("unchecked")
        protected List<CategoryNode> findByCriterion(Criterion criterion) {
                Session session = getSession();
                List<Category> cats =
session.createCriteria(Category.class).add(criterion).list();
                Map<Long, CategoryNode> childNodes = new
LinkedHashMap<Long, CategoryNode>();
                for (Category cat : cats) {
                        CategoryNode childNode = new CategoryNode();
                        childNode.setCategory(cat);
                        childNodes.put(cat.getCategoryId(), childNode);
                }
                StringBuilder questions = new StringBuilder();
                for (int i = 0; i < childNodes.size(); ++ i) {
                        if (i != 0) {
                                questions.append(", ");
                        }
                        questions.append("?");
                }
                Query query = session.createSQLQuery(
                        "select c1.categoryId, count(c2.*) " +
                        "from Category c1 " +
                        "left join Category c2 on c2.parentCategoryId =
c1.categoryId " +
                        "where c1.categoryId in (" + questions + ") " +
                        "group by c1.categoryId"
                );
                int i = 0;
                for (Iterator<CategoryNode> it =
childNodes.values().iterator(); i < childNodes.size(); ++ i) {
                        query.setLong(i + 1,
it.next().getCategory().getCategoryId());
                }

                for (Iterator<Object[]> it = query.iterate(); it.hasNext();
) {
                        Object[] result = it.next();
                        Long childId = (Long) result[0];
                        Integer grandChildCount = (Integer) result[1];
                        CategoryNode childNode = childNodes.get(childId);
                        childNode.setHasChildren(grandChildCount != 0);
                        childNode.setLeaf(grandChildCount == 0);
                }

                return new ArrayList<CategoryNode>(childNodes.values());
        }
}

On Wednesday, 15 February 2012, George Christman <gchrist...@cardaddy.com>
wrote:
> Hi Lance, I think I made some excellent progress tonight, however I seem
to
> be stuck at the Value Encoder and DAO. I apologize if these questions seem
> simple. I specialize in UI, so this component tends to present a big
> challenge for me.
>
> I've implemented everything you outlined without issue.
>
> Below is my page class containing the value encoder. I'm having a
difficult
> time getting the root nodes as well as the toValue node from the
> CategoryNode. Do you think you could take a quick look at my code snippet
> and offer a suggestion?
>
>    @Inject
>    private Session session;
>
>    private TreeModel<CategoryNode> selectModel;
>
>    @InjectComponent
>    private Tree tree;
>
>    public TreeModel<CategoryNode> getTreeModel() {
>
>        if (selectModel == null) {
>
>            ValueEncoder<CategoryNode> encoder = new
> ValueEncoder<CategoryNode>() {
>
>                @Override
>                public String toClient(CategoryNode node) {
>                    return node.getCategory().getId().toString();
>                }
>
>                @Override
>                public CategoryNode toValue(String nodeId) {
>                    return;
>                }
>            };
> //Haven't figured out how to obtain rootNodes.
>            selectModel = new DefaultTreeModel<CategoryNode>(encoder, new
> CategoryTreeModelAdapter(session), rootNodes);
>        }
>
>        return selectModel;
>    }
>
> Cheers,
> George
>
> --
> View this message in context:
http://tapestry.1045711.n5.nabble.com/Tapestry-TreeGrid-tp5462126p5484903.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> For additional commands, e-mail: users-h...@tapestry.apache.org
>
>

Reply via email to