public void loadSerializedData() {
    // fixme        Race x = loadXML();
    // return;
    // lastRace.getName();
    //        RaceRun run;
    try {
      //          lastRace.loadSerializedData();
      String fileName = lastRace.getName();
      FileInputStream fileIn = new FileInputStream(fileName + ".ser"); // "RaceRun.ser");
      try {
        ObjectInputStream in = new ObjectInputStream(fileIn);
        deSerialize(in);

        in.close();
        fileIn.close();

        tagHeuerConnected = new Boolean(false); // / make sure it exists - transient object
        microgateConnected = new Boolean(false); // / make sure it exists - transient object

      } catch (InvalidClassException ice) {
        log.info("Invalid Class from deserialization " + ice.classname);
      } catch (EOFException eof) {
        log.info("EOF on Serialized data");
      } catch (IOException i) {
        i.printStackTrace();
        // } catch (ClassNotFoundException cnf) {
        //    cnf.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }
    } catch (FileNotFoundException fnf) {
      // Empty block OK - ignore this exception
    }

    // load required transient members
    Log raceRunLog = Log.getInstance();
    for (RaceRun r : activeRuns) {
      r.setLog(raceRunLog);
    }
    for (RaceRun r : completedRuns) {
      r.setLog(raceRunLog);
    }
    if (pendingRerun != null) {
      pendingRerun.setLog(raceRunLog);
    }

    //     updateResults();   //todo OK Here ???        NO - didn't set

  }
  private Race() {
    if (trainingMode == 1) {
      MAX_RUNS_ALLOWED = 99;
    }

    log = Log.getInstance();
    //  xstream = initXML();   D20160407 RIO CAUSING CRASH
    clearRace();

    lastRace = new LastRace();
    tagHeuerConnected = false; // new Boolean(false);
    microgateConnected = false;
    // D161004        resultsHTTP = new SlalomResultsHTTP_Save();
    // todo set up on timing page to test and then enable CP520
    // C160315        Thread t = new Thread( photoEyeListener = new PhotoEyeListener());
    // C160315        t.start();

    // TODO - determine if any photo eyes in use, add appropriate handler/listener
    // D20160305maybeStartPhotoCellInterface();  Was being called in ClietnPenalty App

    // D161004
    racerResultsHTTP = new SlalomRacerResultsHTTP();
    scoreboardResultsHTTP_New = new SlalomResultsScoringHTTP();
    someResultsHTTP = new SlalomResultsHTTP();
  }
  private void deSerialize(ObjectInputStream in) throws DuplicateBibException {
    Object o;
    try {
      while ((o = in.readObject()) != null) {
        Race raceFromSerialized = (Race) o;

        this.date = raceFromSerialized.date;
        this.name = raceFromSerialized.name;
        this.location = raceFromSerialized.location;
        setNbrGates(raceFromSerialized.nbrGates); // this.nbrGates = race.nbrGates;// = 25;
        this.upstreamGates = raceFromSerialized.upstreamGates;
        this.judgingSections = raceFromSerialized.judgingSections;
        for (JudgingSection s : judgingSections) {
          // must assign all transients, otherwise they are null and cause problsm
          s.setClientDeviceAttached(new Boolean(false));
        }

        // Race Status
        this.pendingRerun = raceFromSerialized.pendingRerun;
        this.activeRuns = raceFromSerialized.activeRuns;
        this.completedRuns = raceFromSerialized.completedRuns;

        this.startList = raceFromSerialized.startList;
        this.runsStartedOrCompletedCnt = raceFromSerialized.runsStartedOrCompletedCnt;
        this.currentRunIteration =
            raceFromSerialized.currentRunIteration; // are we on 1st runs, or 2nd runs ?
        this.racers = raceFromSerialized.racers;
        this.tagHeuerEnabled =
            raceFromSerialized.tagHeuerEnabled; // todo REMOVE ->tagHeuerEmulation =
        // raceFromSerialized.tagHeuerEmulation;
        this.microgateEnabled = raceFromSerialized.microgateEnabled;
        this.microgateEnabled = raceFromSerialized.timyEnabled;

        this.icfPenalties = raceFromSerialized.icfPenalties;
      }

    } catch (InvalidClassException ice) {
      clearRace();
      System.out.println("All data cleared, incompatible race object version information");
    } catch (EOFException io) {
      // io.printStackTrace();

    } catch (IOException io) {
      io.printStackTrace();
    } catch (ClassNotFoundException cnfe) {
      cnfe.printStackTrace();
    }
    String duplicates = lookForDuplicateBibsInTheSameClass();
    if (duplicates != null) {
      log.error(duplicates);
      throw new DuplicateBibException();
    }
  }
  private void addaFewCompletedRuns(ArrayList<RaceRun> scorable) {

    // add all runs for now
    try {

      for (RaceRun r : completedRuns) {
        scorable.add(r);
      }
    } catch (Exception e) {
      log.warn("BAD ARRAY EXCEPTION AJM");
    }
  }
  public void maybeStartPhotoCellInterface() {

    if (photoEyeThread == null) {

      photoEyeListener = new PhotoEyeListener();
      photoEyeThread = new Thread(photoEyeListener);
      photoEyeThread.start();

      photoCellAgent = photoEyeListener.getAgent();

    } else {
      log.info("ALREADY HAVE A THREAD HERE !!!");
    }
  }
  public String lookForDuplicateBibsInTheSameClass() {
    String returnString = null;
    for (BoatEntry boat : startList) {
      if (count(boat.getRacer().getBibNumber(), boat.getBoatClass()) > 1) {

        returnString =
            "Bib number "
                + boat.getRacer().getBibNumber()
                + " occurs more than once in "
                + boat.getBoatClass();
        System.out.println(returnString);
        log.warn(returnString);
      }
    }
    return (returnString);
  }
 /**
  * Add an entry to the StartList rejecting any duplicate Bib assignments in the same race class It
  * is possible to have the same bib in a different classes in lower levels of races (e.g. C1 and
  * C2)
  *
  * @param racer
  * @param boatClass
  * @throws DuplicateBibException
  */
 public void addBoat(Racer racer, String boatClass) throws DuplicateBibException {
   if (count(racer.getBibNumber(), boatClass) < 1) {
     racers.add(racer);
     startList.add(new BoatEntry(racer, boatClass));
   } else {
     log.error(
         "Can't add bib "
             + racer.getBibNumber()
             + " for "
             + boatClass
             + " "
             + racer.getShortName()
             + " already used by "
             + whoIs(racer.getBibNumber(), boatClass));
     throw new DuplicateBibException();
   }
 }
  // fixme todo change all to XML serialization, so class versions are NOT an issue !
  public void saveSerializedData() {

    // saveXML();
    try {
      lastRace.setName(getName());
      lastRace.saveSerializedData();
      String filename = getName() + ".ser";

      FileOutputStream fileOut = new FileOutputStream(filename);
      ObjectOutputStream out =
          new ObjectOutputStream(
              fileOut); /// MUST FIX 2016 Nationals ERROR CONCurrentMpdificationException
      out.writeObject(
          this); // See   Ref#20161008 This was Line 913 BELOW on 20161008   //// This was line 869
      // in bwlow Log -->> Got CONCurrentMpdificationException ///20160727 in test
      /*
                  PRIOR CRASH  Prior to 20161008  See bottom of source file for new carsh dump
      at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:329)
      	at com.tcay.slalom.Race.saveSerializedData(Race.java:869)
      	at com.tcay.slalom.Race.updateResults(Race.java:1177)
      	at com.tcay.slalom.Race.updateResults(Race.java:1168)
      	at com.tcay.slalom.RaceRun.updateResults(RaceRun.java:535)
      	at com.tcay.slalom.RaceRun.setPhotoCellRaceRun(RaceRun.java:139)
      	at com.tcay.slalom.Race.associatePhotoCellRun(Race.java:1163)
      	at com.tcay.slalom.timingDevices.PhotoCellAgent.saveResult(PhotoCellAgent.java:57)
      	at com.tcay.slalom.timingDevices.tagHeuer.TagHeuerAgent.processDeviceOutput(TagHeuerAgent.java:174)
      	at com.tcay.RS232.PhotoEyeListener.readAndProcess(PhotoEyeListener.java:241)
      	at com.tcay.RS232.PhotoEyeListener.processPhotoEyeDataFromDevice(PhotoEyeListener.java:190)
      	at com.tcay.RS232.PhotoEyeListener.listenAndProcessPortOutput(PhotoEyeListener.java:304)
      	at com.tcay.RS232.PhotoEyeListener.run(PhotoEyeListener.java:76)
                  */

      out.close();
      fileOut.close();
      log.trace("Saved serialized data to " + filename);

    } catch (IOException i) {
      i.printStackTrace();
    }
  }
  private ArrayList<Result> accumulateResults(ArrayList<RaceRun> runs) {
    Result res;
    results = new ArrayList<Result>();

    for (RaceRun r : runs) {
      res = findRacer(r);
      if (res == null) {
        res = new Result();
        results.add(res);
      }
      res.setRacer(r.getBoat().getRacer());
      res.setBoat(r.getBoat());

      switch (r.getRunNumber()) {
        case 1:
          res.setRun1(r);
          break;
        case 2:
          res.setRun2(r);
          break;
      }
    }

    float run1Time;
    float run2Time;
    for (Result res1 : results) {
      // run1Time = (float)999999.99;
      // run2Time = (float)999999.99;

      try {

        if (res1.getRun1() == null || res1.getRun2() == null) {
          if (res1.getRun1() == null) {
            res1.setBestRun(res1.getRun2());
          }
          if (res1.getRun2() == null) {
            res1.setBestRun(res1.getRun1());
          }
        } else {
          run1Time = res1.getRun1().getElapsed() + res1.getRun1().getTotalPenalties();
          run2Time = res1.getRun2().getElapsed() + res1.getRun2().getTotalPenalties();
          if (run1Time <= run2Time) {
            res1.setBestRun(res1.getRun1());
          } else {
            res1.setBestRun(res1.getRun2());
          }
        }
      } catch (Exception e) {
        log.write(e);
      }
    }

    ArrayList<Result> sorted = Result.getResultsByClassTime(results);
    results = sorted; // A10112013

    String lastBoatClass = "";
    int place = 1;

    for (Result r : sorted) {
      try {
        r.getRun1()
            .setGold(false); // / TODO: if skipping 1st runs fro some reason, this will cause a null
        // pointer reference
        r.getRun1().setSilver(false);
        r.getRun1().setBronze(false);
      } catch (NullPointerException e) {
        // Intentionally empty exception block
      }

      try {
        r.getRun2().setGold(false);
        r.getRun2().setSilver(false);
        r.getRun2().setBronze(false);
      } catch (NullPointerException e) {
        // Intentionally empty exception block
      }

      // todo check this logic   20141122
      try {
        if (lastBoatClass.compareTo(r.getBoat().getBoatClass()) != 0) {
          lastBoatClass = r.getBoat().getBoatClass();
          place = 1;
        }
        switch (place) {
          case 1:
            r.getBestRun().setGold(true);
            break;
          case 2:
            r.getBestRun().setSilver(true);
            break;
          case 3:
            r.getBestRun().setBronze(true);
            break;
          default:
            break;
        }
      } catch (NullPointerException e) {
        // Intentionally empty exception block
      }

      r.getBestRun().setPlaceInClass(place++);
    }
    return (sorted);
  }