001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.util;
018:
019: import info.jtrac.domain.AbstractItem;
020: import info.jtrac.domain.ColumnHeading;
021: import info.jtrac.domain.Field;
022: import info.jtrac.domain.History;
023: import info.jtrac.domain.ItemSearch;
024: import java.util.Date;
025: import java.util.List;
026: import org.apache.poi.hssf.usermodel.HSSFCell;
027: import org.apache.poi.hssf.usermodel.HSSFCellStyle;
028: import org.apache.poi.hssf.usermodel.HSSFDataFormat;
029: import org.apache.poi.hssf.usermodel.HSSFFont;
030: import org.apache.poi.hssf.usermodel.HSSFRow;
031: import org.apache.poi.hssf.usermodel.HSSFSheet;
032: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
033:
034: /**
035: * Excel Sheet generation helper
036: */
037: public class ExcelUtils {
038:
039: private HSSFSheet sheet;
040: private List<AbstractItem> items;
041: private ItemSearch itemSearch;
042: private HSSFCellStyle csBold;
043: private HSSFCellStyle csDate;
044: private HSSFWorkbook wb;
045:
046: public ExcelUtils(List items, ItemSearch itemSearch) {
047: this .wb = new HSSFWorkbook();
048: this .sheet = wb.createSheet("jtrac");
049: this .sheet.setDefaultColumnWidth((short) 12);
050:
051: HSSFFont fBold = wb.createFont();
052: fBold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
053: this .csBold = wb.createCellStyle();
054: this .csBold.setFont(fBold);
055:
056: this .csDate = wb.createCellStyle();
057: this .csDate.setDataFormat(HSSFDataFormat
058: .getBuiltinFormat("m/d/yy"));
059:
060: this .items = items;
061: this .itemSearch = itemSearch;
062: }
063:
064: private HSSFCell getCell(int row, int col) {
065: HSSFRow sheetRow = sheet.getRow(row);
066: if (sheetRow == null) {
067: sheetRow = sheet.createRow(row);
068: }
069: HSSFCell cell = sheetRow.getCell((short) col);
070: if (cell == null) {
071: cell = sheetRow.createCell((short) col);
072: }
073: return cell;
074: }
075:
076: private void setText(int row, int col, String text) {
077: HSSFCell cell = getCell(row, col);
078: cell.setCellType(HSSFCell.CELL_TYPE_STRING);
079: cell.setEncoding(HSSFCell.ENCODING_UTF_16);
080: cell.setCellValue(text);
081: }
082:
083: private void setDate(int row, int col, Date date) {
084: if (date == null) {
085: return;
086: }
087: HSSFCell cell = getCell(row, col);
088: cell.setCellValue(date);
089: cell.setCellStyle(csDate);
090: }
091:
092: private void setDouble(int row, int col, Double value) {
093: if (value == null) {
094: return;
095: }
096: HSSFCell cell = getCell(row, col);
097: cell.setCellValue(value);
098: }
099:
100: private void setHistoryIndex(int row, int col, int value) {
101: if (value == 0) {
102: return;
103: }
104: HSSFCell cell = getCell(row, col);
105: cell.setCellValue(value);
106: }
107:
108: private void setHeader(int row, int col, String text) {
109: HSSFCell cell = getCell(row, col);
110: cell.setCellStyle(csBold);
111: cell.setCellType(HSSFCell.CELL_TYPE_STRING);
112: cell.setEncoding(HSSFCell.ENCODING_UTF_16);
113: cell.setCellValue(text);
114: }
115:
116: public HSSFWorkbook exportToExcel() {
117:
118: boolean showDetail = itemSearch.isShowDetail();
119: boolean showHistory = itemSearch.isShowHistory();
120: List<ColumnHeading> columnHeadings = itemSearch
121: .getColumnHeadingsToRender();
122:
123: int row = 0;
124: int col = 0;
125:
126: // begin header row
127: for (ColumnHeading ch : columnHeadings) {
128: setHeader(row, col++, ch.getLabel());
129: }
130:
131: // iterate over list
132: for (AbstractItem item : items) {
133: row++;
134: col = 0;
135: for (ColumnHeading ch : columnHeadings) {
136: if (ch.isField()) {
137: Field field = ch.getField();
138: switch (field.getName().getType()) {
139: case 4: // double
140: setDouble(row, col++, (Double) item
141: .getValue(field.getName()));
142: break;
143: case 6: // date
144: setDate(row, col++, (Date) item.getValue(field
145: .getName()));
146: break;
147: default:
148: setText(row, col++, item.getCustomValue(field
149: .getName()));
150: }
151: } else {
152: // TODO optimize if-then for performance
153: String name = ch.getName();
154: if (name.equals(ColumnHeading.ID)) {
155: if (showHistory) {
156: int index = ((History) item).getIndex();
157: if (index > 0) {
158: setText(row, col++, item.getRefId()
159: + " (" + index + ")");
160: } else {
161: setText(row, col++, item.getRefId());
162: }
163: } else {
164: setText(row, col++, item.getRefId());
165: }
166: } else if (name.equals(ColumnHeading.SUMMARY)) {
167: setText(row, col++, item.getSummary());
168: } else if (name.equals(ColumnHeading.DETAIL)) {
169: if (showHistory) {
170: History h = (History) item;
171: if (h.getIndex() > 0) {
172: setText(row, col++, h.getComment());
173: } else {
174: setText(row, col++, h.getDetail());
175: }
176: } else {
177: setText(row, col++, item.getDetail());
178: }
179: } else if (name.equals(ColumnHeading.LOGGED_BY)) {
180: setText(row, col++, item.getLoggedBy()
181: .getName());
182: } else if (name.equals(ColumnHeading.STATUS)) {
183: setText(row, col++, item.getStatusValue());
184: } else if (name.equals(ColumnHeading.ASSIGNED_TO)) {
185: setText(row, col++,
186: (item.getAssignedTo() == null ? ""
187: : item.getAssignedTo()
188: .getName()));
189: } else if (name.equals(ColumnHeading.TIME_STAMP)) {
190: setDate(row, col++, item.getTimeStamp());
191: } else if (name.equals(ColumnHeading.SPACE)) {
192: setText(row, col++, item.getSpace().getName());
193: } else {
194: throw new RuntimeException("Unexpected name: '"
195: + name + "'");
196: }
197: }
198: }
199: }
200: return wb;
201: }
202:
203: }
|