1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.bridges.jsf.icefaces;
24  
25  import com.icesoft.faces.async.render.RenderManager;
26  import com.icesoft.faces.async.render.Renderable;
27  import com.icesoft.faces.component.inputfile.InputFile;
28  import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
29  import com.icesoft.faces.webapp.xmlhttp.RenderingException;
30  
31  import com.liferay.util.bridges.jsf.common.FacesMessageUtil;
32  
33  import java.text.DecimalFormat;
34  
35  import java.util.EventObject;
36  
37  import javax.faces.context.FacesContext;
38  import javax.faces.event.ActionEvent;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /**
44   * <a href="FileUploadManagedBean.java.html"><b><i>View Source</i></b></a>
45   *
46   * <p>
47   * This class is a managed bean that is designed specifically to work with the
48   * ICEfaces framework, by utilizing the <code><ice:inputFile/></code> component.
49   * The basic ideas found in this bean were taken from the ICEfaces tuorial,
50   * found here:
51   * </p>
52   *
53   * <p>
54   * http://facestutorials.icefaces.org/tutorial/inputFile-tutorial.html
55   * </p>
56   *
57   * <p>
58   * The server initiated rendering API documentation is found here:
59   * </p>
60   *
61   * <p>
62   * http://www.icesoft.com/developer_guides/icefaces/htmlguide/devguide/advanced_topics2.html
63   * </p>
64   *
65   * @author Neil Griffin
66   *
67   */
68  public class FileUploadManagedBean implements Renderable {
69  
70      public FileUploadManagedBean() {
71          _state = PersistentFacesState.getInstance();
72      }
73  
74      public PersistentFacesState getState() {
75          return _state;
76      }
77  
78      public void setRenderManager(RenderManager renderManager) {
79          _renderManager = renderManager;
80      }
81  
82      public InputFile getInputFile() {
83          return _inputFile;
84      }
85  
86      public void setInputFile(InputFile inputFile) {
87          _inputFile = inputFile;
88      }
89  
90      public int getPercent() {
91          return _percent;
92      }
93  
94      public void setPercent(int percent) {
95          _percent = percent;
96      }
97  
98      public boolean isComplete() {
99          if (_percent == 100) {
100             return true;
101         }
102         else {
103             return false;
104         }
105     }
106 
107     public void actionListener(ActionEvent actionEvent) {
108         InputFile inputFile = (InputFile)actionEvent.getSource();
109 
110         int status = inputFile.getStatus();
111 
112         try {
113             if (status == InputFile.INVALID) {
114                 addErrorMessage("file-type-is-invalid");
115 
116                 _percent = 100;
117             }
118             else if (status == InputFile.SAVED) {
119                 _percent = 100;
120             }
121             else if (status == InputFile.SIZE_LIMIT_EXCEEDED) {
122                 long maxFileSizeInBytes = _inputFile.getSizeMax();
123 
124                 DecimalFormat decimalFormat = new DecimalFormat();
125 
126                 decimalFormat.setGroupingUsed(false);
127                 decimalFormat.setMaximumFractionDigits(2);
128                 decimalFormat.setMinimumFractionDigits(0);
129 
130                 String maxFileSizeInMegs =
131                     decimalFormat.format(
132                         (double)maxFileSizeInBytes / 1024 / 1024);
133 
134                 addErrorMessage(
135                     "file-size-is-larger-than-x-megabytes", maxFileSizeInMegs);
136 
137                 _percent = 100;
138             }
139             else if (status == InputFile.UNKNOWN_SIZE) {
140                 addErrorMessage("file-size-was-not-specified-in-the-request");
141 
142                 _percent = 100;
143             }
144         }
145         catch (Exception e) {
146             _log.error(e, e);
147 
148             addErrorMessage(e.getMessage());
149         }
150     }
151 
152     public void progressListener(EventObject eventObject) {
153         InputFile inputFile = (InputFile)eventObject.getSource();
154 
155         _percent = inputFile.getFileInfo().getPercent();
156 
157         _renderManager.requestRender(this);
158     }
159 
160     public void renderingException(RenderingException renderingException) {
161         _log.error(renderingException.getMessage());
162     }
163 
164     protected void addErrorMessage(String key) {
165         addErrorMessage(key, null);
166     }
167 
168     protected void addErrorMessage(String key, String argument) {
169         FacesContext facesContext = FacesContext.getCurrentInstance();
170 
171         if (_inputFile == null) {
172             FacesMessageUtil.error(facesContext, key, argument);
173         }
174         else {
175             FacesMessageUtil.error(
176                 _inputFile.getClientId(facesContext), facesContext, key,
177                 argument);
178         }
179     }
180 
181     private static Log _log = LogFactory.getLog(FileUploadManagedBean.class);
182 
183     private PersistentFacesState _state;
184     private RenderManager _renderManager;
185     private InputFile _inputFile;
186     private int _percent;
187 
188 }