001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.ecm.webengine.ui;
018
019import javax.ws.rs.GET;
020import javax.ws.rs.Path;
021import javax.ws.rs.Produces;
022import javax.ws.rs.QueryParam;
023import javax.ws.rs.core.Response;
024
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.PathRef;
027import org.nuxeo.ecm.webengine.model.WebObject;
028import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
029import org.nuxeo.ecm.webengine.session.UserSession;
030import org.nuxeo.ecm.webengine.ui.tree.document.DocumentTree;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> When calling GET ${This.path}?root=source - the content
034 *         of the root is required When calling GET ${This.path}?root=/default-domain/... - the content of the given
035 *         path folder is required
036 */
037@Path("/ui")
038@WebObject(type = "ui")
039@Produces("text/html;charset=UTF-8")
040public class Test extends ModuleRoot {
041
042    @GET
043    public Object getView() {
044        return getTemplate("tree.ftl");
045    }
046
047    @GET
048    @Path("tree")
049    public Response getContent(@QueryParam("root") String root) {
050        // TODO here you may want to put tree in httpsession to have state
051        // preserved after reload
052        UserSession us = UserSession.getCurrentSession(ctx.getRequest());
053        DocumentTree tree = (DocumentTree) us.get("TREE");
054        if (tree == null) {
055            DocumentModel rootDoc = ctx.getCoreSession().getDocument(new PathRef("/default-domain"));
056            tree = new DocumentTree(ctx, rootDoc);
057            us.put("TREE", tree);
058        }
059        String result = "";
060        if (root == null || "source".equals(root)) { // ask for the the root content (if the tree is stateful this will
061                                                     // return the tree in the current state)
062            tree.enter(ctx, "/"); // expand root by default - comment this to avoid expanding first level
063            result = tree.getTreeAsJSONArray(ctx);
064        } else { // ask for the content of the tree which path is given by root parameter
065            result = tree.enter(ctx, root);
066        }
067        return Response.ok().entity(result).build();
068    }
069
070}