001    /**
002     * Copyright (c) 2000-2013 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 void actionListener(ActionEvent actionEvent) {
047                    InputFile inputFile = (InputFile)actionEvent.getSource();
048    
049                    int status = inputFile.getStatus();
050    
051                    try {
052                            if (status == InputFile.INVALID) {
053                                    addErrorMessage("file-type-is-invalid");
054    
055                                    _percent = 100;
056                            }
057                            else if (status == InputFile.SAVED) {
058                                    _percent = 100;
059                            }
060                            else if (status == InputFile.SIZE_LIMIT_EXCEEDED) {
061                                    long maxFileSizeInBytes = _inputFile.getSizeMax();
062    
063                                    DecimalFormat decimalFormat = new DecimalFormat();
064    
065                                    decimalFormat.setGroupingUsed(false);
066                                    decimalFormat.setMaximumFractionDigits(2);
067                                    decimalFormat.setMinimumFractionDigits(0);
068    
069                                    String maxFileSizeInMegs = decimalFormat.format(
070                                            (double)maxFileSizeInBytes / 1024 / 1024);
071    
072                                    addErrorMessage(
073                                            "file-size-is-larger-than-x-megabytes", maxFileSizeInMegs);
074    
075                                    _percent = 100;
076                            }
077                            else if (status == InputFile.UNKNOWN_SIZE) {
078                                    addErrorMessage("file-size-was-not-specified-in-the-request");
079    
080                                    _percent = 100;
081                            }
082                    }
083                    catch (Exception e) {
084                            _log.error(e, e);
085    
086                            addErrorMessage(e.getMessage());
087                    }
088            }
089    
090            public InputFile getInputFile() {
091                    return _inputFile;
092            }
093    
094            public int getPercent() {
095                    return _percent;
096            }
097    
098            @Override
099            public PersistentFacesState getState() {
100                    return _state;
101            }
102    
103            public boolean isComplete() {
104                    if (_percent == 100) {
105                            return true;
106                    }
107                    else {
108                            return false;
109                    }
110            }
111    
112            public void progressListener(EventObject eventObject) {
113                    InputFile inputFile = (InputFile)eventObject.getSource();
114    
115                    _percent = inputFile.getFileInfo().getPercent();
116    
117                    _renderManager.requestRender(this);
118            }
119    
120            @Override
121            public void renderingException(RenderingException renderingException) {
122                    _log.error(renderingException.getMessage());
123            }
124    
125            public void setInputFile(InputFile inputFile) {
126                    _inputFile = inputFile;
127            }
128    
129            public void setPercent(int percent) {
130                    _percent = percent;
131            }
132    
133            public void setRenderManager(RenderManager renderManager) {
134                    _renderManager = renderManager;
135            }
136    
137            protected void addErrorMessage(String key) {
138                    addErrorMessage(key, null);
139            }
140    
141            protected void addErrorMessage(String key, String argument) {
142                    FacesContext facesContext = FacesContext.getCurrentInstance();
143    
144                    if (_inputFile == null) {
145                            FacesMessageUtil.error(facesContext, key, argument);
146                    }
147                    else {
148                            FacesMessageUtil.error(
149                                    _inputFile.getClientId(facesContext), facesContext, key,
150                                    argument);
151                    }
152            }
153    
154            private static Log _log = LogFactoryUtil.getLog(
155                    FileUploadManagedBean.class);
156    
157            private InputFile _inputFile;
158            private int _percent;
159            private RenderManager _renderManager;
160            private PersistentFacesState _state;
161    
162    }