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