Package org.molgenis.io.csv

Examples of org.molgenis.io.csv.CsvReader


    String action = req.getString(INPUT_ACTION);

    // create a database
    Database db = req.getDatabase();

    CsvReader csvReader;
    if (req.get(INPUT_DATA) != null)
    {
      csvReader = new CsvReader(new StringReader(req.getString(INPUT_DATA)));
    }
    else if (req.get(INPUT_FILE) != null)
    {
      csvReader = new CsvReader(req.getFile(INPUT_FILE));
    }
    else
      csvReader = null;

    try
    {
      int nRowsChanged = 0;
      if (action.equals("ADD"))
      {
        File temp = File.createTempFile("molgenis", "tab");
        CsvWriter csvWriter = new CsvWriter(temp);
        try
        {
          if (req.get(INPUT_SILENT) != null && req.getBoolean(INPUT_SILENT) == true)
          {
            csvWriter.close();
            csvWriter = null;
          }

          if (req.get(INPUT_DATA) != null)
          {
            logger.info("processing textarea upload...");
            nRowsChanged = db.add(entityClass, csvReader, csvWriter);
          }
          else if (req.get(INPUT_FILE) != null)
          {
            logger.info("processing file upload...");
            nRowsChanged = db.add(entityClass, csvReader, csvWriter);
          }
          else
          {
            logger.error("no input data or input file provided.");
            out.print("ERROR: no input data or input file provided.");
          }
          out.print("Uploaded " + formatter.format(nRowsChanged) + " rows of "
              + entityClass.getCanonicalName() + "\n");
        }
        finally
        {
          if (csvWriter != null) csvWriter.close();
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(temp),
            Charset.forName("UTF-8")));
        try
        {
          String line = null;
          while ((line = reader.readLine()) != null)
          {
            out.println(line);
          }
        }
        finally
        {
          IOUtils.closeQuietly(reader);
        }
        boolean ok = temp.delete();
        if (!ok) logger.warn("failed to delete file: " + temp);
      }
      else if (action.equals("UPDATE"))
      {
        if (req.get(INPUT_DATA) != null)
        {
          nRowsChanged = db.update(entityClass, csvReader);
          out.print("Updated " + formatter.format(nRowsChanged) + " rows of "
              + entityClass.getCanonicalName() + "\n");
        }
        else if (req.get(INPUT_FILE) != null)
        {
          nRowsChanged = db.update(entityClass, csvReader);
          out.print("Updated " + formatter.format(nRowsChanged) + " rows of "
              + entityClass.getCanonicalName() + "\n");
        }
      }
      else if (action.equals("REMOVE"))
      {
        if (req.get(INPUT_DATA) != null)
        {
          nRowsChanged = db.remove(entityClass, csvReader);
          out.print("Removed " + formatter.format(nRowsChanged) + " rows of "
              + entityClass.getCanonicalName() + "\n");
        }
        else if (req.get(INPUT_FILE) != null)
        {
          nRowsChanged = db.remove(entityClass, csvReader);
          out.print("Removed " + formatter.format(nRowsChanged) + " rows of "
              + entityClass.getCanonicalName() + "\n");
        }
      }
      else
      {
        throw new Exception("Unknown action " + action);
      }
    }
    finally
    {
      if (csvReader != null) csvReader.close();
    }
  }
View Full Code Here


    if (request.getString(FormModel.INPUT_SHOW) == null)
    {
      ScreenMessage msg = null;
      try
      {
        CsvReader csvReader = new CsvReader(new StringReader(request.getString("__csvdata")));
        String entityName = this.getFormScreen().getEntityClass().getSimpleName();

        EntitiesImporter entitiesImporter = this.getFormScreen().getCsvEntityImporter();
        entitiesImporter.setDatabase(db);

        EntityImportReport importReport = null;
        try
        {
          importReport = entitiesImporter.importEntities(csvReader, entityName, DatabaseAction.ADD);
        }
        finally
        {
          csvReader.close();
        }

        msg = new ScreenMessage("CSV UPLOAD SUCCESS: added " + importReport.getNrImported() + " rows", null,
            true);
        logger.debug("CSV UPLOAD SUCCESS: added " + importReport.getNrImported() + " rows");
View Full Code Here

  {
    String name = zipEntry.getName();
    if (name.endsWith(".csv") || name.endsWith(".txt"))
    {
      Reader reader = new InputStreamReader(zipFile.getInputStream(zipEntry), Charset.forName("UTF-8"));
      return new CsvReader(reader);
    }
    else if (name.endsWith(".tsv"))
    {
      Reader reader = new InputStreamReader(zipFile.getInputStream(zipEntry), Charset.forName("UTF-8"));
      return new CsvReader(reader, '\t');
    }
    else
    {
      throw new IOException("unknown file type: " + name);
    }
View Full Code Here

    String name = file.getName();
    if (name.endsWith(".csv") || name.endsWith(".txt"))
    {
      String tableName = FilenameUtils.getBaseName(name);
      return new SingleTableReader(new CsvReader(file), tableName);
    }
    else if (name.endsWith(".tsv"))
    {
      String tableName = FilenameUtils.getBaseName(name);
      return new SingleTableReader(new CsvReader(file, '\t'), tableName);
    }
    else if (name.endsWith(".xls") || name.endsWith(".xlsx"))
    {
      return new ExcelReader(file);
    }
View Full Code Here

    if (request.getString(FormModel.INPUT_SHOW) == null)
    {
      ScreenMessage msg = null;
      try
      {
        CsvReader csvReader = new CsvReader(request.getFile("filefor___csvdata"));
        String entityName = this.getFormScreen().getEntityClass().getSimpleName();

        EntityImportReport importReport = null;
        try
        {
          EntitiesImporter entityImporter = this.getFormScreen().getCsvEntityImporter();
          entityImporter.setDatabase(db);
          importReport = entityImporter.importEntities(csvReader, entityName, DatabaseAction.ADD);
        }
        finally
        {
          csvReader.close();
        }

        msg = new ScreenMessage("CSV UPLOAD SUCCESS: added " + importReport.getNrImported() + " rows", null,
            true);
        logger.debug("CSV UPLOAD SUCCESS: added " + importReport.getNrImported() + " rows");
View Full Code Here

    if (dir.exists())
    {
      File file = new File(dir.getAbsoluteFile() + "/" + fileName);
      if (file.exists())
      {
        CsvReader csvReader = new CsvReader(file);
        try
        {
          for (Tuple tuple : csvReader)
          {
            result.add(tuple);
          }

        }
        finally
        {
          csvReader.close();
        }
      }
    }

    return result;
View Full Code Here

  private void resetStreams() throws FileNotFoundException, IOException, DataFormatException
  {
    IOUtils.closeQuietly(csvReader);
    if (csvFile != null)
    {
      csvReader = new CsvReader(csvFile);
      countStream = new FileInputStream(csvFile);
    }
    else
    {
      csvReader = new CsvReader(new StringReader(csvString));
      countStream = new ByteArrayInputStream(csvString.getBytes(Charset.forName("UTF-8")));
    }
  }
View Full Code Here

TOP

Related Classes of org.molgenis.io.csv.CsvReader

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.