View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-6-20 下午02:59:53
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  	 * 获得webapproot
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  	 * 得到当前ID的附件json
97  	 * @param belongId
98  	 * @return
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 	 * TODO
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 	 * 设置未提交状态 (non-Javadoc)
151 	 * 
152 	 * @see gboat2.base.core.web.BaseActionSupport#initSave()
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 		//		String[] fileFileName = (String[]) params.get("fileFileName");
162 		//		report.setFileName(fileFileName[0]);
163 		//		report.setFileName(FileName);
164 		//		report.setDesignFilePath(web_app_root + "/" + report.getFileName());
165 		//
166 		//		report.setRealUrl("/Report/reportJsp/showReport.jsp?raq=/"
167 		//				+ report.getFileName());		
168 
169 		//
170 		//		File[] file = (File[]) params.get("file");
171 		//		String[] fileFileName = (String[]) params.get("fileFileName");
172 		//		// 如果是修改,没选择附件,则是默认为原来上传的附件
173 		//		if (file == null || file.length == 0)
174 		//			return;
175 		//
176 		//		report.setFileName(fileFileName[0]);
177 		//
178 		//		report.setDesignFilePath(web_app_root + "/" + report.getFileName());
179 		//
180 		//		report.setRealUrl("/Report/reportJsp/showReport.jsp?raq=/"
181 		//				+ report.getFileName());
182 		//
183 		//		// 把报表文件写到别处
184 		//		File designReport = new File(web_app_root);
185 		//		// 路径不存在,创建路径
186 		//		if (!designReport.exists()) {
187 		//			boolean mkdirs = designReport.mkdirs();
188 		//			if (!mkdirs) {
189 		//				throw new RuntimeException(" create directory  {"
190 		//						+ web_app_root + "}  fail ");
191 		//			}
192 		//		}
193 		//		designReport.setWritable(true);
194 		//		File outFile = new File(report.getDesignFilePath());
195 		//
196 		//		writeFileToDisk(file, outFile);
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 	 * @param file
246 	 * @param outFile
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 	 * desktop的Ajax保存处理
266 	 * 
267 	 * @return null
268 	 */
269 	public String ajaxSave() {
270 		String result = super.ajaxSave();
271 		return result;
272 	}
273 
274 	/**
275 	 * desktop的Ajax删除处理
276 	 * 
277 	 * @return null
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 }