blob: 50caecbe68fe55351452760d1ccc100425c72eb4 (
plain)
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
|
package server;
import java.util.*;
import shared.*;
import shared.remotecall.*;
/**
* A object of this class encapsulates the whole calendar database on the server side.
* This class is used for getting/modifying and adding/removing of calendar events.
* @author buetow
*
*/
public final class CalendarDatabase {
private Vector vecCategories;
private HashMap mapEvents;
private HashMap mapCategories;
/**
* Simple constructur. Creates a calendar database.
* @param vecCategories Specifies the vector of CalendarCategory objects for initial database content.
*/
public CalendarDatabase(Vector vecCategories) {
this.vecCategories = vecCategories;
initializeMaps();
}
/**
* Initializes a private hash map so that the server can access events by their id numbers very fast.
*/
private void initializeMaps() {
this.mapEvents = new HashMap();
this.mapCategories = new HashMap();
Enumeration enumCategory = vecCategories.elements();
while (enumCategory.hasMoreElements()) {
CalendarCategory category = (CalendarCategory) enumCategory.nextElement();
mapCategories.put(category.getName(), category);
Vector vecEvents = category.getEvents();
Enumeration enumEvents = vecEvents.elements();
while (enumEvents.hasMoreElements()) {
CalendarEvent event = (CalendarEvent) enumEvents.nextElement();
mapEvents.put(new Integer(event.getEventID()), event);
}
}
}
/**
* Returns a specific calendar event using fast hash lookup.
* @param iEventID Specifies the ID of the calendar event to lookup.
* @return Returns the requested calendar event.
*/
public CalendarEvent getEvent(int iEventID) {
return (CalendarEvent) mapEvents.get(new Integer(iEventID));
}
/**
* Returns a specific calendar category using fast hash lookup.
* @param sCategoryName Specifies the name of the calendar category to lookup.
* @return Returns the requested calendar category.
*/
public CalendarCategory getCategory(String sCategoryName) {
return (CalendarCategory) mapCategories.get(sCategoryName);
}
/**
* Gets all calendar categories.
* @return Returns a Vector of all available CalendarCategory objects.
*/
public Vector getCategories() {
return vecCategories;
}
/**
* This method returns a Vector of all available calendar events.
* @return Returns all available CalendarEvent objects of the database.
*/
public Vector getAllEvents() {
MyVector vecAllEvents = new MyVector();
Enumeration enumCategories = vecCategories.elements();
while (enumCategories.hasMoreElements()) {
CalendarCategory category = (CalendarCategory) enumCategories.nextElement();
vecAllEvents.appendVector(category.getEvents());
}
return vecAllEvents;
}
/**
* This method looks for all calendar events which match the a given client request.
* @param clientRequest Specifies the client request sent by the calendar client.
* @return Returns a Vector of all matching CalendarEvent objects.
*/
public Vector getMatchingEvents(ClientRequest clientRequest) {
MyVector vecEvents = new MyVector();
int iNumEventsToRequest = clientRequest.getNumEventsToRequest();
Enumeration enumCategories = vecCategories.elements();
while (enumCategories.hasMoreElements()) {
CalendarCategory category = (CalendarCategory) enumCategories.nextElement();
Vector vecNewEvents = category.getMatchingEvents(clientRequest);
vecEvents.appendVector(vecNewEvents);
if (iNumEventsToRequest > -1) {
if (iNumEventsToRequest == vecEvents.size())
break;
} else {
clientRequest.setNumEventsToRequest(iNumEventsToRequest - vecNewEvents.size());
}
}
return vecEvents;
}
/**
* This method adds a calendar event to the calendar database.
* @param clientRequest Specifies the client request sent by the netcalendar client. Its containing the new event to add.
*/
public void addEvent(ClientRequest clientRequest) {
CalendarEvent event = clientRequest.getEvent();
String sCategoryName = event.getCategoryName();
CalendarCategory category = getCategory(sCategoryName);
// Check if the have to create a new category
if (category == null) {
category = new CalendarCategory(sCategoryName);
mapCategories.put(sCategoryName, category);
vecCategories.add(category);
}
category.addEvent(event);
category.setHasChanged(true);
event.setCategory(category);
mapEvents.put(new Integer(event.getEventID()), event);
}
/**
* This method modifies an calendar event of the calendar database.
* @param clientRequest Specifies the client request sent by the calendar client.
*/
public void modifyEvent(ClientRequest clientRequest) {
CalendarEvent newEvent = clientRequest.getEvent();
CalendarEvent orgEvent = getEvent(newEvent.getEventID());
// If the event doesnt exist any more, recreate it
if (orgEvent == null) {
clientRequest.setEvent(newEvent);
addEvent(clientRequest);
return;
}
orgEvent.setDate(newEvent.getDate());
orgEvent.setYearly(newEvent.isYearly());
orgEvent.setDescription(newEvent.getDescription());
orgEvent.setPlace(newEvent.getPlace());
// Modify the events category
if (!orgEvent.getCategoryName().equals(newEvent.getCategoryName())) {
String sNewCategoryName = newEvent.getCategoryName();
// First remove the event from its current category
orgEvent.removeFromCurrentCategory();
CalendarCategory category = getCategory(sNewCategoryName);
if (category == null) {
category = new CalendarCategory(sNewCategoryName);
mapCategories.put(sNewCategoryName, category);
vecCategories.add(category);
}
category.addEvent(orgEvent);
category.setHasChanged(true);
orgEvent.setCategory(category);
} else {
orgEvent.getCategory().setHasChanged(true);
}
}
/**
* This method deletes a calendar event of the calendar database. If the specified calendar event doesnt
* exist eny more, nothing will be changed.
* @param clientRequest Specifies the client request sent by the netcalendar client.
*/
public void deleteEvent(ClientRequest clientRequest) {
// Get the server side reference of the calendar event
CalendarEvent event = getEvent(clientRequest.getEvent().getEventID());
if (event != null) {
event.getCategory().setHasChanged(true);
event.removeFromCurrentCategory();
mapEvents.remove(new Integer(event.getEventID()));
}
}
/**
* This method renames a calendar category.
* @param clientRequest Specifies the client request sent by the netcalendar client.
*/
public void renameCategory(ClientRequest clientRequest) {
String sOldCategoryName = clientRequest.getEvent().getCategoryName();
CalendarCategory category = getCategory(sOldCategoryName);
if (category == null)
return;
String sNewCategoryName = clientRequest.getString();
if (sOldCategoryName.equals(sNewCategoryName))
return;
mapCategories.remove(sOldCategoryName);
category.setName(sNewCategoryName);
Vector vecEvents = category.getEvents();
Enumeration enumEvents = vecEvents.elements();
while (enumEvents.hasMoreElements()) {
CalendarEvent event = (CalendarEvent) enumEvents.nextElement();
event.setCategoryName(sNewCategoryName);
}
// Check if the category exists already
CalendarCategory categoryExists = getCategory(sNewCategoryName);
if (categoryExists == null) {
mapCategories.put(category.getName(), category);
} else {
vecCategories.remove(category);
categoryExists.merge(category);
}
}
/**
* This method daletes a calendar category.
* @param clientRequest Specifies the client request sent by the netcalendar client.
*/
public void deleteCategory(ClientRequest clientRequest) {
String sCategoryName = clientRequest.getEvent().getCategoryName();
CalendarCategory category = getCategory(sCategoryName);
if (category == null)
return;
mapCategories.remove(sCategoryName);
vecCategories.remove(category);
category.deleteDatabaseFile();
}
/**
* This method flushes all the changed calendar categories of the calendar database to the filesystem.
*/
public void flush() {
Enumeration enumCategories = vecCategories.elements();
while (enumCategories.hasMoreElements()) {
CalendarCategory category = (CalendarCategory) enumCategories.nextElement();
if (category.hasChanged())
category.flush();
}
Main.execExternalCommand(Config.getStringValue("server_updatedb_command"));
}
}
|