001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Olivier Grisel <ogrisel@nuxeo.com>
016 *     Antoine Taillefer <ataillefer@nuxeo.com>
017 */
018package org.nuxeo.drive.operations;
019
020import java.util.Map;
021import java.util.Set;
022
023import javax.servlet.http.HttpServletRequest;
024
025import org.apache.commons.lang.StringUtils;
026import org.nuxeo.drive.service.NuxeoDriveManager;
027import org.nuxeo.drive.service.SynchronizationRoots;
028import org.nuxeo.ecm.automation.OperationContext;
029import org.nuxeo.ecm.automation.core.Constants;
030import org.nuxeo.ecm.automation.core.annotations.Context;
031import org.nuxeo.ecm.automation.core.annotations.Operation;
032import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModelList;
035import org.nuxeo.ecm.core.api.DocumentRef;
036import org.nuxeo.ecm.core.api.IdRef;
037import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Fetch the list of synchronization roots for the currently authenticated user.
042 */
043@Operation(id = NuxeoDriveGetRootsOperation.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Get Roots")
044public class NuxeoDriveGetRootsOperation {
045
046    public static final String ID = "NuxeoDrive.GetRoots";
047
048    @Context
049    protected OperationContext ctx;
050
051    @Context
052    protected CoreSession session;
053
054    @OperationMethod
055    public DocumentModelList run() {
056
057        // By default get synchronization roots from all repositories, except if
058        // a specific repository name is passed as a request header
059        boolean allRepositories = true;
060        HttpServletRequest request = (HttpServletRequest) ctx.get("request");
061        if (request != null) {
062            String respositoryName = request.getHeader("X-NXRepository");
063            if (!StringUtils.isEmpty(respositoryName)) {
064                allRepositories = false;
065            }
066        }
067        NuxeoDriveManager driveManager = Framework.getLocalService(NuxeoDriveManager.class);
068        Map<String, SynchronizationRoots> roots = driveManager.getSynchronizationRoots(ctx.getPrincipal());
069        DocumentModelList rootDocumentModels = new DocumentModelListImpl();
070        for (Map.Entry<String, SynchronizationRoots> rootsEntry : roots.entrySet()) {
071            if (session.getRepositoryName().equals(rootsEntry.getKey())) {
072                Set<IdRef> references = rootsEntry.getValue().getRefs();
073                rootDocumentModels.addAll(session.getDocuments(references.toArray(new DocumentRef[references.size()])));
074            } else {
075                if (allRepositories) {
076                    // XXX: do we really need to implement this now?
077                    throw new RuntimeException("Multi repo roots not yet implemented");
078                }
079            }
080        }
081        return rootDocumentModels;
082    }
083
084}