Skip to content
Snippets Groups Projects
Commit a70341f3 authored by Rafi's avatar Rafi
Browse files

Made changes to the Schedule class methods to return data that the client...

Made changes to the Schedule class methods to return data that the client needs to update there local copy.
Cleaned up some spacing and variable names for consistency.
Cleaned up all the warnings.
parent 87c9a19a
No related branches found
No related tags found
1 merge request!6Changed update implementation for employees so rather than sending the whole...
......@@ -21,15 +21,26 @@ public class Schedule {
Return: none
*/
protected void addShift(int ID, String date, int start, int end){
protected String addShift(int ID, String date, int start, int end){
String newShift = "";
Date newDate = Date.valueOf(date);
try {
Statement myStatement = dbConnection.createStatement();
Statement myStatement = dbConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
myStatement.executeUpdate("INSERT INTO shifts (employee_id, full_date, start_time, end_time) " +
"VALUES ('" + ID +"', '"+ newDate +"', '"+ start +"', '"+ end +"')");
} catch (SQLException e) {
e.printStackTrace();
ResultSet myRs = myStatement.executeQuery("select * from Shifts where employee_id=" + ID +
" and full_date='" + newDate + "' and start_time=" + start + " and end_time=" + end);
if (myRs.last()) {
newShift = myRs.getString("employee_id" ) + "." +
myRs.getString("full_date") + "." + myRs.getString("start_time") +
"." + myRs.getString("end_time") + "." + myRs.getString("shift_id");
}
} catch (SQLException exception) {
exception.printStackTrace();
return newShift;
}
System.out.println("Shift successfully added");
return newShift;
}
/*
......@@ -39,13 +50,15 @@ public class Schedule {
Description: Removes the employees shift matching the shift date.
Return: none
*/
protected void removeShiftByID(String shiftID){
protected int removeShiftByID(int shiftID){
try {
Statement myStatement = dbConnection.createStatement();
myStatement.executeUpdate("DELETE FROM shifts WHERE (shift_id =" + shiftID + ")");
} catch (SQLException e) {
e.printStackTrace();
} catch (SQLException exception) {
exception.printStackTrace();
return -1;
}
return shiftID;
}
/*
......@@ -58,98 +71,23 @@ public class Schedule {
Description: Edits the time and date of the of a shift if it has the same shift id
Return: none
*/
protected void editShift(Integer shift_id, String date, String start,String end){
protected String editShift(Integer shiftID, String date, String start,String end){
String editedShift = "";
Date newDate = Date.valueOf(date);
System.out.println(newDate);
try {
Statement myStatement = dbConnection.createStatement();
myStatement.executeUpdate("update shifts set full_date='" + newDate + "',start_time=" + start + ",end_time=" + end
+ " where shift_id=" + shift_id);
} catch (SQLException e) {
e.printStackTrace();
+ " where shift_id=" + shiftID);
ResultSet myRs = myStatement.executeQuery("select * from Shifts where shift_id=" + shiftID);
editedShift = myRs.getString("employee_id" ) + "." + myRs.getString("full_date") +
"." + myRs.getString("start_time") + "." + myRs.getString("end_time") +
"." + myRs.getString("shift_id");
} catch (SQLException exception) {
exception.printStackTrace();
return editedShift;
}
}
/*
Name: getShifts
Parameters:
Date: The date as a string in the form of "yyyy-mm-dd".
Description: Returns info for all shifts on a given date.
Return: String
*/
protected String getShifts(String date){
Date newDate = Date.valueOf(date);
String response = "getShifts";
try {
Statement myStatement = dbConnection.createStatement();
ResultSet myRs = myStatement.executeQuery("SELECT FROM shifts WHERE (full_date = " + newDate +")");
while (myRs.next()) {
response = response + "/" + myRs.getString("full_date") + "." +
myRs.getString("employee_id") + "." + myRs.getString("start_time") +
"." + myRs.getString("end_time");
}
} catch (SQLException e) {
e.printStackTrace();
}
return response;
}
/*
Name: getShiftsByWeek
Parameters:
String startDate: The start date of the week. In the form "YYYY-MM-DD".
String endDate: The last date of the week. In the form "YYYY-MM-DD".
Description: Returns info for all shifts with in the given start date and end date .
Return: String
*/
protected String getShiftsByWeek(String startDate, String endDate){
Date newStart = Date.valueOf(startDate);
Date newEnd = Date.valueOf(endDate);
String response = "getShiftsByWeek";
try {
Statement myStatement = dbConnection.createStatement();
ResultSet myRs = myStatement.executeQuery("SELECT FROM shifts WHERE (full_date >= " + newStart +")" +
"AND WHERE (full_date <= " + newEnd +")");
while (myRs.next()) {
response = response + "/" + myRs.getString("full_date") + "." +
myRs.getString("employee_id") + "." + myRs.getString("start_time") +
"." + myRs.getString("end_time");
}
} catch (SQLException e) {
e.printStackTrace();
}
return response;
}
/*
Name: getEmployeeShiftsByWeek
Parameters:
Int ID: An employee id.
String startDate: The start date of the week. In the form "YYYY-MM-DD".
String endDate: The last date of the week. In the form "YYYY-MM-DD".
Description: Returns info for all shifts with in the given start date and end date, for the given employee.
Return: String
*/
protected String getEmployeeShiftsByWeek(int ID, String startDate, String endDate){
Date newStart = Date.valueOf(startDate);
Date newEnd = Date.valueOf(endDate);
String response = "getEmployeeShiftsByWeek";
try {
Statement myStatement = dbConnection.createStatement();
ResultSet myRs = myStatement.executeQuery("SELECT FROM shifts WHERE (full_date >= " + newStart +")" +
"AND WHERE (full_date <= " + newEnd +") AND WHERE (employee_id = "+ ID + ")");
while (myRs.next()) {
response = response + "/" + myRs.getString("full_date") + "." +
myRs.getString("employee_id") + "." + myRs.getString("start_time") +
"." + myRs.getString("end_time");
}
} catch (SQLException e) {
e.printStackTrace();
}
return response;
return editedShift;
}
/*
......@@ -161,18 +99,20 @@ public class Schedule {
Return: String
*/
protected String allShifts(){
String response = "allShifts";
StringBuilder response = new StringBuilder("allShifts");
try {
Statement myStatement = dbConnection.createStatement();
ResultSet myRs = myStatement.executeQuery("SELECT * FROM shifts");
while (myRs.next()) {
response = response + "/" + myRs.getString("employee_id" ) + "." +
myRs.getString("full_date") + "." + myRs.getString("start_time") +
"." + myRs.getString("end_time") + "." + myRs.getString("shift_id");
response.append("/").append(myRs.getString("employee_id")).append(".")
.append(myRs.getString("full_date")).append(".")
.append(myRs.getString("start_time"))
.append(".").append(myRs.getString("end_time"))
.append(".").append(myRs.getString("shift_id"));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (SQLException exception) {
exception.printStackTrace();
}
return response;
return response.toString();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment