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
|
package shared.remotecall;
import java.util.*;
import java.io.*;
import java.util.regex.*;
import shared.*;
/**
* An object of this class is sent from the calendar client to the calendar server each time
* a request is made. This class encapsulates all the needed information so that the server can
* response with a serialized server response object.
* @author buetow
*
*/
public final class ClientRequest extends RemoteCall implements Serializable {
private static final long serialVersionUID = 6L;
private static final Pattern emptyPattern = Pattern.compile("[^\\s]");
public static final int REQUEST_EVENTS = 0;
public static final int REQUEST_ALL_EVENTS = 1;
public static final int MODIFY_EVENT = 2;
public static final int DELETE_EVENT = 3;
public static final int ADD_EVENT = 4;
public static final int RELOAD_DATABASE = 5;
public static final int FLUSH_DATABASE = 6;
public static final int SHUTDOWN_SERVER = 7;
public static final int RENAME_CATEGORY = 8;
public static final int DELETE_CATEGORY = 9;
private int iAction = 0;
private int iNumEventsToRequest;
private boolean bMainRegexp;
private boolean bCaseInsensitive;
private Pattern patAll = null;
private Pattern patCategory = null;
private Pattern patDescription = null;
private Pattern patPlace = null;
private Pattern patDate = null;
private Date dateRangeFrom = null;
private Date dateRangeTo = null;
private CalendarEvent event = null;
private String sPassphrase;
private String sString = null;
/**
* Simple constructor, creates a client request object requesting events up
* from the current date.
*/
public ClientRequest() {
dateRangeFrom = new Date();
initialize(REQUEST_EVENTS);
}
/**
* Simple constructor, creates a client request object requesting events up
* from the current date.
* @param iAction Specifies the request type of this request.
*/
public ClientRequest(int iAction) {
dateRangeFrom = new Date();
initialize(iAction);
}
/**
* This method initializes some common stuff. Called by each constructor.
* @param iAction Specifies the request type of this request.
*/
private void initialize(int iAction) {
this.iAction = iAction;
this.sPassphrase = Config.getStringValue("passphrase", false);
this.bCaseInsensitive = Config.getBooleanValue("regexp_case_insensitive");
this.iNumEventsToRequest = Config.getIntValue("client_max_events");
}
/**
* This method is needed by the clanedar server to get the type of action requested.
* @param iAction Specifies the action to test against.
* @return Returns true if the specified action ID matches with the action ID of this request object.
*/
public boolean actionIs(int iAction) {
return this.iAction == iAction;
}
/**
* This method is needed by the calendar server to get the type of action requested.
* @return Returns true if this object requests new events from the server.
*/
public boolean requestsNewEvents() {
return this.iAction < ClientRequest.MODIFY_EVENT;
}
/**
* This method sets the date range to request.
* @param dateRangeFrom Specifies the beginning date.
* @param dateRangeTo Specifies the ending date.
*/
public void setDateRange(Date dateRangeFrom, Date dateRangeTo) {
this.dateRangeFrom = dateRangeFrom;
this.dateRangeTo = dateRangeTo;
}
/**
* This method sets a calendar event object to be transfered to the calendar server because the event has been modified.
* @param event Specifies an event to be transfered to the calendar server.
*/
public void setEvent(CalendarEvent event) {
this.event = event;
if (actionIs(ADD_EVENT))
event.setEventID(-1);
}
/**
* This method returns the event object.
* @return Returns the calendar event which is stored inside this client request.
*/
public CalendarEvent getEvent() {
return event;
}
/**
* With this method the client can define the maximum number of events to request.
* @param iNumEvents Specifies the maximum number of events to request.
*/
public void setNumEventsToRequest(int iNumEvents) {
this.iNumEventsToRequest = iNumEvents;
}
/**
* This method returns the maximum number of events the client wants to request.
* @return Returns the maximum number of events the client wants to request.
*/
public int getNumEventsToRequest() {
return iNumEventsToRequest;
}
/**
* This method sets if the client contains a regexp from the main session window.
* @param bMainRegexp Set to true if the user used the main regexp field of the main GUI window.
*/
public void setMainRegexp(boolean bMainRegexp) {
this.bMainRegexp = bMainRegexp;
}
/**
* This method compiles a specific pattern object to match with on the server part after serialization.
* @param sRegexp Compiles a Pattern object with sRegexp as the regular expression.
*/
public void setRegexpAll(String sRegexp) {
if (isEmptyPattern(sRegexp))
return;
if (bCaseInsensitive)
patAll = Pattern.compile(sRegexp, Pattern.CASE_INSENSITIVE);
else
patAll = Pattern.compile(sRegexp);
}
/**
* This method compiles a specific pattern object to match with on the server part after serialization.
* @param sRegexp Compiles a Pattern object with sRegexp as the regular expression.
*/
public void setRegexpCategory(String sRegexp) {
if (isEmptyPattern(sRegexp))
return;
if (bCaseInsensitive)
patCategory = Pattern.compile(sRegexp, Pattern.CASE_INSENSITIVE);
else
patCategory = Pattern.compile(sRegexp);
}
/**
* This method compiles a specific pattern object to match with on the server part after serialization.
* @param sRegexp Compiles a Pattern object with sRegexp as the regular expression.
*/
public void setRegexpDescription(String sRegexp) {
if (isEmptyPattern(sRegexp))
return;
if (bCaseInsensitive)
patDescription = Pattern.compile(sRegexp, Pattern.CASE_INSENSITIVE);
else
patDescription = Pattern.compile(sRegexp);
}
/**
* This method sets a string to be transfered to the netcalendar server.
* @param sString specifies the string to be transfered.
*/
public void setString(String sString) {
this.sString = sString;
}
/**
* This method returns a string transfered by the netcalendar client. If no string has been transfered,
* null will be returned.
* @return Returns a string transfered by the netcalendar client.
*/
public String getString() {
return sString;
}
/**
* This method compiles a specific pattern object to match with on the server part after serialization.
* @param sRegexp Compiles a Pattern object with sRegexp as the regular expression.
*/
public void setRegexpPlace(String sRegexp) {
if (isEmptyPattern(sRegexp))
return;
if (bCaseInsensitive)
patPlace = Pattern.compile(sRegexp, Pattern.CASE_INSENSITIVE);
else
patPlace = Pattern.compile(sRegexp);
}
/**
* This method compiles a specific pattern object to match with on the server part after serialization.
* @param sRegexp Compiles a Pattern object with sRegexp as the regular expression.
*/
public void setRegexpDate(String sRegexp) {
if (isEmptyPattern(sRegexp))
return;
if (bCaseInsensitive)
patDate = Pattern.compile(sRegexp, Pattern.CASE_INSENSITIVE);
else
patDate = Pattern.compile(sRegexp);
}
/**
* Checks if a specific calendar event matches this client request.
* @param calendarEvent Specifies the event to match against.
* @return Returns true if the given event matched. Else, false will be returned.
*/
public boolean match(CalendarEvent calendarEvent) {
// If we want to get all events!
if (actionIs(REQUEST_ALL_EVENTS))
return true;
// If we use the regex field of the main GUI
if (bMainRegexp)
return calendarEvent.matches(patAll);
// If we use advanced searching:
if (dateRangeFrom != null)
if (dateRangeFrom.getTime() > calendarEvent.getDate().getTime())
return false;
if (dateRangeTo != null)
if (dateRangeTo.getTime() < calendarEvent.getDate().getTime())
return false;
if (calendarEvent.matches(patAll) ||
(calendarEvent.matchesDescription(patDescription)
&& calendarEvent.matchesCategory(patCategory)
&& calendarEvent.matchesPlace(patPlace)
&& calendarEvent.matchesDateString(patDate)))
return true;
return false;
}
/**
* Needed for ojbect serialization (sending part).
* @param objectOutputStream Specifies the output stream to use.
* @throws IOException
*/
private void writeObject(ObjectOutputStream objectOutputStream)
throws IOException {
objectOutputStream.writeInt(iAction);
objectOutputStream.writeInt(iNumEventsToRequest);
objectOutputStream.writeObject(sPassphrase);
objectOutputStream.writeBoolean(bMainRegexp);
objectOutputStream.writeBoolean(bCaseInsensitive);
super.writeObjectIfDefined(objectOutputStream, sString);
super.writeObjectIfDefined(objectOutputStream, patAll);
super.writeObjectIfDefined(objectOutputStream, patCategory);
super.writeObjectIfDefined(objectOutputStream, patDescription);
super.writeObjectIfDefined(objectOutputStream, patPlace);
super.writeObjectIfDefined(objectOutputStream, patDate);
super.writeObjectIfDefined(objectOutputStream, dateRangeFrom);
super.writeObjectIfDefined(objectOutputStream, dateRangeTo);
super.writeObjectIfDefined(objectOutputStream, event);
}
/**
* Needed for object serialization (recieving part).
* @param objectInputStream Specifies the input stream to use.
* @throws IOException
* @throws ClassNotFoundException
*/
private void readObject(ObjectInputStream objectInputStream)
throws IOException, ClassNotFoundException {
iAction = objectInputStream.readInt();
iNumEventsToRequest = objectInputStream.readInt();
sPassphrase = (String) objectInputStream.readObject();
setMainRegexp(objectInputStream.readBoolean());
bCaseInsensitive = objectInputStream.readBoolean();
if (objectInputStream.readBoolean())
sString = (String) objectInputStream.readObject();
if (objectInputStream.readBoolean())
patAll = (Pattern) objectInputStream.readObject();
if (objectInputStream.readBoolean())
patCategory = (Pattern) objectInputStream.readObject();
if (objectInputStream.readBoolean())
patDescription = (Pattern) objectInputStream.readObject();
if (objectInputStream.readBoolean())
patPlace = (Pattern) objectInputStream.readObject();
if (objectInputStream.readBoolean())
patDate = (Pattern) objectInputStream.readObject();
if (objectInputStream.readBoolean())
dateRangeFrom = (Date) objectInputStream.readObject();
if (objectInputStream.readBoolean())
dateRangeTo = (Date) objectInputStream.readObject();
if (objectInputStream.readBoolean())
event = (CalendarEvent) objectInputStream.readObject();
}
/**
* Checks if a given pattern string has other charactars than whitespaces inside or not.
* @param sRegexp Specifies the pattern string to check against.
* @return Returns true if the pattern string only contains whitespaces. Else, false will be returned.
*/
private boolean isEmptyPattern(String sRegexp) {
return !emptyPattern.matcher(sRegexp).find();
}
/**
* This method checks if the request's passphrase is correct or not.
* @param sPassphrase Specifies the server passphrase to chack against the request passphrase.
* @return Returns true if the passphrases equal. Else, false will be returned.
*/
public boolean checkPassphrase(String sPassphrase) {
return this.sPassphrase.equals(sPassphrase);
}
/**
* This method returns a representation the object as a string.
* @return Returns the object represented as a string.
*/
public String toString() {
return
"Action : " + iAction + "\n" +
"NumEvents : " + iNumEventsToRequest + "\n" +
"MainRegexp : " + bMainRegexp + "\n" +
"patAll : " + patAll + "\n" +
"patCategory : " + patCategory + "\n" +
"patDescription: " + patDescription + "\n" +
"patDate : " + patDate + "\n" +
"dateRangeFrom : " + dateRangeFrom + "\n" +
"dateRangeTo : " + dateRangeTo + "\n" +
"modifiedEvent :\n" + event+ "\n";
}
}
|