Example usage for org.apache.poi.hssf.usermodel HSSFSheet rowIterator

List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet rowIterator

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFSheet rowIterator.

Prototype

@Override
public Iterator<Row> rowIterator() 

Source Link

Usage

From source file:de.sub.goobi.forms.ProzessverwaltungForm.java

License:Open Source License

/**
 * Generate result as PDF./* w w  w  . j  a v a  2 s.  c  o m*/
 */
public void generateResultAsPdf() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    if (!facesContext.getResponseComplete()) {

        /*
         * Vorbereiten der Header-Informationen
         */
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        try {
            ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
            String contentType = servletContext.getMimeType("search.pdf");
            response.setContentType(contentType);
            response.setHeader("Content-Disposition", "attachment;filename=\"search.pdf\"");
            ServletOutputStream out = response.getOutputStream();

            SearchResultGeneration sr = new SearchResultGeneration(this.filter, this.showClosedProcesses,
                    this.showArchivedProjects);
            HSSFWorkbook wb = sr.getResult();
            List<List<HSSFCell>> rowList = new ArrayList<>();
            HSSFSheet mySheet = wb.getSheetAt(0);
            Iterator<Row> rowIter = mySheet.rowIterator();
            while (rowIter.hasNext()) {
                HSSFRow myRow = (HSSFRow) rowIter.next();
                Iterator<Cell> cellIter = myRow.cellIterator();
                List<HSSFCell> row = new ArrayList<>();
                while (cellIter.hasNext()) {
                    HSSFCell myCell = (HSSFCell) cellIter.next();
                    row.add(myCell);
                }
                rowList.add(row);
            }
            Document document = new Document();
            Rectangle a4quer = new Rectangle(PageSize.A3.getHeight(), PageSize.A3.getWidth());
            PdfWriter.getInstance(document, out);
            document.setPageSize(a4quer);
            document.open();
            if (rowList.size() > 0) {
                Paragraph p = new Paragraph(rowList.get(0).get(0).toString());
                document.add(p);
                PdfPTable table = new PdfPTable(9);
                table.setSpacingBefore(20);
                for (List<HSSFCell> row : rowList) {
                    for (HSSFCell hssfCell : row) {
                        // TODO aufhbschen und nicht toString() nutzen
                        String stringCellValue = hssfCell.toString();
                        table.addCell(stringCellValue);
                    }
                }
                document.add(table);
            }

            document.close();
            out.flush();
            facesContext.responseComplete();
        } catch (Exception e) {
            logger.error(e);
        }
    }
}

From source file:domain.Excel.java

public static void leerArchivoSesion(String archivoSesion) {

        List sheetData = new ArrayList();

        FileInputStream fis = null;

        try {/*from ww w .j av  a 2  s  .c o m*/

            fis = new FileInputStream(archivoSesion);

            HSSFWorkbook workbook = new HSSFWorkbook(fis);
            HSSFSheet sheet = workbook.getSheetAt(0);

            Iterator rows = sheet.rowIterator();

            while (rows.hasNext()) {

                HSSFRow row = (HSSFRow) rows.next();

                Iterator cells = row.cellIterator();

                List data = new ArrayList();

                while (cells.hasNext()) {

                    HSSFCell cell = (HSSFCell) cells.next();
                    data.add(cell);
                }

                sheetData.add(data);

            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (fis != null) {
                try {
                    fis.close();

                } catch (IOException ex) {
                    Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        showExelData(sheetData);
    }

From source file:edu.duke.cabig.c3pr.web.study.tabs.StudyEligibilityChecklistTab.java

License:BSD License

public void parseCadsrFile(Study study, POIFSFileSystem pfs, String name) {

    List<InclusionEligibilityCriteria> incList = null;
    List<ExclusionEligibilityCriteria> excList = null;
    if (study.getEpochByName(name) != null) {
        incList = study.getEpochByName(name).getInclusionEligibilityCriteria();
        excList = study.getEpochByName(name).getExclusionEligibilityCriteria();
    } else {/*from   w ww  . ja v  a2 s  .  c  om*/
        return;
    }
    try {
        HSSFWorkbook wb = new HSSFWorkbook(pfs);
        HSSFSheet sheet = wb.getSheetAt(0);

        HSSFRow currentRow;
        HSSFCell currentCell;
        InclusionEligibilityCriteria inc = null;
        ExclusionEligibilityCriteria exc = null;

        Iterator rowIter = sheet.rowIterator();
        // iterating over the entire file
        while (rowIter.hasNext()) {
            currentRow = (HSSFRow) rowIter.next();

            // inclusion section
            if (currentRow.getCell((short) 0) != null
                    && currentRow.getCell((short) 0).getCellType() == HSSFCell.CELL_TYPE_STRING && currentRow
                            .getCell((short) 0).getRichStringCellValue().toString().startsWith(INCLUSION)) {
                // iterating over the inclusion rows
                HSSFRow innerCurrentRow;
                while (rowIter.hasNext()) {
                    innerCurrentRow = (HSSFRow) rowIter.next();
                    if (innerCurrentRow.getCell((short) 0) != null
                            && innerCurrentRow.getCell((short) 0).getCellType() == HSSFCell.CELL_TYPE_STRING
                            && innerCurrentRow.getCell((short) 0).getRichStringCellValue().toString()
                                    .startsWith(EXCLUSION)) {
                        currentRow = innerCurrentRow;
                        break;
                    }
                    currentCell = innerCurrentRow.getCell((short) 3);
                    if (currentCell != null && currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING
                            && currentCell.getRichStringCellValue().toString().length() > 0) {
                        // create the new inc and populate the list
                        if (inc != null) {
                            incList.add(inc);
                        }
                        inc = new InclusionEligibilityCriteria();
                        inc.setQuestionText(currentCell.getRichStringCellValue().toString());
                    } else {
                        // get the answers
                        currentCell = innerCurrentRow.getCell((short) 15);
                        if (currentCell != null && currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING
                                && currentCell.getRichStringCellValue().toString()
                                        .equalsIgnoreCase(NOT_APPLICABLE)) {
                            if (inc != null) {
                                inc.setNotApplicableIndicator(true);
                            }
                        }
                    }
                }
                // adding the last criteria to the list
                incList.add(inc);
            } // end of inclusion if

            // exclusion section
            if (currentRow.getCell((short) 0) != null
                    && currentRow.getCell((short) 0).getCellType() == HSSFCell.CELL_TYPE_STRING && currentRow
                            .getCell((short) 0).getRichStringCellValue().toString().startsWith(EXCLUSION)) {
                // iterating over the exclusion rows
                HSSFRow innerCurrentRow;
                while (rowIter.hasNext()) {
                    innerCurrentRow = (HSSFRow) rowIter.next();
                    // This if loop is not needed as we only have 1 inc section and 1 exc
                    // section
                    // however leaving it in place as it is harmless and will be useful if we
                    // ever have repeated inc/exc sections in the input file
                    if (innerCurrentRow.getCell((short) 0) != null
                            && innerCurrentRow.getCell((short) 0).getCellType() == HSSFCell.CELL_TYPE_STRING
                            && innerCurrentRow.getCell((short) 0).getRichStringCellValue().toString()
                                    .startsWith(INCLUSION)) {
                        currentRow = innerCurrentRow;
                        break;
                    }
                    currentCell = innerCurrentRow.getCell((short) 3);
                    if (currentCell != null && currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING
                            && currentCell.getRichStringCellValue().toString().length() > 0) {
                        // create the new exc and populate the list
                        if (exc != null) {
                            excList.add(exc);
                        }
                        exc = new ExclusionEligibilityCriteria();
                        exc.setQuestionText(currentCell.getRichStringCellValue().toString());
                    } else {
                        // get the answers
                        currentCell = innerCurrentRow.getCell((short) 15);
                        if (currentCell != null && currentCell.getRichStringCellValue() != null
                                && currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING && currentCell
                                        .getRichStringCellValue().toString().equalsIgnoreCase(NOT_APPLICABLE)) {
                            if (exc != null) {
                                exc.setNotApplicableIndicator(true);
                            }
                        }
                    }
                }
                // adding the last criteria to the list
                excList.add(exc);
            } // end of exclusion if
        } // end if while loop that iterates over the entire file.
    } catch (IOException ioe) {
        log.error(ioe.getMessage());
    }

}

From source file:edu.ku.brc.specify.tasks.subpane.wb.ConfigureXLS.java

License:Open Source License

/**
 * Fills badHeads with indexes for columns that contain data but don't have a header or have an non-string header (because it makes things difficult with HSSF).
 * Fills emptyCols with indexes for columns that are totally empty.
 * Assumes that badHeads and emptyCols are not null and empty.
 * /*from   w w  w .  j a va2 s . c om*/
 */
public void checkHeadsAndCols(final HSSFSheet sheet, Vector<Integer> badHeads, Vector<Integer> emptyCols) {
    boolean firstRow = true;
    Vector<Boolean> firstRowCells = new Vector<Boolean>();
    Vector<Boolean> restCells = new Vector<Boolean>();

    // Iterate over each row in the sheet
    Iterator<?> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        HSSFRow row = (HSSFRow) rows.next();
        int maxSize = Math.max(row.getPhysicalNumberOfCells(), row.getLastCellNum());
        for (int col = 0; col < maxSize; col++) {
            if (firstRow) {
                if (row.getCell(col) == null) {
                    firstRowCells.add(false);
                } else if (row.getCell(col).getCellType() == HSSFCell.CELL_TYPE_STRING) {
                    firstRowCells.add(true);
                } else {
                    firstRowCells.add(null);
                }
            } else {
                if (col == restCells.size()) {
                    restCells.add(false);
                }
                if (!restCells.get(col)) {
                    restCells.set(col, row.getCell(col) != null);
                }
            }
        }
        firstRow = false;
    }

    //pad smaller vector with false if necessary.
    while (restCells.size() < firstRowCells.size()) {
        restCells.add(false);
    }
    while (firstRowCells.size() < restCells.size()) {
        firstRowCells.add(false);
    }

    for (int c = 0; c < firstRowCells.size(); c++) {
        if (firstRowCells.get(c) == null || (!firstRowCells.get(c) && restCells.get(c))) {
            badHeads.add(c);
        }
        if (firstRowCells.get(c) != null && !firstRowCells.get(c) && !restCells.get(c)) {
            emptyCols.add(c);
        }
    }
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.ConfigureXLS.java

License:Open Source License

@Override
protected void nonInteractiveConfig() {
    try {//from ww w  .  ja  va  2  s. c o m
        InputStream input = new FileInputStream(externalFile);
        POIFSFileSystem fs = new POIFSFileSystem(input);
        HSSFWorkbook workBook = new HSSFWorkbook(fs);
        HSSFSheet sheet = workBook.getSheetAt(0);

        // Calculate the number of rows and columns
        colInfo = new Vector<ImportColumnInfo>(16);

        Hashtable<Integer, Boolean> colTracker = new Hashtable<Integer, Boolean>();

        boolean firstRow = true;
        int col = 0;
        colTracker.clear();

        Vector<Integer> badHeads = new Vector<Integer>();
        Vector<Integer> emptyCols = new Vector<Integer>();
        checkHeadsAndCols(sheet, badHeads, emptyCols);

        if (firstRowHasHeaders && badHeads.size() > 0) {
            status = ConfigureExternalDataIFace.Status.Error;
            showBadHeadingsMsg(badHeads, null, getResourceString("Error"));
            return;
        }

        // Iterate over each row in the sheet
        @SuppressWarnings("unchecked")
        Iterator<HSSFRow> rows = sheet.rowIterator();
        while (rows.hasNext()) {
            HSSFRow row = rows.next();
            if (firstRow || numRows == 1) {
                // Iterate over each cell in the row and print out the cell's content
                int colNum = 0;
                int maxSize = Math.max(row.getPhysicalNumberOfCells(), row.getLastCellNum());
                while (colNum < maxSize) {
                    if (emptyCols.indexOf(new Integer(colNum)) == -1) {
                        ImportColumnInfo.ColumnType disciplinee = ImportColumnInfo.ColumnType.Integer;
                        String value = null;
                        boolean skip = false;
                        HSSFCell cell = row.getCell(colNum);
                        if (cell == null) {
                            //assuming numRows == 1 or not firstRowHasHeaders.
                            //the call to checkHeadsAndCols would have already blank headers.
                            value = "";
                            disciplinee = ImportColumnInfo.ColumnType.String;
                        } else
                            switch (cell.getCellType()) {
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                double numeric = cell.getNumericCellValue();
                                value = Double.toString(numeric);
                                disciplinee = ImportColumnInfo.ColumnType.Double;
                                break;
                            case HSSFCell.CELL_TYPE_STRING:
                                HSSFRichTextString richVal = cell.getRichStringCellValue();
                                value = richVal.getString().trim();
                                disciplinee = ImportColumnInfo.ColumnType.String;
                                break;
                            case HSSFCell.CELL_TYPE_BLANK:
                                value = "";
                                disciplinee = ImportColumnInfo.ColumnType.String;
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                boolean bool = cell.getBooleanCellValue();
                                value = Boolean.toString(bool);
                                disciplinee = ImportColumnInfo.ColumnType.Boolean;
                                break;
                            default:
                                skip = true;
                                break;
                            }

                        if (numRows == 1 && !skip) {
                            colInfo.get(col).setData(value);
                            col++;
                        } else if (!skip) {
                            if (firstRowHasHeaders) {
                                colInfo.add(new ImportColumnInfo(colNum, disciplinee, value, value, null, null,
                                        null));
                                colTracker.put(col, true);
                            } else {
                                String colName = getResourceString("DEFAULT_COLUMN_NAME") + " " + (colNum + 1);
                                colInfo.add(new ImportColumnInfo(colNum, disciplinee, colName, colName, null,
                                        null, null));
                                colTracker.put(colNum, true);
                            }
                            numCols++;
                        }
                    }
                    colNum++;
                }
                firstRow = false;
            }
            numRows++;
        }
        Collections.sort(colInfo);
        readMappings(fs);
        status = Status.Valid;
    } catch (IOException ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ConfigureXLS.class, ex);
        status = Status.Error;
    }
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.DataImportDialog.java

License:Open Source License

/**
 * Parses the given import xls file according to the users selection and creates/updates the
 * Preview table, showing the user how the import options effect the way the data will be
 * imported into the spreadsheet.//  w  w w  .  j  a  v  a2 s  . c o  m
 * 
 * @param table - the table to display the data
 * @return JTable - the table to display the data
 */
private JTable setXLSTableData(final JTable table) {
    int numRows = 0;
    int numCols = 0;
    String[] headers = {};
    Vector<Vector<String>> tableDataVector = new Vector<Vector<String>>();
    Vector<String> rowData = new Vector<String>();
    Vector<String> headerVector = new Vector<String>();
    DateWrapper scrDateFormat = AppPrefsCache.getDateWrapper("ui", "formatting", "scrdateformat");
    try {
        log.debug("setXLSTableData - file - " + configXLS.getFile().toString());

        InputStream input = new FileInputStream(configXLS.getFile());
        POIFSFileSystem fs = new POIFSFileSystem(input);
        HSSFWorkbook workBook = new HSSFWorkbook(fs);
        HSSFSheet sheet = workBook.getSheetAt(0);

        Vector<Integer> badHeads = new Vector<Integer>();
        Vector<Integer> emptyCols = new Vector<Integer>();
        ((ConfigureXLS) config).checkHeadsAndCols(sheet, badHeads, emptyCols);
        if (badHeads.size() > 0 && doesFirstRowHaveHeaders) {
            if (table != null) {
                ((ConfigureXLS) config).showBadHeadingsMsg(badHeads, emptyCols, getTitle());
            }
            this.doesFirstRowHaveHeaders = false;
            try {
                ignoreActions = true;
                this.containsHeaders.setSelected(false);
            } finally {
                ignoreActions = false;
            }
            if (table != null) {
                return table;
            }
        }
        boolean firstRow = true;

        //quick fix to prevent ".0" at end of catalog numbers etc
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumFractionDigits(0);
        nf.setMaximumFractionDigits(20);
        nf.setGroupingUsed(false); //gets rid of commas

        int maxCols = 0;

        // Iterate over each row in the sheet
        Iterator<?> rows = sheet.rowIterator();
        while (rows.hasNext()) {
            numCols = 0;
            rowData = new Vector<String>();
            HSSFRow row = (HSSFRow) rows.next();
            //log.debug(row.getLastCellNum()+"  "+row.getPhysicalNumberOfCells());
            int maxSize = Math.max(row.getPhysicalNumberOfCells(), row.getLastCellNum());
            if (maxSize > maxCols) {
                maxCols = maxSize;
            }
            while (numCols < maxSize) {
                if (emptyCols.indexOf(new Integer(numCols)) == -1) {
                    HSSFCell cell = row.getCell(numCols);
                    String value = null;
                    // if cell is blank, set value to ""
                    if (cell == null) {
                        value = "";
                    } else {
                        int type = cell.getCellType();

                        switch (type) {
                        case HSSFCell.CELL_TYPE_NUMERIC:
                            // The best I can do at this point in the app is to guess if a
                            // cell is a date.
                            // Handle dates carefully while using HSSF. Excel stores all
                            // dates as numbers, internally.
                            // The only way to distinguish a date is by the formatting of
                            // the cell. (If you
                            // have ever formatted a cell containing a date in Excel, you
                            // will know what I mean.)
                            // Therefore, for a cell containing a date, cell.getCellType()
                            // will return
                            // HSSFCell.CELL_TYPE_NUMERIC. However, you can use a utility
                            // function,
                            // HSSFDateUtil.isCellDateFormatted(cell), to check if the cell
                            // can be a date.
                            // This function checks the format against a few internal
                            // formats to decide the issue,
                            // but by its very nature it is prone to false negatives.
                            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                value = scrDateFormat.getSimpleDateFormat().format(cell.getDateCellValue());
                                //value = scrDateFormat.getSimpleDateFormat().format(cell.getDateCellValue());
                            } else {
                                double numeric = cell.getNumericCellValue();
                                value = nf.format(numeric);
                            }
                            break;

                        case HSSFCell.CELL_TYPE_STRING:
                            value = cell.getRichStringCellValue().getString();
                            break;

                        case HSSFCell.CELL_TYPE_BLANK:
                            value = "";
                            break;

                        case HSSFCell.CELL_TYPE_BOOLEAN:
                            value = Boolean.toString(cell.getBooleanCellValue());
                            break;

                        case HSSFCell.CELL_TYPE_FORMULA:
                            value = UIRegistry.getResourceString("WB_FORMULA_IMPORT_NO_PREVIEW");
                            break;

                        default:
                            value = "";
                            log.error("unsuported cell type");
                            break;
                        }
                    }
                    if (firstRow && doesFirstRowHaveHeaders) {
                        checkUserColInfo(value, numCols);
                    }
                    if (isUserCol(numCols)) {
                        rowData.add(value.toString());
                    }
                }
                numCols++;
            }
            if (doesFirstRowHaveHeaders && firstRow) {
                headerVector = rowData;
                headers = new String[rowData.size()];
            } else if (!doesFirstRowHaveHeaders && firstRow) {
                //headers = createDummyHeaders(rowData.size());
                tableDataVector.add(rowData);
            } else {
                tableDataVector.add(rowData);
            }
            firstRow = false;
            numRows++;
        }
        maxCols -= emptyCols.size();
        if (!doesFirstRowHaveHeaders) {
            headerVector = createDummyHeadersAsVector(maxCols);
            headers = new String[maxCols];
        }
        for (int i = 0; i < headerVector.size(); i++) {
            headers[i] = headerVector.elementAt(i);
        }
        printArray(headers);

        String[][] tableData = new String[tableDataVector.size()][maxCols];
        for (int i = 0; i < tableDataVector.size(); i++) {
            Vector<String> v = tableDataVector.get(i);
            for (int j = 0; j < v.size(); j++) {
                tableData[i][j] = v.get(j).toString();
            }

        }
        if (checkForErrors(headers, tableData)) {
            errorPanel.showDataImportStatusPanel(true);
        } else {
            errorPanel.showDataImportStatusPanel(false);
        }

        if ((doesFirstRowHaveHeaders ? numRows - 1 : numRows) > WorkbenchTask.MAX_ROWS) {
            hasTooManyRows = true;
            showTooManyRowsErrorDialog();
        } else {
            hasTooManyRows = false;
        }
        log.debug(headers);
        log.debug(tableData);
        model = new PreviewTableModel(headers, tableData);
        JTable result = null;
        if (table == null) {
            result = new JTable();
            result.setColumnSelectionAllowed(false);
            result.setRowSelectionAllowed(false);
            result.setCellSelectionEnabled(false);
            result.getTableHeader().setReorderingAllowed(false);
            result.setPreferredScrollableViewportSize(new Dimension(500, 100));
            result.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        } else {
            result = table;
        }
        result.setModel(model);
        result.setDefaultRenderer(String.class, new BiColorTableCellRenderer(false));
        model.fireTableDataChanged();
        model.fireTableStructureChanged();
        return result;
    } catch (Exception ex) {
        UIRegistry.displayErrorDlgLocalized(UIRegistry.getResourceString("WB_ERROR_READING_IMPORT_FILE"));
        if (table != null) {
            String[] columnNames = {};
            String[][] blankData = { {} };
            model = new PreviewTableModel(columnNames, blankData);
            table.setModel(model);
            table.setColumnSelectionAllowed(false);
            table.setRowSelectionAllowed(false);
            table.setCellSelectionEnabled(false);
            table.getTableHeader().setReorderingAllowed(false);
            table.setPreferredScrollableViewportSize(new Dimension(500, 100));
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            table.setDefaultRenderer(String.class, new BiColorTableCellRenderer(false));
            model.fireTableDataChanged();
            model.fireTableStructureChanged();
            return table;
        }
        //log.error("Error attempting to parse input xls file:" + ex);
        //ex.printStackTrace();
    }

    return null;
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.XLSImport.java

License:Open Source License

public DataImportIFace.Status getData(final Workbench workbench) {
    if (config.getStatus() == ConfigureExternalDataIFace.Status.Valid) {
        DateWrapper scrDateFormat = AppPrefsCache.getDateWrapper("ui", "formatting", "scrdateformat");
        try {//w ww  .  ja  va2 s  .  com
            InputStream input = new FileInputStream(config.getFile());
            POIFSFileSystem fs = new POIFSFileSystem(input);
            HSSFWorkbook workBook = new HSSFWorkbook(fs);
            HSSFSheet sheet = workBook.getSheetAt(0);
            int numRows = 0;

            // Calculate the number of rows and columns

            Set<WorkbenchTemplateMappingItem> wbtmiSet = workbench.getWorkbenchTemplate()
                    .getWorkbenchTemplateMappingItems();
            Vector<WorkbenchTemplateMappingItem> wbtmiList = new Vector<WorkbenchTemplateMappingItem>();
            NumberFormat nf = NumberFormat.getInstance();
            nf.setMinimumFractionDigits(0);
            nf.setMaximumFractionDigits(20);
            nf.setGroupingUsed(false); //gets rid of commas
            NumberFormat nfGeoCoord = NumberFormat.getInstance();
            nfGeoCoord.setMinimumFractionDigits(0);
            nfGeoCoord.setMaximumFractionDigits(
                    LatLonConverter.DECIMAL_SIZES[LatLonConverter.FORMAT.DDDDDD.ordinal()]);
            nfGeoCoord.setGroupingUsed(false); //gets rid of commas
            char decSep = new DecimalFormatSymbols().getDecimalSeparator();
            wbtmiList.addAll(wbtmiSet);

            Collections.sort(wbtmiList);

            this.truncations.clear();
            Vector<HSSFHyperlink> activeHyperlinks = new Vector<HSSFHyperlink>();

            // Iterate over each row in the sheet
            Iterator<?> rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                if (numRows == 0 && config.getFirstRowHasHeaders()) {
                    numRows++;
                    getSystemCols(row);
                    continue;
                }

                WorkbenchRow wbRow = workbench.addRow();

                for (WorkbenchTemplateMappingItem wbtmi : wbtmiList) {
                    int cellNum = wbtmi.getOrigImportColumnIndex().intValue();
                    if (cellNum == -1) {
                        if (wbtmi.getViewOrder() != null) {
                            cellNum = wbtmi.getViewOrder().intValue();
                            if (cellNum == -1) {
                                continue;
                            }
                        }
                    }
                    HSSFCell cell = row.getCell(cellNum);
                    if (cell == null) {
                        continue;
                    }
                    int type = cell.getCellType();
                    if (type == HSSFCell.CELL_TYPE_FORMULA) {
                        type = cell.getCachedFormulaResultType();
                    }
                    String value = "";
                    boolean skip = false;

                    switch (type) {
                    case HSSFCell.CELL_TYPE_NUMERIC: {
                        if (HSSFDateUtil.isCellDateFormatted(cell)) {
                            //even if WorkbenchTask.getDataType(wbtmi) is not Calendar or Date. Hmmmm.
                            value = scrDateFormat.getSimpleDateFormat().format(cell.getDateCellValue());
                        } else {
                            Class<?> classObj = WorkbenchTask.getDataType(wbtmi);
                            if (classObj.equals(Integer.class)) {
                                double numeric = cell.getNumericCellValue();
                                value = Integer.toString((int) numeric);

                            } else if (classObj.equals(Calendar.class) || classObj.equals(Date.class)) {
                                Date d = cell.getDateCellValue();
                                if (d != null) {
                                    value = scrDateFormat.getSimpleDateFormat().format(cell.getDateCellValue());
                                } else {
                                    value = null;
                                }
                            } else {
                                double numeric = cell.getNumericCellValue();
                                value = nf.format(numeric);
                                if (isGeoCoordinate(wbtmi)) {
                                    int sepInx = value.indexOf(decSep);
                                    if (sepInx > -1 && value.substring(sepInx).length() > nfGeoCoord
                                            .getMaximumFractionDigits()) {
                                        String value2 = nfGeoCoord.format(numeric);
                                        int maxlen = wbtmi.getFieldName().startsWith("latitude")
                                                ? nfGeoCoord.getMaximumFractionDigits() + 3
                                                : nfGeoCoord.getMaximumFractionDigits() + 4;
                                        if (numeric < 0) {
                                            maxlen++;
                                        }
                                        //System.out.println(value + " " + trackTrunc(value, numRows, wbtmi.getViewOrder(), wbtmi.getCaption(), 
                                        //      maxlen) + " " + value2);
                                        value = value2;
                                    }
                                }
                            }
                        }
                        break;
                    }

                    case HSSFCell.CELL_TYPE_STRING:
                        HSSFHyperlink hl = checkHyperlinks(cell, activeHyperlinks);
                        if (hl == null /*|| (hl != null && hl.getType() == HSSFHyperlink.LINK_EMAIL)*/) {
                            value = cell.getRichStringCellValue().getString();
                        } else {
                            //value = hl.getAddress();
                            value = hl.getLabel();
                        }
                        break;

                    case HSSFCell.CELL_TYPE_BLANK:
                        value = "";
                        type = HSSFCell.CELL_TYPE_STRING;
                        break;

                    case HSSFCell.CELL_TYPE_BOOLEAN:
                        boolean bool = cell.getBooleanCellValue();
                        value = Boolean.toString(bool);
                        break;

                    default:
                        skip = true;
                        break;
                    }

                    if (!skip && value != null && !value.trim().equals("")) {
                        wbRow.setData(truncateIfNecessary(value, numRows, wbtmi), wbtmi.getViewOrder(), true);
                    }
                }
                addImageInfo(row, wbRow);
                addGeoInfo(row, wbRow);
                numRows++;
            }
            if (activeHyperlinks.size() > 0) {
                log.warn("Hyperlinks vector not empty after import. Overlapping hyperlink ranges?");
            }
            return status = this.truncations.size() == 0 && this.messages.size() == 0
                    ? DataImportIFace.Status.Valid
                    : DataImportIFace.Status.Modified;
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(XLSImport.class, ex);
            log.error(ex);
        }
    }
    return status = DataImportIFace.Status.Error;
}

From source file:edu.ku.brc.specify.utilapps.BuildSampleDatabase.java

License:Open Source License

/**
 * @param treeDef//from w  ww  .jav a2  s.com
 * @return
 */
public Geography convertGeographyFromXLS(final GeographyTreeDef treeDef) {
    frame.setDesc("Building Geography Tree...");

    Hashtable<String, Geography> geoHash = new Hashtable<String, Geography>();

    geoHash.clear();

    String fileName = "Geography.xls";
    File file = XMLHelper.getConfigDir("../demo_files/" + fileName);
    if (!file.exists()) {
        log.error("Couldn't file[" + file.getAbsolutePath() + "] checking the config dir");
        file = XMLHelper.getConfigDir(fileName);
        if (!file.exists()) {
            file = new File("Specify/demo_files/" + fileName);
        }
    }

    if (file == null || !file.exists()) {
        log.error("Couldn't file[" + file.getAbsolutePath() + "]");
        return null;
    }

    // setup the root Geography record (planet Earth)
    Geography earth = new Geography();
    earth.initialize();
    earth.setName(getResourceString("Earth"));
    earth.setFullName(earth.getName());
    earth.setNodeNumber(1);
    earth.setHighestChildNodeNumber(1);
    earth.setRankId(0);
    earth.setDefinition(treeDef);
    GeographyTreeDefItem defItem = treeDef.getDefItemByRank(0);
    earth.setDefinitionItem(defItem);

    int counter = 0;

    try {
        startTx();

        persist(earth);

        String[] cells = new String[4];
        InputStream input = new FileInputStream(file);
        POIFSFileSystem fs = new POIFSFileSystem(input);
        HSSFWorkbook workBook = new HSSFWorkbook(fs);
        HSSFSheet sheet = workBook.getSheetAt(0);
        Iterator<?> rows = sheet.rowIterator();

        int lastRowNum = sheet.getLastRowNum();
        if (frame != null) {
            final int mx = lastRowNum;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    frame.setProcess(0, mx);
                }
            });
        }

        while (rows.hasNext()) {
            if (counter == 0) {
                counter = 1;
                rows.next();
                continue;
            }
            if (counter % 100 == 0) {
                if (frame != null)
                    frame.setProcess(counter);
                log.info("Converted " + counter + " Geography records");
            }

            HSSFRow row = (HSSFRow) rows.next();
            Iterator<?> cellsIter = row.cellIterator();
            int i = 0;
            while (cellsIter.hasNext() && i < 4) {
                HSSFCell cell = (HSSFCell) cellsIter.next();
                if (cell != null) {
                    cells[i] = StringUtils.trim(cell.getRichStringCellValue().getString());
                    i++;
                }
            }
            // Sets nulls to unused cells
            for (int j = i; j < 4; j++) {
                cells[j] = null;
            }
            //System.out.println();
            @SuppressWarnings("unused")
            Geography newGeo = convertGeographyRecord(cells[0], cells[1], cells[2], cells[3], earth);

            counter++;
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (frame != null)
        frame.setProcess(counter);

    log.info("Converted " + counter + " Geography records");

    frame.setDesc("Saving Geography Tree...");
    frame.getProcessProgress().setIndeterminate(true);

    TreeHelper.fixFullnameForNodeAndDescendants(earth);
    earth.setNodeNumber(1);
    fixNodeNumbersFromRoot(earth);

    commitTx();

    /*startTx();
    TreeHelper.fixFullnameForNodeAndDescendants(earth);
    earth.setNodeNumber(1);
    fixNodeNumbersFromRoot(earth);
            
    printTree(earth, 0);
    saveTree(earth);
            
    commitTx();*/

    log.info("Converted " + counter + " Stratigraphy records");

    // set up Geography foreign key mapping for locality
    geoHash.clear();

    return earth;
}

From source file:edu.ku.brc.specify.utilapps.BuildSampleDatabase.java

License:Open Source License

/**
 * @param fileName//from   ww  w  .jav a 2 s . c  o  m
 * @return
 */
public HashSet<String> getColumnNamesFromXLS(final String fileName, final boolean doUserProvidedFile) {
    File file = TaxonLoadSetupPanel.getFileForTaxon(fileName, doUserProvidedFile);
    if (file == null) {
        return null;
    }

    HashSet<String> nameHash = new HashSet<String>();
    try {
        String[] cells = new String[35];
        InputStream input = new FileInputStream(file);
        POIFSFileSystem fs = new POIFSFileSystem(input);
        HSSFWorkbook workBook = new HSSFWorkbook(fs);
        HSSFSheet sheet = workBook.getSheetAt(0);
        Iterator<?> rows = sheet.rowIterator();

        rows = sheet.rowIterator();
        if (rows.hasNext()) {
            for (int i = 0; i < cells.length; i++) {
                cells[i] = null;
            }

            HSSFRow row = (HSSFRow) rows.next();
            Iterator<?> cellsIter = row.cellIterator();
            while (cellsIter.hasNext()) {
                HSSFCell cell = (HSSFCell) cellsIter.next();
                if (cell != null) {
                    nameHash.add(StringUtils.trim(cell.getRichStringCellValue().getString()));
                }
            }
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return nameHash;
}

From source file:edu.ku.brc.specify.utilapps.BuildSampleDatabase.java

License:Open Source License

/**
 * @param treeDef//from www. j a  va2s.c  om
 * @param fileName
 * @param doUserProvidedFile
 * @return
 */
public Taxon convertTaxonFromXLS(final TaxonTreeDef treeDef, final String fileName,
        final boolean doUserProvidedFile) {
    Hashtable<String, Taxon> taxonHash = new Hashtable<String, Taxon>();

    taxonHash.clear();

    File file = TaxonLoadSetupPanel.getFileForTaxon(fileName, doUserProvidedFile);
    if (file == null) {
        return null;
    }

    Vector<TaxonTreeDefItem> rankedItems = new Vector<TaxonTreeDefItem>();
    Hashtable<String, Boolean> colNames = new Hashtable<String, Boolean>();
    for (TaxonTreeDefItem item : treeDef.getTreeDefItems()) {
        colNames.put(item.getName().toLowerCase(), true);
        rankedItems.add(item);
    }

    Collections.sort(rankedItems, new Comparator<TaxonTreeDefItem>() {
        @Override
        public int compare(TaxonTreeDefItem o1, TaxonTreeDefItem o2) {
            return o1.getRankId().compareTo(o2.getRankId());
        }
    });

    Connection conn = null;
    Statement stmt = null;

    TaxonTreeDefItem rootTreeDefItem = rankedItems.get(0);
    Set<Taxon> rootKids = rootTreeDefItem.getTreeEntries();
    Taxon root = rootKids.iterator().next();

    Vector<Pair<String, Integer>> nodeList = new Vector<Pair<String, Integer>>();
    Pair<String, Integer> rootNode = new Pair<String, Integer>(root.getName(), root.getId());
    nodeList.add(rootNode);

    int counter = 0;
    int numDataCols = 0;
    try {
        startTx();

        for (TaxonTreeDefItem item : treeDef.getTreeDefItems()) {
            persist(item);
        }

        persist(root);

        commitTx();

        String[] cells = new String[35];
        String[] header = new String[35];
        InputStream input = new FileInputStream(file);
        POIFSFileSystem fs = new POIFSFileSystem(input);
        HSSFWorkbook workBook = new HSSFWorkbook(fs);
        HSSFSheet sheet = workBook.getSheetAt(0);
        Iterator<?> rows = sheet.rowIterator();

        int lastRowNum = sheet.getLastRowNum();
        if (frame != null) {
            final int mx = lastRowNum;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    frame.setProcess(0, mx);
                }
            });
        }

        conn = DBConnection.getInstance().createConnection();
        //conn.setAutoCommit(false);
        stmt = conn.createStatement();

        rows = sheet.rowIterator();
        while (rows.hasNext()) {
            for (int i = 0; i < cells.length; i++) {
                cells[i] = null;
            }

            if (counter == 0) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator<?> cellsIter = row.cellIterator();
                int i = 0;
                while (cellsIter.hasNext()) {
                    HSSFCell cell = (HSSFCell) cellsIter.next();
                    if (cell != null) {
                        cells[i] = getXLSCellValueAsStr(cell);
                        header[i] = cells[i];
                        i++;
                    }
                }
                for (i = 0; i < cells.length; i++) {
                    if (cells[i] == null)
                        break;

                    if (colNames.get(cells[i].toLowerCase()) != null) {
                        numDataCols = i + 1;
                    } else {
                        for (String key : colNames.keySet()) {
                            System.err.println("key[" + key + "]");
                        }
                        System.err.println("Not Found: [" + cells[i].toLowerCase() + "]");
                        break;
                    }
                }
                loadIndexes(cells);
                counter = 1;

                for (String hdr : header) {
                    if (hdr == null)
                        break;

                    int inx = 0;
                    for (TaxonTreeDefItem item : rankedItems) {
                        if (hdr.equalsIgnoreCase(item.getName())) {
                            log.debug("Header: " + hdr + " -> " + inx);
                            taxonIndexes.put(hdr, inx);
                            item.setIsInFullName(item.getRankId() >= TaxonTreeDef.GENUS);

                        } else {
                            log.debug("Header: " + hdr + " -> skipped.");
                        }
                        inx++;
                    }
                }
                continue;
            }

            if (counter % 100 == 0) {
                if (frame != null)
                    frame.setProcess(counter);
                //log.info("Converted " + counter + " of "+lastRowNum+" Taxon records");
            }

            HSSFRow row = (HSSFRow) rows.next();
            Iterator<?> cellsIter = row.cellIterator();
            int i = 0;
            while (cellsIter.hasNext() && i < cells.length) {
                HSSFCell cell = (HSSFCell) cellsIter.next();
                if (cell != null) {
                    cells[i] = getXLSCellValueAsStr(cell);
                    i++;
                }
            }

            convertTaxonNodes(conn, stmt, header, cells, numDataCols, rootNode, nodeList, rankedItems,
                    root.getDefinition().getId());

            counter++;
        }

        stmt.executeUpdate(
                "UPDATE taxon SET IsAccepted = true WHERE IsAccepted IS NULL and AcceptedID IS NULL AND TaxonTReeDefID = "
                        + treeDef.getId());

        // Clear all GUIDs in Taxon.
        stmt.executeUpdate("UPDATE taxon SET GUID = NULL WHERE TaxonTreeDefID = " + treeDef.getId());

        conn.close();

        input.close();

        if (frame != null)
            frame.setProcess(lastRowNum);

        root = (Taxon) session.createQuery("FROM Taxon WHERE id = " + root.getId()).list().get(0);

    } catch (Exception ex) {
        ex.printStackTrace();
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(BackupServiceFactory.class, ex);

    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    if (frame != null) {
        frame.setDesc("Saving Taxon Tree...");
        frame.getProcessProgress().setIndeterminate(true);
    }

    NodeNumberer<Taxon, TaxonTreeDef, TaxonTreeDefItem> nodeNumberer = new NodeNumberer<Taxon, TaxonTreeDef, TaxonTreeDefItem>(
            root.getDefinition());
    nodeNumberer.doInBackground();

    //startTx();
    //TreeHelper.fixFullnameForNodeAndDescendants(root);
    //root.setNodeNumber(1);
    //fixNodeNumbersFromRoot(root);
    //commitTx();

    log.info("Converted " + counter + " Taxon records");

    // set up Taxon foreign key mapping for locality
    taxonHash.clear();

    return root;
}