/**
   * update the new route and sequenceNo of task send to phone
   *
   * @param taskId
   * @param newRouteID
   * @param newSequenceId
   */
  public void updatePhoneTask(int taskId, int newRouteID, int newSequenceId) {
    DBManager m = new DBManager();
    ResultSet rs =
        m.getRs(
            "SELECT s.*,t.* FROM Session s, Task t, Route r WHERE t.id = "
                + taskId
                + " AND t.route_id = r.id AND s.sessionID = r.sessionID");

    try {
      while (rs.next()) {
        String driverName = rs.getString("session_driverName");
        String vehicleID = rs.getString("session_vehicleID");
        long startTime = Long.parseLong(rs.getString("startTime"));
        long expireTime = Long.parseLong(rs.getString("expireTime"));
        int sequenceNo = rs.getInt("sequence_num");
        // save the action into updateTasks
        // remove task first
        XMLSession s = new XMLSession(driverName, vehicleID, startTime, expireTime);
        ArrayList<UpdateManager> ml = Global.getUpdateTasks();
        int l = ml.size();
        int index = -1;
        for (int i = 0; i < l; i++) {
          if (ml.get(i).getSession().equals(s)) {
            index = i;
            break;
          }
        }

        UpdateManager um = new UpdateManager(s);
        ArrayList<SingleUpdateManager> sl = new ArrayList<SingleUpdateManager>();
        if (index != -1) {
          um = ml.get(index);
          sl = um.getTasks();
        }

        Task t = new Task();
        SingleUpdateManager a = new SingleUpdateManager(Action.delete, t);
        a.setSequenceNo(sequenceNo);
        sl.add(a);
        um.setTask(sl);
        if (index != -1) {
          ml.set(index, um);
          Global.setUpdateTasks(ml);
        } else {
          Global.addUpdateTask(um);
        }

        // then add new task
        ResultSet rss =
            m.getRs(
                "SELECT s.* FROM Route r, Session s WHERE r.sessionID = s.sessionID"
                    + " AND r.id = "
                    + newRouteID);
        while (rss.next()) {
          String driverName1 = rss.getString("session_driverName");
          String vehicleID1 = rss.getString("session_vehicleID");
          long startTime1 = Long.parseLong(rss.getString("startTime"));
          long expireTime1 = Long.parseLong(rss.getString("expireTime"));

          ml = Global.getUpdateTasks();
          l = ml.size();
          int index1 = -1;
          XMLSession s1 = new XMLSession(driverName1, vehicleID1, startTime1, expireTime1);
          for (int i = 0; i < l; i++) {
            if (ml.get(i).getSession().equals(s1)) {
              index1 = i;
              break;
            }
          }

          UpdateManager um1 = new UpdateManager(s1);
          ArrayList<SingleUpdateManager> sl1 = new ArrayList<SingleUpdateManager>();
          if (index1 != -1) {
            um1 = ml.get(index1);
            sl1 = um1.getTasks();
          }

          String commodityID = rs.getString("commodity_id");
          ResultSet rsss = m.getRs("SELECT * FROM Commodity WHERE id = '" + commodityID + "'");
          if (rsss.next()) {
            String source = rsss.getString("source");
            String destination = rsss.getString("destination");
            String available_t = rsss.getString("available_t");
            String deadline = rsss.getString("deadline");
            int small = rsss.getInt("small");
            int large = rsss.getInt("large");

            Task t1 = new Task();
            Node src = new Node(source, 0, 0);
            Node dest = new Node(destination, 0, 0);
            Commodity c =
                new Commodity(
                    commodityID,
                    StringToCalendar(available_t),
                    StringToCalendar(deadline),
                    src,
                    dest,
                    small,
                    large);
            t1.cmdt = c;
            SingleUpdateManager b = new SingleUpdateManager(Action.add, t1);
            b.setSequenceNo(newSequenceId);
            sl1.add(b);
            um1.setTask(sl1);

            if (index1 != -1) {
              ml.set(index1, um1);
              Global.setUpdateTasks(ml);
            } else {
              Global.addUpdateTask(um1);
            }
          }
        }
      }
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    this.updateDBSTask(taskId, newRouteID, newSequenceId);
    m.destory();
  }
  // TODO: change the box amount of the commodity
  public void taskAction(XMLSession s, int sequenceNo, Action action) {

    DBManager m = new DBManager();
    String driverName = s.getDriverName();
    String startTime = Long.toString(s.getStartTime());
    String expireTime = Long.toString(s.getExpireTime());
    String vehicleID = s.getVehicleID();
    ResultSet rs =
        m.getRs(
            "SELECT t.id FROM Session s, Route r, Task t"
                + " WHERE session_driverName = '"
                + driverName
                + "' AND s.startTime = "
                + startTime
                + " AND s.expireTime = "
                + expireTime
                + " AND session_vehicleID = '"
                + vehicleID
                + "' AND s.sessionID = r.sessionID"
                + " AND r.id = t.route_id AND t.sequence_num = "
                + sequenceNo);
    try {
      if (rs.next()) {

        int task_id = rs.getInt("id");
        String currentTime = getCurrentTime();
        if (action.equals(Action.start)) {
          m.updb("UPDATE Task SET actual_start = '" + currentTime + "' WHERE id = " + task_id);
        }

        if (action.equals(Action.finish)) {
          m.updb("UPDATE Task SET actual_finish = '" + currentTime + "' WHERE id = " + task_id);
        }

        // TODO:Send to Phone About changing action

        ArrayList<UpdateManager> ml = Global.getUpdateTasks();
        int l = ml.size();
        int index = -1;
        for (int i = 0; i < l; i++) {
          if (ml.get(i).getSession().equals(s)) {
            index = i;
            break;
          }
        }
        UpdateManager um = new UpdateManager(s);
        ArrayList<SingleUpdateManager> sl = new ArrayList<SingleUpdateManager>();
        if (index != -1) {
          um = ml.get(index);
          sl = um.getTasks();
        }

        Task t = new Task();
        SingleUpdateManager a = new SingleUpdateManager(action, t);
        a.setSequenceNo(sequenceNo);
        sl.add(a);
        um.setTask(sl);
        // SAVE THE UPDATE TASK INTO GLOBAL VARIABLE
        if (index != -1) {
          ml.set(index, um);
          Global.setUpdateTasks(ml);
        } else {

          Global.addUpdateTask(um);
        }
      }
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  /**
   * get All the task by sessionID used in case of first-login and re-login
   *
   * @param sessionID
   */
  public void getTasks(int sessionID) {
    DBManager m = new DBManager();
    ResultSet rs = m.getRs("SELECT * FROM Session WHERE sessionID = " + sessionID);
    try {
      if (rs.next()) {
        String driverName = rs.getString("session_driverName");
        String vehicleID = rs.getString("session_vehicleID");
        long startTime = Long.parseLong(rs.getString("startTime"));
        long expireTime = Long.parseLong(rs.getString("expireTime"));
        XMLSession s = new XMLSession(driverName, vehicleID, startTime, expireTime);
        ArrayList<UpdateManager> ml = Global.getUpdateTasks();
        int l = ml.size();
        int index = -1;
        for (int i = 0; i < l; i++) {
          if (ml.get(i).getSession().equals(s)) {
            index = i;
            break;
          }
        }

        UpdateManager um = new UpdateManager(s);
        ArrayList<SingleUpdateManager> sl = new ArrayList<SingleUpdateManager>();
        if (index != -1) {
          um = ml.get(index);
          // Clear the tasks of this session
          // sl = um.getTasks();
        }

        ResultSet rss =
            m.getRs(
                "SELECT commodity_id,sequence_num FROM Route r, Commodity c, Task t WHERE r.sessionID = "
                    + sessionID
                    + " AND route_id = r.id AND c.id = t.commodity_id ORDER BY sequence_num ASC");
        while (rss.next()) {
          String commodityID = rss.getString("commodity_id");
          int sequenceNo = rss.getInt("sequence_num");
          ResultSet rsss = m.getRs("SELECT * FROM Commodity WHERE id = '" + commodityID + "'");
          if (rsss.next()) {
            String source = rsss.getString("source");
            String destination = rsss.getString("destination");
            String available_t = rsss.getString("available_t");
            String deadline = rsss.getString("deadline");
            int small = rsss.getInt("small");
            int large = rsss.getInt("large");

            Task t = new Task();
            t.driver = s.getDriverName();
            Node src = new Node(source, 0, 0);
            Node dest = new Node(destination, 0, 0);
            Commodity c =
                new Commodity(
                    commodityID,
                    StringToCalendar(available_t),
                    StringToCalendar(deadline),
                    src,
                    dest,
                    small,
                    large);
            t.cmdt = c;
            SingleUpdateManager a = new SingleUpdateManager(Action.add, t);
            a.setSequenceNo(sequenceNo);

            sl.add(a);
            um.setTask(sl);
            // SAVE THE UPDATE TASK INTO GLOBAL VARIABLE
            if (index != -1) {
              ml.set(index, um);
              Global.setUpdateTasks(ml);
            } else {

              Global.addUpdateTask(um);
            }
            // System.out.println(Global.getUpdateTasks().size());
          }
        }
      }
    } catch (NumberFormatException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    m.destory();
  }