001/*
002 * (C) Copyright 2010 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.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 *     Nuxeo - initial API and implementation
016 */
017package org.nuxeo.ecm.platform.routing.web;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.jboss.seam.ScopeType;
023import org.jboss.seam.annotations.Factory;
024import org.jboss.seam.annotations.In;
025import org.jboss.seam.annotations.Name;
026import org.jboss.seam.annotations.Scope;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
030import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
031import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * @author <a href="mailto:arussel@nuxeo.com">Alexandre Russel</a>
036 */
037@Name("relatedRouteAction")
038@Scope(ScopeType.EVENT)
039public class RelatedRouteActionBean {
040
041    @In(required = true, create = true)
042    protected NavigationContext navigationContext;
043
044    @In(create = true, required = false)
045    protected CoreSession documentManager;
046
047    @Factory(value = "relatedRoutes")
048    public List<DocumentModel> findRelatedRoute() {
049        DocumentModel currentDoc = navigationContext.getCurrentDocument();
050        if (currentDoc != null) {
051            return findRelatedRoute(currentDoc.getId());
052        }
053        return new ArrayList<DocumentModel>();
054    }
055
056    public List<DocumentModel> findRelatedRoute(String documentId) {
057        List<DocumentModel> docs = new ArrayList<DocumentModel>();
058        if (documentId == null || "".equals(documentId)) {
059            return docs;
060        }
061        List<DocumentRoute> relatedRoutes = getDocumentRoutingService().getDocumentRoutesForAttachedDocument(
062                documentManager, documentId);
063        for (DocumentRoute documentRoute : relatedRoutes) {
064            docs.add(documentRoute.getDocument());
065        }
066        return docs;
067    }
068
069    public boolean hasRelatedRoute(String documentId) {
070        return !findRelatedRoute(documentId).isEmpty();
071    }
072
073    public boolean hasRelatedRoute() {
074        return !findRelatedRoute().isEmpty();
075    }
076
077    public DocumentRoutingService getDocumentRoutingService() {
078        return Framework.getService(DocumentRoutingService.class);
079    }
080
081}