001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (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 *     Thierry Delprat
016 */
017package org.nuxeo.apidoc.tree;
018
019import javax.servlet.http.HttpSession;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.apidoc.adapters.BaseNuxeoArtifactDocAdapter;
024import org.nuxeo.apidoc.snapshot.DistributionSnapshot;
025import org.nuxeo.apidoc.snapshot.SnapshotManager;
026import org.nuxeo.common.utils.Path;
027import org.nuxeo.ecm.core.api.NuxeoPrincipal;
028import org.nuxeo.ecm.webengine.model.WebContext;
029import org.nuxeo.ecm.webengine.ui.tree.TreeItem;
030import org.nuxeo.runtime.api.Framework;
031
032public class TreeHelper {
033
034    protected static final Log log = LogFactory.getLog(TreeHelper.class);
035
036    public static NuxeoArtifactTree getOrBuildAnonymousTree(WebContext ctx) {
037        NuxeoArtifactTree tree = (NuxeoArtifactTree) ctx.getRequest().getAttribute("tree--" + ctx.getProperty("distId"));
038        if (tree == null) {
039            tree = buildTree(ctx);
040            ctx.getRequest().setAttribute("tree--" + ctx.getProperty("distId"), tree);
041        }
042        return tree;
043    }
044
045    public static NuxeoArtifactTree getOrBuildTree(WebContext ctx) {
046
047        HttpSession httpSession = ctx.getRequest().getSession(true);
048        NuxeoArtifactTree tree = (NuxeoArtifactTree) httpSession.getAttribute("tree--" + ctx.getProperty("distId"));
049        if (tree == null) {
050            tree = buildTree(ctx);
051            httpSession.setAttribute("tree--" + ctx.getProperty("distId"), tree);
052        } else {
053            tree.setDs(getRequestDS(ctx));
054        }
055        return tree;
056    }
057
058    public static DistributionSnapshot getRequestDS(WebContext ctx) {
059
060        String id = "tree--ds--" + ctx.getProperty("distId");
061
062        DistributionSnapshot ds = (DistributionSnapshot) ctx.getRequest().getAttribute(id);
063        if (ds == null) {
064            SnapshotManager sm = Framework.getLocalService(SnapshotManager.class);
065            ds = sm.getSnapshot((String) ctx.getProperty("distId"), ctx.getCoreSession());
066            ctx.getRequest().setAttribute(id, ds);
067        }
068
069        return ds;
070    }
071
072    public static NuxeoArtifactTree buildTree(WebContext ctx) {
073        DistributionSnapshot ds = getRequestDS(ctx);
074        return new NuxeoArtifactTree(ctx, ds);
075    }
076
077    public static String updateTree(WebContext ctx, String source) {
078
079        BaseNuxeoArtifactDocAdapter.setLocalCoreSession(ctx.getCoreSession());
080
081        try {
082            boolean anonymous = ((NuxeoPrincipal) ctx.getPrincipal()).isAnonymous();
083
084            NuxeoArtifactTree tree = null;
085            String lastPath = null;
086            HttpSession httpSession = null;
087            if (anonymous) {
088                tree = getOrBuildAnonymousTree(ctx);
089            } else {
090                tree = getOrBuildTree(ctx);
091                httpSession = ctx.getRequest().getSession(true);
092                lastPath = (String) httpSession.getAttribute("tree-last-path");
093            }
094
095            if ("source".equalsIgnoreCase(source) || source == null) {
096                tree.enter(ctx, "/");
097                return tree.getTreeAsJSONArray(ctx);
098            } else if (source.startsWith("source:")) {
099                String anonymousPath = source.replace("source:", "");
100                tree.enter(ctx, anonymousPath);
101                return tree.getTreeAsJSONArray(ctx);
102            } else {
103                if (lastPath != null) {
104                    TreeItem lastNode = tree.getTree().find(lastPath);
105                    if (lastNode != null) {
106                        lastNode.collapse();
107                    } else {
108                        log.warn("Unable to find previous selected tree node at path " + lastPath);
109                    }
110
111                    String lastBranch = new Path(lastPath).segment(0);
112                    String currentBranch = new Path(source).segment(0);
113                    if (!currentBranch.equals(lastBranch)) {
114                        TreeItem lastBranchItem = tree.getTree().find(lastBranch);
115                        if (lastBranchItem != null) {
116                            lastBranchItem.collapse();
117                        } else {
118                            log.warn("Unable to find last branch " + lastBranch);
119                        }
120                    }
121                }
122
123                if (httpSession != null) {
124                    httpSession.setAttribute("tree-last-path", source);
125                }
126                ctx.getRequest().setAttribute("tree-last-path", source);
127
128                return tree.enter(ctx, source);
129            }
130        } finally {
131            BaseNuxeoArtifactDocAdapter.releaseLocalCoreSession();
132        }
133    }
134
135}