001/*
002 * (C) Copyright 2007 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 *     Nuxeo - initial API and implementation
016 *
017 * $Id: DocumentLinkTagHandler.java 28610 2008-01-09 17:13:52Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.tag.handler;
021
022import javax.el.ELContext;
023import javax.el.ExpressionFactory;
024import javax.el.MethodExpression;
025import javax.faces.application.Application;
026import javax.faces.component.ActionSource2;
027import javax.faces.component.UIComponent;
028import javax.faces.context.FacesContext;
029import javax.faces.view.facelets.ComponentConfig;
030import javax.faces.view.facelets.FaceletContext;
031import javax.faces.view.facelets.MetaRuleset;
032import javax.faces.view.facelets.TagAttribute;
033
034import org.apache.commons.logging.Log;
035import org.apache.commons.logging.LogFactory;
036import org.nuxeo.ecm.core.api.DocumentModel;
037
038/**
039 * Component tag handler that wires a document link tag to a command link tag.
040 * <p>
041 * Useful when redirecting to a document using a post.
042 *
043 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
044 */
045public class DocumentLinkTagHandler extends GenericHtmlComponentHandler {
046
047    private static final Log log = LogFactory.getLog(DocumentLinkTagHandler.class);
048
049    private final TagAttribute document;
050
051    private final TagAttribute view;
052
053    public DocumentLinkTagHandler(ComponentConfig config) {
054        super(config);
055        document = getRequiredAttribute("document");
056        view = getAttribute("view");
057    }
058
059    @Override
060    @SuppressWarnings("rawtypes")
061    protected MetaRuleset createMetaRuleset(Class type) {
062        // expected created tag is an html command link
063        MetaRuleset mr = super.createMetaRuleset(type);
064        // alias title
065        mr.alias("title", "value");
066        mr.ignore("document");
067        mr.ignore("action");
068        mr.ignore("view");
069        return mr;
070    }
071
072    /**
073     * Sets action after component has been created.
074     */
075    @Override
076    public void onComponentCreated(FaceletContext ctx, UIComponent c, UIComponent parent) {
077        if (c instanceof ActionSource2) {
078            ActionSource2 command = (ActionSource2) c;
079            String docValue = getDocumentValue();
080            String viewId = getViewValue();
081            String actionValue;
082            if (viewId == null) {
083                actionValue = "#{navigationContext.navigateToDocument(" + docValue + ")}";
084            } else {
085                actionValue = "#{navigationContext.navigateToDocumentWithView(" + docValue + ", " + viewId + ")}";
086            }
087            FacesContext facesContext = ctx.getFacesContext();
088            Application app = facesContext.getApplication();
089            ExpressionFactory ef = app.getExpressionFactory();
090            ELContext context = facesContext.getELContext();
091            MethodExpression action = ef.createMethodExpression(context, actionValue, String.class, new Class[] {
092                    DocumentModel.class, String.class });
093            command.setActionExpression(action);
094        }
095    }
096
097    private String getDocumentValue() {
098        String docValue = document.getValue();
099        docValue = docValue.trim();
100        if (!((docValue.startsWith("${") || docValue.startsWith("#{")) && docValue.endsWith("}"))) {
101            log.error("Invalid value for document " + docValue);
102        }
103        docValue = docValue.substring(2, docValue.length() - 1);
104        return docValue;
105    }
106
107    private String getViewValue() {
108        String viewName = null;
109        if (view != null) {
110            viewName = view.getValue();
111            if (viewName != null) {
112                viewName = viewName.trim();
113                if ((viewName.startsWith("${") || viewName.startsWith("#{")) && viewName.endsWith("}")) {
114                    viewName = viewName.substring(2, viewName.length() - 1);
115                } else {
116                    viewName = '\"' + viewName + '\"';
117                }
118            }
119        }
120        return viewName;
121    }
122
123}