001: package com.technoetic.xplanner.importer.spreadsheet;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.util.*;
006:
007: import com.technoetic.xplanner.importer.*;
008: import org.apache.poi.hssf.usermodel.*;
009: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
010: import org.apache.commons.lang.StringUtils;
011: import org.apache.log4j.Logger;
012:
013: /**
014: * Created by IntelliJ IDEA.
015: * User: Jacques
016: * Date: Dec 28, 2003
017: * Time: 5:23:42 PM
018: * To change this template use Options | File Templates.
019: */
020: public class SpreadsheetStoryReader implements CookbookFields {
021: private ArrayList stories = new ArrayList();
022: private Logger log = Logger.getLogger(SpreadsheetStoryReader.class);
023: private SpreadsheetStoryFactory spreadsheetStoryFactory;
024:
025: public SpreadsheetStoryReader(
026: SpreadsheetStoryFactory spreadsheetStoryFactory) {
027: this .spreadsheetStoryFactory = spreadsheetStoryFactory;
028: }
029:
030: public List readStories(
031: SpreadsheetHeaderConfiguration headerConfiguration,
032: InputStream input) throws IOException {
033: HSSFSheet sheet = getWorksheet(input, headerConfiguration
034: .getWorksheetName());
035: headerConfiguration.setWorksheet(sheet);
036: return readStories(headerConfiguration, sheet);
037: }
038:
039: public HSSFSheet getWorksheet(InputStream input,
040: String worksheetName) throws IOException {
041: POIFSFileSystem fs;
042: try {
043: fs = new POIFSFileSystem(input);
044: } catch (IOException e) {
045: throw new WrongImportFileSpreadsheetImporterException(
046: "Bad spreadsheet file", e);
047: }
048: HSSFWorkbook wb = new HSSFWorkbook(fs);
049: HSSFSheet sheet = wb.getSheet(worksheetName);
050: if (sheet == null) {
051: throw new MissingWorksheetException(worksheetName);
052: }
053:
054: return sheet;
055: }
056:
057: private List readStories(
058: SpreadsheetHeaderConfiguration headerConfiguration,
059: HSSFSheet sheet) {
060: Iterator it = new StoryRowIterator(headerConfiguration, sheet);
061: while (it.hasNext()) {
062: SpreadsheetStory spreadsheetStory = (SpreadsheetStory) it
063: .next();
064: stories.add(spreadsheetStory);
065: }
066: return stories;
067: }
068:
069: private class StoryRowIterator implements Iterator {
070: Iterator it;
071: SpreadsheetStory nextSpreadsheetStory;
072: SpreadsheetHeaderConfiguration headerConfiguration;
073:
074: public StoryRowIterator(
075: SpreadsheetHeaderConfiguration headerConfiguration,
076: HSSFSheet sheet) {
077: this .headerConfiguration = headerConfiguration;
078: it = sheet.rowIterator();
079: it.next();
080: }
081:
082: /**
083: * TODO: *WARNING* Do not call multiple time. It implements a look ahead that does not take this case into account
084: */
085: public boolean hasNext() {
086: boolean hasNext = it.hasNext();
087: if (!hasNext)
088: return false;
089: nextSpreadsheetStory = readRow((HSSFRow) it.next());
090: return nextSpreadsheetStory != null;
091: }
092:
093: private SpreadsheetStory readRow(HSSFRow row) {
094: try {
095: String title = getCellStringValue(row,
096: headerConfiguration.getStoryTitleColumnIndex());
097: int priority = getCellIntValue(row, headerConfiguration
098: .getStoryPriorityColumnIndex());
099: if (StringUtils.isEmpty(title) && priority == 0)
100: return null;
101: String status = getCellStringValue(row,
102: headerConfiguration.getStoryStatusColumnIndex());
103: double estimate = getCellDoubleValue(row,
104: headerConfiguration
105: .getStoryEstimateColumnIndex());
106: Date storyEndDate = getCellDateValue(row,
107: headerConfiguration
108: .getStoryEndDateColumnIndex());
109: return spreadsheetStoryFactory
110: .newInstance(storyEndDate, title, status,
111: estimate, priority);
112: } catch (RuntimeException e) {
113: log.error("Error while reading row " + row.getRowNum(),
114: e);
115: throw e;
116: }
117: }
118:
119: private String getCellStringValue(HSSFRow row, int column) {
120: HSSFCell cell = row.getCell((short) column);
121: if (cell == null)
122: return "";
123: return cell.getStringCellValue();
124: }
125:
126: private int getCellIntValue(HSSFRow row, int column) {
127: HSSFCell cell = row.getCell((short) column);
128: if (cell == null)
129: return 0;
130: return (int) cell.getNumericCellValue();
131: }
132:
133: private double getCellDoubleValue(HSSFRow row, int column) {
134: HSSFCell cell = row.getCell((short) column);
135: if (cell == null)
136: return 0;
137: return cell.getNumericCellValue();
138: }
139:
140: private Date getCellDateValue(HSSFRow row, int column) {
141: HSSFCell cell = row.getCell((short) column);
142: if (cell == null)
143: return null;
144: return cell.getDateCellValue();
145: }
146:
147: public Object next() {
148: return nextSpreadsheetStory;
149: }
150:
151: public void remove() {
152: }
153: }
154: }
|