001/*
002 * (C) Copyright 2010 Nuxeo SAS (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 *    Mariana Cedica
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.routing.web;
021
022import static org.jboss.seam.ScopeType.CONVERSATION;
023import static org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY;
024import static org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider.MAX_RESULTS_PROPERTY;
025import static org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider.PAGE_SIZE_RESULTS_KEY;
026
027import java.io.Serializable;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031
032import org.jboss.seam.annotations.In;
033import org.jboss.seam.annotations.Name;
034import org.jboss.seam.annotations.Scope;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModel;
037import org.nuxeo.ecm.core.api.IdRef;
038import org.nuxeo.ecm.platform.query.api.PageProvider;
039import org.nuxeo.ecm.platform.query.api.PageProviderService;
040import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
041import org.nuxeo.ecm.platform.ui.web.invalidations.AutomaticDocumentBasedInvalidation;
042import org.nuxeo.ecm.platform.ui.web.invalidations.DocumentContextBoundActionBean;
043import org.nuxeo.runtime.api.Framework;
044
045/**
046 * Retrieves relations for current document route
047 *
048 * @author Mariana Cedica
049 */
050@Name("docRoutingSuggestionActions")
051@Scope(CONVERSATION)
052@AutomaticDocumentBasedInvalidation
053public class DocumentRoutingSuggestionActionsBean extends DocumentContextBoundActionBean implements Serializable {
054
055    private static final long serialVersionUID = 1L;
056
057    public static final String CURRENT_DOC_ROUTING_SEARCH_ATTACHED_DOC = "CURRENT_DOC_ROUTING_SEARCH_ATTACHED_DOC";
058
059    @In(create = true, required = false)
060    protected transient CoreSession documentManager;
061
062    public DocumentModel getDocumentModel(String id) {
063        return documentManager.getDocument(new IdRef(id));
064    }
065
066    public List<DocumentModel> getDocumentSuggestions(Object input) {
067        PageProviderService pageProviderService = Framework.getLocalService(PageProviderService.class);
068        Map<String, Serializable> props = new HashMap<String, Serializable>();
069        props.put(MAX_RESULTS_PROPERTY, PAGE_SIZE_RESULTS_KEY);
070        props.put(CORE_SESSION_PROPERTY, (Serializable) documentManager);
071        @SuppressWarnings("unchecked")
072        PageProvider<DocumentModel> pageProvider = (PageProvider<DocumentModel>) pageProviderService.getPageProvider(
073                CURRENT_DOC_ROUTING_SEARCH_ATTACHED_DOC, null, null, 0L, props, String.format("%s%%", input));
074        return pageProvider.getCurrentPage();
075    }
076
077    public List<DocumentModel> getRouteModelSuggestions(Object input) {
078        DocumentRoutingService documentRoutingService = Framework.getLocalService(DocumentRoutingService.class);
079        return documentRoutingService.searchRouteModels(documentManager, (String) input);
080    }
081
082    @Override
083    protected void resetBeanCache(DocumentModel newCurrentDocumentModel) {
084    }
085
086}