Java tutorial
/* * All Rights Reserved. * Copyright (C) 2012 Tsukuba Bunko. * * Licensed under the BSD License ("the License"); you may not use * this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.tsukuba-bunko.org/licenses/LICENSE.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.tsukuba_bunko.lilac.helper.port.impl; import java.text.SimpleDateFormat; import java.util.Date; import javax.annotation.Resource; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.seasar.extension.jdbc.AutoSelect; import org.seasar.extension.jdbc.IterationCallback; import org.seasar.extension.jdbc.IterationContext; import org.seasar.extension.jdbc.JdbcManager; import org.seasar.framework.util.StringUtil; import org.tsukuba_bunko.lilac.entity.UserSession; import org.tsukuba_bunko.lilac.helper.auth.UserSessionHelper; import org.tsukuba_bunko.lilac.helper.port.ExportDataHelper; import org.tsukuba_bunko.lilac.service.UserSessionService; /** * {@link ExportDataHelper} * @author ppoi * @version 2012.04 */ public abstract class ExportDataHelperBase<ENTITY> implements ExportDataHelper, IterationCallback<ENTITY, Integer> { @Resource public UserSessionHelper userSessionHelper; @Resource public UserSessionService userSessionService; @Resource public JdbcManager jdbcManager; protected XSSFSheet sheet; protected XSSFCellStyle headerCellStyle; private int rowCount; /** * @see org.tsukuba_bunko.lilac.helper.port.ExportDataHelper#exportData(org.apache.poi.xssf.usermodel.XSSFWorkbook) */ @Override public void exportData(XSSFWorkbook book) { sheet = book.createSheet(getSheetName()); XSSFFont font = book.createFont(); font.setBold(true); font.setColor(IndexedColors.WHITE.index); headerCellStyle = book.createCellStyle(); headerCellStyle.setAlignment(HorizontalAlignment.CENTER); headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); headerCellStyle .setFillForegroundColor(new XSSFColor(new byte[] { (byte) 256, (byte) 0, (byte) 112, (byte) 192 })); headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); headerCellStyle.setFont(font); prepare(book); XSSFRow row = sheet.createRow(rowCount++); processHeaderRow(row); buildQuery().iterate(this); finish(book); } /** * @see org.seasar.extension.jdbc.IterationCallback#iterate(java.lang.Object, org.seasar.extension.jdbc.IterationContext) */ @Override public Integer iterate(ENTITY entity, IterationContext context) { XSSFRow row = sheet.createRow(rowCount); processRow(entity, row, rowCount); return ++rowCount; }; /** * ?????? * @return ?? */ protected abstract String getSheetName(); /** * ????? * @param book OOXML */ protected abstract void prepare(XSSFWorkbook book); /** * ??? * @return S2JDBC */ protected abstract AutoSelect<ENTITY> buildQuery(); /** * ???? * @param entity * @param row * @param index */ protected abstract void processRow(ENTITY entity, XSSFRow row, int index); protected abstract void processHeaderRow(XSSFRow row); /** * ???? * @param book OOXML */ protected abstract void finish(XSSFWorkbook book); /** * */ protected XSSFCell createHeaderCell(XSSFRow row, int index, String label) { XSSFCell cell = row.createCell(index); cell.setCellStyle(headerCellStyle); setCellValue(cell, label); return cell; } /** * ???? * @param row * @param index * @param style * @return */ protected XSSFCell createCell(XSSFRow row, int index, XSSFCellStyle style) { XSSFCell cell = row.createCell(index); if (style != null) { cell.setCellStyle(style); } return cell; } /** * ???? * @param cell * @param value * @return */ protected XSSFCell setCellValue(XSSFCell cell, String value) { if (value != null) { cell.setCellType(XSSFCell.CELL_TYPE_STRING); cell.setCellValue(value); } else { cell.setCellType(XSSFCell.CELL_TYPE_BLANK); } return cell; } /** * ???? * @param cell * @param value * @return */ protected XSSFCell setCellValue(XSSFCell cell, Integer value) { if (value != null) { cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(value); } else { cell.setCellType(XSSFCell.CELL_TYPE_BLANK); } return cell; } /** * ???? * @param cell * @param value * @return */ protected XSSFCell setCellValue(XSSFCell cell, Date value) { if (value != null) { cell.setCellType(XSSFCell.CELL_TYPE_STRING); cell.setCellValue(new SimpleDateFormat("yyyy/MM/dd").format(value)); } else { cell.setCellType(XSSFCell.CELL_TYPE_BLANK); } return cell; } protected String getCurrentSessionUser() { String sessionId = userSessionHelper.getSessionId(); if (StringUtil.isNotBlank(sessionId)) { UserSession session = userSessionService.getValidSession(sessionId); return session.username; } return null; } }