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/server | |
| parent | 177f346a9bbe455dc12fd26dfa2c586585c28c1e (diff) | |
added copyright to header
Diffstat (limited to 'sources/server')
| -rw-r--r-- | sources/server/CalendarDatabase.java | 448 | ||||
| -rw-r--r-- | sources/server/CalendarFormatParser.java | 466 | ||||
| -rw-r--r-- | sources/server/NetCalendarServer.java | 274 |
3 files changed, 600 insertions, 588 deletions
diff --git a/sources/server/CalendarDatabase.java b/sources/server/CalendarDatabase.java index 3b5e1e0..0e37d0d 100644 --- a/sources/server/CalendarDatabase.java +++ b/sources/server/CalendarDatabase.java @@ -1,3 +1,7 @@ +/* NetCalendar 2006, 2009 (c) Dipl.-Inform. (FH) Paul C. Buetow + * http://netcalendar.buetow.org - netcalendar@dev.buetow.org + */ + package server; import java.util.*; @@ -12,260 +16,260 @@ import shared.remotecall.*; * */ 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); - } + 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(); } - } - - /** - * 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()); + + /** + * 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); + } + } } - return vecAllEvents; - } + /** + * 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)); + } - /** - * 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(); + /** + * 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); + } - Enumeration enumCategories = vecCategories.elements(); - while (enumCategories.hasMoreElements()) { - CalendarCategory category = (CalendarCategory) enumCategories.nextElement(); - Vector vecNewEvents = category.getMatchingEvents(clientRequest); - vecEvents.appendVector(vecNewEvents); + /** + * Gets all calendar categories. + * @return Returns a Vector of all available CalendarCategory objects. + */ + public Vector getCategories() { + return vecCategories; + } - if (iNumEventsToRequest > -1) { - if (iNumEventsToRequest == vecEvents.size()) - break; + /** + * 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(); - } else { - clientRequest.setNumEventsToRequest(iNumEventsToRequest - vecNewEvents.size()); - } + while (enumCategories.hasMoreElements()) { + CalendarCategory category = (CalendarCategory) enumCategories.nextElement(); + vecAllEvents.appendVector(category.getEvents()); + } + return vecAllEvents; } - 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); - } + /** + * 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(); - 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; - } + Enumeration enumCategories = vecCategories.elements(); + while (enumCategories.hasMoreElements()) { + CalendarCategory category = (CalendarCategory) enumCategories.nextElement(); + Vector vecNewEvents = category.getMatchingEvents(clientRequest); + vecEvents.appendVector(vecNewEvents); - orgEvent.setDate(newEvent.getDate()); - orgEvent.setYearly(newEvent.isYearly()); - orgEvent.setDescription(newEvent.getDescription()); - orgEvent.setPlace(newEvent.getPlace()); + if (iNumEventsToRequest > -1) { + if (iNumEventsToRequest == vecEvents.size()) + break; - // Modify the events category - if (!orgEvent.getCategoryName().equals(newEvent.getCategoryName())) { - String sNewCategoryName = newEvent.getCategoryName(); + } else { + clientRequest.setNumEventsToRequest(iNumEventsToRequest - vecNewEvents.size()); + } - // 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); - } + return vecEvents; + } - category.addEvent(orgEvent); - category.setHasChanged(true); - orgEvent.setCategory(category); + /** + * 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); + } - } else { - orgEvent.getCategory().setHasChanged(true); + /** + * 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 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); + /** + * 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; + if (category == null) + return; - String sNewCategoryName = clientRequest.getString(); + String sNewCategoryName = clientRequest.getString(); - if (sOldCategoryName.equals(sNewCategoryName)) - return; + if (sOldCategoryName.equals(sNewCategoryName)) + return; - mapCategories.remove(sOldCategoryName); + mapCategories.remove(sOldCategoryName); - category.setName(sNewCategoryName); + category.setName(sNewCategoryName); - Vector vecEvents = category.getEvents(); - Enumeration enumEvents = vecEvents.elements(); - while (enumEvents.hasMoreElements()) { - CalendarEvent event = (CalendarEvent) enumEvents.nextElement(); - event.setCategoryName(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); - // 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); + } - 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(); + /** + * 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(); } - Main.execExternalCommand(Config.getStringValue("server_updatedb_command")); - } + /** + * 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")); + } } diff --git a/sources/server/CalendarFormatParser.java b/sources/server/CalendarFormatParser.java index 5d38f24..852fd73 100644 --- a/sources/server/CalendarFormatParser.java +++ b/sources/server/CalendarFormatParser.java @@ -1,3 +1,7 @@ +/* NetCalendar 2006, 2009 (c) Dipl.-Inform. (FH) Paul C. Buetow + * http://netcalendar.buetow.org - netcalendar@dev.buetow.org + */ + /** * */ @@ -20,259 +24,259 @@ import shared.*; * */ public final class CalendarFormatParser { - private int iCurrentYear; - private Date dateCurrent; - private String sWorkdir; - private Vector vecCategories; - private static final Pattern datePattern; - private static final Pattern dateYearlyPattern; - private static final Pattern dateElsePattern; - private static final Pattern dateEndPattern; - // private static final Pattern emptyLine; - private static final String DATE_FORMAT = "M/d-y"; - private static final String EXTENDED_DATE_FORMAT = "M/d-y-H:m"; - - // Those pattern will be used later, precompile here once to use often - static { - // Matches calendar date format like: "01/25 2006-14:23" - datePattern = Pattern.compile("\\d{2}/\\d{2}\t\\d{4}-\\d{2}:\\d{2}"); - - // Matches calendar date format like: "01/25 yearly-14:23" - dateYearlyPattern = Pattern.compile("\\d{2}/\\d{2}\tyearly-\\d{2}:\\d{2}"); - - // Matches calendar date format like: "01/25 Some free text here - dateElsePattern = Pattern.compile("\\d{2}/\\d{2}\t"); - - // Matches the end of the date string like: "arly-14:23 " or "2006-14:23" - dateEndPattern = Pattern.compile(".{4}-\\d{2}:\\d{2}"); - } - - /** - * Simple constructor. Creates a calendar format parser object and initializes some private members. - */ - public CalendarFormatParser() { - // Use current dir as workdir by default - this.sWorkdir = "."; - this.vecCategories = new Vector(); - - // Events without a year specified will use the current year! - GregorianCalendar cal = new GregorianCalendar(); - iCurrentYear = cal.get(Calendar.YEAR); - - dateCurrent = new Date(); - } - - /** - * This method returns a vector of all found calendar categories after parsing. - * @return Returns a Vector of all available CalendarCategory objects. - */ - public Vector getCategories() { - return vecCategories; - } - - /** - * This method sets the working directory. Its the "server_database_dir" variable defined in the current configuration by default. - * @param sWorkdir Specifies the working directory. - */ - public void setWorkdir(String sWorkdir) { - this.sWorkdir = sWorkdir; - } - - /** - * Starts the parsing work of the calendar database files. - */ - public void start() { - lookForCategories(); - parseAllCategories(); - Main.execExternalCommand(Config.getStringValue("server_startup_command")); - } - - /** - * Parses for the available calendar categories. - */ - private void lookForCategories() { - File dir = new File(sWorkdir); - File[] dirContent = dir.listFiles(); - - for (int i = 0; i < dirContent.length; ++i) - if (dirContent[i].isFile()) - // Ignore the 'calendar' file, only read the 'calendar.*' files - if (!dirContent[i].getName().equals("calendar")) - vecCategories.add(new CalendarCategory(dirContent[i])); - } - - /** - * Parses all events of all available categories. - */ - private void parseAllCategories() { - Enumeration enumCategories = vecCategories.elements(); - while (enumCategories.hasMoreElements()) - parseCategory((CalendarCategory) enumCategories.nextElement()); - } - - /** - * Parses all events of a category file. - * @param category Specifies the calendar category to be parsed. - */ - private void parseCategory(CalendarCategory category) { - Vector vecEvents = new Vector(); - File file = category.getFile(); - - try { - BufferedReader in = new BufferedReader(new FileReader(file)); - String sLine; - - while ((sLine = in.readLine()) != null) { - CalendarEvent event = new CalendarEvent(category); - - // Ignore empty lines! - if (sLine.equals("")) - continue; - - setEventsDate(event, sLine); - setEventsPlace(event, sLine); - setEventsDescription(event, sLine); - - vecEvents.add(event); - } - - } catch (Exception e) { - Main.infoMessage("Error: " + e.toString()); + private int iCurrentYear; + private Date dateCurrent; + private String sWorkdir; + private Vector vecCategories; + private static final Pattern datePattern; + private static final Pattern dateYearlyPattern; + private static final Pattern dateElsePattern; + private static final Pattern dateEndPattern; + // private static final Pattern emptyLine; + private static final String DATE_FORMAT = "M/d-y"; + private static final String EXTENDED_DATE_FORMAT = "M/d-y-H:m"; + + // Those pattern will be used later, precompile here once to use often + static { + // Matches calendar date format like: "01/25 2006-14:23" + datePattern = Pattern.compile("\\d{2}/\\d{2}\t\\d{4}-\\d{2}:\\d{2}"); + + // Matches calendar date format like: "01/25 yearly-14:23" + dateYearlyPattern = Pattern.compile("\\d{2}/\\d{2}\tyearly-\\d{2}:\\d{2}"); + + // Matches calendar date format like: "01/25 Some free text here + dateElsePattern = Pattern.compile("\\d{2}/\\d{2}\t"); + + // Matches the end of the date string like: "arly-14:23 " or "2006-14:23" + dateEndPattern = Pattern.compile(".{4}-\\d{2}:\\d{2}"); } - category.setEvents(vecEvents); - category.unsetFile(); - } - - /** - * This method parses all known informations from a given calendar format line and saves them - * into the given calendar event object. - * @param event Specifies the calendar event to be modified. - * @param sLine Specifies the single line of the category file to be parsed. - */ - private void setEventsDate(CalendarEvent event, String sLine) { - // Create a local copy because the string may be modified - String sMyLine = new String(sLine).replaceAll(" ", ""); - String sDateFormat; - boolean bValidDateFormat = false; - - // Check if its a yearly event - Matcher matcher = dateYearlyPattern.matcher(sMyLine); - if (matcher.find()) { - sMyLine = sMyLine.replaceFirst("\tyearly", "-" + iCurrentYear); - sDateFormat = EXTENDED_DATE_FORMAT; - event.setYearly(true); - bValidDateFormat = true; - - } else { - // Event is not yearly, but check if its still using NetCalendars - // extended format which contains the event's time (hours:minutes) - matcher = datePattern.matcher(sMyLine); - if (matcher.find()) { - sMyLine = sMyLine.replaceFirst("\t", "-"); - sDateFormat = EXTENDED_DATE_FORMAT; - event.setYearly(false); - bValidDateFormat = true; - - } else { - //matcher = dateElsePattern.matcher(sMyLine); - //if (matcher.find()) { - // Just use original Calendar format without any year and time - // informations - // Assume yearly - sMyLine = sMyLine.replaceFirst("\t", "-" + iCurrentYear); - sDateFormat = DATE_FORMAT; - event.setYearly(true); - bValidDateFormat = true; - /*} else { - return; - }*/ - } + /** + * Simple constructor. Creates a calendar format parser object and initializes some private members. + */ + public CalendarFormatParser() { + // Use current dir as workdir by default + this.sWorkdir = "."; + this.vecCategories = new Vector(); + + // Events without a year specified will use the current year! + GregorianCalendar cal = new GregorianCalendar(); + iCurrentYear = cal.get(Calendar.YEAR); + + dateCurrent = new Date(); } - // Create a new date object containing the events time informations - SimpleDateFormat formatter = new SimpleDateFormat(sDateFormat); - Date date = null; + /** + * This method returns a vector of all found calendar categories after parsing. + * @return Returns a Vector of all available CalendarCategory objects. + */ + public Vector getCategories() { + return vecCategories; + } - if (bValidDateFormat) { - try { - date = formatter.parse(sMyLine); + /** + * This method sets the working directory. Its the "server_database_dir" variable defined in the current configuration by default. + * @param sWorkdir Specifies the working directory. + */ + public void setWorkdir(String sWorkdir) { + this.sWorkdir = sWorkdir; + } - } catch (ParseException e) { - Main.infoMessage("Error: Calendar format parser error at category " - + event.getCategoryName() + ": " + e.getMessage()); - } + /** + * Starts the parsing work of the calendar database files. + */ + public void start() { + lookForCategories(); + parseAllCategories(); + Main.execExternalCommand(Config.getStringValue("server_startup_command")); } - // The event is yearly, but occured already this year, so increment the events - // year by one! - if (event.isYearly() && date.getTime() < dateCurrent.getTime()) { - Calendar calendar = new GregorianCalendar(); - calendar.setTime(date); - calendar.set(Calendar.YEAR, iCurrentYear + 1); - date = calendar.getTime(); + /** + * Parses for the available calendar categories. + */ + private void lookForCategories() { + File dir = new File(sWorkdir); + File[] dirContent = dir.listFiles(); + + for (int i = 0; i < dirContent.length; ++i) + if (dirContent[i].isFile()) + // Ignore the 'calendar' file, only read the 'calendar.*' files + if (!dirContent[i].getName().equals("calendar")) + vecCategories.add(new CalendarCategory(dirContent[i])); } - event.setDate(date); - } + /** + * Parses all events of all available categories. + */ + private void parseAllCategories() { + Enumeration enumCategories = vecCategories.elements(); + while (enumCategories.hasMoreElements()) + parseCategory((CalendarCategory) enumCategories.nextElement()); + } - /** - * Parses a single calendar line for the place information. - * @param event Specifies the calendar event to be modified. - * @param sLine Specifies the single line of the category file to be parsed. - */ - private void setEventsPlace(CalendarEvent event, String sLine) { - int iPos = sLine.indexOf(";;"); + /** + * Parses all events of a category file. + * @param category Specifies the calendar category to be parsed. + */ + private void parseCategory(CalendarCategory category) { + Vector vecEvents = new Vector(); + File file = category.getFile(); - // No event! Return empty string! - if (iPos < 0) { - event.setPlace(""); + try { + BufferedReader in = new BufferedReader(new FileReader(file)); + String sLine; - } else { - event.setPlace(trim(sLine.substring(iPos + 2))); - } - } + while ((sLine = in.readLine()) != null) { + CalendarEvent event = new CalendarEvent(category); - /** - * Parses a single calendar line for the description information. - * @param event Specifies the calendar event to be modified. - * @param sLine Specifies the single line of the category file to be parsed. - */ - private void setEventsDescription(CalendarEvent event, String sLine) { - // We need a local copy because we may modify the string - String sTmp = new String(sLine); + // Ignore empty lines! + if (sLine.equals("")) + continue; - // Check if there is a place string... - int iPos = sLine.indexOf(";;"); + setEventsDate(event, sLine); + setEventsPlace(event, sLine); + setEventsDescription(event, sLine); - // ... if yes, dont include it! - if (iPos >= 0) - sTmp = sTmp.substring(0, iPos); + vecEvents.add(event); + } - // Remove the events date from the string - Matcher matcher = dateEndPattern.matcher(sTmp); + } catch (Exception e) { + Main.infoMessage("Error: " + e.toString()); + } - if (matcher.find()) { - sTmp = sTmp.substring(matcher.start()+11); + category.setEvents(vecEvents); + category.unsetFile(); + } - } else { - sTmp = sTmp.substring(sTmp.indexOf("\t")+1); + /** + * This method parses all known informations from a given calendar format line and saves them + * into the given calendar event object. + * @param event Specifies the calendar event to be modified. + * @param sLine Specifies the single line of the category file to be parsed. + */ + private void setEventsDate(CalendarEvent event, String sLine) { + // Create a local copy because the string may be modified + String sMyLine = new String(sLine).replaceAll(" ", ""); + String sDateFormat; + boolean bValidDateFormat = false; + + // Check if its a yearly event + Matcher matcher = dateYearlyPattern.matcher(sMyLine); + if (matcher.find()) { + sMyLine = sMyLine.replaceFirst("\tyearly", "-" + iCurrentYear); + sDateFormat = EXTENDED_DATE_FORMAT; + event.setYearly(true); + bValidDateFormat = true; + + } else { + // Event is not yearly, but check if its still using NetCalendars + // extended format which contains the event's time (hours:minutes) + matcher = datePattern.matcher(sMyLine); + if (matcher.find()) { + sMyLine = sMyLine.replaceFirst("\t", "-"); + sDateFormat = EXTENDED_DATE_FORMAT; + event.setYearly(false); + bValidDateFormat = true; + + } else { + //matcher = dateElsePattern.matcher(sMyLine); + //if (matcher.find()) { + // Just use original Calendar format without any year and time + // informations + // Assume yearly + sMyLine = sMyLine.replaceFirst("\t", "-" + iCurrentYear); + sDateFormat = DATE_FORMAT; + event.setYearly(true); + bValidDateFormat = true; + /*} else { + return; + }*/ + } + } + + // Create a new date object containing the events time informations + SimpleDateFormat formatter = new SimpleDateFormat(sDateFormat); + Date date = null; + + if (bValidDateFormat) { + try { + date = formatter.parse(sMyLine); + + } catch (ParseException e) { + Main.infoMessage("Error: Calendar format parser error at category " + + event.getCategoryName() + ": " + e.getMessage()); + } + } + + // The event is yearly, but occured already this year, so increment the events + // year by one! + if (event.isYearly() && date.getTime() < dateCurrent.getTime()) { + Calendar calendar = new GregorianCalendar(); + calendar.setTime(date); + calendar.set(Calendar.YEAR, iCurrentYear + 1); + date = calendar.getTime(); + } + + event.setDate(date); } - event.setDescription(trim(sTmp)); - } + /** + * Parses a single calendar line for the place information. + * @param event Specifies the calendar event to be modified. + * @param sLine Specifies the single line of the category file to be parsed. + */ + private void setEventsPlace(CalendarEvent event, String sLine) { + int iPos = sLine.indexOf(";;"); + + // No event! Return empty string! + if (iPos < 0) { + event.setPlace(""); + + } else { + event.setPlace(trim(sLine.substring(iPos + 2))); + } + } - /** - * Its like String.trim() but also removes a ending newline. - * @param sTrimString Specifies the String to be trimmed. - * @return Returns a copy of the string, with leading and trailing whitespace omitted, also a trailing newline will be omitted. - */ - private String trim(String sTrimString) { - int iPos = sTrimString.indexOf("\n"); + /** + * Parses a single calendar line for the description information. + * @param event Specifies the calendar event to be modified. + * @param sLine Specifies the single line of the category file to be parsed. + */ + private void setEventsDescription(CalendarEvent event, String sLine) { + // We need a local copy because we may modify the string + String sTmp = new String(sLine); - if (iPos >= 0) - return sTrimString.substring(0, iPos).trim(); + // Check if there is a place string... + int iPos = sLine.indexOf(";;"); - return sTrimString.trim(); - } + // ... if yes, dont include it! + if (iPos >= 0) + sTmp = sTmp.substring(0, iPos); + + // Remove the events date from the string + Matcher matcher = dateEndPattern.matcher(sTmp); + + if (matcher.find()) { + sTmp = sTmp.substring(matcher.start()+11); + + } else { + sTmp = sTmp.substring(sTmp.indexOf("\t")+1); + } + + event.setDescription(trim(sTmp)); + } + + /** + * Its like String.trim() but also removes a ending newline. + * @param sTrimString Specifies the String to be trimmed. + * @return Returns a copy of the string, with leading and trailing whitespace omitted, also a trailing newline will be omitted. + */ + private String trim(String sTrimString) { + int iPos = sTrimString.indexOf("\n"); + + if (iPos >= 0) + return sTrimString.substring(0, iPos).trim(); + + return sTrimString.trim(); + } } diff --git a/sources/server/NetCalendarServer.java b/sources/server/NetCalendarServer.java index ad02d2b..6fe6f74 100644 --- a/sources/server/NetCalendarServer.java +++ b/sources/server/NetCalendarServer.java @@ -1,3 +1,7 @@ +/* NetCalendar 2006, 2009 (c) Dipl.-Inform. (FH) Paul C. Buetow + * http://netcalendar.buetow.org - netcalendar@dev.buetow.org + */ + /** * */ @@ -18,143 +22,143 @@ import shared.remotecall.*; * @author buetow */ public class NetCalendarServer extends Thread { - private int iPort; - private String sWorkdir; - - /** - * Creates a NetCalendarServer instance and runs it as a thread. - * @param iPort Specifies the server port of the server to be used. - * @param sWorkdir Specifies the working directory of the server. - */ - public NetCalendarServer(int iPort, String sWorkdir) { - this.iPort = iPort; - this.sWorkdir = sWorkdir; - - start(); - } - - /** - * This method initializes a new server socket. - * @return Returns the server socket object. - */ - private ServerSocket makeServerSocket() throws IOException { - if (!Config.getBooleanValue("use_ssl")) - return new ServerSocket(iPort); - - ServerSocketFactory sslSocketFactory = SSLServerSocketFactory.getDefault(); - return sslSocketFactory.createServerSocket(iPort); - } - - /** - * This method specifies the start method of the Thread. Its setting up the server port. - */ - public void run() { - ServerSocket serverSocket = null; - Socket socket = null; - - // Read and parse the whole calendar database from file! - CalendarFormatParser parser = new CalendarFormatParser(); - parser.setWorkdir(sWorkdir); - parser.start(); - - CalendarDatabase calendarDatabase = new CalendarDatabase(parser.getCategories()); - - try { - serverSocket = makeServerSocket(); - - while (true) { - // This will wait for a connection to be made to this socket. - socket = serverSocket.accept(); - // serverSocket.accept(); - socket.setKeepAlive(true); - - // Recieve the client's request object - InputStream inputStream = socket.getInputStream(); - ObjectInput objectInput = new ObjectInputStream(inputStream); - ClientRequest clientRequest = (ClientRequest) objectInput.readObject(); - - if (!clientRequest.checkPassphrase(Config.getStringValue("passphrase", false))) { - Main.infoMessage("Server: Client refused, wrong passphrase!"); - - // Check if a event has been modified! - } else if (clientRequest.requestsNewEvents()) { - Main.infoMessage("Server: Client requests new events"); - - // Get all calendar events which match the request - Vector vecEvents = calendarDatabase.getMatchingEvents(clientRequest); - ServerResponse serverResponse = new ServerResponse(vecEvents); - - OutputStream outputStream = socket.getOutputStream(); - ObjectOutput objectOutput = new ObjectOutputStream(outputStream); - objectOutput.writeObject(serverResponse); - objectOutput.flush(); - objectOutput.close(); - - } else if (clientRequest.actionIs(ClientRequest.MODIFY_EVENT)) { - Main.infoMessage("Server: Client wants to modify an event"); - calendarDatabase.modifyEvent(clientRequest); - // if (Config.getBooleanValue("client_run")) - calendarDatabase.flush(); - - } else if (clientRequest.actionIs(ClientRequest.DELETE_EVENT)) { - Main.infoMessage("Server: Client wants to delete an event"); - calendarDatabase.deleteEvent(clientRequest); - // if (Config.getBooleanValue("client_run")) - calendarDatabase.flush(); - - } else if (clientRequest.actionIs(ClientRequest.ADD_EVENT)) { - Main.infoMessage("Server: Client wants to add an event"); - calendarDatabase.addEvent(clientRequest); - // if (Config.getBooleanValue("client_run")) - calendarDatabase.flush(); - - } else if (clientRequest.actionIs(ClientRequest.RENAME_CATEGORY)) { - Main.infoMessage("Server: Client wants to rename a category"); - calendarDatabase.renameCategory(clientRequest); - // if (Config.getBooleanValue("client_run")) - calendarDatabase.flush(); - - } else if (clientRequest.actionIs(ClientRequest.DELETE_CATEGORY)) { - Main.infoMessage("Server: Client wants to delete a category (NYI)"); - calendarDatabase.deleteCategory(clientRequest); - // if (Config.getBooleanValue("client_run")) - calendarDatabase.flush(); - - } else if (clientRequest.actionIs(ClientRequest.RELOAD_DATABASE)) { - // Read and parse the whole calendar database from file! - parser = new CalendarFormatParser(); - parser.setWorkdir(sWorkdir); - parser.start(); - - calendarDatabase = new CalendarDatabase(parser.getCategories()); - - } else if (clientRequest.actionIs(ClientRequest.FLUSH_DATABASE)) { - Main.infoMessage("Server: Client wants to flush the database"); - calendarDatabase.flush(); - - } else if (clientRequest.actionIs(ClientRequest.SHUTDOWN_SERVER)) { - Main.infoMessage("Server: Client wants the server to shut down"); - calendarDatabase.flush(); - socket.close(); - - if (Config.getBooleanValue("client_run")) { - Main.infoMessage("Server: Shutting down the server thread"); - socket.close(); - serverSocket.close(); - break; - } else { - Main.exit(0); - } - } + private int iPort; + private String sWorkdir; + + /** + * Creates a NetCalendarServer instance and runs it as a thread. + * @param iPort Specifies the server port of the server to be used. + * @param sWorkdir Specifies the working directory of the server. + */ + public NetCalendarServer(int iPort, String sWorkdir) { + this.iPort = iPort; + this.sWorkdir = sWorkdir; + + start(); + } - socket.close(); - } + /** + * This method initializes a new server socket. + * @return Returns the server socket object. + */ + private ServerSocket makeServerSocket() throws IOException { + if (!Config.getBooleanValue("use_ssl")) + return new ServerSocket(iPort); - } catch (ClassNotFoundException e) { - Main.infoMessage("Error: Server error during serialization: " + e.getMessage()); + ServerSocketFactory sslSocketFactory = SSLServerSocketFactory.getDefault(); + return sslSocketFactory.createServerSocket(iPort); + } - } catch (IOException e) { - Main.infoMessage("Error: Server error during serialization: " + e.getMessage()); + /** + * This method specifies the start method of the Thread. Its setting up the server port. + */ + public void run() { + ServerSocket serverSocket = null; + Socket socket = null; + + // Read and parse the whole calendar database from file! + CalendarFormatParser parser = new CalendarFormatParser(); + parser.setWorkdir(sWorkdir); + parser.start(); + + CalendarDatabase calendarDatabase = new CalendarDatabase(parser.getCategories()); + + try { + serverSocket = makeServerSocket(); + + while (true) { + // This will wait for a connection to be made to this socket. + socket = serverSocket.accept(); + // serverSocket.accept(); + socket.setKeepAlive(true); + + // Recieve the client's request object + InputStream inputStream = socket.getInputStream(); + ObjectInput objectInput = new ObjectInputStream(inputStream); + ClientRequest clientRequest = (ClientRequest) objectInput.readObject(); + + if (!clientRequest.checkPassphrase(Config.getStringValue("passphrase", false))) { + Main.infoMessage("Server: Client refused, wrong passphrase!"); + + // Check if a event has been modified! + } else if (clientRequest.requestsNewEvents()) { + Main.infoMessage("Server: Client requests new events"); + + // Get all calendar events which match the request + Vector vecEvents = calendarDatabase.getMatchingEvents(clientRequest); + ServerResponse serverResponse = new ServerResponse(vecEvents); + + OutputStream outputStream = socket.getOutputStream(); + ObjectOutput objectOutput = new ObjectOutputStream(outputStream); + objectOutput.writeObject(serverResponse); + objectOutput.flush(); + objectOutput.close(); + + } else if (clientRequest.actionIs(ClientRequest.MODIFY_EVENT)) { + Main.infoMessage("Server: Client wants to modify an event"); + calendarDatabase.modifyEvent(clientRequest); + // if (Config.getBooleanValue("client_run")) + calendarDatabase.flush(); + + } else if (clientRequest.actionIs(ClientRequest.DELETE_EVENT)) { + Main.infoMessage("Server: Client wants to delete an event"); + calendarDatabase.deleteEvent(clientRequest); + // if (Config.getBooleanValue("client_run")) + calendarDatabase.flush(); + + } else if (clientRequest.actionIs(ClientRequest.ADD_EVENT)) { + Main.infoMessage("Server: Client wants to add an event"); + calendarDatabase.addEvent(clientRequest); + // if (Config.getBooleanValue("client_run")) + calendarDatabase.flush(); + + } else if (clientRequest.actionIs(ClientRequest.RENAME_CATEGORY)) { + Main.infoMessage("Server: Client wants to rename a category"); + calendarDatabase.renameCategory(clientRequest); + // if (Config.getBooleanValue("client_run")) + calendarDatabase.flush(); + + } else if (clientRequest.actionIs(ClientRequest.DELETE_CATEGORY)) { + Main.infoMessage("Server: Client wants to delete a category (NYI)"); + calendarDatabase.deleteCategory(clientRequest); + // if (Config.getBooleanValue("client_run")) + calendarDatabase.flush(); + + } else if (clientRequest.actionIs(ClientRequest.RELOAD_DATABASE)) { + // Read and parse the whole calendar database from file! + parser = new CalendarFormatParser(); + parser.setWorkdir(sWorkdir); + parser.start(); + + calendarDatabase = new CalendarDatabase(parser.getCategories()); + + } else if (clientRequest.actionIs(ClientRequest.FLUSH_DATABASE)) { + Main.infoMessage("Server: Client wants to flush the database"); + calendarDatabase.flush(); + + } else if (clientRequest.actionIs(ClientRequest.SHUTDOWN_SERVER)) { + Main.infoMessage("Server: Client wants the server to shut down"); + calendarDatabase.flush(); + socket.close(); + + if (Config.getBooleanValue("client_run")) { + Main.infoMessage("Server: Shutting down the server thread"); + socket.close(); + serverSocket.close(); + break; + } else { + Main.exit(0); + } + } + + socket.close(); + } + + } catch (ClassNotFoundException e) { + Main.infoMessage("Error: Server error during serialization: " + e.getMessage()); + + } catch (IOException e) { + Main.infoMessage("Error: Server error during serialization: " + e.getMessage()); + } } - } } |
