Esempio n. 1
0
  public void writeFile(String name) {
    if (log.isDebugEnabled()) {
      log.debug("writeFile {}", name);
    }
    // This is taken in large part from "Java and XML" page 368
    File file = findFile(name);
    if (file == null) {
      file = new File(name);
    }

    PrintWriter fileOut = null;

    try {
      fileOut =
          new PrintWriter(
              new BufferedWriter(
                  new OutputStreamWriter(new FileOutputStream(file), "UTF-8")), // NOI18N
              true); // NOI18N
    } catch (IOException e) {
      log.error("Can not open export cars CSV file: " + file.getName());
      return;
    }

    // create header
    String header =
        Bundle.getMessage("Name")
            + del
            + Bundle.getMessage("Description")
            + del
            + Bundle.getMessage("Time")
            + del
            + Bundle.getMessage("Route")
            + del
            + Bundle.getMessage("Departs")
            + del
            + Bundle.getMessage("Terminates")
            + del
            + Bundle.getMessage("Status")
            + del
            + Bundle.getMessage("Comment");
    fileOut.println(header);

    int count = 0;

    for (Train train : TrainManager.instance().getTrainsByTimeList()) {
      if (!train.isBuildEnabled()) continue;
      count++;
      String routeName = "";
      if (train.getRoute() != null) routeName = train.getRoute().getName();
      String line =
          ESC
              + train.getName()
              + ESC
              + del
              + ESC
              + train.getDescription()
              + ESC
              + del
              + ESC
              + train.getDepartureTime()
              + ESC
              + del
              + ESC
              + routeName
              + ESC
              + del
              + ESC
              + train.getTrainDepartsName()
              + ESC
              + del
              + ESC
              + train.getTrainTerminatesName()
              + ESC
              + del
              + ESC
              + train.getStatus()
              + ESC
              + del
              + ESC
              + train.getComment()
              + ESC;
      fileOut.println(line);
    }

    fileOut.println();
    // second create header for built trains
    header =
        Bundle.getMessage("Name")
            + del
            + Bundle.getMessage("csvParameters")
            + del
            + Bundle.getMessage("Attributes");
    fileOut.println(header);

    for (Train train : TrainManager.instance().getTrainsByTimeList()) {
      if (!train.isBuildEnabled()) continue;

      if (train.isBuilt() && train.getRoute() != null) {
        StringBuffer line =
            new StringBuffer(ESC + train.getName() + ESC + del + Bundle.getMessage("Route"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + rl.getName() + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(
                ESC + train.getName() + ESC + del + Bundle.getMessage("csvArrivalTime"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getExpectedArrivalTime(rl) + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(
                ESC + train.getName() + ESC + del + Bundle.getMessage("csvDepartureTime"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getExpectedDepartureTime(rl) + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(
                ESC + train.getName() + ESC + del + Bundle.getMessage("csvTrainDirection"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + rl.getTrainDirectionString() + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(
                ESC + train.getName() + ESC + del + Bundle.getMessage("csvTrainWeight"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getTrainWeight(rl) + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(
                ESC + train.getName() + ESC + del + Bundle.getMessage("csvTrainLength"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getTrainLength(rl) + ESC);
        }
        fileOut.println(line);

        line = new StringBuffer(ESC + train.getName() + ESC + del + Bundle.getMessage("Cars"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getNumberCarsInTrain(rl) + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(ESC + train.getName() + ESC + del + Bundle.getMessage("csvEmpties"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getNumberEmptyCarsInTrain(rl) + ESC);
        }
        fileOut.println(line);
        fileOut.println();
      }
    }

    fileOut.flush();
    fileOut.close();
    log.info("Exported {} trains to file {}", count, defaultOperationsFilename());
    JOptionPane.showMessageDialog(
        null,
        MessageFormat.format(
            Bundle.getMessage("ExportedTrainsToFile"),
            new Object[] {count, defaultOperationsFilename()}),
        Bundle.getMessage("ExportComplete"),
        JOptionPane.INFORMATION_MESSAGE);
  }