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 @Override 061 public Object[] getElements(Object input) { 062 if (input instanceof Directory) { 063 return getChildren(null); 064 } else { // may be a document 065 return getChildren(input); 066 } 067 } 068 069 @Override 070 public Object[] getChildren(Object obj) { 071 if (obj == null || obj instanceof DocumentModel) { 072 DocumentModel parent = (DocumentModel) obj; 073 Map<String, Serializable> args = new HashMap<>(); 074 createQuery(parent, args); 075 DocumentModelList list = session.query(args); 076 return list.toArray(new DocumentModel[list.size()]); 077 } 078 return null; 079 } 080 081 @Override 082 public boolean isContainer(Object obj) { 083 if (obj instanceof DocumentModel) { 084 return ((DocumentModel) obj).isFolder(); 085 } 086 return false; 087 } 088 089 @Override 090 public String getName(Object obj) { 091 if (obj instanceof DocumentModel) { 092 return ((DocumentModel) obj).getId(); 093 } 094 return null; 095 } 096 097 @Override 098 public String getLabel(Object obj) { 099 if (obj instanceof DocumentModel) { 100 return ((DocumentModel) obj).getName(); 101 } 102 return null; 103 } 104 105 @Override 106 public String[] getFacets(Object object) { 107 return null; 108 } 109 110}