-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerThread.java
333 lines (319 loc) · 17.1 KB
/
ServerThread.java
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
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
/**
* Project 5 - ServerThread.java
*
* Class that handles database interactions with client.
*
* @author Shafer Anthony Hofmann, Qihang Gan, Shreyas Viswanathan, Nathan Pasic
* Miller, Oliver Long
*
* @version December 8, 2023
*/
public class ServerThread extends Thread {
Socket socket;
/**
* Constructs a new ServerThread instance utilizing the socket that communication will be taking place over.
*
* @param socket The communication channel between all clients and the server
*/
public ServerThread(Socket socket) {
this.socket = socket;
}
/**
* Process initial client, seller client, and customer client requests and send data back to all of them
*/
@Override
public void run() {
try {
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
oos.flush();
Dashboard db = new Dashboard();
String[] response;
Object output = null;
boolean exit = false;
Database database = new Database();
User u = null;
while (socket.isConnected()) {
exit = false;
u = new User();
// Handle communication with the initial client
String[] userInfo = (String[]) ois.readObject();
switch (userInfo[0]) {
// Log In
case "LOGIN" -> {
try {
String userMatch = database.retrieveUserMatchForLogin(userInfo[1], userInfo[2]);
if (userMatch.split(",")[3].equals("CUSTOMER")) {
u = new Customer(userMatch.split(",")[0], userInfo[1], userInfo[2],
UserRole.CUSTOMER);
} else if (userMatch.split(",")[3].equals("SELLER")) {
u = new Seller(userMatch.split(",")[0], userInfo[1], userInfo[2],
UserRole.SELLER);
}
} catch (Exception e) {
oos.writeObject(e.getMessage());
oos.flush();
}
}
// Customer Sign up
case "CREATE_CUSTOMER" -> {
try {
u = new Customer(userInfo[1], userInfo[2], UserRole.CUSTOMER);
} catch (Exception e) {
oos.writeObject(e.getMessage());
oos.flush();
}
}
// Seller Sign up
case "CREATE_SELLER" -> {
try {
u = new Seller(userInfo[1], userInfo[2], UserRole.SELLER);
} catch (Exception e) {
oos.writeObject(e.getMessage());
oos.flush();
}
}
// Reset password
case "RESET_PASSWORD" -> {
try {
u.resetPassword(userInfo[1], userInfo[2]);
oos.writeObject("Password reset successfully!");
oos.flush();
} catch (Exception e) {
oos.writeObject(e.getMessage());
oos.flush();
}
}
default -> oos.writeObject("ERROR");
}
// Handle communication with the customer client
if (u instanceof Customer) {
Customer c = (Customer) u;
oos.writeObject("Customer Connection to Server Established");
oos.flush();
oos.writeObject(u.getEmail());
oos.flush();
while (!exit) {
response = (String[]) ois.readObject();
output = null;
try {
switch (response[0]) {
// Get all stores
case "FETCH_ALL_STORES" -> output = c.fetchAllStores();
// View All Products
case "GET_ALL_PRODUCTS" -> output = c.getAllProducts();
// Get all reviews
case "FETCH_REVIEWS" -> output = c.fetchReviews();
// Get Product Info
case "GET_PRODUCT_INFO" -> output = c.getProductInfo(Integer.parseInt(response[1]));
// Add Product to Cart
case "ADD_TO_CART" -> {
c.addToCart(Integer.parseInt(response[1]), response[2]);
output = "Successfully added item to cart.";
}
// Remove Product from Cart
case "REMOVE_FROM_CART" -> {
c.removeFromCart(Integer.parseInt(response[1]), response[2]);
output = "Successfully removed item from cart.";
}
case "LEAVE_REVIEW" -> {
c.addReview(Integer.parseInt(response[1]), response[2]);
output = "Review uploaded successfully";
}
case "EDIT_REVIEW" -> {
c.modifyReview(Integer.parseInt(response[1]), response[2]);
output = "Review modified successfully!";
}
case "DELETE_REVIEW" -> {
c.deleteReview(Integer.parseInt(response[1]));
output = "Review deleted successfully!";
}
case "RETURN_ITEM" -> {
c.returnItems(Integer.parseInt(response[1]));
output = "Item returned successfully!";
}
// View Cart
case "GET_CART" -> output = c.getCart();
// View Shopping History
case "GET_SHOPPING_HISTORY" -> output = c.getShoppingHistory();
// Search Products
case "SEARCH_PRODUCTS" -> output = c.searchProducts(response[1]);
// Sort Products
case "SORT_PRODUCTS" -> output = c.sortProducts(response[1], response[2].equals("true"));
// Export Shopping History
case "EXPORT_PURCHASE_HISTORY" -> {
c.exportPurchaseHistory();
output = "Successfully exported purchase history.";
}
// Purchase Items
case "PURCHASE_ITEMS" -> {
c.purchaseItems();
output = "Items checked out successfully!";
}
// View Store Dashboard
case "CUSTOMER_GET_STORES_DASHBOARD" ->
output = db.customerGetStoresDashboard(Integer.parseInt(response[1]),
response[2].equals("true"));
// View Purchases Dashboard
case "CUSTOMER_GET_PURCHASES_DASHBOARD" ->
output =
db.customerGetPersonalPurchasesDashboard
(Integer.parseInt(response[1]),
response[2].equals("true"), c.getUserID());
// Modify Email
case "EDIT_EMAIL" -> {
c.setEmail(response[1]);
output = c.getEmail();
}
// Modify Password
case "EDIT_PASSWORD" -> {
c.setPassword(response[1]);
output = "Password edited successfully!";
}
// Delete Account
case "DELETE_ACCOUNT" -> {
c.deleteAccount();
oos.writeObject(new Object[]{"SUCCESS", "Account deleted successfully!"});
oos.flush();
exit = true;
}
case "SIGN_OUT" -> {
oos.writeObject(new Object[]{"SUCCESS", "Signed out successfully!"});
oos.flush();
exit = true;
}
default -> output = null;
}
if (!exit) {
oos.writeObject(new Object[]{"SUCCESS", output});
oos.flush();
}
} catch (CustomerException e) {
oos.writeObject(new Object[]{"ERROR", e.getMessage()});
oos.flush();
} catch (Exception e) {
oos.writeObject(new Object[]{"ERROR", e.getMessage()});
oos.flush();
}
}
// Handle communication with the seller client
} else if (u instanceof Seller) {
Seller s = (Seller) u;
oos.writeObject("Seller Connection to Server Established");
oos.flush();
oos.writeObject(u.getEmail());
oos.flush();
while (!exit) {
response = (String[]) ois.readObject();
output = null;
try {
switch (response[0]) {
// Get all customers
case "FETCH_ALL_CUSTOMERS" -> output = s.getAllCustomers();
// Get all products
case "FETCH_ALL_PRODUCTS" -> output = s.getAllProducts();
// Get Stores
case "GET_ALL_STORES" -> output = s.getStores();
// Get Products
case "GET_STORE_PRODUCTS" -> output = s.getProducts(response[1]);
// Create Product
case "CREATE_NEW_PRODUCT" -> {
s.createNewProduct(response[1], response[2], response[3], response[4],
response[5], response[6], response[7], response[8]);
output = "Successfully created new product.";
}
// Edit Product
case "EDIT_PRODUCT" -> {
s.editProduct(response[1], response[2], response[3], response[4]);
output = "Successfully edited product.";
}
// Delete Product
case "DELETE_PRODUCT" -> {
s.deleteProduct(response[1], response[2]);
output = "Successfully deleted producted.";
}
// Create Store
case "CREATE_NEW_STORE" -> {
s.createNewStore(response[1]);
output = "Successfully created new store.";
}
// Delete Store
case "DELETE_STORE" -> {
s.deleteStore(response[1]);
output = "Successfully deleted store.";
}
// Modify Store
case "MODIFY_STORE_NAME" -> {
s.modifyStoreName(response[1], response[2]);
output = "Successfully modifed store.";
}
// Export Products
case "EXPORT_PRODUCTS" -> {
s.exportProducts(response[1]);
output = "Successfully exported products.";
}
// Import Products
case "IMPORT_PRODUCTS" -> {
s.importProducts(response[1], response[2]);
output = "Successfully imported products.";
}
// View product reviews
case "VIEW_PRODUCT_REVIEWS" -> output = s.viewProductReviews(response[1], response[2]);
// View Customer Shopping Cart
case "VIEW_CUSTOMER_SHOPPING_CARTS" -> output = s.viewCustomerShoppingCarts();
// View Sales by Store
case "VIEW_SALES_BY_STORE" -> output = s.viewStoreSales();
// Sort Customer Dashboard
case "SELLER_GET_CUSTOMERS_DASHBOARD" ->
output = db.sellerGetCustomersDashboard(Integer.parseInt(response[1]),
response[2].equals("true"));
// Sort Products Dashboard
case "SELLER_GET_PRODUCTS_DASHBOARD" ->
output = db.sellerGetProductsDashboard(Integer.parseInt(response[1]),
response[2].equals("true"));
// Modify Email
case "EDIT_EMAIL" -> {
s.setEmail(response[1]);
output = s.getEmail();
}
// Modify Password
case "EDIT_PASSWORD" -> {
s.setPassword(response[1]);
output = "Password edited successfully!";
}
// Delete Account
case "DELETE_ACCOUNT" -> {
s.deleteAccount();
oos.writeObject(new Object[]{"SUCCESS", "Account deleted successfully!"});
oos.flush();
exit = true;
}
case "SIGN_OUT" -> {
oos.writeObject(new Object[]{"SUCCESS", "Signed out successfully!"});
oos.flush();
exit = true;
}
default -> output = null;
}
if (!exit) {
oos.writeObject(new Object[]{"SUCCESS", output});
oos.flush();
}
} catch (SellerException e) {
oos.writeObject(new Object[]{"ERROR", e.getMessage()});
oos.flush();
} catch (Exception e) {
oos.writeObject(new Object[]{"ERROR", e.getMessage()});
oos.flush();
}
}
}
}
} catch (Exception e) {
new ErrorMessageGUI("A client shutdown occurred. Relaunch the client if you would like to continue using the application.");
}
}
}