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