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