Example usage for org.apache.poi.hssf.usermodel HSSFHyperlink getLabel

List of usage examples for org.apache.poi.hssf.usermodel HSSFHyperlink getLabel

Introduction

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

Prototype

@Override
public String getLabel() 

Source Link

Document

Return text label for this hyperlink

Usage

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 {/*ww w .ja v  a2s.  c om*/
            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;
}