001/*
002 * (C) Copyright 2012 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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.adapter.impl;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.Iterator;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027
028import org.apache.logging.log4j.LogManager;
029import org.apache.logging.log4j.Logger;
030import org.nuxeo.drive.adapter.FileSystemItem;
031import org.nuxeo.drive.adapter.FolderItem;
032import org.nuxeo.drive.service.NuxeoDriveManager;
033import org.nuxeo.drive.service.SynchronizationRoots;
034import org.nuxeo.ecm.core.api.CoreInstance;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModel;
037import org.nuxeo.ecm.core.api.IdRef;
038import org.nuxeo.ecm.core.api.NuxeoPrincipal;
039import org.nuxeo.ecm.core.api.security.SecurityConstants;
040import org.nuxeo.runtime.api.Framework;
041
042/**
043 * Default implementation of the top level {@link FolderItem}.
044 *
045 * @author Antoine Taillefer
046 */
047public class DefaultTopLevelFolderItem extends AbstractVirtualFolderItem {
048
049    private static final Logger log = LogManager.getLogger(DefaultTopLevelFolderItem.class);
050
051    public DefaultTopLevelFolderItem(String factoryName, NuxeoPrincipal principal, String folderName) {
052        super(factoryName, principal, null, null, folderName);
053    }
054
055    protected DefaultTopLevelFolderItem() {
056        // Needed for JSON deserialization
057    }
058
059    /*--------------------- FolderItem -----------------*/
060    @Override
061    public List<FileSystemItem> getChildren() {
062
063        List<FileSystemItem> children = new ArrayList<>();
064        Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getService(NuxeoDriveManager.class)
065                                                                     .getSynchronizationRoots(principal);
066        for (Map.Entry<String, SynchronizationRoots> entry : syncRootsByRepo.entrySet()) {
067            CoreSession session = CoreInstance.getCoreSession(entry.getKey(), principal);
068            Set<IdRef> syncRootRefs = entry.getValue().getRefs();
069            Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator();
070            while (syncRootRefsIt.hasNext()) {
071                IdRef idRef = syncRootRefsIt.next();
072                // TODO: ensure sync roots cache is up-to-date if ACL
073                // change, for now need to check permission
074                // See https://jira.nuxeo.com/browse/NXP-11146
075                if (!session.hasPermission(idRef, SecurityConstants.READ)) {
076                    log.debug(
077                            "User {} has no READ access on synchronization root {}, not including it in children.",
078                            session::getPrincipal, () -> idRef);
079                    continue;
080                }
081                DocumentModel doc = session.getDocument(idRef);
082                // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
083                FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(doc, this, false, false,
084                        false);
085                if (child == null) {
086                    log.debug(
087                            "Synchronization root {} cannot be adapted as a FileSystemItem, not including it in children.",
088                            idRef);
089                    continue;
090                }
091                log.debug("Including synchronization root {} in children.", idRef);
092                children.add(child);
093            }
094        }
095        Collections.sort(children);
096        return children;
097    }
098
099}