001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.bridges.jsf.icefaces;
016    
017    import com.icesoft.faces.async.render.RenderManager;
018    import com.icesoft.faces.async.render.Renderable;
019    import com.icesoft.faces.component.inputfile.InputFile;
020    import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
021    import com.icesoft.faces.webapp.xmlhttp.RenderingException;
022    
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.util.bridges.jsf.common.FacesMessageUtil;
026    
027    import java.text.DecimalFormat;
028    
029    import java.util.EventObject;
030    
031    import javax.faces.context.FacesContext;
032    import javax.faces.event.ActionEvent;
033    
034    /**
035     * This class is a managed bean that is designed specifically to work with the
036     * ICEfaces framework, by utilizing the <code><ice:inputFile/></code> component.
037     *
038     * @author Neil Griffin
039     */
040    public class FileUploadManagedBean implements Renderable {
041    
042            public FileUploadManagedBean() {
043                    _state = PersistentFacesState.getInstance();
044            }
045    
046            public PersistentFacesState getState() {
047                    return _state;
048            }
049    
050            public void setRenderManager(RenderManager renderManager) {
051                    _renderManager = renderManager;
052            }
053    
054            public InputFile getInputFile() {
055                    return _inputFile;
056            }
057    
058            public void setInputFile(InputFile inputFile) {
059                    _inputFile = inputFile;
060            }
061    
062            public int getPercent() {
063                    return _percent;
064            }
065    
066            public void setPercent(int percent) {
067                    _percent = percent;
068            }
069    
070            public boolean isComplete() {
071                    if (_percent == 100) {
072                            return true;
073                    }
074                    else {
075                            return false;
076                    }
077            }
078    
079            public void actionListener(ActionEvent actionEvent) {
080                    InputFile inputFile = (InputFile)actionEvent.getSource();
081    
082                    int status = inputFile.getStatus();
083    
084                    try {
085                            if (status == InputFile.INVALID) {
086                                    addErrorMessage("file-type-is-invalid");
087    
088                                    _percent = 100;
089                            }
090                            else if (status == InputFile.SAVED) {
091                                    _percent = 100;
092                            }
093                            else if (status == InputFile.SIZE_LIMIT_EXCEEDED) {
094                                    long maxFileSizeInBytes = _inputFile.getSizeMax();
095    
096                                    DecimalFormat decimalFormat = new DecimalFormat();
097    
098                                    decimalFormat.setGroupingUsed(false);
099                                    decimalFormat.setMaximumFractionDigits(2);
100                                    decimalFormat.setMinimumFractionDigits(0);
101    
102                                    String maxFileSizeInMegs =
103                                            decimalFormat.format(
104                                                    (double)maxFileSizeInBytes / 1024 / 1024);
105    
106                                    addErrorMessage(
107                                            "file-size-is-larger-than-x-megabytes", maxFileSizeInMegs);
108    
109                                    _percent = 100;
110                            }
111                            else if (status == InputFile.UNKNOWN_SIZE) {
112                                    addErrorMessage("file-size-was-not-specified-in-the-request");
113    
114                                    _percent = 100;
115                            }
116                    }
117                    catch (Exception e) {
118                            _log.error(e, e);
119    
120                            addErrorMessage(e.getMessage());
121                    }
122            }
123    
124            public void progressListener(EventObject eventObject) {
125                    InputFile inputFile = (InputFile)eventObject.getSource();
126    
127                    _percent = inputFile.getFileInfo().getPercent();
128    
129                    _renderManager.requestRender(this);
130            }
131    
132            public void renderingException(RenderingException renderingException) {
133                    _log.error(renderingException.getMessage());
134            }
135    
136            protected void addErrorMessage(String key) {
137                    addErrorMessage(key, null);
138            }
139    
140            protected void addErrorMessage(String key, String argument) {
141                    FacesContext facesContext = FacesContext.getCurrentInstance();
142    
143                    if (_inputFile == null) {
144                            FacesMessageUtil.error(facesContext, key, argument);
145                    }
146                    else {
147                            FacesMessageUtil.error(
148                                    _inputFile.getClientId(facesContext), facesContext, key,
149                                    argument);
150                    }
151            }
152    
153            private static Log _log = LogFactoryUtil.getLog(
154                    FileUploadManagedBean.class);
155    
156            private PersistentFacesState _state;
157            private RenderManager _renderManager;
158            private InputFile _inputFile;
159            private int _percent;
160    
161    }