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