-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovieTheatre.java
executable file
·291 lines (241 loc) · 10.2 KB
/
MovieTheatre.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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.UUID;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Locale;
/**
*
*/
public class MovieTheatre {
/**
* Static function to generate a UUID.
*
* @return The generated UUID as string to store in the tables.
*/
// Using java function to create a unnique id.
static public String generateUUID() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
/**
* Function which takes an emailID as input and checks whether it is in the correct format or
* not.
*
* @param email the email to be checked for correctness.
* @return If the emailID is in correct format or not.
*/
static public boolean isEmail(String email) throws IllegalArgumentException {
return email.matches("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$");
}
/**
* Function to check if the customer who is writting the review for the movie has attended the
* movie in the last 7 days.
*
* @param date The date the review is posted on.
* @param customerID The customerID of the customer who posts the review.
* @param movieID The movieID whose review the customer has posted.
* @return true if the review is within 7 days of latest attendence or else false.
* @throws SQLException if unable to find attendence in the table.
* @throws ParseException if unable to parse String of date.
*/
static public boolean isReviewDateCorrect(Timestamp date, String customerID, String movieID)
throws SQLException, ParseException {
String protocol = "jdbc:derby:";
String dbName = "iRate";
String connStr = protocol + dbName + ";create=true";
Properties props = new Properties(); // connection properties
// providing a user name and password is optional in the embedded
// and derbyclient frameworks
props.put("user", "user1");
props.put("password", "user1");
try (
Connection conn = DriverManager.getConnection(connStr, props);
// statement is channel for sending commands thru connection
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(
"select AttendanceDate "
+ "from Attendance "
+ "WHERE (MOVIEID = '" + movieID
+ "' AND CUSTOMERID = '"
+ customerID + "')"
+ "order by AttendanceDate desc")) {
if (!rs.next()) {
return false;
}
String dateOfAttendanceString = rs.getString(1);
//dateOfAttendanceString = dateOfAttendanceString.substring(0,9);
dateOfAttendanceString = dateOfAttendanceString.replace('-', '/');
String reviewDateString = date.toString().replace('-', '/');
Date reviewDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH)
.parse(reviewDateString);
Date dateOfAttendance = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH)
.parse(dateOfAttendanceString);
long difference = reviewDate.getTime() - dateOfAttendance.getTime();
conn.close();
return !(difference > 6048 * Math.pow(10, 5) && difference >= 0);
}
}
/**
* Function to find if the customer has endorsed the same movie within the last 24 hours.
*
* @param customerID of the custumer who is endorsing the review.
* @param reviewID The reviewID of the review the customer is endorsing.
* @param date The date on which the customer is endorsing the reviw.
* @return true if the endorsement is not within 1 day of the custumer endorsing the review of the
* same movie else false.
* @throws SQLException if unable to execute SQL query for finding customer.
* @throws ParseException if unable to parse String of date.
*/
static public boolean hasEndorsedSameMovie(String customerID, String reviewID, Timestamp date)
throws SQLException, ParseException {
String protocol = "jdbc:derby:";
String dbName = "iRate";
String connStr = protocol + dbName + ";create=true";
Properties props = new Properties(); // connection properties
// providing a user name and password is optional in the embedded
// and derbyclient frameworks
props.put("user", "user1");
props.put("password", "user1");
String movieID = "";
try {
Connection conn = DriverManager.getConnection(connStr, props);
// statement is channel for sending commands thru connection
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(
"select MOVIEID "
+ "from REVIEW "
+ "WHERE (REVIEWID = '" + reviewID
+ "')"
);
rs.next();
movieID = rs.getString(1);
} catch (Exception e) {
return false;
}
try {
Connection conn = DriverManager.getConnection(connStr, props);
// statement is channel for sending commands thru connection
Statement stmt = conn.createStatement();
// Getting all reviewIDs for all endorsements by the customer.
ResultSet rs = stmt.executeQuery(
"select REVIEWID "
+ "from ENDORSEMENT "
+ "WHERE "
+ "CUSTOMERID = '"
+ customerID
+ "'");
boolean flag = true;
while (rs.next()) {
String reviewIDString = rs.getString(1);
Statement stmt2 = conn.createStatement();
//getting the movieIDs for all the reviews from the above query.
ResultSet rs1 = stmt2.executeQuery(
"SELECT MOVIEID "
+ "from REVIEW "
+ "where REVIEWID = '"
+ reviewID
+ "'"
);
rs1.next();
String previousMovieID = rs1.getString(1);
if (!previousMovieID.equals(movieID)) {
continue;
} else {
// if any of the review returns false then falg will be false.
flag = flag && isDateWithinOneDay(customerID, reviewIDString, date);
}
}
return flag;
} catch (Exception e) {
return false;
}
}
/**
* Helper function to find if date of the review is within one day of the endorsement. This method
* is required because there can be multiple reviews for same movie the customer may have
* endorsed.
*
* @param customerID Of the customer who is endorsing the review..
* @param reviewID Of the review the customer is going to endorse.
* @param date On which the customer is endorsing the review.
* @return true if the endorsement is after 24 hours of
*/
static public boolean isDateWithinOneDay(String customerID, String reviewID, Timestamp date)
throws SQLException, ParseException {
String protocol = "jdbc:derby:";
String dbName = "iRate";
String connStr = protocol + dbName + ";create=true";
Properties props = new Properties(); // connection properties
// providing a user name and password is optional in the embedded
// and derbyclient frameworks
props.put("user", "user1");
props.put("password", "user1");
try (
Connection conn = DriverManager.getConnection(connStr, props);
// statement is channel for sending commands thru connection
Statement stmt = conn.createStatement();
//Finding the Date of endorsement of the given reviewID and CustomerID.
ResultSet rs = stmt.executeQuery(
"select DATEOFENDORSEMENT "
+ "from ENDORSEMENT "
+ "WHERE (REVIEWID = '" + reviewID
+ "' AND CUSTOMERID = '"
+ customerID + "')"
)
) {
if (!rs.next()) {
return true;
}
String pastDateOfEndorsementString = rs.getString(1);
pastDateOfEndorsementString = pastDateOfEndorsementString.replace('-', '/');
String currentDateOfEndorsementString = date.toString().replace('-', '/');
Date currentDateOfEndorsement = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH)
.parse(currentDateOfEndorsementString);
Date pastDateOfEndorsement = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH)
.parse(pastDateOfEndorsementString);
long difference = currentDateOfEndorsement.getTime() - pastDateOfEndorsement.getTime();
conn.close();
return difference > 864 * Math.pow(10, 5);
}
}
/**
* Function to find if the customer his trying to endorse his own movie or not.
*
* @param customerId of the customer who is endorsing the review.
* @param reviewID of the review the customer is trying to endorse.
* @return true if the customer is not endorsing his own movie.
* @throws SQLException if unable to execute the SQL query for finding costumerID form review
* table.
*/
static public boolean isCustomerDifferentThanEndorser(String customerId, String reviewID)
throws SQLException {
String protocol = "jdbc:derby:";
String dbName = "iRate";
String connStr = protocol + dbName + ";create=true";
Properties props = new Properties(); // connection properties
// providing a user name and password is optional in the embedded
// and derbyclient frameworks
props.put("user", "user1");
props.put("password", "user1");
try (Connection conn = DriverManager.getConnection(connStr, props);
// statement is channel for sending commands thru connection
Statement stmt = conn.createStatement();
//Quering for the customerID of the person who wrote the review.
ResultSet rs = stmt.executeQuery(
"select CustomerID "
+ "from Review "
+ "WHERE (REVIEWID = '" + reviewID + "')")) {
rs.next();
String reviewer = rs.getString(1);
conn.close();
return !customerId.equals(reviewer);
}
}
}