001
014
015 package com.liferay.util.bridges.jsf.icefaces;
016
017 import com.icesoft.faces.async.render.RenderManager;
018 import com.icesoft.faces.async.render.Renderable;
019 import com.icesoft.faces.component.inputfile.InputFile;
020 import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
021 import com.icesoft.faces.webapp.xmlhttp.RenderingException;
022
023 import com.liferay.portal.kernel.log.Log;
024 import com.liferay.portal.kernel.log.LogFactoryUtil;
025 import com.liferay.util.bridges.jsf.common.FacesMessageUtil;
026
027 import java.text.DecimalFormat;
028
029 import java.util.EventObject;
030
031 import javax.faces.context.FacesContext;
032 import javax.faces.event.ActionEvent;
033
034
040 public class FileUploadManagedBean implements Renderable {
041
042 public FileUploadManagedBean() {
043 _state = PersistentFacesState.getInstance();
044 }
045
046 public void actionListener(ActionEvent actionEvent) {
047 InputFile inputFile = (InputFile)actionEvent.getSource();
048
049 int status = inputFile.getStatus();
050
051 try {
052 if (status == InputFile.INVALID) {
053 addErrorMessage("file-type-is-invalid");
054
055 _percent = 100;
056 }
057 else if (status == InputFile.SAVED) {
058 _percent = 100;
059 }
060 else if (status == InputFile.SIZE_LIMIT_EXCEEDED) {
061 long maxFileSizeInBytes = _inputFile.getSizeMax();
062
063 DecimalFormat decimalFormat = new DecimalFormat();
064
065 decimalFormat.setGroupingUsed(false);
066 decimalFormat.setMaximumFractionDigits(2);
067 decimalFormat.setMinimumFractionDigits(0);
068
069 String maxFileSizeInMegs = decimalFormat.format(
070 (double)maxFileSizeInBytes / 1024 / 1024);
071
072 addErrorMessage(
073 "file-size-is-larger-than-x-megabytes", maxFileSizeInMegs);
074
075 _percent = 100;
076 }
077 else if (status == InputFile.UNKNOWN_SIZE) {
078 addErrorMessage("file-size-was-not-specified-in-the-request");
079
080 _percent = 100;
081 }
082 }
083 catch (Exception e) {
084 _log.error(e, e);
085
086 addErrorMessage(e.getMessage());
087 }
088 }
089
090 public InputFile getInputFile() {
091 return _inputFile;
092 }
093
094 public int getPercent() {
095 return _percent;
096 }
097
098 public PersistentFacesState getState() {
099 return _state;
100 }
101
102 public boolean isComplete() {
103 if (_percent == 100) {
104 return true;
105 }
106 else {
107 return false;
108 }
109 }
110
111 public void progressListener(EventObject eventObject) {
112 InputFile inputFile = (InputFile)eventObject.getSource();
113
114 _percent = inputFile.getFileInfo().getPercent();
115
116 _renderManager.requestRender(this);
117 }
118
119 public void renderingException(RenderingException renderingException) {
120 _log.error(renderingException.getMessage());
121 }
122
123 public void setInputFile(InputFile inputFile) {
124 _inputFile = inputFile;
125 }
126
127 public void setPercent(int percent) {
128 _percent = percent;
129 }
130
131 public void setRenderManager(RenderManager renderManager) {
132 _renderManager = renderManager;
133 }
134
135 protected void addErrorMessage(String key) {
136 addErrorMessage(key, null);
137 }
138
139 protected void addErrorMessage(String key, String argument) {
140 FacesContext facesContext = FacesContext.getCurrentInstance();
141
142 if (_inputFile == null) {
143 FacesMessageUtil.error(facesContext, key, argument);
144 }
145 else {
146 FacesMessageUtil.error(
147 _inputFile.getClientId(facesContext), facesContext, key,
148 argument);
149 }
150 }
151
152 private static Log _log = LogFactoryUtil.getLog(
153 FileUploadManagedBean.class);
154
155 private InputFile _inputFile;
156 private int _percent;
157 private RenderManager _renderManager;
158 private PersistentFacesState _state;
159
160 }