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   * This class is a managed bean that is designed specifically to work with the
46   * ICEfaces framework, by utilizing the <code><ice:inputFile/></code> component.
47   *
48   * @author Neil Griffin
49   */
50  public class FileUploadManagedBean implements Renderable {
51  
52      public FileUploadManagedBean() {
53          _state = PersistentFacesState.getInstance();
54      }
55  
56      public PersistentFacesState getState() {
57          return _state;
58      }
59  
60      public void setRenderManager(RenderManager renderManager) {
61          _renderManager = renderManager;
62      }
63  
64      public InputFile getInputFile() {
65          return _inputFile;
66      }
67  
68      public void setInputFile(InputFile inputFile) {
69          _inputFile = inputFile;
70      }
71  
72      public int getPercent() {
73          return _percent;
74      }
75  
76      public void setPercent(int percent) {
77          _percent = percent;
78      }
79  
80      public boolean isComplete() {
81          if (_percent == 100) {
82              return true;
83          }
84          else {
85              return false;
86          }
87      }
88  
89      public void actionListener(ActionEvent actionEvent) {
90          InputFile inputFile = (InputFile)actionEvent.getSource();
91  
92          int status = inputFile.getStatus();
93  
94          try {
95              if (status == InputFile.INVALID) {
96                  addErrorMessage("file-type-is-invalid");
97  
98                  _percent = 100;
99              }
100             else if (status == InputFile.SAVED) {
101                 _percent = 100;
102             }
103             else if (status == InputFile.SIZE_LIMIT_EXCEEDED) {
104                 long maxFileSizeInBytes = _inputFile.getSizeMax();
105 
106                 DecimalFormat decimalFormat = new DecimalFormat();
107 
108                 decimalFormat.setGroupingUsed(false);
109                 decimalFormat.setMaximumFractionDigits(2);
110                 decimalFormat.setMinimumFractionDigits(0);
111 
112                 String maxFileSizeInMegs =
113                     decimalFormat.format(
114                         (double)maxFileSizeInBytes / 1024 / 1024);
115 
116                 addErrorMessage(
117                     "file-size-is-larger-than-x-megabytes", maxFileSizeInMegs);
118 
119                 _percent = 100;
120             }
121             else if (status == InputFile.UNKNOWN_SIZE) {
122                 addErrorMessage("file-size-was-not-specified-in-the-request");
123 
124                 _percent = 100;
125             }
126         }
127         catch (Exception e) {
128             _log.error(e, e);
129 
130             addErrorMessage(e.getMessage());
131         }
132     }
133 
134     public void progressListener(EventObject eventObject) {
135         InputFile inputFile = (InputFile)eventObject.getSource();
136 
137         _percent = inputFile.getFileInfo().getPercent();
138 
139         _renderManager.requestRender(this);
140     }
141 
142     public void renderingException(RenderingException renderingException) {
143         _log.error(renderingException.getMessage());
144     }
145 
146     protected void addErrorMessage(String key) {
147         addErrorMessage(key, null);
148     }
149 
150     protected void addErrorMessage(String key, String argument) {
151         FacesContext facesContext = FacesContext.getCurrentInstance();
152 
153         if (_inputFile == null) {
154             FacesMessageUtil.error(facesContext, key, argument);
155         }
156         else {
157             FacesMessageUtil.error(
158                 _inputFile.getClientId(facesContext), facesContext, key,
159                 argument);
160         }
161     }
162 
163     private static Log _log =
164         LogFactoryUtil.getLog(FileUploadManagedBean.class);
165 
166     private PersistentFacesState _state;
167     private RenderManager _renderManager;
168     private InputFile _inputFile;
169     private int _percent;
170 
171 }