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