001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (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 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.ui.tree.document;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentModelList;
027import org.nuxeo.ecm.core.api.repository.Repository;
028import org.nuxeo.ecm.webengine.ui.tree.ContentProvider;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033// FIXME: properly handle exceptions
034public class DocumentContentProvider implements ContentProvider {
035
036    private static final Log log = LogFactory.getLog(DocumentContentProvider.class);
037
038    private static final long serialVersionUID = 1L;
039
040    protected CoreSession session;
041
042    public DocumentContentProvider(CoreSession session) {
043        this.session = session;
044    }
045
046    public void setSession(CoreSession session) {
047        this.session = session;
048    }
049
050    public CoreSession getSession() {
051        return session;
052    }
053
054    public Object[] getElements(Object input) {
055        if (input instanceof Repository) {
056            return new DocumentModel[] { session.getRootDocument() };
057        } else { // may be a document
058            return getChildren(input);
059        }
060    }
061
062    public Object[] getChildren(Object obj) {
063        if (obj instanceof DocumentModel) {
064            DocumentModelList list = session.getChildren(((DocumentModel) obj).getRef());
065            return list.toArray(new DocumentModel[list.size()]);
066        }
067        return null;
068    }
069
070    public boolean isContainer(Object obj) {
071        if (obj instanceof DocumentModel) {
072            return ((DocumentModel) obj).isFolder();
073        }
074        return false;
075    }
076
077    public String getLabel(Object obj) {
078        if (obj instanceof DocumentModel) {
079            return ((DocumentModel) obj).getTitle();
080        }
081        return null;
082    }
083
084    public String[] getFacets(Object object) {
085        return null;
086    }
087
088    public String getName(Object obj) {
089        if (obj instanceof DocumentModel) {
090            return ((DocumentModel) obj).getName();
091        }
092        return null;
093    }
094
095}