Package weave.utils

Examples of weave.utils.CSVParser


  public void checkKeyColumnsForCSVImport(String csvFile, String[] keyColumns) throws RemoteException
  {
    if (keyColumns.length == 0)
      throw new RemoteException("No key columns specified");
   
    final CSVParser csvParser = CSVParser.defaultParser;

    String[][] rows;
    try
    {
      rows = csvParser.parseCSV(new File(getUploadPath(), csvFile), true);
    }
    catch (IOException cause)
    {
      if (cause instanceof RemoteException)
        throw (RemoteException)cause;
      throw new RemoteException(String.format("Unable to read file \"%s\"", csvFile), cause);
    }
    if (rows.length == 0)
      throw new RemoteException(String.format("File is empty: %s", csvFile));
   
    String[] headers = rows[0];
   
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    int[] keyColumnIndices = new int[keyColumns.length];
     
    for (int i = 0; i < keyColumns.length;i++)
    {
      keyColumnIndices[i] = ListUtils.findString(keyColumns[i], headers);
      if (keyColumnIndices[i] < 0)
        throw new RemoteException(String.format("CSV file \"%s\" does not have a column named \"%s\"", csvFile, keyColumns[i]));
    }
   
    for (int row = 1; row < rows.length; row++)
    {
      String[] keyArray = ListUtils.getItems(rows[row], keyColumnIndices);
      String key = csvParser.createCSVRow(keyArray, true);
      if (!map.containsKey(key))
      {
        map.put(key, row);
      }
      else
      {
        // ignore blank rows
        boolean allEmpty = true;
        for (String value : rows[row])
        {
          if (!Strings.isEmpty(value))
          {
            allEmpty = false;
            break;
          }
        }
        if (allEmpty)
          continue;
       
        int prevRow = map.get(key);
        String msg = String.format(
            "Found duplicate key (%s) on rows %s and %s of \"%s\"\n" +
            "Row %s: %s\n" +
            "Row %s: %s",
            key, prevRow, row, csvFile,
            prevRow, csvParser.createCSVRow(rows[prevRow], true),
            row, csvParser.createCSVRow(rows[row], true)
          );
        throw new RemoteException(msg);
      }
    }
  }
View Full Code Here

TOP

Related Classes of weave.utils.CSVParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.