001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.bridges.jsf.icefaces;
022:
023: import com.icesoft.faces.async.render.RenderManager;
024: import com.icesoft.faces.async.render.Renderable;
025: import com.icesoft.faces.component.inputfile.InputFile;
026: import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
027: import com.icesoft.faces.webapp.xmlhttp.RenderingException;
028:
029: import com.liferay.util.bridges.jsf.common.FacesMessageUtil;
030:
031: import java.text.DecimalFormat;
032:
033: import java.util.EventObject;
034:
035: import javax.faces.context.FacesContext;
036: import javax.faces.event.ActionEvent;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: /**
042: * <a href="FileUploadManagedBean.java.html"><b><i>View Source</i></b></a>
043: *
044: * <p>
045: * This class is a managed bean that is designed specifically to work with the
046: * ICEfaces framework, by utilizing the <code><ice:inputFile/></code> component.
047: * The basic ideas found in this bean were taken from the ICEfaces tuorial,
048: * found here:
049: * </p>
050: *
051: * <p>
052: * http://facestutorials.icefaces.org/tutorial/inputFile-tutorial.html
053: * </p>
054: *
055: * <p>
056: * The server initiated rendering API documentation is found here:
057: * </p>
058: *
059: * <p>
060: * http://www.icesoft.com/developer_guides/icefaces/htmlguide/devguide/advanced_topics2.html
061: * </p>
062: *
063: * @author Neil Griffin
064: *
065: */
066: public class FileUploadManagedBean implements Renderable {
067:
068: public FileUploadManagedBean() {
069: _state = PersistentFacesState.getInstance();
070: }
071:
072: public PersistentFacesState getState() {
073: return _state;
074: }
075:
076: public void setRenderManager(RenderManager renderManager) {
077: _renderManager = renderManager;
078: }
079:
080: public InputFile getInputFile() {
081: return _inputFile;
082: }
083:
084: public void setInputFile(InputFile inputFile) {
085: _inputFile = inputFile;
086: }
087:
088: public int getPercent() {
089: return _percent;
090: }
091:
092: public void setPercent(int percent) {
093: _percent = percent;
094: }
095:
096: public boolean isComplete() {
097: if (_percent == 100) {
098: return true;
099: } else {
100: return false;
101: }
102: }
103:
104: public void actionListener(ActionEvent actionEvent) {
105: InputFile inputFile = (InputFile) actionEvent.getSource();
106:
107: int status = inputFile.getStatus();
108:
109: try {
110: if (status == InputFile.INVALID) {
111: addErrorMessage("file-type-is-invalid");
112:
113: _percent = 100;
114: } else if (status == InputFile.SAVED) {
115: _percent = 100;
116: } else if (status == InputFile.SIZE_LIMIT_EXCEEDED) {
117: long maxFileSizeInBytes = _inputFile.getSizeMax();
118:
119: DecimalFormat decimalFormat = new DecimalFormat();
120:
121: decimalFormat.setGroupingUsed(false);
122: decimalFormat.setMaximumFractionDigits(2);
123: decimalFormat.setMinimumFractionDigits(0);
124:
125: String maxFileSizeInMegs = decimalFormat
126: .format((double) maxFileSizeInBytes / 1024 / 1024);
127:
128: addErrorMessage("file-size-is-larger-than-x-megabytes",
129: maxFileSizeInMegs);
130:
131: _percent = 100;
132: } else if (status == InputFile.UNKNOWN_SIZE) {
133: addErrorMessage("file-size-was-not-specified-in-the-request");
134:
135: _percent = 100;
136: }
137: } catch (Exception e) {
138: _log.error(e, e);
139:
140: addErrorMessage(e.getMessage());
141: }
142: }
143:
144: public void progressListener(EventObject eventObject) {
145: InputFile inputFile = (InputFile) eventObject.getSource();
146:
147: _percent = inputFile.getFileInfo().getPercent();
148:
149: _renderManager.requestRender(this );
150: }
151:
152: public void renderingException(RenderingException renderingException) {
153: _log.error(renderingException.getMessage());
154: }
155:
156: protected void addErrorMessage(String key) {
157: addErrorMessage(key, null);
158: }
159:
160: protected void addErrorMessage(String key, String argument) {
161: FacesContext facesContext = FacesContext.getCurrentInstance();
162:
163: if (_inputFile == null) {
164: FacesMessageUtil.error(facesContext, key, argument);
165: } else {
166: FacesMessageUtil.error(
167: _inputFile.getClientId(facesContext), facesContext,
168: key, argument);
169: }
170: }
171:
172: private static Log _log = LogFactory
173: .getLog(FileUploadManagedBean.class);
174:
175: private PersistentFacesState _state;
176: private RenderManager _renderManager;
177: private InputFile _inputFile;
178: private int _percent;
179:
180: }
|