Search in sources :

Example 26 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class PointComparison method onClick$btnCompare.

public void onClick$btnCompare(Event event) {
    try {
        //sampling
        String url = CommonData.getSettings().getProperty("layers_batch_intersect_url") + "/intersect/batch";
        NameValuePair[] params = new NameValuePair[2];
        params[0] = new NameValuePair("fids", layerList());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < points.size(); i++) {
            try {
                //validate points as doubles
                String part = "" + Double.parseDouble(points.get(i)[1]) + "," + Double.parseDouble(points.get(i)[0]);
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(part);
            } catch (Exception e) {
            }
        }
        params[1] = new NameValuePair("points", sb.toString());
        String batchId = Util.readUrlPost(url, params);
        JSONParser jp = new JSONParser();
        batchId = ((JSONObject) jp.parse(batchId)).get("batchId").toString();
        LOGGER.debug("batch intersect id: " + batchId);
        Thread.sleep(2000);
        //30s
        long maxtime = 30000;
        long starttime = System.currentTimeMillis();
        String csv = "";
        while (starttime + maxtime > System.currentTimeMillis()) {
            Thread.sleep(2000);
            String response = Util.readUrl(url + "/" + batchId);
            try {
                String status = ((JSONObject) jp.parse(response)).get("status").toString();
                if ("finished".equals(status)) {
                    URL get = new URL(((JSONObject) jp.parse(response)).get("downloadUrl").toString());
                    InputStream is = get.openStream();
                    ZipInputStream zip = new ZipInputStream(is);
                    ZipEntry ze = zip.getNextEntry();
                    if (ze != null) {
                        csv = new String(StreamUtils.getBytes(zip), "UTF-8");
                    }
                    zip.close();
                    is.close();
                    break;
                }
            } catch (Exception e) {
            }
        }
        //transpose csv and merge longitude & latitude columns
        List<String[]> data = new CSVReader(new StringReader(csv)).readAll();
        List<Integer> rowNumbers = new ArrayList<Integer>();
        for (int j = 2; j < data.get(0).length; j++) {
            for (int i = 1; i < data.size(); i++) {
                if (data.get(i)[j] != null && !data.get(i)[j].isEmpty() && !"n/a".equals(data.get(i)[j])) {
                    rowNumbers.add(j);
                    break;
                }
            }
        }
        String[][] tdata = new String[rowNumbers.size() + 1][data.size()];
        StringBuilder tcsv = new StringBuilder();
        int row = 0;
        for (int i = 0; i < data.size(); i++) {
            if (i > 0) {
                tcsv.append(",");
            }
            tdata[row][i] = data.get(i)[0] + " " + data.get(i)[1];
            tcsv.append(tdata[0][i]);
        }
        row++;
        for (int j = 2; j < data.get(0).length; j++) {
            if (rowNumbers.size() > 0 && rowNumbers.get(0) == j) {
                rowNumbers.remove(0);
                tcsv.append("\n");
                for (int i = 0; i < data.size(); i++) {
                    if (i > 0) {
                        tcsv.append(",");
                    }
                    if (i == 0) {
                        tcsv.append("\"").append(layerDisplayNames.get(data.get(i)[j])).append("\"");
                        tdata[row][i] = layerDisplayNames.get(data.get(i)[j]);
                    } else {
                        tcsv.append(data.get(i)[j]);
                        tdata[row][i] = data.get(i)[j];
                    }
                }
                row++;
            }
        }
        comparisonCsv = tcsv.toString();
        btnDownload.setDisabled(false);
        lbResults.setModel(new SimpleListModel<String[]>(tdata));
    } catch (Exception e) {
        LOGGER.error("error comparing points", e);
    }
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) CSVReader(au.com.bytecode.opencsv.CSVReader) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) URL(java.net.URL) ZipInputStream(java.util.zip.ZipInputStream) JSONObject(org.json.simple.JSONObject) StringReader(java.io.StringReader) JSONParser(org.json.simple.parser.JSONParser)

Example 27 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project opennms by OpenNMS.

the class AssetsUpdater method parseCsv2.

protected static void parseCsv2(final File csv) throws ClassNotFoundException, SQLException, IOException {
    String sql = m_dbQuery;
    Connection con = createConnection(false);
    PreparedStatement ps = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(csv)));
    CSVReader csvReader = new CSVReader(br);
    String[] line;
    int lineCnt = 0;
    while ((line = csvReader.readNext()) != null) {
        System.out.println("Processing csv line: " + String.valueOf(++lineCnt));
        if (line.length != m_fieldMap.size() + 1) {
            continue;
        }
        String foreignSource = m_formatter.formatForeignSource(StringUtils.isBlank(line[0]) ? null : line[0]);
        System.out.println("Running query for foreignSource: " + foreignSource + " ...");
        ps.setString(1, foreignSource);
        ResultSet rs = ps.executeQuery();
        rs.last();
        int rows = rs.getRow();
        if (rows < 1) {
            rs.close();
            System.out.println("No results found for foreignsource: " + foreignSource + "; continuing to next foreignsource...");
            continue;
        }
        System.out.println("Found " + rows + " rows.");
        rs.beforeFirst();
        while (rs.next()) {
            System.out.println("Updating node: " + rs.getInt("nodeid"));
            Set<Entry<Integer, String>> entrySet = m_fieldMap.entrySet();
            for (Entry<Integer, String> entry : entrySet) {
                int csvField = entry.getKey() - 1;
                String columnName = entry.getValue();
                System.out.println("\t" + "updating column: " + columnName + " with csv field: " + line[csvField]);
                rs.updateString(columnName, line[csvField]);
            }
            rs.updateRow();
        }
        rs.close();
    }
    try {
        con.commit();
    } catch (SQLException e) {
        e.printStackTrace();
        con.rollback();
    }
    csvReader.close();
    ps.close();
    con.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) FileInputStream(java.io.FileInputStream) Entry(java.util.Map.Entry) BufferedReader(java.io.BufferedReader) ResultSet(java.sql.ResultSet)

Example 28 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project opennms by OpenNMS.

the class ImportAssetsServlet method decodeAssetsText.

/**
     * <p>decodeAssetsText</p>
     *
     * @param text a {@link java.lang.String} object.
     * @return a {@link java.util.List} object.
     * @throws org.opennms.web.asset.ImportAssetsServlet$AssetException if any.
     */
public List<Asset> decodeAssetsText(String text) throws AssetException {
    CSVReader csvReader = null;
    StringReader stringReader = null;
    String[] line;
    List<Asset> list = new ArrayList<Asset>();
    text = text.trim();
    int count = 0;
    try {
        stringReader = new StringReader(text);
        csvReader = new CSVReader(stringReader);
        while ((line = csvReader.readNext()) != null) {
            count++;
            try {
                logger.debug("asset line is:'{}'", (Object) line);
                if (line.length <= 37) {
                    logger.error("csv test row length was not at least 37 line length: '{}' line was:'{}', line length", line.length, line);
                    throw new NoSuchElementException();
                }
                // skip the first line if it's the headers
                if (line[0].equals("Node Label")) {
                    logger.debug("line was header. line:'{}'", (Object) line);
                    continue;
                }
                final Asset asset = new Asset();
                asset.setNodeId(WebSecurityUtils.safeParseInt(line[1]));
                asset.setCategory(Util.decode(line[2]));
                asset.setManufacturer(Util.decode(line[3]));
                asset.setVendor(Util.decode(line[4]));
                asset.setModelNumber(Util.decode(line[5]));
                asset.setSerialNumber(Util.decode(line[6]));
                asset.setDescription(Util.decode(line[7]));
                asset.setCircuitId(Util.decode(line[8]));
                asset.setAssetNumber(Util.decode(line[9]));
                asset.setOperatingSystem(Util.decode(line[10]));
                asset.setRack(Util.decode(line[11]));
                asset.setSlot(Util.decode(line[12]));
                asset.setPort(Util.decode(line[13]));
                asset.setRegion(Util.decode(line[14]));
                asset.setDivision(Util.decode(line[15]));
                asset.setDepartment(Util.decode(line[16]));
                asset.setAddress1(Util.decode(line[17]));
                asset.setAddress2(Util.decode(line[18]));
                asset.setCity(Util.decode(line[19]));
                asset.setState(Util.decode(line[20]));
                asset.setZip(Util.decode(line[21]));
                asset.setBuilding(Util.decode(line[22]));
                asset.setFloor(Util.decode(line[23]));
                asset.setRoom(Util.decode(line[24]));
                asset.setVendorPhone(Util.decode(line[25]));
                asset.setVendorFax(Util.decode(line[26]));
                asset.setDateInstalled(Util.decode(line[27]));
                asset.setLease(Util.decode(line[28]));
                asset.setLeaseExpires(Util.decode(line[29]));
                asset.setSupportPhone(Util.decode(line[30]));
                asset.setMaintContract(Util.decode(line[31]));
                asset.setVendorAssetNumber(Util.decode(line[32]));
                asset.setMaintContractExpires(Util.decode(line[33]));
                asset.setDisplayCategory(Util.decode(line[34]));
                asset.setNotifyCategory(Util.decode(line[35]));
                asset.setPollerCategory(Util.decode(line[36]));
                if (line.length > 37) {
                    asset.setThresholdCategory(Util.decode(line[37]));
                    asset.setUsername(Util.decode(line[38]));
                    asset.setPassword(Util.decode(line[39]));
                    asset.setEnable(Util.decode(line[40]));
                    asset.setConnection(Util.decode(line[41]));
                    asset.setAutoenable(Util.decode(line[42]));
                    asset.setComments(Util.decode(line[43]));
                }
                if (line.length > 44) {
                    asset.setCpu(Util.decode(line[44]));
                    asset.setRam(Util.decode(line[45]));
                    asset.setStoragectrl(Util.decode(line[46]));
                    asset.setHdd1(Util.decode(line[47]));
                    asset.setHdd2(Util.decode(line[48]));
                    asset.setHdd3(Util.decode(line[49]));
                    asset.setHdd4(Util.decode(line[50]));
                    asset.setHdd5(Util.decode(line[51]));
                    asset.setHdd6(Util.decode(line[52]));
                    asset.setNumpowersupplies(Util.decode(line[53]));
                    asset.setInputpower(Util.decode(line[54]));
                    asset.setAdditionalhardware(Util.decode(line[55]));
                    asset.setAdmin(Util.decode(line[56]));
                    asset.setSnmpcommunity(Util.decode(line[57]));
                    asset.setRackunitheight(Util.decode(line[58]));
                }
                if (line.length > 59) {
                    asset.setCountry(Util.decode(line[59]));
                    asset.setLongitude(Util.decode(line[60]));
                    asset.setLatitude(Util.decode(line[61]));
                }
                list.add(asset);
                logger.debug("decoded asset:'{}'", (Object) asset);
            } catch (NoSuchElementException e) {
                errors.add("Ignoring malformed import for entry " + count + ", not enough values.");
            } catch (NumberFormatException e) {
                logger.error("NodeId parsing to int faild, ignoreing malformed import for entry number '{}' exception message:'{}'", count, e.getMessage());
                errors.add("Ignoring malformed import for entry " + count + ", node id not a number.");
            }
        }
    } catch (IOException e) {
        logger.error("An error occurred reading the CSV input. Message:'{}'", e.getMessage());
        throw new AssetException("An error occurred reading the CSV input.", e);
    } finally {
        IOUtils.closeQuietly(stringReader);
        IOUtils.closeQuietly(csvReader);
    }
    if (list.size() == 0) {
        logger.error("No asset information was found, list size was 0");
        throw new AssetException("No asset information was found.");
    }
    return list;
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException)

Example 29 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class DistributionsController method init.

public void init(String[] text, String type, String count) {
    ((Caption) getFellow(StringConstants.CTITLE)).setLabel(type);
    this.originalCount = count;
    this.type = type;
    this.text = text == null ? null : text.clone();
    if (text != null && text.length > 1) {
        List<String[]> data = new ArrayList<String[]>();
        for (int i = 1; i < text.length; i++) {
            if (text[i] != null && text[i].length() > 0) {
                try {
                    StringReader sreader = new StringReader(text[i]);
                    CSVReader reader = new CSVReader(sreader);
                    // last row indicates if it is mapped or not
                    String[] row = reader.readNext();
                    String[] newrow = java.util.Arrays.copyOf(row, 15);
                    try {
                        String niceAreaSqKm = String.format("%.1f", (float) Double.parseDouble(row[12]));
                        newrow[12] = niceAreaSqKm;
                    } catch (Exception e) {
                        LOGGER.error("error parsing area from: " + ((row != null && row.length > 12) ? row[12] : "bad data"));
                    }
                    data.add(newrow);
                    reader.close();
                    sreader.close();
                } catch (Exception e) {
                    LOGGER.error("error reading distributions row", e);
                }
            }
        }
        if (this.originalData == null) {
            this.originalData = data;
        }
        update(data, originalCount);
    }
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader)

Example 30 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class SpeciesListResults method populateList.

public void populateList() {
    if (selectedArea == null) {
        selectedArea = new SelectedArea(null, getMapComposer().getViewArea());
    }
    try {
        Query sq = QueryUtil.queryFromSelectedArea(null, selectedArea, extraParams, false, geospatialKosher);
        sq = sq.newFacet(new Facet("occurrence_status_s", "absent", false), false);
        if (sq.getSpeciesCount() <= 0) {
            getMapComposer().showMessage("No species records in the active area.");
            results = null;
            popupListboxResults.setVisible(false);
            resultsLabel.setVisible(false);
            this.detach();
            return;
        }
        //remove header
        String speciesList = chooseEndemic ? sq.endemicSpeciesList() : sq.speciesList();
        results = speciesList.substring(speciesList.indexOf('\n') + 1).split("\n");
        java.util.Arrays.sort(results);
        // results should already be sorted
        String[] tmp = results;
        if (results.length > 200) {
            tmp = java.util.Arrays.copyOf(results, 200);
            resultsLabel.setValue("preview of first 200 of " + results.length + " species found");
        } else {
            resultsLabel.setValue("preview of all " + results.length + " species found");
        }
        popupListboxResults.setModel(new ListModelArray(tmp, false));
        popupListboxResults.setItemRenderer(new ListitemRenderer() {

            public void render(Listitem li, Object data, int itemIdx) {
                String s = (String) data;
                CSVReader reader = new CSVReader(new StringReader(s));
                String[] ss;
                try {
                    ss = reader.readNext();
                } catch (Exception e) {
                    ss = new String[0];
                }
                if (ss == null || ss.length == 0) {
                    return;
                }
                Listcell lc = new Listcell(ss[0]);
                lc.setParent(li);
                int col = 1;
                while (col < ss.length) {
                    lc = new Listcell(ss[col]);
                    lc.setParent(li);
                    col++;
                }
                try {
                    reader.close();
                } catch (IOException e) {
                    LOGGER.error("error closing after reading species list", e);
                }
            }
        });
    } catch (Exception e) {
        LOGGER.error("error reading species list data", e);
    }
}
Also used : Query(au.org.ala.spatial.util.Query) CSVReader(au.com.bytecode.opencsv.CSVReader) SelectedArea(au.org.emii.portal.menu.SelectedArea) IOException(java.io.IOException) IOException(java.io.IOException) StringReader(java.io.StringReader) Facet(au.org.ala.legend.Facet)

Aggregations

CSVReader (au.com.bytecode.opencsv.CSVReader)32 StringReader (java.io.StringReader)12 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)6 HttpClient (org.apache.commons.httpclient.HttpClient)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 InputStreamReader (java.io.InputStreamReader)4 HashMap (java.util.HashMap)4 JSONObject (org.json.simple.JSONObject)4 JSONParser (org.json.simple.parser.JSONParser)4 Query (au.org.ala.spatial.util.Query)3 FileReader (java.io.FileReader)3 ZipEntry (java.util.zip.ZipEntry)3 ZipInputStream (java.util.zip.ZipInputStream)3 JSONArray (org.json.simple.JSONArray)3 ParseException (org.json.simple.parser.ParseException)3 Event (org.zkoss.zk.ui.event.Event)3 Facet (au.org.ala.legend.Facet)2 LegendObject (au.org.ala.legend.LegendObject)2 SelectedArea (au.org.emii.portal.menu.SelectedArea)2