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