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