示例#1
0
  private void exportCsvFile(File destFile, ExportConfig exportConfig) throws ExportFileException {
    CSVWriter csvWriter = null;
    try {
      csvWriter = new CSVWriter(new FileWriterWithEncoding(destFile, "gbk"));

      List<Map> list = this.listAllAsMap(exportConfig.getSearchCondition());

      String[] line = null;
      line =
          new String[] {
            "机房名称", "场地号", "模块号", "框号 ", "槽号", "端口号", "设备号", "是否反极", "产品号码", "MDF横列(不准)",
          };
      csvWriter.writeNext(line);

      MapValueGetter mvg = null;
      for (Map map : list) {
        mvg = new MapValueGetter(map);
        line =
            new String[] {
              mvg.getAsString("jfmc"),
              mvg.getAsIntegerString("cdh"),
              mvg.getAsIntegerString("mkh"),
              mvg.getAsIntegerString("kh"),
              mvg.getAsIntegerString("ch"),
              mvg.getAsIntegerString("dkh"),
              mvg.getAsIntegerString("sbh"),
              mvg.getAsTrimedString("fj"),
              mvg.getAsTrimedString("dhhm"),
              mvg.getAsTrimedString("hl"),
            };
        csvWriter.writeNext(line);
      }

      csvWriter.flush();
      csvWriter.close();

    } catch (IOException e) {
      throw new ExportFileException(e);
    } finally {
      if (csvWriter != null) {
        try {
          csvWriter.close();
        } catch (IOException e) {
        }
      }
    }
  }
    @Override
    protected Boolean doInBackground(final String... args) {

      File dbFile = getDatabasePath("person.db");
      Log.v(TAG, "Db path is: " + dbFile); // get the path of db

      File exportDir = new File(Environment.getExternalStorageDirectory(), "");
      if (!exportDir.exists()) {
        exportDir.mkdirs();
      }

      file = new File(exportDir, "PersonCSV.csv");
      try {

        file.createNewFile();
        CSVWriter csvWrite = new CSVWriter(new FileWriter(file));

        // ormlite core method
        List<Person> listdata = dbhelper.GetDataPerson();
        Person person = null;

        // this is the Column of the table and same for Header of CSV
        // file
        String arrStr1[] = {"First Name", "Last Name", "Address", "Email"};
        csvWrite.writeNext(arrStr1);

        if (listdata.size() > 1) {
          for (int index = 0; index < listdata.size(); index++) {
            person = listdata.get(index);
            String arrStr[] = {
              person.getFirtname(), person.getLastname(), person.getAddress(), person.getEmail()
            };
            csvWrite.writeNext(arrStr);
          }
        }
        // sqlite core query

        /*
         * SQLiteDatabase db = DBob.getReadableDatabase(); //Cursor
         * curCSV=mydb.rawQuery("select * from " + TableName_ans,null);
         * Cursor curCSV =
         * db.rawQuery("SELECT * FROM table_ans12",null);
         * csvWrite.writeNext(curCSV.getColumnNames());
         *
         * while(curCSV.moveToNext()) {
         *
         * String arrStr[] ={curCSV.getString(0),curCSV.getString(1)};
         * curCSV.getString(2),curCSV.getString(3),curCSV.getString(4)
         * csvWrite.writeNext(arrStr);
         *
         * }
         */
        csvWrite.close();
        return true;
      } catch (IOException e) {
        Log.e("MainActivity", e.getMessage(), e);
        return false;
      }
    }
示例#3
0
 /**
  * Write the List as a CSV string. The List will contain Maps Each string in this array represents
  * a field in the CSV file.
  *
  * @param l the List of Maps
  * @throws Exception
  */
 protected void write(List l) throws Exception {
   try {
     int rowPos = 0;
     for (Iterator i = l.iterator(); i.hasNext(); ) {
       writeRow((Map) i.next(), rowPos);
     }
   } finally {
     writer.close();
   }
 }
示例#4
0
  @Override
  public Data[] execute() throws AlgorithmExecutionException {
    LogService logger = getLogger();
    Data inputData = getInputData();

    logger.log(LogService.LOG_INFO, "Creating PostScript..");
    CSVWriter csvWriter = null;

    try {
      File barSizesFile =
          FileUtilities.createTemporaryFileInDefaultTemporaryDirectory("barSizes", CSV_FILE_SUFFIX);
      csvWriter = new CSVWriter(new FileWriter(barSizesFile));
      String[] header = new String[] {"Record Name", "Width", "Height", "Area (Width x Height)"};
      csvWriter.writeNext(header);
      String postScriptCode = createPostScriptCode(csvWriter);
      csvWriter.close();

      File temporaryPostScriptFile =
          writePostScriptCodeToTemporaryFile(postScriptCode, "horizontal-line-graph");

      return formOutData(temporaryPostScriptFile, barSizesFile, inputData);
    } catch (IOException e) {
      String message =
          String.format("An error occurred when creating %s.", getLabel(inputData.getMetadata()));
      message += e.getMessage();
      throw new AlgorithmExecutionException(message, e);
    } catch (PostScriptCreationException e) {
      String exceptionMessage =
          String.format(
              "An error occurred when creating the PostScript for the %s.",
              getLabel(inputData.getMetadata()) + e.getMessage());
      throw new AlgorithmExecutionException(exceptionMessage, e);
    } finally {
      if (csvWriter != null) {
        try {
          csvWriter.close();
        } catch (IOException e) {
          throw new AlgorithmExecutionException(e.getMessage(), e);
        }
      }
    }
  }
示例#5
0
  @Override
  public void runTool(CommandLine line) throws Exception {

    // keep it light and fast
    System.setProperty("cassandra.readcl", "ONE");

    startSpring();
    setVerbose(line);

    applyOrgId(line);
    prepareBaseOutputFileName(line);
    outputDir = createOutputParentDir();
    LOG.info("Export directory: {}", outputDir.getAbsolutePath());

    // create writer
    applyStartTime(line);
    applyEndTime(line);
    LOG.error("startTime: {}, endTime: {}", startTime, endTime);
    if (startTime.getTime() >= endTime.getTime()) {
      LOG.error("startTime must be before endTime. exiting.");
      System.exit(1);
    }

    // create "modified" query to select data
    StringBuilder builder = new StringBuilder();
    builder.append("modified >= ").append(startTime.getTime()).append(" and ");
    builder.append("modified <= ").append(endTime.getTime());
    String queryString = builder.toString();

    // create writer
    String dateString = DATE_FORMAT.format(new Date());
    String fileName = outputDir.getAbsolutePath() + "/" + dateString + ".csv";
    FileWriter fw = new FileWriter(fileName);
    writer = new CSVWriter(fw, SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, '\'');

    try {
      writeMetadata();
      writeHeaders();

      // Loop through the organizations
      Map<UUID, String> organizations = getOrganizations();
      for (Entry<UUID, String> orgIdAndName : organizations.entrySet()) {
        exportApplicationsForOrg(orgIdAndName, queryString);
      }
    } finally {
      writer.close();
    }

    // now that file is written, copy it to S3
    if (line.hasOption("upload")) {
      LOG.info("Copy to S3");
      copyToS3(fileName);
    }
  }
 public void saveToFile(String saveFilePath) {
   try {
     CSVWriter writer = new CSVWriter(new FileWriter(saveFilePath, true));
     writer.writeNext(
         new String[] {
           section.getSubTitle(), apiElement.getAPIElementName(), Boolean.toString(label)
         });
     writer.close();
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
示例#7
0
 // Write master list of point of interest objects out as a CSV file
 public void saveCSVFile(String fileName, List<ParsedPointOfInterest> myList) throws IOException {
   FileOutputStream output = openFileOutput(fileName, MODE_PRIVATE);
   OutputStreamWriter writer = new OutputStreamWriter(output);
   CSVWriter csvWriter = new CSVWriter(writer);
   String columns[];
   for (ParsedPointOfInterest poi : myList) {
     columns = poi.getColumns();
     csvWriter.writeNext(columns);
   }
   writer.close();
   csvWriter.flush();
   csvWriter.close();
 }
  private byte[] createCSVInputStream(
      EventEx event,
      List<Pair<EventTicket, EventTicketHolderList>> ticketAndHolders,
      Map<String, List<String>> userTicketInfoMap)
      throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CSVWriter writer = new CSVWriter(new OutputStreamWriter(baos, Charset.forName("UTF-8")));

    writeHeader(writer, event);
    for (int i = 0; i < ticketAndHolders.size(); ++i) {
      EventTicket ticket = ticketAndHolders.get(i).getFirst();
      EventTicketHolderList list = ticketAndHolders.get(i).getSecond();
      writeTicket(writer, event, ticket, list, i, userTicketInfoMap);
    }

    writer.flush();
    writer.close();

    return baos.toByteArray();
  }
  public void save() throws IOException {
    FileOutputStream output = null;

    try {
      output = new FileOutputStream(file);
      OutputStreamWriter streamWriter = new OutputStreamWriter(output, "utf-8");
      BufferedWriter writer = new BufferedWriter(streamWriter);

      CSVWriter csv = new CSVWriter(writer);

      synchronized (this) {
        for (Map.Entry<String, NamedLocation> entry : locs.entrySet()) {
          NamedLocation warp = entry.getValue();

          csv.writeNext(
              new String[] {
                warp.getName(),
                warp.getWorldName() != null
                    ? warp.getWorldName()
                    : warp.getLocation().getWorld().getName(),
                warp.getCreatorName(),
                String.valueOf(warp.getLocation().getX()),
                String.valueOf(warp.getLocation().getY()),
                String.valueOf(warp.getLocation().getZ()),
                String.valueOf(warp.getLocation().getPitch()),
                String.valueOf(warp.getLocation().getYaw()),
              });
        }
      }

      csv.flush();
      csv.close();
    } finally {
      if (output != null) {
        try {
          output.close();
        } catch (IOException e) {
        }
      }
    }
  }
  @Override
  public Writable serialize(Object obj, ObjectInspector objInspector) throws SerDeException {
    final StructObjectInspector outputRowOI = (StructObjectInspector) objInspector;
    final List<? extends StructField> outputFieldRefs = outputRowOI.getAllStructFieldRefs();

    if (outputFieldRefs.size() != numCols) {
      throw new SerDeException(
          "Cannot serialize the object because there are "
              + outputFieldRefs.size()
              + " fields but the table has "
              + numCols
              + " columns.");
    }

    // Get all data out.
    for (int c = 0; c < numCols; c++) {
      final Object field = outputRowOI.getStructFieldData(obj, outputFieldRefs.get(c));
      final ObjectInspector fieldOI = outputFieldRefs.get(c).getFieldObjectInspector();

      // The data must be of type String
      final StringObjectInspector fieldStringOI = (StringObjectInspector) fieldOI;

      // Convert the field to Java class String, because objects of String
      // type
      // can be stored in String, Text, or some other classes.
      outputFields[c] = fieldStringOI.getPrimitiveJavaObject(field);
    }

    final StringWriter writer = new StringWriter();
    final CSVWriter csv = newWriter(writer, separatorChar, quoteChar, escapeChar);

    try {
      csv.writeNext(outputFields);
      csv.close();

      return new Text(writer.toString());
    } catch (final IOException ioe) {
      throw new SerDeException(ioe);
    }
  }
  @Override
  public void writeTo(
      SearchResponse searchResponse,
      Class<?> type,
      Type genericType,
      Annotation[] annotations,
      MediaType mediaType,
      MultivaluedMap<String, Object> httpHeaders,
      OutputStream entityStream)
      throws IOException, WebApplicationException {
    final CSVWriter csvWriter =
        new CSVWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
    final ImmutableSortedSet<String> sortedFields =
        ImmutableSortedSet.copyOf(
            Iterables.concat(searchResponse.fields(), Lists.newArrayList("source", "timestamp")));

    // write field headers
    csvWriter.writeNext(sortedFields.toArray(new String[sortedFields.size()]));

    // write result set in same order as the header row
    final String[] fieldValues = new String[sortedFields.size()];
    for (ResultMessageSummary message : searchResponse.messages()) {
      int idx = 0;
      // first collect all values from the current message
      for (String fieldName : sortedFields) {
        final Object val = message.message().get(fieldName);
        fieldValues[idx++] = ((val == null) ? null : val.toString().replaceAll("\n", "\\\\n"));
        fieldValues[idx++] = ((val == null) ? null : val.toString().replaceAll("\r", "\\\\r"));
      }
      // write the complete line, some fields might not be present in the message, so there might be
      // null values
      csvWriter.writeNext(fieldValues);
    }
    if (csvWriter.checkError()) {
      LOG.error(
          "Encountered unspecified error when writing message result as CSV, result is likely malformed.");
    }
    csvWriter.close();
  }
示例#12
0
 public static void calculateOverlap(String file1, String file2, String outFile)
     throws IOException {
   CSVReader csvReader = new CSVReader(new FileReader(new File(file1)));
   List<String[]> file1Lines = csvReader.readAll();
   csvReader.close();
   csvReader = new CSVReader(new FileReader(new File(file2)));
   List<String[]> file2Lines = csvReader.readAll();
   Map<String, Object> cache = new HashMap<String, Object>();
   csvReader.close();
   for (String[] columns : file2Lines) {
     cache.put(columns[0], null);
   }
   CSVWriter csvWriter = new CSVWriter(new FileWriter(new File(outFile)));
   for (String[] columns : file1Lines) {
     if (cache.containsKey(columns[0])) {
       csvWriter.writeNext(new String[] {columns[0], "MATCH"});
     } else {
       csvWriter.writeNext(new String[] {columns[0], "NO_MATCH"});
     }
   }
   csvWriter.flush();
   csvWriter.close();
 }
  public void write(String file, String databaseName) {
    String queryString = "jdbc:sqlite:" + databaseName + ".db";
    try {
      Class.forName("org.sqlite.JDBC");
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    try {
      Connection conn = DriverManager.getConnection(queryString);
      // String extension = ".csv";
      CSVWriter writer;
      try {
        writer = new CSVWriter(new FileWriter(file), GraphicalInterface.getSplitCharacter());

        String headerNames = "";
        // start with 1 to avoid reading database id
        for (int i = 1; i < GraphicalInterfaceConstants.REACTIONS_COLUMN_NAMES.length; i++) {
          headerNames += GraphicalInterfaceConstants.REACTIONS_COLUMN_NAMES[i] + "#";
        }

        ReactionsMetaColumnManager reactionsMetaColumnManager = new ReactionsMetaColumnManager();

        int metaColumnCount =
            reactionsMetaColumnManager.getMetaColumnCount(
                LocalConfig.getInstance().getDatabaseName());
        for (int j = 1; j < metaColumnCount + 1; j++) {
          headerNames +=
              reactionsMetaColumnManager.getColumnName(
                      LocalConfig.getInstance().getDatabaseName(), j)
                  + "#";
        }

        String[] header = (headerNames.substring(0, headerNames.length() - 1)).split("#");

        writer.writeNext(header);
        int numReactions = GraphicalInterface.reactionsTable.getModel().getRowCount();
        for (int n = 0; n < numReactions; n++) {
          int viewRow = GraphicalInterface.reactionsTable.convertRowIndexToModel(n);

          String knockout = "false";
          String fluxValue = "0.0";
          String reactionAbbreviation = " ";
          String reactionName = " ";
          String reactionString = " ";
          String reversible = " ";
          String lowerBound = "-999999";
          String upperBound = "999999";
          String objective = "0.0";
          String meta1 = " ";
          String meta2 = " ";
          String meta3 = " ";
          String meta4 = " ";
          String meta5 = " ";
          String meta6 = " ";
          String meta7 = " ";
          String meta8 = " ";
          String meta9 = " ";
          String meta10 = " ";
          String meta11 = " ";
          String meta12 = " ";
          String meta13 = " ";
          String meta14 = " ";
          String meta15 = " ";

          // check if null before toString() to avoid null pointer error
          if (GraphicalInterface.reactionsTable
                  .getModel()
                  .getValueAt(viewRow, GraphicalInterfaceConstants.KO_COLUMN)
              != null) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.KO_COLUMN)
                    .toString()
                    .length()
                > 0) {
              knockout =
                  (String)
                      GraphicalInterface.reactionsTable
                          .getModel()
                          .getValueAt(viewRow, GraphicalInterfaceConstants.KO_COLUMN);
            } else {
              knockout = "false";
            }
          }
          if (GraphicalInterface.reactionsTable
                  .getModel()
                  .getValueAt(viewRow, GraphicalInterfaceConstants.FLUX_VALUE_COLUMN)
              != null) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.FLUX_VALUE_COLUMN)
                    .toString()
                    .length()
                > 0) {
              fluxValue =
                  (String)
                      GraphicalInterface.reactionsTable
                          .getModel()
                          .getValueAt(viewRow, GraphicalInterfaceConstants.FLUX_VALUE_COLUMN);
            } else {
              fluxValue = "0.0";
            }
          }
          if (GraphicalInterface.reactionsTable
                  .getModel()
                  .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_ABBREVIATION_COLUMN)
              != null) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_ABBREVIATION_COLUMN)
                    .toString()
                    .length()
                > 0) {
              reactionAbbreviation =
                  (String)
                      GraphicalInterface.reactionsTable
                          .getModel()
                          .getValueAt(
                              viewRow, GraphicalInterfaceConstants.REACTION_ABBREVIATION_COLUMN);
            } else {
              reactionAbbreviation = " ";
            }
          }
          if (GraphicalInterface.reactionsTable
                  .getModel()
                  .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_NAME_COLUMN)
              != null) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_NAME_COLUMN)
                    .toString()
                    .length()
                > 0) {
              reactionName =
                  (String)
                      GraphicalInterface.reactionsTable
                          .getModel()
                          .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_NAME_COLUMN);
            } else {
              reactionName = " ";
            }
          }
          if (GraphicalInterface.reactionsTable
                  .getModel()
                  .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_STRING_COLUMN)
              != null) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_STRING_COLUMN)
                    .toString()
                    .length()
                > 0) {
              reactionString =
                  (String)
                      GraphicalInterface.reactionsTable
                          .getModel()
                          .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_STRING_COLUMN);
            } else {
              reactionString = " ";
            }
          }
          if (GraphicalInterface.reactionsTable
                  .getModel()
                  .getValueAt(viewRow, GraphicalInterfaceConstants.REVERSIBLE_COLUMN)
              != null) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REVERSIBLE_COLUMN)
                    .toString()
                    .length()
                > 0) {
              reversible =
                  (String)
                      GraphicalInterface.reactionsTable
                          .getModel()
                          .getValueAt(viewRow, GraphicalInterfaceConstants.REVERSIBLE_COLUMN);
            } else {
              reversible = " ";
            }
          }
          if (GraphicalInterface.reactionsTable
                  .getModel()
                  .getValueAt(viewRow, GraphicalInterfaceConstants.LOWER_BOUND_COLUMN)
              != null) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.LOWER_BOUND_COLUMN)
                    .toString()
                    .length()
                > 0) {
              lowerBound =
                  (String)
                      GraphicalInterface.reactionsTable
                          .getModel()
                          .getValueAt(viewRow, GraphicalInterfaceConstants.LOWER_BOUND_COLUMN);
            } else {
              lowerBound = "-999999";
            }
          }
          if (GraphicalInterface.reactionsTable
                  .getModel()
                  .getValueAt(viewRow, GraphicalInterfaceConstants.UPPER_BOUND_COLUMN)
              != null) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.UPPER_BOUND_COLUMN)
                    .toString()
                    .length()
                > 0) {
              upperBound =
                  (String)
                      GraphicalInterface.reactionsTable
                          .getModel()
                          .getValueAt(viewRow, GraphicalInterfaceConstants.UPPER_BOUND_COLUMN);
            } else {
              upperBound = "999999";
            }
          }
          if (GraphicalInterface.reactionsTable
                  .getModel()
                  .getValueAt(viewRow, GraphicalInterfaceConstants.BIOLOGICAL_OBJECTIVE_COLUMN)
              != null) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.BIOLOGICAL_OBJECTIVE_COLUMN)
                    .toString()
                    .length()
                > 0) {
              objective =
                  (String)
                      GraphicalInterface.reactionsTable
                          .getModel()
                          .getValueAt(
                              viewRow, GraphicalInterfaceConstants.BIOLOGICAL_OBJECTIVE_COLUMN);
            } else {
              objective = "0.0";
            }
          }

          String metaString = "";
          if (metaColumnCount > 0) {
            // check if null before toString() to avoid null pointer error
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META1_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META1_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta1 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META1_COLUMN);
              }
            }
            metaString += meta1 + "#";
          }
          if (metaColumnCount > 1) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META2_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META2_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta2 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META2_COLUMN);
              }
            }
            metaString += meta2 + "#";
          }
          if (metaColumnCount > 2) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META3_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META3_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta3 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META3_COLUMN);
              }
            }
            metaString += meta3 + "#";
          }
          if (metaColumnCount > 3) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META4_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META4_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta4 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META4_COLUMN);
              }
            }
            metaString += meta4 + "#";
          }
          if (metaColumnCount > 4) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META5_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META5_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta5 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META5_COLUMN);
              }
            }
            metaString += meta5 + "#";
          }
          if (metaColumnCount > 5) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META6_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META6_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta6 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META6_COLUMN);
              }
            }
            metaString += meta6 + "#";
          }
          if (metaColumnCount > 6) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META7_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META7_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta7 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META7_COLUMN);
              }
            }
            metaString += meta7 + "#";
          }
          if (metaColumnCount > 7) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META8_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META8_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta8 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META8_COLUMN);
              }
            }
            metaString += meta8 + "#";
          }
          if (metaColumnCount > 8) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META9_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META9_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta9 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META9_COLUMN);
              }
            }
            metaString += meta9 + "#";
          }
          if (metaColumnCount > 9) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META10_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META10_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta10 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(
                                viewRow, GraphicalInterfaceConstants.REACTION_META10_COLUMN);
              }
            }
            metaString += meta10 + "#";
          }
          if (metaColumnCount > 10) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META11_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META11_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta11 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(
                                viewRow, GraphicalInterfaceConstants.REACTION_META11_COLUMN);
              }
            }
            metaString += meta11 + "#";
          }
          if (metaColumnCount > 11) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META12_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META12_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta12 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(
                                viewRow, GraphicalInterfaceConstants.REACTION_META12_COLUMN);
              }
            }
            metaString += meta12 + "#";
          }
          if (metaColumnCount > 12) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META13_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META13_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta13 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(
                                viewRow, GraphicalInterfaceConstants.REACTION_META13_COLUMN);
              }
            }
            metaString += meta13 + "#";
          }
          if (metaColumnCount > 13) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META14_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META14_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta14 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(
                                viewRow, GraphicalInterfaceConstants.REACTION_META14_COLUMN);
              }
            }
            metaString += meta14 + "#";
          }
          if (metaColumnCount > 14) {
            if (GraphicalInterface.reactionsTable
                    .getModel()
                    .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META15_COLUMN)
                != null) {
              if (GraphicalInterface.reactionsTable
                      .getModel()
                      .getValueAt(viewRow, GraphicalInterfaceConstants.REACTION_META15_COLUMN)
                      .toString()
                      .length()
                  > 0) {
                meta15 =
                    (String)
                        GraphicalInterface.reactionsTable
                            .getModel()
                            .getValueAt(
                                viewRow, GraphicalInterfaceConstants.REACTION_META15_COLUMN);
              }
            }
            metaString += meta15 + "#";
          }

          if (metaString.length() > 0) {
            String[] entries =
                (knockout
                        + "#"
                        + fluxValue
                        + "#"
                        + reactionAbbreviation
                        + "#"
                        + reactionName
                        + "#"
                        + reactionString
                        + "#"
                        + reversible
                        + "#"
                        + lowerBound
                        + "#"
                        + upperBound
                        + "#"
                        + objective
                        + "#"
                        + metaString.substring(0, metaString.length() - 1))
                    .split("#");
            writer.writeNext(entries);
          } else {
            String[] entries =
                (knockout
                        + "#"
                        + fluxValue
                        + "#"
                        + reactionAbbreviation
                        + "#"
                        + reactionName
                        + "#"
                        + reactionString
                        + "#"
                        + reversible
                        + "#"
                        + lowerBound
                        + "#"
                        + upperBound
                        + "#"
                        + objective)
                    .split("#");
            writer.writeNext(entries);
          }
        }
        writer.close();
        conn.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  public static void exportCsv(String unitId) throws IOException {

    Phone p = Phone.find("unitId = ?", unitId).first();

    if (p == null) index(true);

    List<Route> routes = Route.find("phone = ?", p).fetch();

    File outputDirectory =
        new File(Play.configuration.getProperty("application.exportDataDirectory"), unitId);
    File outputZipFile =
        new File(
            Play.configuration.getProperty("application.exportDataDirectory"), unitId + ".zip");

    // write routes
    File routesFile = new File(outputDirectory, unitId + "_routes.csv");
    File stopsFile = new File(outputDirectory, unitId + "_stops.csv");

    if (!outputDirectory.exists()) {
      outputDirectory.mkdir();
    }

    if (outputZipFile.exists()) {
      outputZipFile.delete();
    }

    FileWriter routesCsv = new FileWriter(routesFile);
    CSVWriter rotuesCsvWriter = new CSVWriter(routesCsv);

    FileWriter stopsCsv = new FileWriter(stopsFile);
    CSVWriter stopsCsvWriter = new CSVWriter(stopsCsv);

    String[] routesHeader =
        "unit_id, route_id, route_name, route_description, field_notes, vehicle_type, vehicle_capacity, start_capture"
            .split(",");

    String[] stopsHeader =
        "route_id, stop_sequence, lat, lon, travel_time, dwell_time, board, alight".split(",");

    rotuesCsvWriter.writeNext(routesHeader);
    stopsCsvWriter.writeNext(stopsHeader);

    for (Route r : routes) {

      String[] routeData = new String[routesHeader.length];
      routeData[0] = unitId;
      routeData[1] = r.id.toString();
      routeData[2] = r.routeLongName;
      routeData[3] = r.routeDesc;
      routeData[4] = r.routeNotes;
      routeData[5] = r.vehicleType;
      routeData[6] = r.vehicleCapacity;
      routeData[7] = (r.captureTime != null) ? r.captureTime.toGMTString() : "";

      rotuesCsvWriter.writeNext(routeData);

      List<TripPatternStop> stops = TripPatternStop.find("pattern.route = ?", r).fetch();

      for (TripPatternStop s : stops) {

        String[] stopData = new String[stopsHeader.length];

        stopData[0] = r.id.toString();
        stopData[1] = s.stopSequence.toString();
        stopData[2] = "" + s.stop.location.getCoordinate().y;
        stopData[3] = "" + s.stop.location.getCoordinate().x;
        stopData[4] = "" + s.defaultTravelTime;
        stopData[5] = "" + s.defaultDwellTime;
        stopData[6] = "" + s.board;
        stopData[7] = "" + s.alight;

        stopsCsvWriter.writeNext(stopData);
      }
    }

    rotuesCsvWriter.flush();
    rotuesCsvWriter.close();

    stopsCsvWriter.flush();
    stopsCsvWriter.close();

    DirectoryZip.zip(outputDirectory, outputZipFile);
    FileUtils.deleteDirectory(outputDirectory);

    redirect("/public/data/exports/" + unitId + ".zip");
  }
  /**
   * reads in time plan file, including start, end, depart, arrive, origin,destination, time of
   * walk, drive and transit, store them into a map. the key of this map is a describe of this time
   * plan, for example from which hotspots to which streets block vice verse, the value is the time
   * plan.
   */
  public void readFile(String travelCostFilePath, HouseholdPool householdPool) {

    String pattern = "\\d+";

    final int hholdColumn = 0;

    final int personColumn = 1;

    final int tripColumn = 3;

    // final int modeColumn = 10;

    final int originColumn = 7;

    final int destinationColumn = 8;

    // final int purposeColumn = 9;

    final int walkTimeColumn = 19;

    final int driveTimeColumn = 20;

    final int transitTimeColumn = 21;

    final int waitTimeColumn = 22;

    final int otherTimeColumn = 23;

    final int costColumn = 25;

    // =======2nd row

    final int legTypeColumn = 1;
    final int legIdColumn = 2;
    final int legTimeColumn = 3;

    String[] nextline;
    CSVReader csvReader = null;
    CSVWriter csvWriter = null;

    if (logger.isTraceEnabled()) {
      logger.trace("Dumping household pool");
      for (Map.Entry<Integer, Household> household : householdPool.getHouseholds().entrySet()) {
        logger.trace("Household: " + household.getKey());
        logger.trace(household.getValue().toString());
      }
    }

    String[] headings = {
      "household id",
      "person id",
      "trip id",
      "pirpose",
      "mode",
      "total travel time",
      "income",
      "travel cost"
    };

    try {
      // Skip first two lines as they are headers.
      csvReader =
          new CSVReader(
              new BufferedReader(new FileReader(new File(timePlanFilePath))),
              '\t',
              CSVParser.DEFAULT_QUOTE_CHARACTER,
              2);

      csvWriter = new CSVWriter(new BufferedWriter(new FileWriter(travelCostFilePath)));
      csvWriter.writeNext(headings);

      double[] legs = new double[2];

      int lastHholdID = 0;
      // get the last household ID in the pool to determine trips started within the study area.
      for (Household household : main.getHouseholdPool().getHouseholds().values()) {
        if (household.getId() > lastHholdID) {
          lastHholdID = household.getId();
        }
      }

      while ((nextline = csvReader.readNext()) != null) {
        if (nextline[0].matches(pattern)) {
          // ========trips time=====
          if (legs[0] != 0) {
            ArrayList<Double> trips = (ArrayList<Double>) linksTripsTime.get((int) legs[0]);
            if (trips == null) {
              ArrayList<Double> temp = new ArrayList<>();
              temp.add(legs[1]);
              linksTripsTime.put((int) legs[0], temp);
            } else {
              trips.add(legs[1]);
            }
          }
          legs[0] = legs[1] = 0;

          // ======time plan=====
          /*
           * Only read Time plan with trips that have Origin inside
           * study area, which means all trips with ID less than or
           * equal to "maxID" in HholdPool. The other trips with ID,
           * which is greater than "maxID" in HholdPool, have Origin
           * outside study area
           */
          if (Integer.parseInt(nextline[hholdColumn].trim()) <= lastHholdID) {

            TimePlan timePlan = new TimePlan();

            String editedValue = nextline[originColumn].replace("\"", "").trim();
            timePlan.setOrigin(Integer.parseInt(editedValue));

            editedValue = nextline[destinationColumn].replace("\"", "").trim();
            timePlan.setDestination(Integer.parseInt(editedValue));

            editedValue = nextline[walkTimeColumn].replace("\"", "").trim();
            timePlan.setWalkTime(Double.parseDouble(editedValue));

            editedValue = nextline[driveTimeColumn].replace("\"", "").trim();
            timePlan.setDriveTime(Double.parseDouble(editedValue));

            editedValue = nextline[transitTimeColumn].replace("\"", "").trim();
            timePlan.setTransitTime(Double.parseDouble(editedValue));

            editedValue = nextline[waitTimeColumn].replace("\"", "").trim();
            timePlan.setWaitTime(Double.parseDouble(editedValue));

            editedValue = nextline[otherTimeColumn].replace("\"", "").trim();
            timePlan.setWaitTime(Double.parseDouble(editedValue));

            editedValue = nextline[hholdColumn].replace("\"", "").trim();
            int hhold = Integer.parseInt(editedValue);
            timePlan.setHhold(hhold);

            editedValue = nextline[personColumn].replace("\"", "").trim();
            int person = Integer.parseInt(editedValue);
            timePlan.setPerson(person);

            editedValue = nextline[tripColumn].replace("\"", "").trim();
            int trip = Integer.parseInt(editedValue);
            timePlan.setTrip(trip);

            timePlan.setStart(0);
            timePlan.setEnd(0);
            timePlan.setDepart(0);
            timePlan.setArrive(0);

            /* save successful into Hash Map */
            String hhPersonTrip = String.valueOf(hhold) + "_" + person + "_" + trip;
            // logger.debug(hhPersonTrip);

            // for testing only if commented
            try {
              int mode = main.getHmHhPersonTripTravelMode().get(hhPersonTrip);
              timePlan.setMode(TravelModes.classify(mode));
            } catch (Exception e) {
              logger.error("Exception caught", e);
              logger.debug(hhPersonTrip);
            }

            String label = generateLabel(timePlan.getOrigin(), timePlan.getDestination());

            timePlan.setParkingFee(new BigDecimal(Double.parseDouble(nextline[costColumn])));

            if (main.getHmHhPersonTripTravelMode().get(hhPersonTrip) != null) {
              timePlan.setCost(
                  travelModeSelection
                      .calculateCost(
                          TravelModes.classify(
                              main.getHmHhPersonTripTravelMode()
                                  .get(String.valueOf(hhold) + "_" + person + "_" + trip)),
                          timePlan)
                      .doubleValue());
            }
            TimePlan storedTimePlan = timePlanMap.get(label);
            if (storedTimePlan == null) {
              timePlanMap.put(label, timePlan);
            } else {
              timePlanMap.put(label, compareCost(timePlan, storedTimePlan));
            }

            // store travel cost of each individual in the csv files
            // for testing only if commented
            //						logger.debug(householdPool.getByID(hhold));
            //
            //                        Household household = householdPool.getByID(hhold);
            //
            //                        if (household != null) {
            //                            if (household.getResidents().size() > (person - 1)) {
            //                                storeTravelCost(
            //                                        household.getResidents()
            //                                                .get(person - 1).getIncome(),
            //                                        timePlan, csvWriter);
            //                            } else {
            //                                logger.warn("Individual with ID: " + person + "
            // doesn't exist in household: " + hhold + ". Skipping entry");
            //                            }
            //                        } else {
            //                            logger.warn("Household with ID: " + hhold + " doesn't
            // exist in Pool. Skipping entry");
            //                        }

            /*
             * setup Delta FixedCost & Variable Cost for new Travel
             * Mode Choice design
             */
            setupDeltaFixedCostVariableCost(timePlan, hhPersonTrip);
          }
        } else {
          if (nextline[legTypeColumn].contains("LINK")) {
            if (legs[0] == 0) {
              legs[0] = Math.abs(Double.parseDouble(nextline[legIdColumn]));
              legs[1] = Double.parseDouble(nextline[legTimeColumn]);
            } else {
              if (legs[1] < Double.parseDouble(nextline[legTimeColumn])) {
                legs[0] = Math.abs(Double.parseDouble(nextline[legIdColumn]));
                legs[1] = Double.parseDouble(nextline[legTimeColumn]);
              }
            }
          }
        }
      }

    } catch (FileNotFoundException e) {
      logger.error("Exception caught", e);
    } catch (IOException e) {
      logger.error("Exception caught", e);
    } finally {
      try {
        if (csvReader != null) {
          csvReader.close();
        }
        if (csvWriter != null) {
          csvWriter.close();
        }
      } catch (IOException e) {
        logger.error("Exception caught", e);
      }
    }
  }
示例#16
0
  /** Dumps the results as a series of KML's..one per trip */
  public void writeKMLandCSV() {

    String csv_header =
        "INDEX,DATE,TIME,SECONDS_SINCE_MIDNIGHT,TRIP_ID,DIR,SPEED,LAT,LONG,ALT,EXITS,ENTRANCES";

    try {
      CSVWriter writer = new CSVWriter(new FileWriter("bus_data.csv"), ',');

      // Write the header
      String[] entries = csv_header.split(",");
      writer.writeNext(entries);

      // Setup some style
      LineStyle lineStyle = new LineStyle();
      lineStyle.setColor("ff005555");
      lineStyle.setWidth(5.0);

      IconStyle peopleIconStyle = new IconStyle();
      // Icon peopleIcon = new Icon();
      // peopleIcon.setHref("http://maps.google.com/mapfiles/kml/man.png");
      peopleIconStyle.setIconHref("http://maps.google.com/mapfiles/kml/shapes/man.png");
      peopleIconStyle.setScale(1.50);

      IconStyle wpIconStyleNorth = new IconStyle();
      // wpIcon.setHref("http://maps.google.com/mapfiles/kml/paddle/red-stars.png");
      // wpIconStyleNorth.setIconHref("http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png");
      wpIconStyleNorth.setIconHref("http://maps.google.com/mapfiles/ms/micons/green-dot.png");
      wpIconStyleNorth.setScale(0.50);

      IconStyle wpIconStyleSouth = new IconStyle();
      // wpIcon.setHref("http://maps.google.com/mapfiles/kml/paddle/red-stars.png");
      wpIconStyleSouth.setIconHref("http://maps.google.com/mapfiles/ms/micons/yellow-dot.png");
      wpIconStyleSouth.setScale(0.50);

      Style style_lineStyle = new Style();
      style_lineStyle.setLineStyle(lineStyle);

      Style style_peopleIconStyle = new Style();
      style_peopleIconStyle.setId("peopleIcon");
      style_peopleIconStyle.setIconStyle(peopleIconStyle);

      Style style_wpIconStyleNorth = new Style();
      style_wpIconStyleNorth.setId("wpIconNorth");
      style_wpIconStyleNorth.setIconStyle(wpIconStyleNorth);

      Style style_wpIconStyleSouth = new Style();
      style_wpIconStyleSouth.setId("wpIconSouth");
      style_wpIconStyleSouth.setIconStyle(wpIconStyleSouth);

      Kml[] kml = new Kml[tripCounter];
      Document[] document = new Document[tripCounter];
      Folder[] northFolders = new Folder[tripCounter];
      Folder[] southFolders = new Folder[tripCounter];
      Folder[] peopleFolders = new Folder[tripCounter];
      // Folder[] tracksFolders = new Folder[tripCounter];

      for (int i = 0; i < tripCounter; i++) {
        kml[i] = new Kml();
        kml[i].setXmlIndent(true);
        document[i] = new Document();
        document[i].addStyleSelector(style_lineStyle);
        document[i].addStyleSelector(style_peopleIconStyle);
        document[i].addStyleSelector(style_wpIconStyleNorth);
        document[i].addStyleSelector(style_wpIconStyleSouth);
        kml[i].setFeature(document[i]);

        northFolders[i] = new Folder();
        northFolders[i].setName("North Route");

        southFolders[i] = new Folder();
        southFolders[i].setName("South Route");

        peopleFolders[i] = new Folder();
        peopleFolders[i].setName("People");

        // tracksFolders[i] = new Folder();
        // tracksFolders[i].setName("Tracks");

        document[i].addFeature(northFolders[i]);
        document[i].addFeature(southFolders[i]);
        document[i].addFeature(peopleFolders[i]);
        // document[i].addFeature(tracksFolders[i]);

      }

      double[] lowerKey = new double[1];
      double[] upperKey = new double[1];

      lowerKey[0] = firstTime;
      upperKey[0] = lastTime;

      Object[] foo = geoRecords.range(lowerKey, upperKey);
      for (int i = 0; i < foo.length; i++) {
        GeoRecord nextRecord = (GeoRecord) foo[i];
        int tripNumber = nextRecord.getTripId();
        // System.out.println(foo[i]);
        if (nextRecord.getDirection().equals("N")) {
          northFolders[tripNumber - 1].addFeature(nextRecord.toKML());
        } else {
          southFolders[tripNumber - 1].addFeature(nextRecord.toKML());
        }
        if (nextRecord.getAssociatedPeople().size() > 0) {
          peopleFolders[tripNumber - 1].addFeature(nextRecord.toKMLWithPeople());
        }

        // Write CSV entry
        String csvLine = nextRecord.toCSV();
        String[] entries2 = csvLine.split(",");
        writer.writeNext(entries2);
      }

      // Write the KML files
      for (int i = 0; i < tripCounter; i++) {
        kml[i].createKml("bus_route" + i + ".kml");
      }

      // Close the csv writer
      writer.close();

    } catch (Exception e) {
      System.err.println(e);
    }
  }
  /** {@inheritDoc} */
  @Override
  public void onDataWriterException(final DataWriterException e) {
    CSVWriter writer = null;
    if (e.getErrorId().equalsIgnoreCase("SINGLE_ROW")) {
      try {
        String path =
            GAPageViewDataExceptionHandler.getErrorFilePath(
                e.getTableDefinition().getProperty("relativePath"),
                e.getTableDefinition().getProperty("startDate"));
        File file = FileSystem.getDefault().getFile("logs/" + path, true, true);
        writer = new CSVWriter(new FileWriter(file, true));
        Map<String, Object> row = (Map<String, Object>) e.getData().get("row");
        Object[] values = row.values().toArray();
        Object[] keys = row.keySet().toArray();
        String[] stringValues = new String[values.length];
        String[] headerValue = new String[keys.length];
        for (int i = 0; i < stringValues.length; i++) {
          stringValues[i] = (values[i] != null) ? values[i].toString() : null;
          headerValue[i] = (keys[i] != null) ? keys[i].toString() : null;
        }
        if (!header) {
          writer.writeNext(headerValue);
          writer.flush();
          header = true;
        }
        String data = stringValues[stringValues.length - 1] + " " + e.getData().get("e");
        stringValues[stringValues.length - 1] = data;
        writer.writeNext(stringValues);
        writer.flush();
        writer.close();
      } catch (Exception ee) {
        logger.error(ee.getMessage());
      }
    } else if (e.getErrorId().equalsIgnoreCase("BATCH_READ")) {
      try {
        String path =
            GAPageViewDataExceptionHandler.getErrorFilePath(
                e.getTableDefinition().getProperty("relativePath"),
                e.getTableDefinition().getProperty("startDate"));
        File file = FileSystem.getDefault().getFile("logs/" + path, true, true);
        writer = new CSVWriter(new FileWriter(file, true));
        String[] row = (String[]) e.getData().get("row");
        String[] headerRow = (String[]) e.getData().get("header");
        if (!header) {
          writer.writeNext(headerRow);
          header = true;
        }

        String data = row[row.length - 1] + e.getData().get("e");
        row[row.length - 1] = data;
        writer.writeNext(row);

        writer.flush();
        writer.close();
      } catch (Exception exceptionObject) {
        logger.error(exceptionObject.getMessage());
      }
    } else if (e.getErrorId().equalsIgnoreCase("ROW_UPDATE")) {
      String[] headerValue = null;
      try {

        String path =
            GAPageViewDataExceptionHandler.getErrorFilePath(
                e.getTableDefinition().getProperty("relativePath"),
                e.getTableDefinition().getProperty("startDate"));
        File file = FileSystem.getDefault().getFile("logs/" + path, true, true);
        writer = new CSVWriter(new FileWriter(file, true));
        Collection<Map<String, Object>> batch =
            (Collection<Map<String, Object>>) e.getData().get("batch");
        super.writerFailedCount = writerFailedCount + batch.size();
        List<String[]> rows = new ArrayList<String[]>(batch.size());
        for (Map<String, Object> row : batch) {
          Object[] values = row.values().toArray();
          Object[] keys = row.keySet().toArray();
          String[] stringValues = new String[values.length];
          headerValue = new String[keys.length];

          for (int i = 0; i < stringValues.length; i++) {
            boolean exceptionFlag = false;
            stringValues[i] = (values[i] != null) ? values[i].toString() : null;
            headerValue[i] = (keys[i] != null) ? keys[i].toString() : null;
          }
          rows.add(stringValues);
        }
        if (!header) {
          writer.writeNext(headerValue);
          writer.flush();
          header = true;
        }

        writer.writeAll(rows);
        writer.flush();
        writer.close();
      } catch (Exception exceptionObject) {
        logger.error(exceptionObject.getMessage(), exceptionObject);
      }
    } else if (e.getErrorId().equalsIgnoreCase("BATCH_UPDATE")) {
      String[] headerValue = null;
      try {
        String path =
            GAPageViewDataExceptionHandler.getErrorFilePath(
                e.getTableDefinition().getProperty("relativePath"),
                e.getTableDefinition().getProperty("startDate"));
        File file = FileSystem.getDefault().getFile("logs/" + path, true, true);
        writer = new CSVWriter(new FileWriter(file, true));
        Collection<Map<String, Object>> batch =
            (Collection<Map<String, Object>>) e.getData().get("batch");
        super.writerFailedCount = writerFailedCount + batch.size();

        List<String[]> rows = new ArrayList<String[]>(batch.size());
        for (Map<String, Object> row : batch) {
          Object[] values = row.values().toArray();
          Object[] keys = row.keySet().toArray();
          String[] stringValues = new String[values.length];
          headerValue = new String[keys.length];

          for (int i = 0; i < stringValues.length; i++) {
            stringValues[i] = (values[i] != null) ? values[i].toString() : null;
            headerValue[i] = (keys[i] != null) ? keys[i].toString() : null;
          }
          rows.add(stringValues);
        }
        if (!header) {
          writer.writeNext(headerValue);
          writer.flush();
          header = true;
        }
        writer.writeAll(rows);
        writer.flush();
        writer.close();
      } catch (IOException e1) {
        logger.warn("GAEventDataExceptionHandler onDataWriterException() " + e.getMessage());
      }
    } else if (e.getErrorId().equalsIgnoreCase("ROW_DUPLICATE")) {
      String[] headerValue = null;
      try {
        String path =
            GAPageViewDataExceptionHandler.getErrorFilePath(
                e.getTableDefinition().getProperty("relativePath"),
                e.getTableDefinition().getProperty("startDate"));
        File file = FileSystem.getDefault().getFile("logs/" + path, true, true);
        writer = new CSVWriter(new FileWriter(file, true));
        Collection<Map<String, Object>> batch =
            (Collection<Map<String, Object>>) e.getData().get("batch");
        super.writerFailedCount = writerFailedCount + batch.size();

        List<String[]> rows = new ArrayList<String[]>(batch.size());
        for (Map<String, Object> row : batch) {
          Object[] values = row.values().toArray();
          Object[] keys = row.keySet().toArray();
          String[] stringValues = new String[values.length];
          headerValue = new String[keys.length];

          for (int i = 0; i < stringValues.length; i++) {
            stringValues[i] = (values[i] != null) ? values[i].toString() : null;
            headerValue[i] = (keys[i] != null) ? keys[i].toString() : null;
          }
          String data = stringValues[stringValues.length - 1] + "  " + e.getData().get("e");
          stringValues[stringValues.length - 1] = data;
          rows.add(stringValues);
        }
        if (!header) {
          writer.writeNext(headerValue);
          writer.flush();
          header = true;
        }

        writer.writeAll(rows);
        writer.flush();
        writer.close();
      } catch (IOException e1) {
        logger.warn("GAEventDataExceptionHandler onDataWriterException() " + e.getMessage());
      }
    }
    super.onDataWriterException(e, false);
  }
示例#18
0
  public static void main(String args[]) {

    //
    // Sets paths for our dev systems based on Windows/Linux
    boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
    boolean isMacOsX = System.getProperty("os.name").toLowerCase().contains("mac os x");
    String subjRootDir;
    if (isWindows) {
      subjRootDir = "C:\\sideprojects\\TrackIt\\experiments\\feb2012\\";
      Util.dPrintln("Detected Windows-based operating system");
    } else if (isMacOsX) {
      subjRootDir = "/Users/spook/code/TrackIt/sample/sample_data_August_2013";
      Util.dPrintln("Detected Mac OS X as operating system");
    } else {
      // subjRootDir = "/home/spook/sideprojects/TrackIt/experiments/feb2012/";
      // subjRootDir = "/usr0/home/jpdicker/Dropbox/organized_DirectTrackIt Files";
      // subjRootDir = "/usr0/home/jpdicker/Dropbox/Gold Standard";
      subjRootDir = "/usr0/home/jpdicker/Dropbox/Moving Eyes 2012-2013 New Numbers";
      // subjRootDir = "/usr0/home/jpdicker/code/TrackIt/sample/sample_data_August_2013";
      Util.dPrintln("Detected *nix-based operating system");
    }

    // Start traversal at root.  This root is organized as follows:
    // root -> {Subject1 -> {tobii1, tobbi2, ..}, Subject2 -> {tobii1, tobii2, ..}, ..}
    File rootDir = new File(subjRootDir);
    if (!rootDir.isDirectory()) {
      Util.dPrintln("Need root directory for files; path not directory: " + subjRootDir);
      return;
    }

    // Lets the user loop through different time offsets
    Vector<Long> timeOffsets = new Vector<Long>();
    for (Long offset = 0L; offset <= 0L; offset += 100L) {
      timeOffsets.add(offset);
    }

    // Lets the user loop through different fixation thresholds (for FixationPointScorer)
    Vector<Double> fixationThresholds = new Vector<Double>();
    fixationThresholds.add(150.0);
    fixationThresholds.add(200.0);
    fixationThresholds.add(250.0);
    fixationThresholds.add(300.0);

    for (Double fixationThreshold : fixationThresholds) {
      for (Long offset : timeOffsets) {

        PilotOutputRecord recorder = new PilotOutputRecord();

        for (File subjectDir : rootDir.listFiles()) {

          // Skip any non-directory files sitting around at the subject directory level
          if (!subjectDir.isDirectory()) {
            Util.dPrintln(subjectDir.getAbsolutePath() + " is not a directory; skipping.");
            continue;
          }

          String subjectID = subjectDir.getName();

          // First, load the "gold standards" (e.g., the .csv files) representing the actual
          // trajectories of the
          // distractors and targets from Track-It
          //
          // Load the actual trajectories for each object in this task, taken straight from Track-It
          Map<String, Experiment> goldStandards = new HashMap<String, Experiment>();

          for (String goldPath : subjectDir.list()) {

            // Ignore non-Track-It files
            if (!goldPath.endsWith(".csv")) {
              continue;
            }

            Experiment goldExperiment = null;
            try {
              Util.dPrintln("Attempting to load gold standard data from " + goldPath);
              goldExperiment = Loader.loadGoldStandard(new File(subjectDir, goldPath));
            } catch (IOException e) {
              System.err.println("IO Error loading gold standard file " + goldPath);
              e.printStackTrace();
              return;
            }

            Util.dPrintln("Loaded " + goldPath);
            Util.dPrintln("Found " + goldExperiment.getNumTrials() + " trials total.");

            // Map the identifier (filename except .csv) to the Experiment
            String goldID = goldPath.substring(0, goldPath.lastIndexOf('.')).toUpperCase();
            goldStandards.put(goldID, goldExperiment);
          }

          // Second, go through each Track-It file in the directory and compare it against the
          // proper gold
          // standard that has (hopefully) been loaded and mapped in the goldStandards HashMap.
          // Unlike with the old
          // version, we'll need to compare timestamps in Track-It to timestamps in Tobii to
          // synchronize the data
          for (String subjEventPath : subjectDir.list()) {

            // Ignore non-Tobii files
            if (!subjEventPath.endsWith(".tsv")) {
              continue;
            }

            // Ignore non-Tobii Event-Map files
            int eventDataIndex = subjEventPath.lastIndexOf("-Event-Data");
            if (eventDataIndex < 0) {
              continue;
            }

            // If the file is a Tobii Event Map file, find its matching
            // All Data file and move on to loading both
            String allDataPrefix = subjEventPath.substring(0, eventDataIndex) + "-All-Data";
            String subjDataPath = null;
            for (String candSubjDataPath : subjectDir.list()) {
              if (candSubjDataPath.startsWith(allDataPrefix)) {
                subjDataPath = candSubjDataPath;
                break;
              }
            }

            // If we couldn't find a match to this Event Data, tell the user and skip
            if (subjDataPath == null) {
              Util.dPrintln("Couldn't find a match to expected Event Data file " + subjEventPath);
              return;
            }

            // Now try to match the Tobii data file to the pre-loaded gold standard file from
            // Track-It\
            // We assume that a file named   02_MackJ_AllSame_01_02-All-Data.tsv  matches with a
            // file named
            //                               02_MackJ_AllSame_01_02.csv           from Track-It.
            String goldID = subjEventPath.substring(0, subjEventPath.indexOf('-')).toUpperCase();
            if (!goldStandards.containsKey(goldID)) {
              Util.dPrintln(
                  "Couldn't find a matching Track-It file for Tobii file "
                      + subjEventPath.toUpperCase()
                      + "; we expected "
                      + goldID
                      + ".csv");
              return;
            }
            Experiment goldExperiment = goldStandards.get(goldID);

            // Try to figure out what type of trial this is (ALL DIFF A, ALL SAME B, etc)
            PilotOutputRecord.TRIAL_TYPE trialType =
                PilotOutputRecord.TRIAL_TYPE.inferTrialType(subjEventPath);
            if (trialType.equals(PilotOutputRecord.TRIAL_TYPE.UNKNOWN)) {
              Util.dPrintln(
                  "Couldn't discern what type of trial (e.g., ALL DIFF A, ALL SAME B) this was from the Event Data file "
                      + subjEventPath);
              return;
            } else {
              Util.dPrintln(
                  "I think I've found trial type " + trialType + " from filename " + subjEventPath);
            }

            File subjEventFile = new File(subjectDir, subjEventPath);
            File subjDataFile = new File(subjectDir, subjDataPath);

            //
            // Grab the overview event data for a single user
            TobiiEventMap eventMap = null;
            try {
              Util.dPrintln("Attempting to load trial data from subject path " + subjEventPath);
              eventMap = Loader.loadTobiiEvent3(subjEventFile);
            } catch (IOException e) {
              System.err.println("IO Error loading event data file " + subjEventPath);
              e.printStackTrace();
              return;
            }

            Util.dPrintln("Loaded event data from file " + subjEventPath);

            //
            // Grab the overview event data for a single user
            TobiiData subjectData = null;
            try {
              Util.dPrintln("Attempting to load Tobii data chunk from " + subjDataPath);
              subjectData = Loader.loadTobiiData3(subjDataFile, eventMap);
            } catch (IOException e) {
              System.err.println("Error loading Tobii data chunk from " + subjDataPath);
              e.printStackTrace();
              return;
            }

            Util.dPrintln("Loaded Tobii data chunk from " + subjDataPath);

            //
            // Score the subject's curves against the gold standard
            Util.dPrintln("Comparing subject's trajectory against gold standard.");

            // long offset = 000;   // in ms
            PointComparator f = new DeltaOffsetPointComparator(offset);

            double exponent = 2.0;
            DistanceFunction dist = new EuclideanDistanceFunction();
            // DistanceFunction dist = new FalloffDistanceFunction(exponent);
            Scorer scorer = new FixationPointScorer(f, dist, fixationThreshold);
            // Scorer scorer = new PointByPointScorer(f, dist);

            // Record ALL score data in separate files, if desired
            boolean recordEverything = false;
            Map<Integer, List<Double>> recordMap = null;
            if (recordEverything) {
              recordMap = new HashMap<Integer, List<Double>>();
            }

            // Requires that a subject's eyes be tracked by Tobii for at least X% of the time; else,
            // score = NaN
            double lookThreshold = 0.0;

            // if(goldExperiment.getTrials().size() != 3) {
            //	System.out.println(goldExperiment);
            //	System.exit(-1);
            // }

            Map<Integer, SingleRunScore> scores =
                scorer.scoreSubject(goldExperiment, subjectData, recordMap, lookThreshold);

            // Possibly record every single score in a separate file
            if (recordEverything) {
              try {
                for (Integer trialID : scores.keySet()) {
                  CSVWriter writer =
                      new CSVWriter(
                          new FileWriter(
                              new File(
                                  "exact_"
                                      + trialType
                                      + "_"
                                      + subjectID
                                      + "_"
                                      + trialID
                                      + "_"
                                      + System.currentTimeMillis()
                                      + ".csv")));
                  for (Double score : recordMap.get(trialID)) {
                    if (score == null) {
                      // For now, record invalid looks as negative score
                      // score = -1.0;
                      Util.dPrintln("Null score for subject " + subjectID + " on trial " + trialID);
                      continue;
                    }
                    writer.writeNext(new String[] {String.valueOf(score)});
                  }
                  writer.close();
                }
              } catch (IOException e) {
                e.printStackTrace();
              }
            }

            // Record the subject's score in our map of scores, for charting later
            recorder.addHeader("scorer", scorer.getClass().getName());
            recorder.addHeader("offset", offset + " ms");
            recorder.addHeader("exponent", String.valueOf(exponent));
            recorder.addHeader("comparator function", f.getClass().getName());
            recorder.addHeader("distance function", dist.getClass().getName());
            recorder.addHeader("scorer", scorer.getClass().getName());
            recorder.addHeader("fixationThreshold", String.valueOf(fixationThreshold));
            recorder.addHeader("lookThreshold", String.valueOf(lookThreshold) + " %");

            recorder.addScore(trialType, subjectID, scores);
          } // end of individual subject's listing loop

          // For printing purposes, make sure we fill in any Tobii files that were missing with NaN
          // scores
          recorder.fakeMissedTobiiTests(subjectID);
        } // end of root directory listing loop

        // Write all our records to a .csv file
        try {
          recorder.writeToCSV(
              "eyetrack_offset_" + offset.toString() + "_" + System.currentTimeMillis() + ".csv");
        } catch (IOException e) {
          e.printStackTrace();
        }
      } // end of time offsets outer loop
    } // end of fixation thresholds outer loop

    return;
  }
  /**
   * Extract trips among travel modes: BUS, LIGHT RAIL, CAR_DRIVER, CAR_PASSENGER in order to write
   * into CSV file and database.
   *
   * @param timePlanFileName
   * @param tripFileName
   */
  public ArrayList<String[]> postProcessTIMEPLANS(String timePlanFileName, String tripFileName) {
    ArrayList<String[]> outputLineArrayList = new ArrayList<>();
    final String[] outputHeader = {"HHOLD,PERSON,TRIP,MODE,DEPART,PURPOSE,TRIP_LENGTH"};

    try {
      String[] line;
      //			HashMap<String, String> hmHHoldPersonTripMode = new HashMap<>();
      //
      //			/* A - PROCESS THE TRIPS FILE */
      //			CSVReader csvReader = new CSVReader(new BufferedReader(new FileReader(tripFileName)),
      // '\t');
      //
      //			// skip the header rows
      //            csvReader.readNext();
      //
      //			while ((line = csvReader.readNext()) != null) {
      //                String hHold = line[0];
      //                String person = line[1];
      //                String trip = line[2];
      //                String mode = line[4];
      //
      //				String hHoldPersonTrip = hHold + "_" + person + "_" + trip;
      //				hmHHoldPersonTripMode.put(hHoldPersonTrip,
      //                        TravelModes.classify(Integer.parseInt(mode)).getTranssimsName());
      //			}
      //            csvReader.close();

      /* B - PROCESS THE TIMEPLANS FILE */
      CSVReader csvReader =
          new CSVReader(new BufferedReader(new FileReader(timePlanFileName)), '\t');
      CSVWriter csvWriter =
          new CSVWriter(
              new BufferedWriter(new FileWriter(timePlanFileName + "_new.csv")),
              CSVWriter.DEFAULT_SEPARATOR,
              CSVWriter.NO_QUOTE_CHARACTER);

      // skip the 2 header rows
      String[] firstHeaderLine = csvReader.readNext();
      csvReader.readNext();

      // write the header for .csv output file
      csvWriter.writeNext(outputHeader);

      //            int lastHholdID = 0;
      //			// get the last household ID in the pool to determine trips started within the study
      // area.
      //			for (Household household : main.getHouseholdPool().getHouseholds().values()) {
      //				if (household.getId() > lastHholdID) {
      //					lastHholdID = household.getId();
      //				}
      //			}

      while ((line = csvReader.readNext()) != null) {
        // only save the overall rows not the leg rows
        if (line.length == firstHeaderLine.length) {

          String hHold = line[0];
          String person = line[1];
          String trip = line[3];
          String depart = line[16];
          String purpose = line[9];
          String tripLength = line[24];

          String hHoldPersonTrip = hHold + "_" + person + "_" + trip;
          String purposeString = getPurposeString(Integer.parseInt(purpose));

          // only save and write the travel modes: BUS, LIGHT RAIL,
          // CAR_DRIVER, CAR_PASSENGER
          // if (!mode.equalsIgnoreCase("WALK")
          // && !mode.equalsIgnoreCase("HOV4")
          // && !mode.equalsIgnoreCase("BIKE")) {
          // mode = hmHHoldPersonTripMode.get(hHoldPersonTrip);
          int modeTransimInteger =
              ModelMain.getInstance().getHmHhPersonTripTravelMode().get(hHoldPersonTrip);
          String mode = TravelModes.classify(modeTransimInteger).getTranssimsName();

          String[] outputLine = {hHold, person, trip, mode, depart, purposeString, tripLength};
          csvWriter.writeNext(outputLine);

          outputLineArrayList.add(outputLine);
        }
      }
      //			}
      // logger.debug("");
      csvReader.close();
      csvWriter.close();

    } catch (Exception e) { // Catch exception if any
      logger.error("Exception caught", e);
    }

    return outputLineArrayList;
  }
示例#20
0
  public boolean saveAsCSVFile(File file) {
    if (this.type == Statement.Type.Unknown) {
      return false;
    }

    boolean status = false;

    FileOutputStream fileOutputStream = null;
    OutputStreamWriter outputStreamWriter = null;
    CSVWriter csvwriter = null;

    try {
      fileOutputStream = new FileOutputStream(file);
      outputStreamWriter = new OutputStreamWriter(fileOutputStream, Charset.forName("UTF-8"));
      csvwriter = new CSVWriter(outputStreamWriter);

      for (Map.Entry<String, String> metadata : metadatas.entrySet()) {
        String key = metadata.getKey();
        String value = metadata.getValue();
        String output = key + "=" + value;
        csvwriter.writeNext(new String[] {output});
      }

      // Do not obtain "type" through statements, as there is possible that
      // statements is empty.
      final List<String> strings =
          Statement.typeToStrings(this.getType(), this.getGUIBundleWrapper());
      final int columnCount = strings.size();
      String[] datas = new String[columnCount];

      // First row. Print out table header.
      for (int i = 0; i < columnCount; i++) {
        datas[i] = strings.get(i);
      }

      csvwriter.writeNext(datas);

      final int rowCount = statements.size();
      for (int i = 0; i < rowCount; i++) {
        for (int j = 0; j < columnCount; j++) {
          // Value shouldn't be null, as we prevent atom with null value.
          final String value = statements.get(i).getAtom(j).getValue().toString();
          datas[j] = value;
        }
        csvwriter.writeNext(datas);
      }

      status = true;
    } catch (IOException ex) {
      log.error(null, ex);
    } finally {
      if (csvwriter != null) {
        try {
          csvwriter.close();
        } catch (IOException ex) {
          log.error(null, ex);
        }
      }
      org.yccheok.jstock.gui.Utils.close(outputStreamWriter);
      org.yccheok.jstock.gui.Utils.close(fileOutputStream);
    }

    return status;
  }
示例#21
0
 public void close() throws IOException {
   logFW_count.close();
   logFile_count.close();
   logFW_names.close();
   logFile_names.close();
 }