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