1 package gboat2.base.view.components;
2
3 import gboat2.base.bridge.util.json.JsonUtil;
4 import gboat2.base.view.GboatViewException;
5
6 import java.util.Map;
7 import java.util.Map.Entry;
8 import java.util.Set;
9
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import org.apache.struts2.components.UIBean;
14 import org.apache.struts2.views.annotations.StrutsTag;
15 import org.apache.struts2.views.annotations.StrutsTagAttribute;
16
17 import com.fasterxml.jackson.databind.JsonNode;
18 import com.fasterxml.jackson.databind.ObjectMapper;
19 import com.fasterxml.jackson.databind.node.ArrayNode;
20 import com.fasterxml.jackson.databind.node.ObjectNode;
21 import com.opensymphony.xwork2.util.ValueStack;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 @StrutsTag(
38 name = "attachSequence",
39 tldTagClass = "gboat2.base.view.jsp.AttachSequenceTag",
40 description = "创建一个浮动的附件查看列表",
41 allowDynamicAttributes=true)
42 public class AttachSequence extends UIBean {
43
44 public static final String TEMPLATE = "attach-sequence";
45
46 protected String attachMap;
47 protected String downloadUrl;
48
49 public AttachSequence(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
50 super(stack, request, response);
51 }
52
53 @Override
54 protected String getDefaultTemplate() {
55 return TEMPLATE;
56 }
57
58 @SuppressWarnings({ "unchecked", "rawtypes" })
59 @Override
60 public void evaluateExtraParams() {
61 super.evaluateExtraParams();
62
63 Object obj = findValue(attachMap);
64 if(obj == null)
65 throw new GboatViewException("附件的配置对象及其对应的附件列表的映射关系(Map)不能为空");
66
67 ObjectMapper om = JsonUtil.generateMapper();
68 ArrayNode array = om.createArrayNode();
69 if (obj instanceof Map) {
70 Set<Entry> entrySet = ((Map) obj).entrySet();
71 for (Entry entry : entrySet) {
72 ObjectNode node = om.createObjectNode();
73 node.put("attachConfig", om.valueToTree(entry.getKey()));
74 node.put("uploadeds", om.valueToTree(entry.getValue()));
75 array.add(node);
76 }
77 } else if ((obj instanceof String) && ((String) obj).startsWith("{")) {
78
79
80 String json = ((String) obj).replace('\'', '"');
81 ObjectNode root = JsonUtil.json2Object(json, ObjectNode.class);
82 ArrayNode types = root.withArray("types");
83 ArrayNode allUploadeds = root.withArray("uploadeds");
84 for (int i = 0; i < types.size(); i++) {
85 JsonNode attachConfig = types.get(i);
86 ArrayNode uploadeds = om.createArrayNode();
87 for (int j = 0; j < allUploadeds.size(); j++) {
88 JsonNode attach = allUploadeds.get(j);
89 if (attach.get("configId").asText().equals(attachConfig.get("configId").asText())) {
90 uploadeds.add(attach);
91 }
92 }
93
94 ObjectNode node = om.createObjectNode();
95 node.put("attachConfig", attachConfig);
96 node.put("uploadeds", uploadeds);
97 array.add(node);
98 }
99 } else {
100 throw new GboatViewException("附件上传组件的 attachMap 属性值类型不匹配,只支持 java.util.Map 和 java.lang.String");
101 }
102
103 addParameter("attachMap", obj);
104 addParameter("attachArray", array);
105
106 if(downloadUrl != null)
107 addParameter("downloadUrl", findString(downloadUrl));
108
109 }
110
111 @StrutsTagAttribute(description="附件的配置对象及其对应的附件列表的映射关系(Map), key 的类型为 gboat2.attachment.model.AttachConfig, value 的类型为 java.util.List<gboat2.attachment.model.Attachment>", required = true)
112 public void setAttachMap(String attachMap) {
113 this.attachMap = attachMap;
114 }
115
116 @StrutsTagAttribute(description = "下载附件的 URL", defaultValue = "upload!download.do")
117 public void setDownloadUrl(String downloadUrl) {
118 this.downloadUrl = downloadUrl;
119 }
120
121 }