001/*
002 * (C) Copyright 2014 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-2.1.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 *     <a href="mailto:glefevre@nuxeo.com">Gildas</a>
016 */
017package org.nuxeo.ecm.collections.core.automation;
018
019import java.util.Map;
020
021import org.nuxeo.ecm.automation.AutomationService;
022import org.nuxeo.ecm.automation.OperationChain;
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.OperationException;
025import org.nuxeo.ecm.automation.OperationParameters;
026import org.nuxeo.ecm.automation.core.Constants;
027import org.nuxeo.ecm.automation.core.annotations.Context;
028import org.nuxeo.ecm.automation.core.annotations.Operation;
029import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
030import org.nuxeo.ecm.automation.core.operations.services.DocumentPageProviderOperation;
031import org.nuxeo.ecm.automation.jaxrs.io.documents.PaginableDocumentModelListImpl;
032import org.nuxeo.ecm.collections.api.CollectionConstants;
033import org.nuxeo.ecm.collections.api.FavoritesManager;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.DocumentModelList;
037
038/**
039 * Class for the operation getting the elements present in the Favorites collection.
040 *
041 * @since 6.0
042 */
043@Operation(id = GetDocumentsFromFavoritesOperation.ID, category = Constants.CAT_DOCUMENT, label = "Get documents from favorites", description = "Get the list "
044        + "of documents visible from the currentUser's favorites. This is returning a list of documents.", aliases = { "Collection.GetElementsInFavorite" })
045public class GetDocumentsFromFavoritesOperation {
046
047    public static final String ID = "Favorite.GetDocuments";
048
049    @Context
050    protected CoreSession session;
051
052    @Context
053    protected FavoritesManager favoritesManager;
054
055    @Context
056    protected OperationContext ctx;
057
058    @Context
059    protected AutomationService service;
060
061    @OperationMethod
062    public DocumentModelList run(DocumentModel context) throws OperationException {
063
064        DocumentModel favorites = favoritesManager.getFavorites(context, session);
065
066        Map<String, Object> vars = ctx.getVars();
067        vars.put("searchTerm", favorites.getId());
068        vars.put("providerName", CollectionConstants.COLLECTION_CONTENT_PAGE_PROVIDER);
069
070        OperationContext subctx = new OperationContext(ctx.getCoreSession(), vars);
071
072        OperationChain chain = new OperationChain("operation");
073        OperationParameters oparams = new OperationParameters(DocumentPageProviderOperation.ID, vars);
074        chain.add(oparams);
075        return (PaginableDocumentModelListImpl) service.run(subctx, chain);
076    }
077}