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