View Javadoc
1   package gboat2.base.view.components;
2   
3   import gboat2.base.view.GboatViewUtil;
4   
5   import javax.servlet.http.HttpServletRequest;
6   import javax.servlet.http.HttpServletResponse;
7   
8   import org.apache.commons.lang3.RandomStringUtils;
9   import org.apache.commons.lang3.StringUtils;
10  import org.apache.struts2.components.UIBean;
11  import org.apache.struts2.views.annotations.StrutsTagAttribute;
12  
13  import com.opensymphony.xwork2.util.ValueStack;
14  
15  /**
16   * 上传附件和上传照片等上传控件的抽象类
17   * 
18   * @date 2014年7月21日
19   * @author <a href="mailto:[email protected]">何明旺</a>
20   * @since 3.0.0-SNAPSHOT
21   */
22  public abstract class AbstractAttach extends UIBean {
23  
24  	protected String belongId;
25  	protected String deleteUrl;
26  
27  	protected String url;
28  	protected String fileDataName;
29  	protected String flashSwfUrl;
30  	protected String headers;
31  	protected String maxFileSize;
32  	protected String maxRetries;
33  	protected String multipart;
34  	protected String multipartParams;
35  	protected String requiredFeatures;
36  	protected String runtimes;
37  	protected String silverlightXapUrl;
38  	protected String autostart;
39  	protected String dragdrop;
40  	
41  	protected String onReadyTopics;
42  	protected String onSelectedTopics;
43  	protected String onRemovedTopics;
44  	protected String onStartTopics;
45  	protected String onStopTopics;
46  	protected String onProgressTopics;
47  	protected String onUploadedTopics;
48  	protected String onCompleteTopics;
49  	protected String onErrorTopics;
50  
51  	public AbstractAttach(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
52  		super(stack, request, response);
53  	}
54  
55  	protected abstract String getJqueryAction();
56  
57      @Override
58  	public void evaluateExtraParams() {
59  		super.evaluateExtraParams();
60  		
61  		addParameter("jqueryaction", getJqueryAction());
62  		
63  		if(belongId != null)
64  		    addParameter("belongId", findString(belongId));
65  		
66  		if(deleteUrl != null)
67  		    addParameter("deleteUrl", findString(deleteUrl));
68  		
69  		if(url != null)
70  		    addParameter("url", findString(url));
71  
72  		if (fileDataName != null)
73  		    addParameter("file_data_name", findString(fileDataName));
74  		
75  		if (flashSwfUrl != null)
76  		    addParameter("flash_swf_url", findString(flashSwfUrl));
77  		
78  		if (headers != null)
79  		    addParameter("headers", findString(headers));
80  		
81  		if (maxFileSize != null)
82  		    addParameter("max_file_size", findString(maxFileSize));
83  		
84  		if (maxRetries != null)
85  		    addParameter("max_retries", findValue(maxRetries, Integer.class));
86  		
87  		if (multipart != null)
88  		    addParameter("multipart", findValue(multipart, Boolean.class));
89  		
90  		if (multipartParams != null)
91  		    addParameter("multipart_params", findString(multipartParams));
92  		
93  		if (requiredFeatures != null)
94  		    addParameter("required_features", findString(requiredFeatures));
95  		
96  		if (runtimes != null)
97  		    addParameter("runtimes", findString(runtimes));
98  		
99  		if (silverlightXapUrl != null)
100 		    addParameter("silverlight_xap_url", findString(silverlightXapUrl));
101 		
102 		if (autostart != null)
103 		    addParameter("autostart", findValue(autostart, Boolean.class));
104 		
105 		if (dragdrop != null)
106 		    addParameter("dragdrop", findValue(dragdrop, Boolean.class));
107 		
108 		if (onReadyTopics != null)
109 		    addParameter("onReadyTopics", findString(onReadyTopics));
110 		
111 		if (onSelectedTopics != null)
112 		    addParameter("onSelectedTopics", findString(onSelectedTopics));
113 		
114 		if (onRemovedTopics != null)
115 		    addParameter("onRemovedTopics", findString(onRemovedTopics));
116 		
117 		if (onStartTopics != null)
118 		    addParameter("onStartTopics", findString(onStartTopics));
119 		
120 		if (onStopTopics != null)
121 		    addParameter("onStopTopics", findString(onStopTopics));
122 		
123 		if (onProgressTopics != null)
124 		    addParameter("onProgressTopics", findString(onProgressTopics));
125 		
126 		if (onUploadedTopics != null)
127 		    addParameter("onUploadedTopics", findString(onUploadedTopics));
128 		
129 		if (onCompleteTopics != null)
130 		    addParameter("onCompleteTopics", findString(onCompleteTopics));
131 		
132 		if (onErrorTopics != null)
133 		    addParameter("onErrorTopics", findString(onErrorTopics));
134 
135 		// 设置 readonly 属性
136 		GboatViewUtil.addReadonlyParameter(this, request);
137 		// 如果 ID 为空,则生成一个随机 ID
138 		if (StringUtils.isBlank(id)) {
139 			id = this.getClass().getSimpleName() + "_" + RandomStringUtils.randomNumeric(3);
140 			addParameter("id", id);
141 		}
142 	}
143 
144     @StrutsTagAttribute(description = "附件所归属的数据记录的主键(ID)值")
145     public void setBelongId(String belongId) {
146         this.belongId = belongId;
147     }
148 
149     @StrutsTagAttribute(description = "删除附件的 URL", defaultValue = "upload!deleteFile.do")
150     public void setDeleteUrl(String deleteUrl) {
151         this.deleteUrl = deleteUrl;
152     }
153 
154     @StrutsTagAttribute(description = "处理文件上传逻辑 Action 的 URL", defaultValue = "upload.do")
155     public void setUrl(String url) {
156         this.url = url;
157     }
158 
159 	@StrutsTagAttribute(description = "文件表单域字段的名称", defaultValue = "file")
160     public void setFileDataName(String fileDataName) {
161         this.fileDataName = fileDataName;
162     }
163 
164 	@StrutsTagAttribute(description = "Flash swf 文件的 URL")
165     public void setFlashSwfUrl(String flashSwfUrl) {
166         this.flashSwfUrl = flashSwfUrl;
167     }
168 
169 	@StrutsTagAttribute(description = "自定义上传文件的请求头,是一个键值对的 JSON 格式字符串,如: {'name': 'hemw', 'password': 'testPWD'}")
170     public void setHeaders(String headers) {
171         this.headers = headers;
172     }
173 
174     @StrutsTagAttribute(description = "允许上传的最大值,, 支持的单位有 b, kb, mb, gb, tb, 如: 10mb、1gb")
175     public void setMaxFileSize(String maxFileSize) {
176         this.maxFileSize = maxFileSize;
177     }
178 
179 	@StrutsTagAttribute(description = "在触发错误事件(Error event)之前重试上传文件或分块的次数", type = "Integer", defaultValue = "0")
180     public void setMaxRetries(String maxRetries) {
181         this.maxRetries = maxRetries;
182     }
183 
184 	@StrutsTagAttribute(description = "是否以 Multipart formated 的方式发送文件和附加参数", type = "Boolean", defaultValue = "true")
185     public void setMultipart(String multipart) {
186         this.multipart = multipart;
187     }
188 
189 	@StrutsTagAttribute(description = "上传每个文件时,一并发送的附件参数, 该属性值是一个 JSON 格式字符串,如: {'author': 'hemw'}")
190     public void setMultipartParams(String multipartParams) {
191         this.multipartParams = multipartParams;
192     }
193 
194 	@StrutsTagAttribute(description = "运行时(浏览器)应该具备的特性,可以是 key1=value1,key2=value2 或 {key1:value1,key2:value2} 的形式")
195     public void setRequiredFeatures(String requiredFeatures) {
196         this.requiredFeatures = requiredFeatures;
197     }
198 
199 	@StrutsTagAttribute(description = "逗号分隔的一系列运行环境的值,将逐一尝试,直到找到可以正确执行的运行环境", defaultValue = "html5,flash,silverlight,html4")
200     public void setRuntimes(String runtimes) {
201         this.runtimes = runtimes;
202     }
203 
204 	@StrutsTagAttribute(description = "Silverlight xap 文件的 URL")
205     public void setSilverlightXapUrl(String silverlightXapUrl) {
206         this.silverlightXapUrl = silverlightXapUrl;
207     }
208 
209 	@StrutsTagAttribute(description = "选择完要上传的文件后,是否自动上传", type = "Boolean", defaultValue = "false")
210     public void setAutostart(String autostart) {
211         this.autostart = autostart;
212     }
213 
214 	@StrutsTagAttribute(description = "是否允许通过拖拽选择要上传的文件", type = "Boolean", defaultValue = "true")
215     public void setDragdrop(String dragdrop) {
216         this.dragdrop = dragdrop;
217     }
218 
219 	@StrutsTagAttribute(description = "上传控件完成初始化后(触发 ready 事件时)发布的主题,多值之间使用逗号分隔")
220     public void setOnReadyTopics(String onReadyTopics) {
221         this.onReadyTopics = onReadyTopics;
222     }
223 
224     @StrutsTagAttribute(description = "选择文件后关闭弹出对话框时(触发 selected 事件时)发布的主题,多值之间使用逗号分隔")
225     public void setOnSelectedTopics(String onSelectedTopics) {
226         this.onSelectedTopics = onSelectedTopics;
227     }
228 
229     @StrutsTagAttribute(description = "移除队列中的文件时(触发removed  事件时)发布的主题,多值之间使用逗号分隔")
230     public void setOnRemovedTopics(String onRemovedTopics) {
231         this.onRemovedTopics = onRemovedTopics;
232     }
233 
234     @StrutsTagAttribute(description = "开始文件上传时(触发 start 事件时)发布的主题,多值之间使用逗号分隔")
235     public void setOnStartTopics(String onStartTopics) {
236         this.onStartTopics = onStartTopics;
237     }
238 
239     @StrutsTagAttribute(description = "中断文件上传时(触发 stop 事件时)发布的主题,多值之间使用逗号分隔")
240     public void setOnStopTopics(String onStopTopics) {
241         this.onStopTopics = onStopTopics;
242     }
243 
244     @StrutsTagAttribute(description = "处理文件上传时(触发 progress 事件时)发布的主题,多值之间使用逗号分隔")
245     public void setOnProgressTopics(String onProgressTopics) {
246         this.onProgressTopics = onProgressTopics;
247     }
248 
249     @StrutsTagAttribute(description = "每上传完一个文件时(触发 uploaded 事件时)发布的主题,多值之间使用逗号分隔")
250     public void setOnUploadedTopics(String onUploadedTopics) {
251         this.onUploadedTopics = onUploadedTopics;
252     }
253 
254     @StrutsTagAttribute(description = "上传完所有文件时(触发 complete 事件时)发布的主题,多值之间使用逗号分隔")
255     public void setOnCompleteTopics(String onCompleteTopics) {
256         this.onCompleteTopics = onCompleteTopics;
257     }
258 
259     @StrutsTagAttribute(description = "当发生错误时(触发 error 事件时)发布的主题,多值之间使用逗号分隔")
260     public void setOnErrorTopics(String onErrorTopics) {
261         this.onErrorTopics = onErrorTopics;
262     }
263 
264 }