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 */
019package org.nuxeo.ecm.webengine.ui;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.Path;
023import javax.ws.rs.Produces;
024import javax.ws.rs.QueryParam;
025import javax.ws.rs.core.Response;
026
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.PathRef;
029import org.nuxeo.ecm.webengine.model.WebObject;
030import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
031import org.nuxeo.ecm.webengine.session.UserSession;
032import org.nuxeo.ecm.webengine.ui.tree.document.DocumentTree;
033
034/**
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> When calling GET ${This.path}?root=source - the content
036 *         of the root is required When calling GET ${This.path}?root=/default-domain/... - the content of the given
037 *         path folder is required
038 */
039@Path("/ui")
040@WebObject(type = "ui")
041@Produces("text/html;charset=UTF-8")
042public class Test extends ModuleRoot {
043
044    @GET
045    public Object getView() {
046        return getTemplate("tree.ftl");
047    }
048
049    @GET
050    @Path("tree")
051    public Response getContent(@QueryParam("root") String root) {
052        // TODO here you may want to put tree in httpsession to have state
053        // preserved after reload
054        UserSession us = UserSession.getCurrentSession(ctx.getRequest());
055        DocumentTree tree = (DocumentTree) us.get("TREE");
056        if (tree == null) {
057            DocumentModel rootDoc = ctx.getCoreSession().getDocument(new PathRef("/default-domain"));
058            tree = new DocumentTree(ctx, rootDoc);
059            us.put("TREE", tree);
060        }
061        String result = "";
062        if (root == null || "source".equals(root)) { // ask for the the root content (if the tree is stateful this will
063                                                     // return the tree in the current state)
064            tree.enter(ctx, "/"); // expand root by default - comment this to avoid expanding first level
065            result = tree.getTreeAsJSONArray(ctx);
066        } else { // ask for the content of the tree which path is given by root parameter
067            result = tree.enter(ctx, root);
068        }
069        return Response.ok().entity(result).build();
070    }
071
072}