/*
 * (C) Copyright 2012 Nuxeo SA (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:
 *     Antoine Taillefer <ataillefer@nuxeo.com>
 */
package org.nuxeo.drive.hierarchy.permission.adapter;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.nuxeo.drive.adapter.FileSystemItem;
import org.nuxeo.drive.adapter.FolderItem;
import org.nuxeo.drive.adapter.impl.AbstractVirtualFolderItem;
import org.nuxeo.drive.service.NuxeoDriveManager;
import org.nuxeo.drive.service.SynchronizationRoots;
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.core.api.IdRef;
import org.nuxeo.runtime.api.Framework;

/**
 * Permission based implementation of the parent {@link FolderItem} of the
 * user's shared synchronization roots.
 *
 * @author Antoine Taillefer
 */
public class SharedSyncRootParentFolderItem extends AbstractVirtualFolderItem {

    private static final long serialVersionUID = 1L;

    public SharedSyncRootParentFolderItem(String factoryName,
            Principal principal, String parentId, String folderName)
            throws ClientException {
        super(factoryName, principal, parentId, folderName);
    }

    protected SharedSyncRootParentFolderItem() {
        // Needed for JSON deserialization
    }

    @Override
    public List<FileSystemItem> getChildren() throws ClientException {

        List<FileSystemItem> children = new ArrayList<FileSystemItem>();
        Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getLocalService(
                NuxeoDriveManager.class).getSynchronizationRoots(principal);
        for (String repositoryName : syncRootsByRepo.keySet()) {
            CoreSession session = getSession(repositoryName);
            Set<IdRef> syncRootRefs = syncRootsByRepo.get(repositoryName).refs;
            Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator();
            while (syncRootRefsIt.hasNext()) {
                IdRef idRef = syncRootRefsIt.next();
                DocumentModel doc = session.getDocument(idRef);
                // Filter by creator
                // TODO: allow filtering by dc:creator in
                // NuxeoDriveManager#getSynchronizationRoots(Principal
                // principal)
                if (!session.getPrincipal().getName().equals(
                        doc.getPropertyValue("dc:creator"))) {
                    children.add(getFileSystemItemAdapterService().getFileSystemItem(
                            doc, getId()));
                }
            }
        }
        Collections.sort(children);
        return children;
    }

}

@@ -902,16 +902,101 @@
 l.Set;%0A%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import o
@@ -1030,24 +1030,24 @@
 SystemItem;%0A
-
 import org.n
@@ -1378,16 +1378,73 @@
 tModel;%0A
+import org.nuxeo.ecm.core.api.DocumentSecurityException;%0A
 import o
@@ -1803,16 +1803,109 @@
  = 1L;%0A%0A
+    private static final Log log = LogFactory.getLog(SharedSyncRootParentFolderItem.class);%0A%0A
     publ
@@ -2927,24 +2927,50 @@
 sIt.next();%0A
+                try %7B%0A    
             
@@ -3037,16 +3037,20 @@
         
+   
+ 
 // Filte
@@ -3062,16 +3062,20 @@
 creator%0A
+    
         
@@ -3136,24 +3136,28 @@
             
+ 
+   
 // NuxeoDriv
@@ -3215,16 +3215,20 @@
         
+    
 // princ
@@ -3233,16 +3233,20 @@
 ncipal)%0A
+    
         
@@ -3323,16 +3323,20 @@
         
+    
 doc.getP
@@ -3362,24 +3362,28 @@
 eator%22))) %7B%0A
+    
             
@@ -3484,16 +3484,20 @@
         
+    
 doc, get
@@ -3504,16 +3504,326 @@
 Id()));%0A
+                    %7D%0A                %7D catch (DocumentSecurityException e) %7B%0A                    log.debug(String.format(%0A                            %22User %25s has no READ access on synchronization root %25s, not including it in children.%22,%0A                            session.getPrincipal().getName(), idRef));%0A
         

Fix test