Examples of CSVReader


Examples of au.com.bytecode.opencsv.CSVReader

        return null;
    }

    private int importCsv(FileItem item) throws IOException, TranslatableException {
        CSVReader csvReader = new CSVReader(new InputStreamReader(item.getInputStream()));
        DataPointDao dataPointDao = new DataPointDao();
        PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
        DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss");

        // Basic validation
        String[] nextLine = csvReader.readNext();
        if (nextLine == null)
            throw new TranslatableException(new TranslatableMessage("dataImport.import.noData"));
        if (nextLine.length < 2)
            throw new TranslatableException(new TranslatableMessage("dataImport.import.noPoints"));

        // Find the points by XID
        DataPointVO[] vos = new DataPointVO[nextLine.length - 1];

        for (int i = 1; i < nextLine.length; i++) {
            if (StringUtils.isBlank(nextLine[i]))
                throw new TranslatableException(new TranslatableMessage("dataImport.import.badXid", i));

            DataPointVO vo = dataPointDao.getDataPoint(nextLine[i]);
            if (vo == null)
                throw new TranslatableException(new TranslatableMessage("dataImport.import.xidNotFound", nextLine[i]));

            vos[i - 1] = vo;
        }

        // Find the RTs for the points if they are enabled
        DataPointRT[] rts = new DataPointRT[vos.length];
        for (int i = 0; i < vos.length; i++)
            rts[i] = Common.runtimeManager.getDataPoint(vos[i].getId());

        // Import the data
        int count = 0;
        while ((nextLine = csvReader.readNext()) != null) {
            // The first value is always a date.
            long time = dtf.parseDateTime(nextLine[0]).getMillis();

            // The rest of the values are point samples.
            for (int i = 1; i < nextLine.length; i++) {
View Full Code Here

Examples of au.com.bytecode.opencsv.CSVReader

         FileOutputStream fileOutputStream = new FileOutputStream(uploadedFile);
         IOUtils.copy(request.body, fileOutputStream);
         fileOutputStream.close();
          
          
           CSVReader csvReader = new CSVReader(new FileReader(uploadedFile));
          
           int lineNum = 1;
          
           HashMap<Integer, TripPatternStop> columnStopIndex = new HashMap<Integer, TripPatternStop>();
          
           HashMap<Integer, Integer> columnStopDelta = new HashMap<Integer, Integer>();
          
           Integer cumulativeTime = 0;
          
           List<TripPatternStop> patternStops = TripPatternStop.find("pattern = ? order by stopSequence", pattern).fetch();
          
           for(String[] csvLine : csvReader.readAll())
           {
             int columnIndex = 0;
          
             if(lineNum == 3)
             {
               if(!csvLine[5].equals("stop_id"))
                 throw new Exception("Invalid stop_id row.");
              
               for(String column : csvLine
               {
                 if(columnIndex > 5)
                 {
                   Stop stop = Stop.findById(Long.parseLong(column));
                  
                   TripPatternStop patternStop = patternStops.get(0);
                   patternStops = patternStops.subList(1, patternStops.size());
                  
                   if(!patternStop.stop.id.equals(stop.id))
                     throw new Exception("Stop ID " + stop.id + "doesn't match pattern sequence for stop " + patternStop.stop.id);
                  
                   columnStopIndex.put(columnIndex, patternStop);
                  
                   cumulativeTime += patternStop.defaultDwellTime != null? patternStop.defaultDwellTime: 0;
                   cumulativeTime += patternStop.defaultTravelTime != null? patternStop.defaultTravelTime: ;
                  
                   columnStopDelta.put(columnIndex, new Integer(cumulativeTime));
                 }
                
                 columnIndex++;
               }
             }
             else if(lineNum > 6)
             {
               if(!csvLine[0].isEmpty())
               {
                 Trip trip = new Trip();
                
                 trip.pattern = pattern;
                 trip.serviceCalendar = calendar;
                
                 trip.blockId = csvLine[2];
                 trip.tripHeadsign = csvLine[3];
                 trip.tripShortName = csvLine[4];
                
                 trip.useFrequency = false;
                
                 trip.save();
                
                 Integer firstTimepoint = null;
                 Integer columnCount = 0;
                 Integer previousTime = 0;
                 Integer dayOffset = 0;
                
                 for(String column : csvLine
                 { 
                   if(columnIndex > 5)
                   {
                     if(!column.isEmpty())
                     {
                       StopTime stopTime = new StopTime();
                      
                       stopTime.trip = trip;
                      
                       // check for board/alight only flag
                       if(column.contains(">")) {
                        
                         column = column.replace(">", "");
                        
                         // board only
                         stopTime.dropOffType = StopTimePickupDropOffType.NONE;
                       }
                      
                       if(column.contains("<")) {
                        
                         column = column.replace("<", "");
                        
                         // alight only
                         stopTime.pickupType = StopTimePickupDropOffType.NONE;
                       }
                      
                       column = column.trim();
                      
                       if(column.equals("+"))
                         stopTime.departureTime = firstTimepoint + columnStopDelta.get(columnIndex);
                       else if(column.equals("-"))
                         stopTime.departureTime = null;
                       else
                       {
                         Integer currentTime;
                          
                        
                         try
                         {
                           currentTime = (dfTime.parse(column).getHours() * 60 * 60 ) + (dfTime.parse(column).getMinutes() * 60) + (dfTime.parse(column).getSeconds());
                         }
                         catch(ParseException e)
                         {
                           try
                           {
                             currentTime = (dfsTime.parse(column).getHours() * 60 * 60 ) + (dfsTime.parse(column).getMinutes() * 60) + (dfsTime.parse(column).getSeconds());
                           }
                           catch(ParseException e2)
                           {
                             continue;
                           }
                         }
                        
                         // in case of time that decreases add a day to offset for trips that cross midnight boundary
                         if(previousTime > currentTime)                       
                           dayOffset += 24 * 60 * 60;
                          
                         stopTime.departureTime = currentTime + dayOffset;
                        
                         previousTime = currentTime;
                        
                         if(firstTimepoint == null)
                           {
                           firstTimepoint = stopTime.departureTime;
                           }
                        
                       }
                      
                       stopTime.arrivalTime = stopTime.departureTime;
                      
                       stopTime.patternStop = columnStopIndex.get(columnIndex);
                       stopTime.stop = columnStopIndex.get(columnIndex).stop;
                       stopTime.stopSequence = columnCount + 1;
                      
                       stopTime.save();
                      
                       columnCount++;
                     }
                   }
                    
                   columnIndex++;
                 }             
               }
             }
            
             lineNum++;
           }
          
           csvReader.close();
       }
       catch(Exception e)
       {
         Logger.error(e.toString());
       }
View Full Code Here

Examples of au.com.bytecode.opencsv.CSVReader

    }

    @Override
    public long doConversion(DailyMileClient client) {
        long migrated = 0;
        CSVReader reader = null;
       
        try {
            reader = new CSVReader(new FileReader(getInputFilePath()));
            String [] nextLine = reader.readNext();
          
            //skip the first line, it is a header row
            if (nextLine == null) {
                return 0;
            }
           
            while ((nextLine = reader.readNext()) != null) {
                // nextLine[] is an array of values from the line
                Workout wo = createWorkout(nextLine);
                client.addWorkout(wo, nextLine[RunKeeperField.NOTE.getFieldIndex()]);
                migrated++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    // ignore
                }
            }
        }
View Full Code Here

Examples of au.com.bytecode.opencsv.CSVReader

        String inputFilePath =
            this.getClass().getResource("/com/pc/dailymile/cli/runkeeper/runkeeper.csv")
                    .getFile();
        RunKeeperConverter rkc = new RunKeeperConverter(inputFilePath);

        CSVReader reader = null;
        try {
           
            reader = new CSVReader(new FileReader(inputFilePath));
            List<String[]> lines = reader.readAll();

               
            //first row is headers, so start at 1
            String[] nextLine = lines.get(1);
           
            Workout wo = rkc.createWorkout(nextLine);
            assertEquals(Type.Running, wo.getType());
            assertEquals(1740L, wo.getDuration().longValue());
            assertEquals(Units.kilometers, wo.getDistanceUnits());
            assertEquals("1.17", wo.getDistanceValue());
           
            nextLine = lines.get(2);
            wo = rkc.createWorkout(nextLine);
            assertEquals(Type.Walking, wo.getType());
            assertEquals(1241L, wo.getDuration().longValue());
            assertEquals(Units.kilometers, wo.getDistanceUnits());
            assertEquals("0.96", wo.getDistanceValue());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    // ignore
                }
            }
        }
View Full Code Here

Examples of au.com.bytecode.opencsv.CSVReader

        }
        return _columns;
    }

    private Column[] buildColumns() {
        CSVReader reader = null;
        try {
            reader = _schema.getDataContext().createCsvReader(0);

            final int columnNameLineNumber = _schema.getDataContext().getConfiguration().getColumnNameLineNumber();
            for (int i = 1; i < columnNameLineNumber; i++) {
                reader.readNext();
            }
            final String[] columnHeaders = reader.readNext();

            reader.close();
            return buildColumns(columnHeaders);
        } catch (IOException e) {
            throw new IllegalStateException("Exception reading from resource: "
                    + _schema.getDataContext().getResource().getName(), e);
        } finally {
View Full Code Here

Examples of au.com.bytecode.opencsv.CSVReader

        final boolean failOnInconsistentRowLength = _configuration.isFailOnInconsistentRowLength();

        final Integer maxRowsOrNull = (maxRows > 0 ? maxRows : null);

        if (_configuration.isMultilineValues()) {
            final CSVReader csvReader = createCsvReader(reader);
            return new CsvDataSet(csvReader, columns, maxRowsOrNull, columnCount, failOnInconsistentRowLength);
        }

        final CSVParser csvParser = new CSVParser(_configuration.getSeparatorChar(), _configuration.getQuoteChar(),
                _configuration.getEscapeChar());
View Full Code Here

Examples of au.com.bytecode.opencsv.CSVReader

                failOnInconsistentRowLength);
    }

    protected CSVReader createCsvReader(int skipLines) {
        final Reader reader = FileHelper.getReader(_resource.read(), _configuration.getEncoding());
        final CSVReader csvReader = new CSVReader(reader, _configuration.getSeparatorChar(),
                _configuration.getQuoteChar(), _configuration.getEscapeChar(), skipLines);
        return csvReader;
    }
View Full Code Here

Examples of au.com.bytecode.opencsv.CSVReader

                _configuration.getQuoteChar(), _configuration.getEscapeChar(), skipLines);
        return csvReader;
    }

    protected CSVReader createCsvReader(BufferedReader reader) {
        final CSVReader csvReader = new CSVReader(reader, _configuration.getSeparatorChar(),
                _configuration.getQuoteChar(), _configuration.getEscapeChar());
        return csvReader;
    }
View Full Code Here

Examples of au.com.bytecode.opencsv.CSVReader

            .getSystemResourceAsStream(TikaResourceTest.TEST_DOC));

    Reader reader = new InputStreamReader(
        (InputStream) response.getEntity());

    CSVReader csvReader = new CSVReader(reader);

    Map<String, String> metadata = new HashMap<String, String>();

    String[] nextLine;
    while ((nextLine = csvReader.readNext()) != null) {
      metadata.put(nextLine[0], nextLine[1]);
    }

    assertNotNull(metadata.get("Author"));
    assertEquals("Maxim Valyanskiy", metadata.get("Author"));
View Full Code Here

Examples of au.com.bytecode.opencsv.CSVReader

        .post(ClassLoader.getSystemResourceAsStream(TikaResourceTest.TEST_DOC));
    Assert.assertEquals(Status.OK.getStatusCode(), response.getStatus());

    Reader reader = new InputStreamReader((InputStream) response.getEntity());

    @SuppressWarnings("resource")
    CSVReader csvReader = new CSVReader(reader);

    Map<String, String> metadata = new HashMap<String, String>();

    String[] nextLine;
    while ((nextLine = csvReader.readNext()) != null) {
      metadata.put(nextLine[0], nextLine[1]);
    }

    assertNotNull(metadata.get("Author"));
    assertEquals("Maxim Valyanskiy", metadata.get("Author"));
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.