diff options
| author | Paul Buetow <paul@buetow.org> | 2009-02-18 23:01:21 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2009-02-18 23:01:21 +0000 |
| commit | 89c679eb813611a2ede208b4c8e27c7f596b3be7 (patch) | |
| tree | f44c19435286e78b9e8002990e1b55183a4fbc49 /sources/shared/CalendarCategory.java | |
| parent | 177f346a9bbe455dc12fd26dfa2c586585c28c1e (diff) | |
added copyright to header
Diffstat (limited to 'sources/shared/CalendarCategory.java')
| -rw-r--r-- | sources/shared/CalendarCategory.java | 408 |
1 files changed, 206 insertions, 202 deletions
diff --git a/sources/shared/CalendarCategory.java b/sources/shared/CalendarCategory.java index 94c215c..1e686d7 100644 --- a/sources/shared/CalendarCategory.java +++ b/sources/shared/CalendarCategory.java @@ -1,3 +1,7 @@ +/* NetCalendar 2006, 2009 (c) Dipl.-Inform. (FH) Paul C. Buetow + * http://netcalendar.buetow.org - netcalendar@dev.buetow.org + */ + /** * */ @@ -18,206 +22,206 @@ import shared.remotecall.*; * */ public class CalendarCategory { - private Vector vecEvents; - private String sCategoryName; - private boolean bHasChanged = false; - private File categoryFile; - - /** - * Creates a new calendar category object and sets the categorie's name to the filename of the category. - * @param categoryFile The file object which contains the unparsed events for this specific category. It will be unset after parsing. - */ - public CalendarCategory(File categoryFile) { - this.categoryFile = categoryFile; - this.sCategoryName = categoryFile.getName(); - - // sCategoryName is now "calendar.Name", remove now the leading 'calendar.' - sCategoryName = sCategoryName.substring(sCategoryName.indexOf('.') + 1); - } - - /** - * Creates a new calendar category object and sets the categorie's File to the name of the category. - * @param sCategoryName Specifies the categorie's name. - */ - public CalendarCategory(String sCategoryName) { - this.sCategoryName = sCategoryName; - } - - /** - * This method sets the categorie's initial events. - * @param vecEvents Specifies the categorie's events. - */ - public void setEvents(Vector vecEvents) { - this.vecEvents = vecEvents; - } - - /** - * This method adds an event to the category. - * @param event Specifies the calendar event to add to this category. - */ - public void addEvent(CalendarEvent event) { - if (vecEvents == null) - vecEvents = new Vector(); - - vecEvents.add(event); - } - - /** - * This method returns the calendar events file object. - * @return Returns the categorie's file object. - */ - public File getFile() { - return categoryFile; - } - - /** - * This method Sets the categories file handle to null. This is done if all the data has been read from - * the file database. - */ - public void unsetFile() { - categoryFile = null; - } - - /** - * This method returns the categorie's name. - * @return Returns the categorie's name. - */ - public String getName() { - return sCategoryName; - } - - /** - * This method sets the categorie's name. - */ - public void setName(String sCategoryName) { - if (this.sCategoryName.equals(sCategoryName)) - return; - - deleteDatabaseFile(); - this.sCategoryName = sCategoryName; - categoryFile = new File(Config.getStringValue("server_database_dir")+"/calendar."+sCategoryName); - setHasChanged(true); - } - - /** - * This method deletes the database file of this event. - */ - public void deleteDatabaseFile() { - if (categoryFile == null) - categoryFile = new File(Config.getStringValue("server_database_dir")+"/calendar."+getName()); - - Main.infoMessage("Removing " + categoryFile.getPath()); - categoryFile.delete(); - } - - /** - * This method returns the categorie's events. - * @return Returns a Vector of all CalendarEvent objects. - */ - public Vector getEvents() { - return vecEvents; - } - - /** - * This method looks for all calendar events of this category which matches a given client's 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) { - Vector vecMatching = new Vector(); - int iNumEventsToRequest = clientRequest.getNumEventsToRequest(); - - Enumeration enumEvents = vecEvents.elements(); - while (enumEvents.hasMoreElements()) { - CalendarEvent event = (CalendarEvent) enumEvents.nextElement(); - if (clientRequest.match(event)) - vecMatching.add(event); - - if (iNumEventsToRequest > -1 && iNumEventsToRequest == vecMatching.size()) - break; - } - - return vecMatching; - } - - /** - * This method checks the current category's name against a regular expression pattern. - * @param pattern Specifies the pattern to be used. - * @return Returns true if the event matches, else false will be returned. - */ - public boolean matchesName(Pattern pattern) { - if (pattern == null) - return false; - - return pattern.matcher(getName()).find(); - } - - /** - * This method removes a given event from its event vector. - * @param event Specifies the calendar event object to be removed from this category - */ - public void removeEvent(CalendarEvent event) { - getEvents().removeElement(event); - } - - /** - * This method lets you check if the calendar category has been modified/changed by a - * calendar client request. This is needed by the CalendarDatabase.flush method, to write all - * the changes back to the database files. - * @return Returns true if the category has changed, else it will return false. - */ - public boolean hasChanged() { - return bHasChanged; - } - - /** - * This method merges two categories. - * @param calendarCategory Specifies the category to merge with. - */ - public void merge(CalendarCategory calendarCategory) { - Enumeration enumEvents = calendarCategory.getEvents().elements(); - while (enumEvents.hasMoreElements()) - vecEvents.add(enumEvents.nextElement()); - - System.out.println(vecEvents); - } - - /** - * This method specifies if the calendar category has been modified/changed or not. - * @param bHasChanged Specifies if the category has been modified/changed or not. - */ - public void setHasChanged(boolean bHasChanged) { - this.bHasChanged = bHasChanged; - } - - /** - * This method flushes the calendar category to the database at the filesystem. - */ - public void flush() { - Main.infoMessage("Flushing category " + getName() + " to database!"); - - if (vecEvents.size() == 0) { - deleteDatabaseFile(); - return; - } - - try { - FileWriter fileWriter = new FileWriter( - Config.getStringValue("server_database_dir") + "/calendar." + getName()); - BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); - - Enumeration enumEvent = vecEvents.elements(); - while (enumEvent.hasMoreElements()) { - CalendarEvent event = (CalendarEvent) enumEvent.nextElement(); - event.flush(bufferedWriter); - } - - bufferedWriter.close(); - - } catch (IOException e) { - Main.infoMessage("Error: Database flush error: " + e.getMessage()); - } - - setHasChanged(false); - } + private Vector vecEvents; + private String sCategoryName; + private boolean bHasChanged = false; + private File categoryFile; + + /** + * Creates a new calendar category object and sets the categorie's name to the filename of the category. + * @param categoryFile The file object which contains the unparsed events for this specific category. It will be unset after parsing. + */ + public CalendarCategory(File categoryFile) { + this.categoryFile = categoryFile; + this.sCategoryName = categoryFile.getName(); + + // sCategoryName is now "calendar.Name", remove now the leading 'calendar.' + sCategoryName = sCategoryName.substring(sCategoryName.indexOf('.') + 1); + } + + /** + * Creates a new calendar category object and sets the categorie's File to the name of the category. + * @param sCategoryName Specifies the categorie's name. + */ + public CalendarCategory(String sCategoryName) { + this.sCategoryName = sCategoryName; + } + + /** + * This method sets the categorie's initial events. + * @param vecEvents Specifies the categorie's events. + */ + public void setEvents(Vector vecEvents) { + this.vecEvents = vecEvents; + } + + /** + * This method adds an event to the category. + * @param event Specifies the calendar event to add to this category. + */ + public void addEvent(CalendarEvent event) { + if (vecEvents == null) + vecEvents = new Vector(); + + vecEvents.add(event); + } + + /** + * This method returns the calendar events file object. + * @return Returns the categorie's file object. + */ + public File getFile() { + return categoryFile; + } + + /** + * This method Sets the categories file handle to null. This is done if all the data has been read from + * the file database. + */ + public void unsetFile() { + categoryFile = null; + } + + /** + * This method returns the categorie's name. + * @return Returns the categorie's name. + */ + public String getName() { + return sCategoryName; + } + + /** + * This method sets the categorie's name. + */ + public void setName(String sCategoryName) { + if (this.sCategoryName.equals(sCategoryName)) + return; + + deleteDatabaseFile(); + this.sCategoryName = sCategoryName; + categoryFile = new File(Config.getStringValue("server_database_dir")+"/calendar."+sCategoryName); + setHasChanged(true); + } + + /** + * This method deletes the database file of this event. + */ + public void deleteDatabaseFile() { + if (categoryFile == null) + categoryFile = new File(Config.getStringValue("server_database_dir")+"/calendar."+getName()); + + Main.infoMessage("Removing " + categoryFile.getPath()); + categoryFile.delete(); + } + + /** + * This method returns the categorie's events. + * @return Returns a Vector of all CalendarEvent objects. + */ + public Vector getEvents() { + return vecEvents; + } + + /** + * This method looks for all calendar events of this category which matches a given client's 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) { + Vector vecMatching = new Vector(); + int iNumEventsToRequest = clientRequest.getNumEventsToRequest(); + + Enumeration enumEvents = vecEvents.elements(); + while (enumEvents.hasMoreElements()) { + CalendarEvent event = (CalendarEvent) enumEvents.nextElement(); + if (clientRequest.match(event)) + vecMatching.add(event); + + if (iNumEventsToRequest > -1 && iNumEventsToRequest == vecMatching.size()) + break; + } + + return vecMatching; + } + + /** + * This method checks the current category's name against a regular expression pattern. + * @param pattern Specifies the pattern to be used. + * @return Returns true if the event matches, else false will be returned. + */ + public boolean matchesName(Pattern pattern) { + if (pattern == null) + return false; + + return pattern.matcher(getName()).find(); + } + + /** + * This method removes a given event from its event vector. + * @param event Specifies the calendar event object to be removed from this category + */ + public void removeEvent(CalendarEvent event) { + getEvents().removeElement(event); + } + + /** + * This method lets you check if the calendar category has been modified/changed by a + * calendar client request. This is needed by the CalendarDatabase.flush method, to write all + * the changes back to the database files. + * @return Returns true if the category has changed, else it will return false. + */ + public boolean hasChanged() { + return bHasChanged; + } + + /** + * This method merges two categories. + * @param calendarCategory Specifies the category to merge with. + */ + public void merge(CalendarCategory calendarCategory) { + Enumeration enumEvents = calendarCategory.getEvents().elements(); + while (enumEvents.hasMoreElements()) + vecEvents.add(enumEvents.nextElement()); + + System.out.println(vecEvents); + } + + /** + * This method specifies if the calendar category has been modified/changed or not. + * @param bHasChanged Specifies if the category has been modified/changed or not. + */ + public void setHasChanged(boolean bHasChanged) { + this.bHasChanged = bHasChanged; + } + + /** + * This method flushes the calendar category to the database at the filesystem. + */ + public void flush() { + Main.infoMessage("Flushing category " + getName() + " to database!"); + + if (vecEvents.size() == 0) { + deleteDatabaseFile(); + return; + } + + try { + FileWriter fileWriter = new FileWriter( + Config.getStringValue("server_database_dir") + "/calendar." + getName()); + BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); + + Enumeration enumEvent = vecEvents.elements(); + while (enumEvent.hasMoreElements()) { + CalendarEvent event = (CalendarEvent) enumEvent.nextElement(); + event.flush(bufferedWriter); + } + + bufferedWriter.close(); + + } catch (IOException e) { + Main.infoMessage("Error: Database flush error: " + e.getMessage()); + } + + setHasChanged(false); + } } |
