forked from zachtaylor1/SWProj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cs
More file actions
448 lines (411 loc) · 20.9 KB
/
Server.cs
File metadata and controls
448 lines (411 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace SWProjv1
{
class Server
{
static SqlConnection sql;
public static SqlCommand command;
public static bool Init()
{
try
{
sql = new SqlConnection("Data Source =JAKES-LAPTOP\\SQLEXPRESS; Initial Catalog = SEProjectDB ; Integrated Security = SSPI");
sql.Open();
command = sql.CreateCommand();
return true;
}
catch (Exception e)
{
MessageBox.Show("Error Connecting to Database. Please ensure the Connection String is correct. \n\n" + e.Message);
return false;
}
}
public static void setCommand(String type, String searchTerm)
{
command.Parameters.Clear();
if (type.Equals("Room"))
command.CommandText = "SELECT * FROM Room WHERE (building LIKE '" + searchTerm + "%' OR buildingLocation LIKE '" + searchTerm + "%')";
else if (type.Equals("Student"))
command.CommandText = "SELECT * FROM Student, User_T WHERE Student.UserID = User_T.UserID AND Student.userID= Student.studentID AND (User_T.firstName LIKE '" + searchTerm + "%' OR lastName LIKE '" + searchTerm + "%' OR studentID like '" + searchTerm + "%')";
else if (type.Equals("Message"))
command.CommandText = "SELECT * FROM Message, User_T WHERE messageAcknowledge = 0 AND recieverUserID = '" + User.userID + "' AND USer_T.userID = senderUserID AND (User_T.firstName LIKE '" + searchTerm + "%' OR lastName LIKE '" + searchTerm + "%')";
//command.CommandText = "SELECT * FROM Message, User_T WHERE messageAcknowledge = 0 AND recieverUserID IN (SELECT recieverUserID FROM Message, Admin WHERE recieverUserID=userID) AND user_T.userID=Message.senderUserID;";
else if (type.Equals("Key"))
command.CommandText = "SELECT * FROM Message, User_T, Student, Room WHERE recieverUserID = '000000000000000' AND Student.roomID=Room.RoomID AND messageAcknowledge = '0' AND senderUserID = User_T.userID AND Student.userID = Message.senderUserID AND (User_T.firstName LIKE '" + searchTerm + "%' OR lastName LIKE '" + searchTerm + "%')";
else if (type.Equals("RA Application"))
command.CommandText = "select * from RAApplication,Student,User_T where isAcknowledged = 0 AND RAApplication.studentID = Student.studentID AND Student.userID = User_T.userID AND (User_T.firstName LIKE '" + searchTerm + "%' OR lastName LIKE '" + searchTerm + "%')";
else
command.CommandText = "SELECT 'Uh oh!'";
//command.Parameters.Add(new SqlParameter("@searchTerm", searchTerm));
}
public static List<ListBoxItem> runQuery(String type)
{
List<ListBoxItem> results = new List<ListBoxItem>();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
switch (type)
{
case "SWProjv1.Room":
String roomSide;
try
{
roomSide = reader.GetString(1).Trim();
}
catch (Exception excep)
{
roomSide = "";
}
Room room = new Room(
reader.GetString(0).Trim(),
roomSide,
reader.GetString(2).Trim(),
reader.GetString(3).Trim(),
reader.GetString(4).Trim(),
reader.GetString(5).Trim()
);
room.setListBoxItem();
results.Add(room.listboxitem);
break;
case "SWProjv1.Student":
String roommateNum, roomNum;
try { roommateNum = reader.GetString(2).Trim(); }
catch (Exception) { roommateNum = ""; }
try { roomNum = reader.GetString(1).Trim(); }
catch (Exception) { roomNum = ""; }
Student student = new Student(
reader.GetString(0).Trim(),
reader.GetBoolean(3),
roommateNum,
roomNum,
reader.GetString(7).Trim(),
reader.GetString(9).Trim(),
reader.GetString(8).Trim(),
reader.GetString(5).Trim(),
reader.GetString(6).Trim(),
reader.GetDateTime(10).ToString().Trim()
);
student.setListBoxItem();
results.Add(student.listboxitem);
break;
case "SWProjv1.Message":
Message message = new Message(
reader.GetString(1).Trim(),
reader.GetString(2).Trim(),
reader.GetString(3).Trim(),
reader.GetDateTime(4).ToString().Trim(),
reader.GetString(8).Trim() + " " + reader.GetString(9).Trim(),
reader.GetInt32(0).ToString()
);
message.setListBoxItem();
results.Add(message.listboxitem);
break;
case "SWProjv1.RA Application":
RAApplicationData rad = new RAApplicationData(
reader.GetString(11).Trim() + " " + reader.GetString(12).Trim(),
reader.GetBoolean(1),
reader.GetBoolean(2),
reader.GetInt32(3),
reader.GetString(5).Trim(),
reader.GetString(4).Trim()
);
rad.setListBoxItem();
results.Add(rad.listboxitem);
break;
case "SWProjv1.Key":
TempKey key = new TempKey(
reader.GetString(8).Trim() + " " + reader.GetString(9).Trim(),
reader.GetString(23).Trim()+" "+ reader.GetString(20).Trim(),
reader.GetString(13).Trim()
);
key.setListBoxItem();
results.Add(key.listboxitem);
break;
case "SWProjv1.Audit":
Audit audit = new Audit(
reader.GetString(1).Trim(),
reader.GetString(2).Trim()
);
audit.setListBoxItem();
results.Add(audit.listboxitem);
break;
default:
break;
}
}
reader.Close();
return results;
}
public static void Executer(String command)
{
SqlCommand cmd = new SqlCommand(command, sql);
cmd.ExecuteNonQuery();
}
public static String[] studentHomeQuery(Student student)
{
SqlCommand cmd = new SqlCommand("SELECT userID FROM User_T WHERE username = '" + student.username + "' AND password = '" + student.password + "'", sql);
SqlDataReader red = cmd.ExecuteReader();
red.Read();
User.userID = red.GetString(0);
red.Close();
cmd.CommandText = ("SELECT studentID FROM Student WHERE userID = '" + Student.userID + "'");
red = cmd.ExecuteReader();
red.Read();
User.userID = red.GetString(0);
red.Close();
cmd.CommandText = ("SELECT * FROM RoomHistory, Room WHERE RoomHistory.studentID = '" + Student.userID + "' AND RoomHistory.roomID = Room.roomID");
red = cmd.ExecuteReader();
red.Read();
String[] x = { red.GetString(6).Trim(), red.GetString(9).Trim() + red.GetString(5).Trim(), red.GetDateTime(2).ToString().Trim().Split()[0], red.GetString(7).Trim(), red.GetString(8).Trim() };
red.Close();
return x;
}
public static String[] adminHomeQuery(Admin admin)
{
//SELECT Admin.userID, Admin.employeeID FROM Admin,User_T WHERE Admin.userID = User_T.userID and username = 'yonda' and password = 'Yonda100'
SqlCommand cmd = new SqlCommand("SELECT Admin.userID, Admin.employeeID FROM Admin,User_T WHERE Admin.userID = User_T.userID and username = '" + admin.username + "'", sql);
SqlDataReader red;
red = cmd.ExecuteReader();
red.Read();
String[] x = { red.GetString(0), red.GetString(1) };
red.Close();
User.userID = x[0];
admin.adminID = x[1];
return x;
}
public static List<Furniture> getFurniture(String roomID)
{
List<Furniture> results = new List<Furniture>();
setCommand("Furniture", roomID);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
results.Add(new Furniture(
reader.GetInt32(2).ToString(),
reader.GetString(1).Trim(),
reader.GetString(0).Trim()
));
}reader.Close();
return results;
}
public static int LogInQuery(String username, String password)
{
Init();
command.CommandText = "LogInProc";
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@uname", username));
command.Parameters.Add(new SqlParameter("@pword", password));
command.Parameters.Add(new SqlParameter("@result", System.Data.SqlDbType.Int)).Direction = System.Data.ParameterDirection.Output;
command.ExecuteNonQuery();
int result = Convert.ToInt32(command.Parameters["@result"].Value);
return result;
}
public static string getcommandtext()
{
return command.CommandText;
}
public static List<ListBoxItem> getRoomHistory(String roomID)
{
command.CommandText = "SELECT * FROM RoomHistory, Student, User_T WHERE RoomHistory.studentID = Student.studentID AND Student.userID = User_t.userID AND RoomHistory.RoomID = " + roomID;
SqlDataReader reader = command.ExecuteReader();
List<ListBoxItem> ll = new List<ListBoxItem>();
while (reader.Read())
{
ListBoxItem item = new ListBoxItem();
String s = reader.GetString(11).Trim()+" "+ reader.GetString(12).Trim();
s+=" "+reader.GetString(1).Trim();
s+=" "+reader.GetDateTime(2).ToString();
s+=" "+reader.GetDateTime(3).ToString();
item.Content = s;
ll.Add(item);
}reader.Close();
return ll;
}
public static void delFurniture(String serialNum)
{
command.CommandText = "DELETE FROM Furniture WHERE serialNumber = " + serialNum;
command.ExecuteNonQuery();
}
public static void addFurniture(String roomID, String serialNum, String name)
{
command.CommandText = "EXEC FurnitureAdd '" + roomID + "', '" + name + "', " + serialNum;//"INSERT INTO Furniture VALUES (" + roomID + "," + serialNum + "," + name + ")";
command.ExecuteNonQuery();
}
public static String getRoommateName(String roommateID)
{
command.CommandText = "SELECT studentID FROM Student, User_T WHERE Student.UserID = User_T.UserID AND Student.userID= Student.studentID AND Student.studentID = '" + roommateID + "'";
SqlDataReader reader = command.ExecuteReader();
String s="";
while (reader.Read())
{
s = reader.GetString(7).Trim() + " " + reader.GetString(9).Trim() + " " + reader.GetString(8).Trim();
}reader.Close();
return s;
}
public static void SendMessage(string text1, string text2, string userID)
{
//SqlCommand cmd = new SqlCommand("EXEC CreateMessage '"+text2+"','"+ userID +"','"+text1+"'", sql);
SqlCommand cmd = new SqlCommand("EXEC CreateMessage @text2,@userID,@text1", sql);
cmd.Parameters.Add(new SqlParameter("@text2", text2));
cmd.Parameters.Add(new SqlParameter("@text1", text1));
cmd.Parameters.Add(new SqlParameter("@userID", userID));
cmd.ExecuteNonQuery();
}
public static void addAudit(String title, String description)
{
SqlCommand cmd = new SqlCommand("EXEC addAudit @title,@description");
cmd.Parameters.Add(new SqlParameter("@title", title));
cmd.Parameters.Add(new SqlParameter("@description", description));
cmd.ExecuteNonQuery();
}
private static void setCommandApplication(String type, String parameter) //parameter is schoolYear or applicationID
{
if (type.Equals("Application"))
command.CommandText = "SELECT * FROM Application, RoomHistory WHERE Application.studentID NOT IN (SELECT studentID FROM RoomHistory WHERE dateEntered > '"+parameter+"') AND Application.studentID = RoomHistory.studentID";
else if (type.Equals("Sport"))
command.CommandText = "SELECT * FROM Sport WHERE applicationID = '" + parameter + "'";
else if (type.Equals("musicType"))
command.CommandText = "SELECT * FROM musicType WHERE applicationID = '" + parameter + "'";
else if (type.Equals("Hobby"))
command.CommandText = "SELECT * FROM Hobby WHERE applicationID = '" + parameter + "'";
}
public static List<ResApplicationForm> runQueryApplication(int schoolYear) //collects the most recent year of applications
{
String schoolString = schoolYear.ToString();
setCommandApplication("Application", schoolString);
List<ResApplicationForm> applications = new List<ResApplicationForm>();
MessageBox.Show(command.Connection.ConnectionString);
//command.Connection = sql;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
ResApplicationForm a0 = new ResApplicationForm();
String applicationID = reader.GetString(0).Trim();
a0.applicationID = applicationID;
a0.studentID = reader.GetString(1).Trim();
a0.firstName = reader.GetString(2).Trim();
a0.lastName = reader.GetString(3).Trim();
a0.otherName = reader.GetString(4).Trim();
a0.schoolYear = schoolYear;
a0.gender = reader.GetString(6).Trim();
a0.email = reader.GetString(7).Trim();
a0.streetAddress = reader.GetString(8).Trim();
a0.city = reader.GetString(9).Trim();
a0.region = reader.GetString(10).Trim();
a0.country = reader.GetString(11).Trim();
a0.postalCode = reader.GetString(12).Trim();
a0.phoneCountryCode = reader.GetString(13).Trim();
a0.phoneAreaCode = reader.GetString(14).Trim();
a0.phoneNumber = reader.GetString(15).Trim();
a0.preferBuilding = reader.GetString(16).Trim();
a0.smokes = reader.GetBoolean(17);
a0.liveWithSmoke = reader.GetBoolean(18);
a0.drinks = reader.GetBoolean(19);
a0.liveWithDrink = reader.GetBoolean(20);
a0.marijuana = reader.GetBoolean(21);
a0.liveWithMarijuana = reader.GetBoolean(22);
a0.socialLevel = reader.GetString(23).Trim();
a0.bedtime = reader.GetString(24).Trim();
a0.wakeUp = reader.GetString(25).Trim();
a0.volumeLevel = reader.GetString(26).Trim();
a0.overnightVisitors = reader.GetBoolean(27);
a0.cleanliness = reader.GetString(28).Trim();
a0.studiesInRoom = reader.GetBoolean(29);
a0.roommateRequest = reader.GetBoolean(30);
a0.roommateName = reader.GetString(31).Trim();
a0.roommateID = reader.GetString(32).Trim();
a0.mealPlan = reader.GetString(33).Trim();
applications.Add(a0);
}
reader.Close();
foreach (ResApplicationForm a0 in applications)
{
setCommandApplication("Sport", a0.applicationID); //gets Sports table
reader = command.ExecuteReader();
List<String> sports = new List<String>();
while (reader.Read())
sports.Add(reader.GetString(1));
a0.sports = sports.ToArray();
reader.Close();
setCommandApplication("musicType", a0.applicationID); //gets Music table
reader = command.ExecuteReader();
List<String> music = new List<String>();
while (reader.Read())
music.Add(reader.GetString(1));
a0.music = music.ToArray();
reader.Close();
setCommandApplication("Hobby", a0.applicationID); //gets Hobbies table
reader = command.ExecuteReader();
List<String> hobbies = new List<String>();
while (reader.Read())
hobbies.Add(reader.GetString(1));
a0.hobbies = hobbies.ToArray();
reader.Close();
}
return applications;
}
public static String getEmptyRoomID(String dorm, String year)
{
String emptyRooms = "select DISTINCT Room.roomID from Room, RoomHistory where " +
"((Room.roomID = RoomHistory.roomID AND RoomHistory.dateLeft IS NOT NULL AND RoomHistory.dateLeft > '"+year+"')" +
" OR (Room.roomID NOT IN (SELECT roomID FROM RoomHistory)))AND Room.building = '"+dorm+"'";
command.CommandText = emptyRooms;
int times = 0;
String roomID = "";
SqlDataReader reader = command.ExecuteReader();
while (reader.Read() && times == 0)
{
roomID = reader.GetString(0).Trim();
times++;
}
reader.Close();
return roomID;
}
public static String getRoomNum(String dorm, String year)
{
String roomID = getEmptyRoomID(dorm, year);
String emptyRooms = "select roomNum from Room where roomID = '" + roomID + "'";
command.CommandText = emptyRooms;
int times = 0;
String roomNum = "";
SqlDataReader reader = command.ExecuteReader();
while (reader.Read() && times == 0)
{
roomNum = reader.GetString(0).Trim();
times++;
}
reader.Close();
return roomNum;
}
public static String getAdjoiningID(String roomID, String roomNum)
{
String adjoining = "select roomID from Room where roomID != '" + roomID + "' AND roomNum = '" + roomNum + "'";
command.CommandText = adjoining;
int times = 0;
String adRoomID = "";
SqlDataReader reader = command.ExecuteReader();
while (reader.Read() && times == 0)
{
adRoomID = reader.GetString(0).Trim();
times++;
}
reader.Close();
return adRoomID;
}
public static void assignRoom(String studentID, String roomID)
{
DateTime dateTime = DateTime.Now;
String formatted = dateTime.ToString("yyyy-MM-dd");
String insertStmt = "INSERT INTO RoomHistory values ('" + roomID + "', '" + studentID + "', '" + formatted + "', NULL)";
command.CommandText = insertStmt;
command.ExecuteNonQuery();
}
}
}