View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.plugin.database;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  import java.util.Iterator;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.io.IOUtils;
26  import org.apache.poi.hssf.usermodel.HSSFCell;
27  import org.apache.poi.hssf.usermodel.HSSFCellStyle;
28  import org.apache.poi.hssf.usermodel.HSSFFont;
29  import org.apache.poi.hssf.usermodel.HSSFFooter;
30  import org.apache.poi.hssf.usermodel.HSSFHeader;
31  import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
32  import org.apache.poi.hssf.usermodel.HSSFRow;
33  import org.apache.poi.hssf.usermodel.HSSFSheet;
34  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
35  import org.apache.poi.hssf.util.HSSFColor;
36  import org.seasar.tuigwaa.cms.core.CmsRequest;
37  import org.seasar.tuigwaa.cms.core.CmsResponse;
38  import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
39  import org.seasar.tuigwaa.database.DataTable;
40  import org.seasar.tuigwaa.database.DataTableFactory;
41  import org.seasar.tuigwaa.database.SpreadSheet;
42  import org.seasar.tuigwaa.database.function.DaoMethod;
43  import org.seasar.tuigwaa.model.common.EntityDAO;
44  import org.seasar.tuigwaa.model.core.TgwEntity;
45  import org.seasar.tuigwaa.plugin.AbstractTgwPlugin;
46  import org.seasar.tuigwaa.plugin.PluginException;
47  import org.seasar.tuigwaa.plugin.PluginRequest;
48  import org.seasar.tuigwaa.system.Constants;
49  import org.seasar.tuigwaa.util.TgwContext;
50  import org.seasar.tuigwaa.view.wiki.ExcelDataViewVisitor;
51  
52  import com.isenshi.util.HtmlBuffer;
53  import com.isenshi.util.HttpUtils;
54  import com.isenshi.util.extlib.StrutsUtil;
55  
56  /***
57   * @author someda
58   */
59  public class ExcellinkPlugin extends AbstractTgwPlugin {
60  
61  	private static final String PARAM_ENTITY = "_PLUGIN.excellink.entity_";
62  
63  	private static final String PARAM_FILTER = "_PLUGIN.excellink.filter_";
64  
65  	private static final String FOOTER_LEFT = "Powered by "
66  			+ Constants.PRODUCT_NAME;
67  
68  	private static final String FOOTER_RIGHT = "Page " + HSSFFooter.page()
69  			+ " of " + HSSFFooter.numPages();
70  
71  	// private static final String DATA_FORMAT = "#,##0";
72  
73  	public String doHTMLView(CmsRequest request, CmsResponse response,
74  			PluginRequest prequest) throws PluginException {
75  
76  		String[] args = prequest.getArgs();
77  		if (args == null) {
78  			return null;
79  		}
80  
81  		String domain = request.getSiteName();
82  		TgwEntity entity = getEntity(domain, args[0]);
83  
84  		if (entity == null)
85  			throw new PluginException("entity not found");
86  
87  		request.setParameter(PARAM_ENTITY, args[0]);
88  
89  		if (args.length > 1 && !args[1].equals("")) {
90  			request.setParameter(PARAM_FILTER, args[1]);
91  		} else {
92  			request.setParameter(PARAM_FILTER, EntityDAO.LIST);
93  		}
94  		WikiContext ctx = getConfiguration().getWikiContext();
95  		HtmlBuffer buf = new HtmlBuffer();
96  		buf.appendAnchor(ctx.getPluginProxyURL(prequest.getName(), request)
97  				.toString(), "excellink");
98  
99  		return buf.toString();
100 	}
101 
102 	public String doAction(HttpServletRequest request,
103 			HttpServletResponse response) {
104 
105 		String siteName = TgwContext.getSiteName();
106 		String entityName = StrutsUtil.getURLDecodedParameter(request,
107 				PARAM_ENTITY);
108 		String filterName = StrutsUtil.getURLDecodedParameter(request,
109 				PARAM_FILTER);
110 
111 		DataTable table = null;
112 		TgwEntity entity = getEntity(siteName, entityName);
113 
114 		try {
115 
116 			if (entity == null) {
117 				return null;
118 			}
119 
120 			DaoMethod method = getEntityDAO(entity).getMethod(filterName);
121 			table = (DataTable) method.evaluate();
122 			table.setFilterMap(DataTableFactory.createFilterMap(entity,
123 					ExcelDataViewVisitor.INSTANCE));
124 
125 		} catch (Exception e) {
126 			e.printStackTrace();
127 			return null;
128 		}
129 
130 		// ----- write
131 		HttpUtils.setResponseHeader(request, response,
132 				"application/vnd.ms-excel", entityName + ".xls");
133 
134 		OutputStream os = null;
135 		try {
136 			ExcelDataTable excel = new ExcelDataTable();
137 			excel.addDataTable(entity, table);
138 
139 			os = response.getOutputStream();
140 			excel.write(os);
141 		} catch (IOException ioe) {
142 			ioe.printStackTrace();
143 		} finally {
144 			IOUtils.closeQuietly(os);
145 		}
146 
147 		return null;
148 	}
149 
150 	class ExcelDataTable {
151 
152 		private HSSFWorkbook workbook;
153 
154 		private HSSFCellStyle headerCellStyle;
155 
156 		private HSSFCellStyle dataCellStyle;
157 
158 		private int currentSheet = -1;
159 
160 		private int currentRowIndex = -1;
161 
162 		private int currentColumnIndex = -1;
163 
164 		public ExcelDataTable() {
165 			this.workbook = new HSSFWorkbook();
166 		}
167 
168 		public void setDataCellStyle(HSSFCellStyle dataCellStyle) {
169 			this.dataCellStyle = dataCellStyle;
170 		}
171 
172 		public HSSFCellStyle getDataCellStyle() {
173 			if (dataCellStyle == null) {
174 				createDefaultDataCellStyle();
175 			}
176 			return dataCellStyle;
177 		}
178 
179 		public void setHeaderCellStyle(HSSFCellStyle headerCellStyle) {
180 			this.headerCellStyle = headerCellStyle;
181 		}
182 
183 		public HSSFCellStyle getHeaderCellStyle() {
184 			if (headerCellStyle == null) {
185 				createDefaultHeaderCellStyle();
186 			}
187 			return headerCellStyle;
188 		}
189 
190 		public void addDataTable(TgwEntity entity, DataTable dataTable) {
191 			// create sheet
192 			HSSFSheet sheet = addSheet(dataTable.getName());
193 			setPrintSetup(sheet, entity.getName());
194 
195 			addTableHeaders(sheet, dataTable);
196 			addTableData(sheet, dataTable);
197 
198 		}
199 
200 		public void write(OutputStream outputStream) throws IOException {
201 			workbook.write(outputStream);
202 		}
203 
204 		private void addTableHeaders(HSSFSheet sheet, DataTable dataTable) {
205 			HSSFRow row = row = addRow(sheet);
206 			Iterator headerItr = dataTable.getHeaderIterator();
207 			if (dataTable.isSpreadSheet()) {
208 				addCell(row, getHeaderCellStyle(), " ");
209 			}
210 			while (headerItr.hasNext()) {
211 				String systemHeader = (String) headerItr.next();
212 				String displayHeader = (String) dataTable.getDisplayHeaderMap()
213 						.get(systemHeader);
214 				addCell(row, getHeaderCellStyle(), displayHeader);
215 			}
216 		}
217 
218 		private void addTableData(HSSFSheet sheet, DataTable table) {
219 			while (table.hasNext()) {
220 				HSSFRow row = addRow(sheet);
221 				Iterator columnItr = (Iterator) table.next();
222 
223 				if (table.isSpreadSheet()) {
224 					String rowHeader = ((SpreadSheet) table).getRowHeader();
225 					addCell(row, getHeaderCellStyle(), rowHeader);
226 				}
227 				while (columnItr.hasNext()) {
228 					Object data = columnItr.next();
229 					if (data != null) {
230 						addCell(row, getDataCellStyle(), String.valueOf(data
231 								.toString()));
232 					} else {
233 						addCell(row, getDataCellStyle(), "");
234 					}
235 				}
236 			}
237 		}
238 
239 		private HSSFSheet addSheet(String sheetName) {
240 			currentColumnIndex = -1;
241 			currentRowIndex = -1;
242 
243 			currentSheet++;
244 			// create sheet
245 			HSSFSheet sheet = workbook.createSheet();
246 
247 			// sheet name
248 			workbook.setSheetName(currentSheet, sheetName,
249 					HSSFWorkbook.ENCODING_UTF_16);
250 
251 			return sheet;
252 		}
253 
254 		private void setPrintSetup(HSSFSheet sheet, String displayName) {
255 			// sheet style
256 			HSSFPrintSetup setup = sheet.getPrintSetup();
257 			setup.setLandscape(true);
258 			setup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
259 
260 			// header and footer
261 			HSSFHeader header = sheet.getHeader();
262 			header.setCenter(displayName);
263 			HSSFFooter footer = sheet.getFooter();
264 			footer.setRight(FOOTER_RIGHT);
265 			footer.setLeft(FOOTER_LEFT);
266 		}
267 
268 		private HSSFRow addRow(HSSFSheet sheet) {
269 			currentColumnIndex = -1;
270 			currentRowIndex++;
271 			HSSFRow row = sheet.createRow(currentRowIndex);
272 			return row;
273 		}
274 
275 		private HSSFCell addCell(HSSFRow row, HSSFCellStyle style, String value) {
276 			currentColumnIndex++;
277 			HSSFCell cell = row.createCell((short) currentColumnIndex);
278 			// The order of setEncoding and setCellValue is important.
279 			// you should specify setEncoding before setCellValue.
280 			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
281 			cell.setCellValue(value);
282 			cell.setCellStyle(style);
283 
284 			return cell;
285 		}
286 
287 		private void createDefaultHeaderCellStyle() {
288 			headerCellStyle = workbook.createCellStyle();
289 			HSSFFont font = workbook.createFont();
290 			font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
291 			font.setColor(HSSFColor.WHITE.index);
292 			headerCellStyle.setFont(font);
293 			headerCellStyle
294 					.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
295 			headerCellStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
296 			headerCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
297 			headerCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
298 		}
299 
300 		private void createDefaultDataCellStyle() {
301 			dataCellStyle = workbook.createCellStyle();
302 			dataCellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
303 		}
304 	}
305 }