001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.ui.json;
023
024import java.util.Collection;
025
026import net.sf.json.JSONArray;
027import net.sf.json.JSONObject;
028
029import org.nuxeo.common.utils.StringUtils;
030
031/**
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public abstract class JQueryTreeBuilder<T> {
035
036    public static final String CHILDREN = "children";
037
038    public JSONArray buildTree(String rootName, String path) {
039        return buildTree(rootName, path);
040    }
041
042    public JSONArray buildTree(T root, String path) {
043        if (path == null || path.length() == 0 || "/".equals(path)) {
044            return buildChildren(root);
045        }
046        String[] ar = StringUtils.split(path, '/', false);
047        if (ar.length > 1) {
048            String name = getName(root);
049            if (name.equals(ar[0])) {
050                return buildChildren(root, ar, 1);
051            }
052        }
053
054        return buildChildren(root);
055    }
056
057    public JSONArray buildChildren(T parent) {
058        JSONArray json = new JSONArray();
059        Collection<T> children = getChildren(parent);
060        if (children != null) {
061            for (T obj : children) {
062                JSONObject map = toJson(obj);
063                json.add(map);
064            }
065        }
066        return json;
067    }
068
069    public JSONArray buildChildren(T parent, String[] path, int off) {
070        JSONArray json = new JSONArray();
071        String expandName = path[off];
072        Collection<T> children = getChildren(parent);
073        if (children != null) {
074            for (T obj : children) {
075                JSONObject map = toJson(obj);
076                String childName = getName(obj);
077                if (expandName.equals(childName)) {
078                    JSONArray jsonChildren = null;
079                    if (off < path.length - 1) {
080                        jsonChildren = buildChildren(obj, path, off + 1);
081                    } else {
082                        jsonChildren = buildChildren(obj);
083                    }
084                    map.element(CHILDREN, jsonChildren);
085                }
086                json.add(map);
087            }
088        }
089        return json;
090    }
091
092    protected abstract T getObject(String name);
093
094    protected abstract String getName(T obj);
095
096    protected abstract Collection<T> getChildren(T obj);
097
098    protected abstract JSONObject toJson(T obj);
099
100}