1
2
3
4
5 package gboat2.report.action;
6
7 import gboat2.attachment.AttachmentHelper;
8 import gboat2.attachment.model.Attachment;
9 import gboat2.attachment.model.TypedAttachments;
10 import gboat2.base.core.annotation.Attach;
11 import gboat2.base.core.annotation.Domain;
12 import gboat2.base.core.annotation.Module;
13 import gboat2.base.core.annotation.Operation;
14 import gboat2.base.core.annotation.Operations;
15 import gboat2.base.core.annotation.Preference;
16 import gboat2.base.core.web.BaseActionSupport;
17 import gboat2.report.model.ReportConfig;
18 import gboat2.report.model.UrlConfig;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.Iterator;
29 import java.util.List;
30
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.struts2.convention.annotation.ResultPath;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @Domain(value = ReportConfig.class)
37 @ResultPath("/content")
38 @Preference("/系统配置/报表配置")
39 @Attach(code = "ATTACH_REPORT_CONFIG")
40 @Module(name = "报表配置管理", desc = "报表配置管理")
41 @Operations(value = { @Operation(name = "新增配置", code = "add", desc = "新增配置"), @Operation(name = "删除配置", code = "delete", desc = "删除配置"),
42 @Operation(name = "编辑配置", code = "edit", desc = "编辑配置"), @Operation(name = "查看配置", code = "view", desc = "查看配置") })
43 public class ReportConfigAction extends BaseActionSupport {
44
45 private static final long serialVersionUID = 1L;
46
47 private Logger logger = LoggerFactory.getLogger(ReportConfigAction.class.getName());
48
49
50
51
52 public final static String WEB_APP_ROOT = System.getProperty("webapp.gboat2.root");
53
54 private File file;
55
56 private String attachsJson = "[]";
57
58 private String[] attachids;
59
60 private List<UrlConfig> urllist = new ArrayList<UrlConfig>();
61
62 public File getFile() {
63 return file;
64 }
65
66 public void setFile(File file) {
67 this.file = file;
68 }
69
70 public void setAttachsJson(String attachsJson) {
71 this.attachsJson = attachsJson;
72 }
73
74 public String getAttachsJson() {
75 return attachsJson;
76 }
77
78 public void setAttachids(String[] attachids) {
79 this.attachids = attachids;
80 }
81
82 public String[] getAttachids() {
83 return attachids;
84 }
85
86 public void setUrllist(List<UrlConfig> urllist) {
87 this.urllist = urllist;
88 }
89
90 public List<UrlConfig> getUrllist() {
91 return urllist;
92 }
93
94
95
96
97
98
99
100 public String getAttachsJsonByBelongId(String belongId) {
101 String attachsJson = "";
102 Attach attach = ReportConfigAction.class.getAnnotation(Attach.class);
103 if (attach != null && StringUtils.isNotEmpty(attach.code()))
104 attachsJson = AttachmentHelper.findAttachsJsonBy(belongId, AttachmentHelper.getAvailableAttachTypeByCode(attach.code()));
105 return attachsJson;
106 }
107
108
109
110
111
112 private void InitData() {
113 ReportConfig report = (ReportConfig) getModel();
114
115 Attach attach = ReportConfigAction.class.getAnnotation(Attach.class);
116 String cfgCode = "";
117 if (attach != null) {
118 cfgCode = attach.code();
119 }
120
121 TypedAttachments attachs = AttachmentHelper.findAttachmentsBy(cfgCode, report.getReportId());
122
123 if (attachs.getUploadeds() != null) {
124 Iterator<Attachment> it = attachs.getUploadeds().iterator();
125 while (it.hasNext()) {
126 Attachment attache = it.next();
127 String OriginalName = attache.getOriginalName();
128 UrlConfig url = new UrlConfig();
129 url.setName(OriginalName);
130 url.setUrl("/Report/reportJsp/showReport.jsp?raq=/" + report.getFileName());
131 urllist.add(url);
132 }
133 }
134
135 attachsJson = getAttachsJsonByBelongId(report.getReportId());
136 }
137
138 @Override
139 protected void initEdit() {
140 InitData();
141
142 }
143
144 @Override
145 protected void initView() {
146 InitData();
147 }
148
149
150
151
152
153
154 protected void initSave() {
155 ReportConfig report = (ReportConfig) getModel();
156
157 if (StringUtils.isEmpty(report.getReportId())) {
158 report.setOperationTime(new Date());
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 }
199
200 protected void postSave() {
201 ReportConfig report = (ReportConfig) getModel();
202 String contextPath = request.getContextPath();
203 contextPath = contextPath.substring(1);
204
205
206 String web_app_root = WEB_APP_ROOT.substring(0, WEB_APP_ROOT.lastIndexOf(contextPath) - 1);
207 web_app_root = web_app_root + "/reportFileHome";
208
209 AttachmentHelper.updateAttachsBelongId(attachids, report.getReportId());
210 Attach attach = ReportConfigAction.class.getAnnotation(Attach.class);
211 String cfgCode = "";
212 if (attach != null) {
213 cfgCode = attach.code();
214 }
215
216 TypedAttachments attachs = AttachmentHelper.findAttachmentsBy(cfgCode, report.getReportId());
217
218 if (attachs.getUploadeds() != null) {
219 Iterator<Attachment> it = attachs.getUploadeds().iterator();
220 while (it.hasNext()) {
221 Attachment attache = it.next();
222 String OriginalName = attache.getOriginalName();
223 File file = AttachmentHelper.getAttachmentFile(attache);
224 File outFile = new File(web_app_root + "/" + OriginalName);
225 try {
226 InputStream inStream = new FileInputStream(file);
227 OutputStream outStream = new FileOutputStream(outFile);
228 byte[] b = new byte[1024];
229 int len = 0;
230 while ((len = inStream.read(b)) != -1) {
231 outStream.write(b, 0, len);
232 }
233 outStream.close();
234 inStream.close();
235 } catch (IOException e) {
236 logger.error("报表设计文件上传失败", e);
237 }
238 }
239 }
240 }
241
242
243
244
245
246
247
248 private void writeFileToDisk(File[] file, File outFile) {
249 try {
250 InputStream inStream = new FileInputStream(file[0]);
251 OutputStream outStream = new FileOutputStream(outFile);
252 byte[] b = new byte[1024];
253 int len = 0;
254 while ((len = inStream.read(b)) != -1) {
255 outStream.write(b, 0, len);
256 }
257 outStream.close();
258 inStream.close();
259 } catch (IOException e) {
260 logger.error("报表设计文件上传失败", e);
261 }
262 }
263
264
265
266
267
268
269 public String ajaxSave() {
270 String result = super.ajaxSave();
271 return result;
272 }
273
274
275
276
277
278
279 public String ajaxDelete() {
280 String result = super.ajaxDelete();
281
282 ReportConfig report = (ReportConfig) getModel();
283 File file = new File(report.getDesignFilePath());
284 file.delete();
285 return result;
286 }
287
288 }