1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.bridges.jsf.icefaces;
16  
17  import com.icesoft.faces.async.render.RenderManager;
18  import com.icesoft.faces.async.render.Renderable;
19  import com.icesoft.faces.component.inputfile.InputFile;
20  import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
21  import com.icesoft.faces.webapp.xmlhttp.RenderingException;
22  
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.util.bridges.jsf.common.FacesMessageUtil;
26  
27  import java.text.DecimalFormat;
28  
29  import java.util.EventObject;
30  
31  import javax.faces.context.FacesContext;
32  import javax.faces.event.ActionEvent;
33  
34  /**
35   * <a href="FileUploadManagedBean.java.html"><b><i>View Source</i></b></a>
36   *
37   * This class is a managed bean that is designed specifically to work with the
38   * ICEfaces framework, by utilizing the <code><ice:inputFile/></code> component.
39   *
40   * @author Neil Griffin
41   */
42  public class FileUploadManagedBean implements Renderable {
43  
44      public FileUploadManagedBean() {
45          _state = PersistentFacesState.getInstance();
46      }
47  
48      public PersistentFacesState getState() {
49          return _state;
50      }
51  
52      public void setRenderManager(RenderManager renderManager) {
53          _renderManager = renderManager;
54      }
55  
56      public InputFile getInputFile() {
57          return _inputFile;
58      }
59  
60      public void setInputFile(InputFile inputFile) {
61          _inputFile = inputFile;
62      }
63  
64      public int getPercent() {
65          return _percent;
66      }
67  
68      public void setPercent(int percent) {
69          _percent = percent;
70      }
71  
72      public boolean isComplete() {
73          if (_percent == 100) {
74              return true;
75          }
76          else {
77              return false;
78          }
79      }
80  
81      public void actionListener(ActionEvent actionEvent) {
82          InputFile inputFile = (InputFile)actionEvent.getSource();
83  
84          int status = inputFile.getStatus();
85  
86          try {
87              if (status == InputFile.INVALID) {
88                  addErrorMessage("file-type-is-invalid");
89  
90                  _percent = 100;
91              }
92              else if (status == InputFile.SAVED) {
93                  _percent = 100;
94              }
95              else if (status == InputFile.SIZE_LIMIT_EXCEEDED) {
96                  long maxFileSizeInBytes = _inputFile.getSizeMax();
97  
98                  DecimalFormat decimalFormat = new DecimalFormat();
99  
100                 decimalFormat.setGroupingUsed(false);
101                 decimalFormat.setMaximumFractionDigits(2);
102                 decimalFormat.setMinimumFractionDigits(0);
103 
104                 String maxFileSizeInMegs =
105                     decimalFormat.format(
106                         (double)maxFileSizeInBytes / 1024 / 1024);
107 
108                 addErrorMessage(
109                     "file-size-is-larger-than-x-megabytes", maxFileSizeInMegs);
110 
111                 _percent = 100;
112             }
113             else if (status == InputFile.UNKNOWN_SIZE) {
114                 addErrorMessage("file-size-was-not-specified-in-the-request");
115 
116                 _percent = 100;
117             }
118         }
119         catch (Exception e) {
120             _log.error(e, e);
121 
122             addErrorMessage(e.getMessage());
123         }
124     }
125 
126     public void progressListener(EventObject eventObject) {
127         InputFile inputFile = (InputFile)eventObject.getSource();
128 
129         _percent = inputFile.getFileInfo().getPercent();
130 
131         _renderManager.requestRender(this);
132     }
133 
134     public void renderingException(RenderingException renderingException) {
135         _log.error(renderingException.getMessage());
136     }
137 
138     protected void addErrorMessage(String key) {
139         addErrorMessage(key, null);
140     }
141 
142     protected void addErrorMessage(String key, String argument) {
143         FacesContext facesContext = FacesContext.getCurrentInstance();
144 
145         if (_inputFile == null) {
146             FacesMessageUtil.error(facesContext, key, argument);
147         }
148         else {
149             FacesMessageUtil.error(
150                 _inputFile.getClientId(facesContext), facesContext, key,
151                 argument);
152         }
153     }
154 
155     private static Log _log = LogFactoryUtil.getLog(
156         FileUploadManagedBean.class);
157 
158     private PersistentFacesState _state;
159     private RenderManager _renderManager;
160     private InputFile _inputFile;
161     private int _percent;
162 
163 }