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 *     Olivier Grisel <ogrisel@nuxeo.com>
018 *     Antoine Taillefer <ataillefer@nuxeo.com>
019 */
020package org.nuxeo.drive.operations;
021
022import java.util.Map;
023import java.util.Set;
024
025import javax.servlet.http.HttpServletRequest;
026
027import org.apache.commons.lang.StringUtils;
028import org.nuxeo.drive.service.NuxeoDriveManager;
029import org.nuxeo.drive.service.SynchronizationRoots;
030import org.nuxeo.ecm.automation.OperationContext;
031import org.nuxeo.ecm.automation.core.Constants;
032import org.nuxeo.ecm.automation.core.annotations.Context;
033import org.nuxeo.ecm.automation.core.annotations.Operation;
034import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModelList;
037import org.nuxeo.ecm.core.api.DocumentRef;
038import org.nuxeo.ecm.core.api.IdRef;
039import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
040import org.nuxeo.runtime.api.Framework;
041
042/**
043 * Fetch the list of synchronization roots for the currently authenticated user.
044 */
045@Operation(id = NuxeoDriveGetRootsOperation.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Get Roots")
046public class NuxeoDriveGetRootsOperation {
047
048    public static final String ID = "NuxeoDrive.GetRoots";
049
050    @Context
051    protected OperationContext ctx;
052
053    @Context
054    protected CoreSession session;
055
056    @OperationMethod
057    public DocumentModelList run() {
058
059        // By default get synchronization roots from all repositories, except if
060        // a specific repository name is passed as a request header
061        boolean allRepositories = true;
062        HttpServletRequest request = (HttpServletRequest) ctx.get("request");
063        if (request != null) {
064            String respositoryName = request.getHeader("X-NXRepository");
065            if (!StringUtils.isEmpty(respositoryName)) {
066                allRepositories = false;
067            }
068        }
069        NuxeoDriveManager driveManager = Framework.getLocalService(NuxeoDriveManager.class);
070        Map<String, SynchronizationRoots> roots = driveManager.getSynchronizationRoots(ctx.getPrincipal());
071        DocumentModelList rootDocumentModels = new DocumentModelListImpl();
072        for (Map.Entry<String, SynchronizationRoots> rootsEntry : roots.entrySet()) {
073            if (session.getRepositoryName().equals(rootsEntry.getKey())) {
074                Set<IdRef> references = rootsEntry.getValue().getRefs();
075                rootDocumentModels.addAll(session.getDocuments(references.toArray(new DocumentRef[references.size()])));
076            } else {
077                if (allRepositories) {
078                    // XXX: do we really need to implement this now?
079                    throw new RuntimeException("Multi repo roots not yet implemented");
080                }
081            }
082        }
083        return rootDocumentModels;
084    }
085
086}