/*
 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id$
 */

package org.nuxeo.webengine.sites;

import java.util.List;
import java.util.Vector;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.webengine.ui.tree.document.DocumentContentProvider;
import org.nuxeo.webengine.sites.utils.SiteConstants;
import org.nuxeo.webengine.sites.utils.SiteUtils;

/**
 * Implementation of provider for the tree.
 */
public class SiteContentProvider extends DocumentContentProvider {

    private static final long serialVersionUID = 1L;

    private static final Log log = LogFactory.getLog(SiteContentProvider.class);

    public SiteContentProvider(CoreSession session) {
        super(session);
    }

    @Override
    public Object[] getChildren(Object obj) {
        Object[] objects = super.getChildren(obj);
        List<Object> v = new Vector<Object>();
        for (Object o : objects) {
            DocumentModel doc = (DocumentModel) o;
            // filter pages
            // WEB-214
            try {
                if (SiteUtils.getBoolean(doc, SiteConstants.WEBPAGE_PUSHTOMENU, false)
                        && !SiteConstants.DELETED.equals(doc.getCurrentLifeCycleState())) {
                    v.add(doc);
                }
            } catch (ClientException e) {
                log.error(e);
            }
        }
        return v.toArray();
    }

    @Override
    public String getLabel(Object obj) {
        String label = super.getLabel(obj);
        return label == null ? null : StringEscapeUtils.escapeXml(label);
    }

}

@@ -735,37 +735,19 @@
 til.
-List;%0Aimport java.util.Vector
+Collections
 ;%0A%0Ai
@@ -1015,16 +1015,65 @@
 tModel;%0A
+import org.nuxeo.ecm.core.api.DocumentModelList;%0A
 import o
@@ -1194,58 +1194,8 @@
 nts;
-%0Aimport org.nuxeo.webengine.sites.utils.SiteUtils;
 %0A%0A/*
@@ -1605,50 +1605,52 @@
     
-Object%5B%5D objects = super.getChildren(
+DocumentModel parent = (DocumentModel) 
 obj
-)
 ;
+%0A
 %0A   
@@ -1658,380 +1658,252 @@
     
-List%3CObject%3E v = new Vector%3CObject%3E();%0A        for (Object o : objects) %7B%0A            DocumentModel doc = (DocumentModel) o;%0A            // filter pages%0A            // WEB-214%0A            try %7B%0A                if (SiteUtils.getBoolean(doc, SiteConstants.WEBPAGE_PUSHTOMENU, false)%0A                        && !SiteConstants.DELETED.equals(doc.getCurrentLifeCycleState())) %7B
+String query = %22SELECT * FROM Document WHERE ecm:parentId = '%22%0A                + parent.getId()%0A                + %22' AND %22%0A                + SiteConstants.WEBPAGE_PUSHTOMENU + %22 = 1 AND ecm:currentLifeCycleState != 'deleted' ORDER BY dc:title%22;
 %0A   
@@ -1899,33 +1899,38 @@
 title%22;%0A        
- 
+try %7B%0A
            v.add
@@ -1928,42 +1928,91 @@
     
-v.add(doc);%0A                %7D%0A    
+ DocumentModelList docs = session.query(query);%0A            return docs.toArray();%0A
     
@@ -2057,20 +2057,16 @@
         
-    
 log.erro
@@ -2075,22 +2075,8 @@
 e);%0A
-            %7D%0A
     
@@ -2096,17 +2096,38 @@
  return 
-v
+Collections.EMPTY_LIST
 .toArray

sort webpages by title