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.util.bridges.jsf.common.FacesMessageUtil;
32
33 import java.text.DecimalFormat;
34
35 import java.util.EventObject;
36
37 import javax.faces.context.FacesContext;
38 import javax.faces.event.ActionEvent;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43
68 public class FileUploadManagedBean implements Renderable {
69
70 public FileUploadManagedBean() {
71 _state = PersistentFacesState.getInstance();
72 }
73
74 public PersistentFacesState getState() {
75 return _state;
76 }
77
78 public void setRenderManager(RenderManager renderManager) {
79 _renderManager = renderManager;
80 }
81
82 public InputFile getInputFile() {
83 return _inputFile;
84 }
85
86 public void setInputFile(InputFile inputFile) {
87 _inputFile = inputFile;
88 }
89
90 public int getPercent() {
91 return _percent;
92 }
93
94 public void setPercent(int percent) {
95 _percent = percent;
96 }
97
98 public boolean isComplete() {
99 if (_percent == 100) {
100 return true;
101 }
102 else {
103 return false;
104 }
105 }
106
107 public void actionListener(ActionEvent actionEvent) {
108 InputFile inputFile = (InputFile)actionEvent.getSource();
109
110 int status = inputFile.getStatus();
111
112 try {
113 if (status == InputFile.INVALID) {
114 addErrorMessage("file-type-is-invalid");
115
116 _percent = 100;
117 }
118 else if (status == InputFile.SAVED) {
119 _percent = 100;
120 }
121 else if (status == InputFile.SIZE_LIMIT_EXCEEDED) {
122 long maxFileSizeInBytes = _inputFile.getSizeMax();
123
124 DecimalFormat decimalFormat = new DecimalFormat();
125
126 decimalFormat.setGroupingUsed(false);
127 decimalFormat.setMaximumFractionDigits(2);
128 decimalFormat.setMinimumFractionDigits(0);
129
130 String maxFileSizeInMegs =
131 decimalFormat.format(
132 (double)maxFileSizeInBytes / 1024 / 1024);
133
134 addErrorMessage(
135 "file-size-is-larger-than-x-megabytes", maxFileSizeInMegs);
136
137 _percent = 100;
138 }
139 else if (status == InputFile.UNKNOWN_SIZE) {
140 addErrorMessage("file-size-was-not-specified-in-the-request");
141
142 _percent = 100;
143 }
144 }
145 catch (Exception e) {
146 _log.error(e, e);
147
148 addErrorMessage(e.getMessage());
149 }
150 }
151
152 public void progressListener(EventObject eventObject) {
153 InputFile inputFile = (InputFile)eventObject.getSource();
154
155 _percent = inputFile.getFileInfo().getPercent();
156
157 _renderManager.requestRender(this);
158 }
159
160 public void renderingException(RenderingException renderingException) {
161 _log.error(renderingException.getMessage());
162 }
163
164 protected void addErrorMessage(String key) {
165 addErrorMessage(key, null);
166 }
167
168 protected void addErrorMessage(String key, String argument) {
169 FacesContext facesContext = FacesContext.getCurrentInstance();
170
171 if (_inputFile == null) {
172 FacesMessageUtil.error(facesContext, key, argument);
173 }
174 else {
175 FacesMessageUtil.error(
176 _inputFile.getClientId(facesContext), facesContext, key,
177 argument);
178 }
179 }
180
181 private static Log _log = LogFactory.getLog(FileUploadManagedBean.class);
182
183 private PersistentFacesState _state;
184 private RenderManager _renderManager;
185 private InputFile _inputFile;
186 private int _percent;
187
188 }