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.directory;
021
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentModelList;
030import org.nuxeo.ecm.directory.Directory;
031import org.nuxeo.ecm.directory.Session;
032import org.nuxeo.ecm.webengine.ui.tree.ContentProvider;
033
034/**
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class DirectoryContentProvider implements ContentProvider {
038
039    private static final Log log = LogFactory.getLog(DirectoryContentProvider.class);
040
041    private static final long serialVersionUID = 1L;
042
043    protected final Session session;
044
045    public DirectoryContentProvider(Session session) {
046        this.session = session;
047    }
048
049    public Session getSession() {
050        return session;
051    }
052
053    protected void createQuery(DocumentModel parent, Map<String, Serializable> query) {
054        String id = parent == null ? null : parent.getId();
055        query.put("parent", id);
056    }
057
058    public Object[] getElements(Object input) {
059        if (input instanceof Directory) {
060            return getChildren(null);
061        } else { // may be a document
062            return getChildren(input);
063        }
064    }
065
066    public Object[] getChildren(Object obj) {
067        if (obj == null || obj instanceof DocumentModel) {
068            DocumentModel parent = (DocumentModel) obj;
069            Map<String, Serializable> args = new HashMap<String, Serializable>();
070            createQuery(parent, args);
071            DocumentModelList list = session.query(args);
072            return list.toArray(new DocumentModel[list.size()]);
073        }
074        return null;
075    }
076
077    public boolean isContainer(Object obj) {
078        if (obj instanceof DocumentModel) {
079            return ((DocumentModel) obj).isFolder();
080        }
081        return false;
082    }
083
084    public String getName(Object obj) {
085        if (obj instanceof DocumentModel) {
086            return ((DocumentModel) obj).getId();
087        }
088        return null;
089    }
090
091    public String getLabel(Object obj) {
092        if (obj instanceof DocumentModel) {
093            return ((DocumentModel) obj).getName();
094        }
095        return null;
096    }
097
098    public String[] getFacets(Object object) {
099        return null;
100    }
101
102}