PageRenderTime 135ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/be/mobila/im/importers/SampleExcelConverter.java

https://github.com/jaav/im
Java | 142 lines | 124 code | 11 blank | 7 comment | 41 complexity | 5f103ad4745433de75773a75ea9a0e41 MD5 | raw file
  1. package be.mobila.im.importers;
  2. import be.mobila.im.importers.exceptions.ConverterException;
  3. import be.mobila.im.models.Insurance;
  4. import be.mobila.im.utils.NameValuePair;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.apache.poi.hssf.extractor.ExcelExtractor;
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  8. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  9. import org.apache.poi.ss.usermodel.*;
  10. import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  12. import org.dom4j.Document;
  13. import org.dom4j.DocumentHelper;
  14. import org.dom4j.Element;
  15. import org.dom4j.io.XMLWriter;
  16. import java.io.*;
  17. import java.util.ArrayList;
  18. import java.util.Date;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. /**
  22. * Created by IntelliJ IDEA.
  23. * User: jefw
  24. * Date: Dec 1, 2010
  25. * Time: 3:11:18 PM
  26. * To change this template use File | Settings | File Templates.
  27. */
  28. public class SampleExcelConverter extends AbstractXMLStorageConverter implements NameValuePairConverter, Converter {
  29. private Sheet sheet;
  30. @Override
  31. public void convert(Insurance insurance){
  32. try {
  33. init(insurance);
  34. read(insurance);
  35. List<NameValuePair> properties = interpret();
  36. writeXml(properties, insurance);
  37. } catch (FileNotFoundException e) {
  38. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  39. } catch (IOException e) {
  40. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  41. } catch (InvalidFormatException e) {
  42. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  43. } catch (ConverterException e) {
  44. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  45. }
  46. }
  47. @Override
  48. public void read(Insurance insurance) throws IOException, InvalidFormatException, ConverterException {
  49. if(StringUtils.isBlank(insurance.getImportFile())) throw new ConverterException("The importFileName is undefined");
  50. InputStream inputStream = new FileInputStream(new File("import", insurance.getImportFile()));
  51. Workbook wb = WorkbookFactory.create(inputStream);
  52. String text;
  53. if(wb instanceof XSSFWorkbook){
  54. XSSFExcelExtractor extractor = new XSSFExcelExtractor((XSSFWorkbook)wb);
  55. extractor.setFormulasNotResults(true);
  56. extractor.setIncludeSheetNames(false);
  57. text = extractor.getText();
  58. String test = text;
  59. }
  60. else if(wb instanceof HSSFWorkbook){
  61. ExcelExtractor extractor = new ExcelExtractor((HSSFWorkbook)wb);
  62. extractor.setFormulasNotResults(true);
  63. extractor.setIncludeSheetNames(false);
  64. text = extractor.getText();
  65. String test = text;
  66. }
  67. sheet = wb.getSheetAt(0);
  68. }
  69. @Override
  70. public void writeXml(List<NameValuePair> properties, Insurance insurance) throws IOException {
  71. Document document = DocumentHelper.createDocument();
  72. Element root = document.addElement(insurance.getInsuranceType().toString());
  73. for (NameValuePair property : properties) {
  74. Element prop = root.addElement("property");
  75. Element name = prop.addElement("name").addText(property.getName());
  76. Element value = prop.addElement("value").addText(property.getValue().toString());
  77. if(property.getValue() instanceof String) value.addAttribute("type", STRING);
  78. else if(property.getValue() instanceof Boolean) value.addAttribute("type", BOOLEAN);
  79. else if(property.getValue() instanceof Double) value.addAttribute("type", NUMERIC);
  80. else if(property.getValue() instanceof Date) value.addAttribute("type", DATE);
  81. if(COMMENT.equals(property.getComment())) value.addAttribute("check", COMMENT);
  82. else if(PROPERTY.equals(property.getComment())) value.addAttribute("check", PROPERTY);
  83. else if(VARIABLE.equals(property.getComment())) value.addAttribute("check", VARIABLE);
  84. else if(FORMULA.equals(property.getComment())) value.addAttribute("check", FORMULA);
  85. }
  86. XMLWriter writer = new XMLWriter(new FileWriter(new File("export",insurance.getImportFile()+".xml")));
  87. writer.write( document );
  88. writer.close();
  89. }
  90. @Override
  91. public List<NameValuePair> interpret() throws ConverterException {
  92. List<NameValuePair> properties = new ArrayList<NameValuePair>();
  93. Iterator<Row> rows = sheet.iterator();
  94. while (rows.hasNext()) {
  95. Row row = rows.next();
  96. Iterator<Cell> cells = row.cellIterator();
  97. NameValuePair pair = new NameValuePair();
  98. while (cells.hasNext()) {
  99. Cell cell = cells.next();
  100. String value = cell.toString();
  101. if(cell.getCellType() != Cell.CELL_TYPE_BLANK){
  102. if(StringUtils.isBlank(pair.getName())){
  103. if(cell.getCellType()==Cell.CELL_TYPE_STRING) pair.setName(cell.getStringCellValue());
  104. else throw new ConverterException("The first cell of every row should be textual data (no numerics, dates, etc)");
  105. }
  106. else if(pair.getValue()==null){
  107. if(cell.getCellType()==Cell.CELL_TYPE_STRING) pair.setValue(cell.getStringCellValue());
  108. if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
  109. if(DateUtil.isCellDateFormatted(cell))
  110. pair.setValue(cell.getDateCellValue());
  111. else
  112. pair.setValue(cell.getNumericCellValue());
  113. }
  114. if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN) pair.setValue(cell.getBooleanCellValue());
  115. }
  116. else{
  117. if(cell.getCellType()==Cell.CELL_TYPE_STRING){
  118. if("comment".equalsIgnoreCase(cell.getStringCellValue())) pair.setComment(COMMENT);
  119. else if("variable".equalsIgnoreCase(cell.getStringCellValue())) pair.setComment(VARIABLE);
  120. else if("property".equalsIgnoreCase(cell.getStringCellValue())) pair.setComment(PROPERTY);
  121. else if("formula".equalsIgnoreCase(cell.getStringCellValue())) pair.setComment(FORMULA);
  122. }
  123. }
  124. }
  125. break;
  126. }
  127. if(pair.isValid()) properties.add(pair);
  128. }
  129. return properties;
  130. }
  131. }