/** * 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(); }
/** * get all the routes that has been assigned from the database save into Vector<Route> to be used * by Change.java controller * * @return */ public Vector<Route> getRoutes() { DBManager m = new DBManager(); ResultSet rs = m.getRs( "SELECT r.id AS route_id,s.sessionID,session_driverName," + "session_vehicleID,startTime,expireTime,status,shift_normal_start_time " + "FROM Route r,Session s WHERE s.sessionID = r.sessionID"); Vector<Route> routes = new Vector<Route>(); try { while (rs.next()) { int route_id = rs.getInt("route_id"); int sessionID = rs.getInt("sessionID"); 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 status = rs.getInt( "status"); // status, 0:UNASSIGNED, 1:ASSIGNED£¬2£ºSESSION_SUSPENDED£¬ // 3£ºSESSION_ENDED Calendar shift_normal_start_time = StringToCalendar(rs.getString("shift_normal_start_time")); // TODO:What is Network and period Route route = new Route(null, route_id, 0); XMLSession session = new XMLSession(driverName, vehicleID, startTime, expireTime); route.setSession(session); route.setStatus(intToStatus(status)); ; // TODO:HOW to use the shift_normal_start_time? Vector<Task> taskSet = new Vector<Task>(); ResultSet rss = m.getRs( "SELECT t.id AS task_id, t.commodity_id,sequence_num," + "t.small AS task_small,t.large AS task_large,planned_start,planned_finish," + "actual_start,actual_finish,source,destination,available_t,deadline," + "c.small AS com_small, c.large AS com_large,finished_small,finished_large " + "FROM Route r, Task t, Commodity c " + "WHERE r.id = t.route_id AND t.commodity_id = c.id AND r.id =" + route_id + " " + "ORDER BY sequence_num ASC"); while (rss.next()) { int task_id = rss.getInt("task_id"); String commodity_id = rss.getString("commodity_id"); // int sequenceNo = rss.getInt("sequence_num"); int task_small = rss.getInt("task_small"); int task_large = rss.getInt("task_large"); Calendar planned_start = StringToCalendar(rss.getString("planned_start")); Calendar planned_finish = StringToCalendar(rss.getString("planned_finish")); Calendar actual_start = StringToCalendar(rss.getString("actual_start")); Calendar actual_finish = StringToCalendar(rss.getString("actual_finish")); String source = rss.getString("source"); String destination = rss.getString("destination"); Calendar available_t = StringToCalendar(rss.getString("available_t")); Calendar deadline = StringToCalendar(rss.getString("deadline")); int com_small = rss.getInt("com_small"); int com_large = rss.getInt("com_large"); int finished_small = rss.getInt("com_small"); int finished_large = rss.getInt("com_large"); Task t = new Task(); // TODO:WHAT IS loadTime and unloadTime Node src = new Node(source, 0, 0); Node des = new Node(destination, 0, 0); Commodity c = new Commodity(commodity_id, available_t, deadline, src, des, com_small, com_large); t.cmdt = c; t.taskID = task_id; // TODO:Not sure if its right.. if (task_small > 0) { t.size = 1; } else { t.size = 2; } t.startT = planned_start; t.finishT = planned_finish; t.actualStartT = actual_start; t.actualFinishT = actual_finish; t.driver = driverName; t.vehicleID = vehicleID; taskSet.add(t); } route.taskSet = taskSet; routes.add(route); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return routes; }
/** * 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(); }