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