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