public void webserviceUpdate() throws Exception {
    log.debug(">>> START webserviceUpdate...");
    setFileNumber(getCurrentStudent().getFileNumber());
    QueryByDLResponseType wsResponse = getQueryByDl();
    Student student = parseQueryByDlResults(wsResponse);
    log.debug("");
    if (student != null) {
      // redmine 28996
      if (student.getStudentNumber() != null) {
        setStudentNumber(student.getStudentNumber());
      }

      // fullUpdate sends nulls
      student.setAlmLog("No");
      int i = getStudentService().fullUpdate(student);
      if (student.getStudentNumber() == null) {
        student.setStudentNumber(getStudentNumber());
      }

      // AUDIT
      checkAuditDates(student);
      setCurrentStudent(student);
      setStudentPk(student.getStudentPk());
    } else {
      log.debug("webserviceUpdate failed");
    }
  }
  private Student parseQueryByDlResults(
      QueryByDLResponseType wsResponse, boolean lookupByStudentNumber) throws Exception {
    log.debug("LOOKUP BY STUDENT NUMBER " + lookupByStudentNumber);
    boolean studentInsert = false;
    Student temp = new Student();
    if (lookupByStudentNumber) {
      temp.setStudentNumber(getStudentNumber());
    } else if (getFileNumber() != null && getFileNumber().length() > 1) {
      temp.setFileNumber(getFileNumber());
    } else {
      temp.setStudentPk(getStudentPk()); // required for multiple student records
    }

    if ("200".equals(wsResponse.getStatus())) {
      try {
        log.debug("Status 200");
        Student student = getStudentService().getStudent(temp);

        if (student.getStudentPk() == null) {
          studentInsert = true;
        }

        student.setDob(wsResponse.getSubject().getBirthDate());
        student.setFirstName(wsResponse.getSubject().getGivenName());
        student.setMiddleName(wsResponse.getSubject().getMiddleName());
        student.setLastName(wsResponse.getSubject().getSurName());

        if (wsResponse.getStudentDriverCertificate() != null) {
          if (wsResponse.getStudentDriverCertificate().getClassroomCompletionDate() != null) {
            student.setClassroomCompletionDate(
                wsResponse.getStudentDriverCertificate().getClassroomCompletionDate());
          }
          if (wsResponse.getStudentDriverCertificate().getClassroomSchoolId() != null) {
            student.setClassroomCompletionSchoolNumber(
                blankInteger(wsResponse.getStudentDriverCertificate().getClassroomSchoolId()));
          }

          if (wsResponse.getStudentDriverCertificate().getObservationCompletionDate() != null) {
            student.setObservationCompletionDate(
                wsResponse.getStudentDriverCertificate().getObservationCompletionDate());
          }
          if (wsResponse.getStudentDriverCertificate().getObservationSchoolId() != null) {
            student.setObservationCompletionSchoolNumber(
                blankInteger(wsResponse.getStudentDriverCertificate().getObservationSchoolId()));
          }

          if (wsResponse.getStudentDriverCertificate().getWheelCompletionDate() != null) {
            student.setBehindWheelCompletionDate(
                wsResponse.getStudentDriverCertificate().getWheelCompletionDate());
          }
          if (wsResponse.getStudentDriverCertificate().getWheelSchoolId() != null) {
            student.setBtwCompletionSchoolNumber(
                blankInteger(wsResponse.getStudentDriverCertificate().getWheelSchoolId()));
          }

          if (wsResponse.getStudentDriverCertificate().getWrittenExamCompletionDateTime() != null) {
            student.setWrittenTestCompletionDate(
                wsResponse
                    .getStudentDriverCertificate()
                    .getWrittenExamCompletionDateTime()
                    .getTime());
          }
          if (wsResponse.getStudentDriverCertificate().getWrittenExamSchoolId() != null) {
            student.setWrittenCompletionSchoolNumber(
                blankInteger(
                    wsResponse.getStudentDriverCertificate().getWrittenExamSchoolId().trim()));
          }

          if (wsResponse.getStudentDriverCertificate().getRoadTestCompletionDateTime() != null) {
            student.setRoadTestCompletionDate(
                wsResponse.getStudentDriverCertificate().getRoadTestCompletionDateTime().getTime());
          }
          if (wsResponse.getStudentDriverCertificate().getRoadTestSchoolId() != null) {
            student.setRoadCompletionSchoolNumber(
                blankInteger(wsResponse.getStudentDriverCertificate().getRoadTestSchoolId()));
          }

          if (wsResponse.getStudentDriverCertificate().getEligibilityDate() != null) {
            student.setEligibilityDate(
                wsResponse.getStudentDriverCertificate().getEligibilityDate());
          }

          if (wsResponse.getDriverLicense().getLicenseCategoryCode() != null) {
            student.setLicenseType(
                wsResponse.getDriverLicense().getLicenseCategoryCode().getValue());
          }

          student.setFileNumber(getFileNumber());
          Person loggedInPerson = (Person) getSession().get(Constants.USER_KEY);
          student.setUpdatedBy(loggedInPerson.getEmail());
        }

        log.debug("#################### returning student");
        return student;
      } catch (DaoException de) {
        log.error("DAO ", de);
        throw new Exception(de);
      }
    } else {
      addActionError("Status " + wsResponse.getStatus() + " " + wsResponse.getStatusDescription());
    }

    log.debug("#################### returning blank student");
    return null;
  }
  // This is a clone of the regular syncFileNumber that allows entry of
  // duplicate student records with the same fileNumber
  public String commercialSyncFileNumber() throws Exception {
    log.debug("******* commercialSyncFileNumber " + getFileNumber());
    boolean noErrors = true;
    boolean studentMerged = false;
    boolean studentExists = false;
    boolean studentFound = false;
    int status = 0;
    Collection<String> col = new ArrayList<String>();

    QueryByDLResponseType wsResponse = getQueryByDl();
    if (wsResponse != null) {
      Student webStudent = parseQueryByDlResults(wsResponse);
      if ("200".equals(wsResponse.getStatus())) {
        studentExists = true;

        // This handles duplicate file numbers, By removing file number from existing record so that
        // it can be added
        if (webStudent.getFileNumber() != null) {
          log.debug("****** Existing FileNumber... preparing to remove");
          Student a = new Student();
          a.setFileNumber(webStudent.getFileNumber());
          Student temp = getDbStudent(a);
          // Only remove if it is the same student otherwise this could be used to remove file
          // numbers from any existing record.
          if (temp != null) {
            log.debug("dbstudent " + temp.getDob() + "  webstudent " + webStudent.getDob());
            if (temp.getDob().equals(webStudent.getDob())) {
              log.debug(
                  "dbstudent lastname "
                      + temp.getLastName()
                      + "  webstudent "
                      + webStudent.getLastName());
              if (temp.getLastName().equalsIgnoreCase(webStudent.getLastName())) {
                temp.setFileNumber(null);
                int i = getStudentService().studentUpdateFileNumber(temp);
                if (i > 0) {
                  log.debug("****** FileNumber removed from: " + temp.getStudentNumber());
                } else {
                  log.debug("****** FileNumber WAS NOT removed from: " + temp.getStudentNumber());
                }
              } else {
                col.add(
                    "Existing student using this filenumber: Last names do not match with DLD record.");
                noErrors = false;
              }
            } else {
              col.add(
                  "Existing student using this filenumber: DOB does not match with DLD record.");
              noErrors = false;
            }
          }
        }

        if (noErrors) {
          log.debug("Student Insert");
          if (getStudentNumber() != null) {
            Student a = new Student();
            a.setStudentNumber(getStudentNumber());
            // getDbStudent returns the sdc student with a matching dob.
            Student dbStudent = getDbStudent(a, webStudent.getDob());

            if (dbStudent != null) {
              log.debug("dbstudent " + dbStudent.getDob() + "  webstudent " + webStudent.getDob());
              studentFound = true;
              log.debug(
                  "db lastname "
                      + dbStudent.getLastName()
                      + "  webstudent "
                      + webStudent.getLastName());
              if (dbStudent.getLastName().equals(webStudent.getLastName())) {
                log.debug(
                    "db firstname "
                        + dbStudent.getFirstName()
                        + "  webstudent "
                        + webStudent.getFirstName());
                if (dbStudent.getFirstName().equals(webStudent.getFirstName())) {
                  studentMerged = true;
                  // merge appropriate values
                  webStudent.setStudentNumber(dbStudent.getStudentNumber());
                  webStudent.setStudentPk(dbStudent.getStudentPk());
                  webStudent.setSchoolFk(getSchoolPk());
                  // fullUpdate sends nulls
                  int i = getStudentService().fullUpdate(webStudent);
                  setCurrentStudent(webStudent);
                  setStudentPk(webStudent.getStudentPk());
                } else {
                  col.add("First names do not match");
                  noErrors = false;
                }
              } else {
                col.add("Last names do not match");
                noErrors = false;
              }
              if (!studentMerged) {
                List<String> web = new ArrayList<String>();
                List<String> db = new ArrayList<String>();

                SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

                web.add(sdf.format(webStudent.getDob()));
                web.add(webStudent.getLastName());
                web.add(webStudent.getFirstName());
                setMergeWebStudent(web);

                db.add(sdf.format(dbStudent.getDob()));
                db.add(dbStudent.getLastName());
                db.add(dbStudent.getFirstName());
                setMergeDbStudent(db);
              }
            } else {
              // dob doesnt match.
              col.add("DOB does not match");
              noErrors = false;
            }
          }
        }
      } else {
        col.add("Student Not Found in DLD(Webservice)");
        noErrors = false;
      }
      if (!noErrors) {
        col.add("The File Number Entered Is Not Valid For This Student");
      }
    } else {
      log.debug("Webservice RESULTS EMPTY");
      noErrors = false;
      col.add("Web service response was empty.");
    }

    if (noErrors) {
      if (studentMerged) {
        return SUCCESS;
      } else if (studentFound) {
        return Constants.MERGE;
      } else {
        col = getActionErrors();
        setCommercialAjaxMessages(col);
        // sendSyncErrorEmail((ArrayList) col, getStudentNumber(), getFileNumber());
        return INPUT;
      }
    } else {
      if (col.isEmpty()) {
        col = getActionErrors();
      }
      setCommercialAjaxMessages(col);
      return INPUT;
    }
  }
  public String webSearch() throws Exception {
    log.debug("******* commercial webSearch " + getFileNumber());
    boolean studentExists = false;
    int status = 0;
    boolean noErrors = true;
    Collection<String> col = new ArrayList<String>();
    QueryByDLResponseType wsResponse = getQueryByDl();

    if (wsResponse != null) {
      Student student = parseQueryByDlResults(wsResponse);
      if ("200".equals(wsResponse.getStatus())) {
        studentExists = true;

        if (student.getStudentPk() == null) {
          log.debug("Student Insert");
          student.setStudentNumber(createStudentNumber(getClassroomPk()));
          int i = getStudentService().insert(student);
          student.setStudentPk(getSdcService().getLastInsertedId());
          // AUDIT
          insertAuditDates(student);
          addRoster(student.getStudentPk(), getClassroomPk());
        } else {
          // check if already on the roster
          boolean roster = checkRoster(student.getStudentPk(), getClassroomPk());
          if (!roster) {
            addRoster(student.getStudentPk(), getClassroomPk());
            if (student.getStudentNumber() == null) {
              log.debug("Student Number Insert");
              student.setStudentNumber(createStudentNumber(getClassroomPk()));
            }
          }
          // fullUpdate sends nulls
          int i = getStudentService().fullUpdate(student);
          // AUDIT
          checkAuditDates(student);
        }
        setCurrentStudent(student);
        setStudentPk(student.getStudentPk());
      } else {
        noErrors = false;
        if (status == 404) {
          col.add("File Number Not Found");
        }
      }
    } else {
      noErrors = false;
      col.add("Web service response was empty.");
      log.debug("Webservice RESULTS EMPTY");
    }

    if (noErrors) {
      if (studentExists) {
        return SUCCESS;
      } else {
        col = getActionErrors();
        setCommercialAjaxMessages(col);
        return INPUT;
      }
    } else {
      if (col.isEmpty()) {
        col = getActionErrors();
      }
      setCommercialAjaxMessages(col);
      return INPUT;
    }
  }
  public String syncFileNumber() throws Exception {
    log.debug("******* syncFileNumber " + getFileNumber());
    boolean studentMerged = false;
    boolean studentFound = false;

    QueryByDLResponseType wsResponse = getQueryByDl();
    if (wsResponse != null) {
      Student webStudent = parseQueryByDlResults(wsResponse);
      if ("200".equals(wsResponse.getStatus())) {
        if (webStudent.getStudentPk() == null) {
          log.debug("Student Insert");
          Student temp = new Student();
          if (getStudentNumber() != null) {
            temp.setStudentNumber(getStudentNumber());
            Student dbStudent = getStudentService().getStudent(temp);
            if (dbStudent.getDob().equals(webStudent.getDob())) {
              studentFound = true;
              if (dbStudent.getLastName().equals(webStudent.getLastName())) {
                if (dbStudent.getFirstName().equals(webStudent.getFirstName())) {
                  studentMerged = true;
                  // merge appropriate values
                  webStudent.setStudentNumber(dbStudent.getStudentNumber());
                  webStudent.setStudentPk(dbStudent.getStudentPk());
                  webStudent.setSchoolFk(getSchoolPk());
                  // fullUpdate sends nulls
                  int i = getStudentService().fullUpdate(webStudent);
                  setCurrentStudent(webStudent);
                  setStudentPk(webStudent.getStudentPk());
                }
              }
              if (!studentMerged) {
                List<String> web = new ArrayList<String>();
                List<String> db = new ArrayList<String>();

                SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

                web.add(sdf.format(webStudent.getDob()));
                web.add(webStudent.getLastName());
                web.add(webStudent.getFirstName());
                setMergeWebStudent(web);

                db.add(sdf.format(dbStudent.getDob()));
                db.add(dbStudent.getLastName());
                db.add(dbStudent.getFirstName());
                setMergeDbStudent(db);
              }
            } else {
              // dob doesnt match.
              addActionError("File Number cannot sync.  Student dob does not match.");
            }
          }
        } else {
          // filenumber already in database.
          String note = null;
          if (webStudent.getStudentNumber() != null) {
            note = webStudent.getStudentNumber().toString();
          } else {
            note = webStudent.getStudentFullName();
          }
          addActionError(
              "File Number already associated with a student "
                  + note
                  + ", please contact SDC administration for assistance.");
        }
      }
    } else {
      addActionError("Web service response was empty.");
      log.debug("Webservice RESULTS EMPTY");
    }

    if (studentMerged) {
      log.debug("SUCCESS");
      return SUCCESS;
    } else if (studentFound) {
      log.debug("MERGE");
      return Constants.MERGE;
    } else {
      log.debug("INPUT");
      Collection list = getActionErrors();
      setCommercialAjaxMessages(list);
      sendSyncErrorEmail((ArrayList) list, getStudentNumber(), getFileNumber());
      return INPUT;
    }
  }