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