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: DownloadMethodExpression.java 28491 2008-01-04 19:04:30Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.binding;
021
022import java.io.Serializable;
023
024import javax.el.ELContext;
025import javax.el.MethodExpression;
026import javax.el.MethodInfo;
027import javax.el.ValueExpression;
028
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
031
032/**
033 * Download method expression for blobs.
034 *
035 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
036 */
037public class DownloadMethodExpression extends MethodExpression implements Serializable {
038
039    private static final long serialVersionUID = 9010857019674405375L;
040
041    private final ValueExpression blobExpression;
042
043    private final ValueExpression fileNameExpression;
044
045    public DownloadMethodExpression(ValueExpression blobExpression, ValueExpression fileNameExpression) {
046        this.blobExpression = blobExpression;
047        this.fileNameExpression = fileNameExpression;
048    }
049
050    // Expression interface
051
052    @Override
053    public boolean equals(Object o) {
054        if (this == o) {
055            return true;
056        }
057        if (!(o instanceof DownloadMethodExpression)) {
058            return false;
059        }
060
061        DownloadMethodExpression other = (DownloadMethodExpression) o;
062
063        if (blobExpression != null ? !blobExpression.equals(other.blobExpression) : other.blobExpression != null) {
064            return false;
065        }
066        if (fileNameExpression != null ? !fileNameExpression.equals(other.fileNameExpression)
067                : other.fileNameExpression != null) {
068            return false;
069        }
070
071        return true;
072    }
073
074    @Override
075    public int hashCode() {
076        int result = blobExpression != null ? blobExpression.hashCode() : 0;
077        result = 31 * result + (fileNameExpression != null ? fileNameExpression.hashCode() : 0);
078        return result;
079    }
080
081    @Override
082    public String getExpressionString() {
083        // return only the blob one
084        return blobExpression == null ? null : blobExpression.getExpressionString();
085    }
086
087    @Override
088    public boolean isLiteralText() {
089        return blobExpression == null ? null : blobExpression.isLiteralText();
090    }
091
092    // MethodExpression interface
093
094    @Override
095    public MethodInfo getMethodInfo(ELContext context) {
096        return null;
097    }
098
099    @Override
100    public Object invoke(ELContext context, Object[] params) {
101        Blob blob = getBlob(context);
102        String filename = getFilename(context);
103        ComponentUtils.download(null, null, blob, filename, "el");
104        return null;
105    }
106
107    protected String getFilename(ELContext context) {
108        if (fileNameExpression == null) {
109            return null;
110        } else {
111            return (String) fileNameExpression.getValue(context);
112        }
113    }
114
115    protected Blob getBlob(ELContext context) {
116        if (blobExpression == null) {
117            return null;
118        } else {
119            return (Blob) blobExpression.getValue(context);
120        }
121    }
122
123}