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 net.sf.json.JSONArray;
020import net.sf.json.JSONObject;
021
022import org.nuxeo.apidoc.api.BundleGroup;
023import org.nuxeo.apidoc.api.BundleInfo;
024import org.nuxeo.apidoc.api.ComponentInfo;
025import org.nuxeo.apidoc.api.ExtensionInfo;
026import org.nuxeo.apidoc.api.ExtensionPointInfo;
027import org.nuxeo.apidoc.api.NuxeoArtifact;
028import org.nuxeo.apidoc.api.ServiceInfo;
029import org.nuxeo.apidoc.snapshot.DistributionSnapshot;
030import org.nuxeo.ecm.webengine.model.WebContext;
031import org.nuxeo.ecm.webengine.ui.tree.JSonTreeSerializer;
032import org.nuxeo.ecm.webengine.ui.tree.TreeItem;
033
034public class NuxeoArtifactSerializer extends JSonTreeSerializer {
035
036    protected final WebContext ctx;
037
038    protected final DistributionSnapshot ds;
039
040    public NuxeoArtifactSerializer(WebContext ctx, DistributionSnapshot ds) {
041        this.ctx = ctx;
042        this.ds = ds;
043    }
044
045    protected static boolean useEmbeddedMode(WebContext ctx) {
046        return ((Boolean) ctx.getProperty("embeddedMode", Boolean.FALSE)).booleanValue();
047    }
048
049    @Override
050    public String getUrl(TreeItem item) {
051
052        NuxeoArtifact obj = (NuxeoArtifact) item.getObject();
053
054        String distId = ds.getKey().replace(" ", "%20");
055        if (ds.isLive()) {
056            if (useEmbeddedMode(ctx)) {
057                distId = "adm";
058            } else {
059                distId = "current";
060            }
061        }
062        String url = ctx.getRoot().getURL() + "/" + distId + "/";
063
064        if (obj.getArtifactType().equals(DistributionSnapshot.TYPE_NAME)) {
065            return url;
066        } else if (obj.getArtifactType().equals(BundleInfo.TYPE_NAME)) {
067            url += "viewBundle/";
068        } else if (obj.getArtifactType().equals(BundleGroup.TYPE_NAME)) {
069            url += "viewBundleGroup/";
070        } else if (obj.getArtifactType().equals(ComponentInfo.TYPE_NAME)) {
071            url += "viewComponent/";
072        } else if (obj.getArtifactType().equals(ExtensionInfo.TYPE_NAME)) {
073            url += "viewContribution/";
074        } else if (obj.getArtifactType().equals(ExtensionPointInfo.TYPE_NAME)) {
075            url += "viewExtensionPoint/";
076        } else if (obj.getArtifactType().equals(ServiceInfo.TYPE_NAME)) {
077            url += "viewService/";
078        } else {
079            url = null;
080        }
081
082        if (url != null) {
083            url += obj.getId();
084        } else {
085            if (obj instanceof VirtualNode) {
086                VirtualNode vn = (VirtualNode) obj;
087                url = ctx.getRoot().getURL() + "/" + distId + "/viewComponent/" + vn.getComponentId() + "#"
088                        + vn.getAnchor();
089            } else {
090                url = "todo";
091            }
092        }
093        return url;
094    }
095
096    @Override
097    protected JSONObject item2JSON(TreeItem item, JSONArray children) {
098        JSONObject json = new JSONObject();
099        String[] classes = { "folder", "Folder" };
100        json.element("text", item.getLabel()).element("id", item.getPath().toString()).element("href", getUrl(item)).element(
101                "classes", classes).element("class", classes);
102        json.element("expanded", item.isExpanded());
103        if (item.isContainer()) {
104            if (item.isContainer()) {
105                if (item.hasChildren()) {
106                    json.element("children", children);
107                } else {
108                    json.element("hasChildren", true);
109                }
110            } else {
111                json.element("hasChildren", false);
112            }
113        }
114        return json;
115    }
116}