1
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
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 }