package calendar; import au.com.bytecode.opencsv.CSVReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Calendar; /** * @author Thomas Milner * @date 24/02/2012 * * @brief * Data Class, Stores the collections, Deals with adding, changing and removing * events, contacts, settings. Also deals with the calls to the CSV package. */ public class Data { private static ContactsCollection m_contacts; private static EventsCollection m_events; private static SettingsCollection m_settings; private static int m_max_event_id = 0; private static int m_max_contact_id = 0; /** * Returns the contacts collection * @return ContactsCollection */ public static ContactsCollection GetContacts() { return m_contacts; } /** * Returns the events collection * @return EventsCollection */ public static EventsCollection GetEvents() { return m_events; } /** * Returns the settings collection * @return */ public static SettingsCollection GetSettings() { return m_settings; } /** * Allocates an event ID, by incrementing the current highest id, * and returning it * @return Event ID */ public static int AllocateEventId() { return ++m_max_event_id; } /** * Allocates a contact ID, by incrementing the current highest id, * and returning it * @return Contact ID */ public static int AllocateContactId() { return ++m_max_contact_id; } /** * Gets the current day * @return Day of the month as an Int */ public static int GetCurrentDay() { return Calendar.getInstance().get(Calendar.DAY_OF_MONTH); } /** * Gets the current month * @return Current Month as an Int */ public static int GetCurrentMonth() { return Calendar.getInstance().get(Calendar.MONTH); } /** * Gets the current year * @return The current year as an Int */ public static int GetCurrentYear() { return Calendar.getInstance().get(Calendar.YEAR); } /** * Adds a new setting * @param name The name of the setting * @param value The value of the setting * @param type The type of setting you are trying to make * @throws Exception If its unable to add the setting */ public static boolean AddSetting(String name, String value, int type) throws Exception { m_settings.Add(new Setting(name, value, type)); writeSettings(); return true; } public static boolean RemoveContact(int id) throws Exception { int idx = m_contacts.GetIndexById(id); m_contacts.Remove(idx); writeContacts(); return true; } /** * Adds a new contact * @param name The contacts Name * @param dob The contacts Date of Birth * @param landphone The home phone number * @param mobilephone The mobile phone number * @param workphone The work phone number * @param email The email address * @param workemail The work email address * @throws Exception If an error occurs */ public static Contact AddContact(String name, Calendar dob, String landphone, String mobilephone, String workphone, String email, String workemail, String website) throws Exception { try{ Contact cnt = new Contact(AllocateContactId(), name, dob, landphone, mobilephone, workphone, email, workemail, website); m_contacts.Add(cnt); writeContacts(); return cnt; }catch(Exception e){ System.out.println("HOLY SHIT AN ERROR"); return null; } } public static Contact EditContact(int id, String name, Calendar dob, String landphone, String mobilephone, String workphone, String email, String workemail, String website) { try{ RemoveContact(id); Contact cnt = new Contact(id, name, dob, landphone, mobilephone, workphone, email, workemail, website); m_contacts.Add(cnt); writeContacts(); return cnt; }catch(Exception e){ System.out.println("HOLY SHIT AN ERROR"); return null; } } /** * Remove an event * @param id The events ID * @throws Exception If an error occurs */ public static boolean RemoveEvent(int id) throws Exception { int idx = m_events.GetEventIndexById(id); m_events.Remove(idx); writeEvents(); return true; } /** * Edit an event * @param id the id of the event to edit * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @param repetition How often the even repeats, as a int. * @throws Exception If an error occurs */ public static boolean EditEvent(int id, String title, Calendar start_date, Calendar end_date, String description, int repetition) throws Exception { RemoveEvent(id); AddEventWithID(id,title, start_date, end_date, description, repetition); return true; } /** * Add a Basic Event with ID * @param id The ID of the event to edit * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @param repetition How often the even repeats, as a int. * @throws Exception If an error occurs */ private static boolean AddEventWithID(int id, String title, Calendar start_date, Calendar end_date, String description, int repetition) throws Exception { m_events.Add(new Event(id, title, start_date, end_date, description, repetition)); writeEvents(); return true; } /** * Add a Basic Event * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @param repetition How often the even repeats, as a int. * @throws Exception If an error occurs */ public static boolean AddEvent(String title, Calendar start_date, Calendar end_date, String description, int repetition) throws Exception { m_events.Add(new Event(AllocateEventId(), title, start_date, end_date, description, repetition)); writeEvents(); return true; } /** * Edit a social event * @param id the id of the event to edit * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @param repetition How often the even repeats, as a int. * @throws Exception If an error occurs */ public static boolean EditSocialEvent(int id, String title, Calendar start_date, Calendar end_date, String description, int repetition, int guests) throws Exception { RemoveEvent(id); AddSocialEventWithID(id,title, start_date, end_date, description, repetition, guests); return true; } /** * Add a Social Event with ID * @param id The ID of the event to edit * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @param repetition How often the even repeats, as a int. * @throws Exception If an error occurs */ private static boolean AddSocialEventWithID(int id, String title, Calendar start_date, Calendar end_date, String description, int repetition, int guests) throws Exception { m_events.Add(new SocialEvent(id, title, start_date, end_date, description, repetition, guests)); writeEvents(); return true; } /** * Add a Social Event * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @param repetition How often the even repeats, as a int. * @param guests Who else is attending the event * @throws Exception */ public static boolean AddSocialEvent(String title, Calendar start_date, Calendar end_date, String description, int repetition, int guests) throws Exception { m_events.Add(new SocialEvent(AllocateEventId(), title, start_date, end_date, description, repetition, guests)); writeEvents(); return true; } /** * Edit a Work event * @param id the id of the event to edit * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @param repetition How often the even repeats, as a int. * @throws Exception If an error occurs */ public static boolean EditWorkEvent(int id, String title, Calendar start_date, Calendar end_date, String description, int repetition, int guests) throws Exception { RemoveEvent(id); AddWorkEventWithID(id, title, start_date, end_date, description, repetition, guests); return true; } /** * Add a work Event with ID * @param id The ID of the event to edit * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @param repetition How often the even repeats, as a int. * @throws Exception If an error occurs */ private static boolean AddWorkEventWithID(int id, String title, Calendar start_date, Calendar end_date, String description, int repetition, int guests) throws Exception { m_events.Add(new WorkEvent(id, title, start_date, end_date, description, repetition, guests)); writeEvents(); return true; } /** * Add a Work Event * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @param repetition How often the even repeats, as a int. * @param guests Who else is attending the event * @throws Exception */ public static boolean AddWorkEvent(String title, Calendar start_date, Calendar end_date, String description, int repetition, int guests) throws Exception { try{ m_events.Add(new WorkEvent(AllocateEventId(), title, start_date, end_date, description, repetition, guests)); writeEvents(); }catch(Exception e){ return false; } return true; } /** * Edit a Birthday event * @param id the id of the event to edit * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @throws Exception If an error occurs */ public static boolean EditBirthdayEvent(int id, String title, Calendar start_date, Calendar end_date, String description, int guests) throws Exception { RemoveEvent(id); AddBirthdayEventWithID(id, title, start_date, end_date, description, guests); return true; } /** * Add a Birthday Event with ID * @param id The ID of the event to edit * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @throws Exception If an error occurs */ private static boolean AddBirthdayEventWithID(int id, String title, Calendar start_date, Calendar end_date, String description, int guests) throws Exception { m_events.Add(new BirthdayEvent(id, title, start_date, end_date, description, guests)); writeEvents(); return true; } /** * Add a Birthday Event * @param title The events title * @param start_date The events start date * @param end_date The events end date * @param description The description of the event * @param guests Who else is attending the event * @throws Exception */ public static boolean AddBirthdayEvent(String title, Calendar start_date, Calendar end_date, String description, int guests) throws Exception { m_events.Add(new BirthdayEvent(AllocateEventId(), title, start_date, end_date, description, guests)); writeEvents(); return true; } /** * Write the settings to the CSV file * @throws Exception if file isn't found or error occurs */ private static boolean writeSettings() throws Exception { String data = m_settings.ToCSV(); FileWriter fw = new FileWriter("data/settings.csv"); BufferedWriter writer = new BufferedWriter(fw); writer.write(data); writer.close(); return true; } /** * Write the Events to the CSV file * @throws Exception if file isn't found or error occurs */ private static boolean writeEvents() throws Exception { String data = m_events.ToCSV(); FileWriter fw = new FileWriter("data/events.csv"); BufferedWriter writer = new BufferedWriter(fw); writer.write(data); writer.close(); return true; } /** * Write the Contacts to the CSV file * @throws Exception if file isn't found or error occurs */ private static boolean writeContacts() throws Exception { String data = m_contacts.ToCSV(); FileWriter fw = new FileWriter("data/contacts.csv"); BufferedWriter writer = new BufferedWriter(fw); writer.write(data); writer.close(); return true; } /** * Set up the Data class, Create the Collections * @throws Exception if an error occurs during execution */ public static boolean Init() throws Exception { m_settings = new SettingsCollection(); m_events = new EventsCollection(); m_contacts = new ContactsCollection(); testDir(); initSettings(); initContacts(); initEvents(); /*//This is test code! Uncomment! Calendar cal = Calendar.getInstance(); Calendar cale = Calendar.getInstance(); cale.add(Calendar.HOUR, 2); AddEvent("title1", cal, cale, "desc", Event.REPEATING_WORKING_DAYS); AddEvent("title2", cal, cale, "desc", Event.REPEATING_WEEKENDS); cal.add(Calendar.HOUR, 4); cale.add(Calendar.HOUR,4); AddSocialEvent("socialEvent1", cal, cale, "descr", Event.REPEATING_FOUR_WEEKS, 0); cal.add(Calendar.HOUR, 2); cale.add(Calendar.HOUR, 2); AddWorkEvent("workEvent1", cal, cale, "descr", Event.REPEATING_MONTHLY, 0);*/ return true; } /** * Test the data directory actually exists, if not make it! */ private static boolean testDir() { File data = new File("data"); if(!data.isDirectory() || !data.exists()) { data.mkdir(); } return true; } /** * Initialise the contacts collection, This loads contacts from the CSV * file and saves them into the collection. * @throws Exception in an error occurs, eg while reading the file */ private static boolean initContacts() throws Exception { try { File contacts_file = new File("data/contacts.csv"); if(!contacts_file.exists()) contacts_file.createNewFile(); CSVReader reader = new CSVReader(new FileReader(contacts_file)); String [] nextLine; int line = 0; while ((nextLine = reader.readNext()) != null) { line++; if(nextLine.length != Contact.NO_FIELDS) { throw new Exception("Wrong format for csv file " + "contacts.csv at line "+line); } else { m_contacts.Add(new Contact( Data.ParseInt(nextLine[Contact.IDX_ID]), nextLine[Contact.IDX_NAME], Data.ParseDate(nextLine[Contact.IDX_DOB]), nextLine[Contact.IDX_LANDPHONE], nextLine[Contact.IDX_MOBILEPHONE], nextLine[Contact.IDX_WORKPHONE], nextLine[Contact.IDX_EMAIL], nextLine[Contact.IDX_WORKEMAIL], nextLine[Contact.IDX_WEBSITE]) ); int id = Data.ParseInt(nextLine[Contact.IDX_ID]); if(id > m_max_contact_id) m_max_contact_id = id; } } } catch(IOException e) { throw new Exception("Something wrong with file reading... Please " +"contact an administrator"); } return true; } /** * Initialise the Events collection, import events form csv and save them * as events in the collection. * @throws Exception */ private static boolean initEvents() throws Exception { try { File events_file = new File("data/events.csv"); if(!events_file.exists()) events_file.createNewFile(); CSVReader reader = new CSVReader(new FileReader(events_file)); String [] nextLine; int line = 0; while ((nextLine = reader.readNext()) != null) { line++; int no_fields; Event e; int id; switch(Data.ParseInt(nextLine[0])) { case Event.GENERAL_EVENT:{ no_fields = Event.NO_FIELDS; e = new Event( Data.ParseInt(nextLine[Event.IDX_ID]), nextLine[Event.IDX_TITLE], Data.ParseDate(nextLine[Event.IDX_START_DATE]), Data.ParseDate(nextLine[Event.IDX_END_DATE]), nextLine[Event.IDX_DESCRIPTION], Data.ParseInt(nextLine[Event.IDX_REPETITION]) ); id = Data.ParseInt(nextLine[Event.IDX_ID]); break; } case Event.BIRTHDAY_EVENT:{ no_fields = BirthdayEvent.NO_FIELDS; e = new BirthdayEvent( Data.ParseInt(nextLine[BirthdayEvent.IDX_ID]), nextLine[BirthdayEvent.IDX_TITLE], Data.ParseDate(nextLine[BirthdayEvent.IDX_START_DATE]), Data.ParseDate(nextLine[BirthdayEvent.IDX_END_DATE]), nextLine[BirthdayEvent.IDX_DESCRIPTION], //Data.ParseInt(nextLine[BirthdayEvent. //IDX_REPETITION]), Data.ParseInt(nextLine[BirthdayEvent.IDX_GUESTS]) ); id = Data.ParseInt(nextLine[BirthdayEvent.IDX_ID]); break; } case Event.WORK_EVENT:{ no_fields = WorkEvent.NO_FIELDS; e = new WorkEvent( Data.ParseInt(nextLine[WorkEvent.IDX_ID]), nextLine[WorkEvent.IDX_TITLE], Data.ParseDate(nextLine[WorkEvent.IDX_START_DATE]), Data.ParseDate(nextLine[WorkEvent.IDX_END_DATE]), nextLine[WorkEvent.IDX_DESCRIPTION], Data.ParseInt(nextLine[WorkEvent.IDX_REPETITION]), Data.ParseInt(nextLine[WorkEvent.IDX_GUESTS]) ); id = Data.ParseInt(nextLine[WorkEvent.IDX_ID]); break; } case Event.SOCIAL_EVENT:{ no_fields = SocialEvent.NO_FIELDS; e = new SocialEvent( Data.ParseInt(nextLine[SocialEvent.IDX_ID]), nextLine[SocialEvent.IDX_TITLE], Data.ParseDate(nextLine[SocialEvent.IDX_START_DATE] ), Data.ParseDate(nextLine[SocialEvent.IDX_END_DATE]), nextLine[SocialEvent.IDX_DESCRIPTION], Data.ParseInt(nextLine[SocialEvent.IDX_REPETITION] ), Data.ParseInt(nextLine[SocialEvent.IDX_GUESTS]) ); id = Data.ParseInt(nextLine[SocialEvent.IDX_ID]); break; } default : { throw new Exception("Unknown event type in events.csv" + " at line "+line); } } if(nextLine.length != no_fields) { throw new Exception("Wrong format for csv file events.csv" +" at line "+line); } else { m_events.Add(e); if(id > m_max_event_id) m_max_event_id = id; } } } catch(IOException e) { throw new Exception("Something wrong with file reading... Please" + " contact an administrator"); } return true; } /** * Initialise the Settings collection, parse all settings into the * collection * @throws Exception */ private static boolean initSettings() throws Exception { try { File settings_file = new File("data/settings.csv"); if(!settings_file.exists()) settings_file.createNewFile(); CSVReader reader = new CSVReader(new FileReader(settings_file)); String [] nextLine; int line = 0; while ((nextLine = reader.readNext()) != null) { line++; if(nextLine.length != Setting.NO_FIELDS) { throw new Exception("Wrong format for csv file settings." + "csv at line "+line); } else { m_settings.Add(new Setting( nextLine[Setting.IDX_NAME], nextLine[Setting.IDX_VALUE], Data.ParseInt(nextLine[Setting.IDX_TYPE])) ); } } //adding must-have pre-defined settings if(!m_settings.Exists("locale")) AddSetting("locale", "EN", Setting.TYPE_STRING); } catch(IOException e) { throw new Exception("Something wrong with file reading... Please" + " contact an administrator: "+e.getMessage()); } return true; } /** * Parse an int from a string * @param value The string of the int * @return The int */ public static int ParseInt(String value) { return new Integer(value).intValue(); } /** * Parse a double from a string * @param value The string of the double * @return the double */ public static double ParseDouble(String value) { return new Double(value).doubleValue(); } /** * Parse the data a data in the format dd/mm/yyyy hh:mm:ss into a correct * calendar object * @param value * @return * @throws Exception */ public static Calendar ParseDate(String value) throws Exception { value = value.trim(); int space = value.indexOf(" "); if(space == -1) throw new Exception("Date '"+value+"' is not in the format" + " dd/mm/yyyy hh:mm:ss"); String date = value.substring(0, space); String time = value.substring(space+1); int sep1 = date.indexOf("/"); if(sep1 == -1) throw new Exception("Date '"+value+"' is not in the format " + "dd/mm/yyyy hh:mm:ss"); int sep2 = date.indexOf("/", sep1+1); if(sep2 == -1) throw new Exception("Date '"+value+"' is not in the format " + "dd/mm/yyyy hh:mm:ss"); int sep3 = time.indexOf(":"); if(sep3 == -1) throw new Exception("Date '"+value+"' is not in the format " + "dd/mm/yyyy hh:mm:ss"); int sep4 = time.indexOf(":", sep3+1); if(sep4 == -1) throw new Exception("Date '"+value+"' is not in the format " + "dd/mm/yyyy hh:mm:ss"); String day = date.substring(0, sep1); String month = date.substring(sep1+1, sep2); String year = date.substring(sep2+1); String hour = time.substring(0, sep3); String minute = time.substring(sep3+1, sep4); String second = time.substring(sep4+1); Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, Data.ParseInt(day)); cal.set(Calendar.MONTH, Data.ParseInt(month)-1); cal.set(Calendar.YEAR, Data.ParseInt(year)); cal.set(Calendar.HOUR_OF_DAY, Data.ParseInt(hour)); cal.set(Calendar.MINUTE, Data.ParseInt(minute)); cal.set(Calendar.SECOND, Data.ParseInt(second)); return cal; } /** * Convert a calendar into the format dd/mm/yyyy hh:mm:ss * @param date A calendar object to convert * @return the correctly formated string */ public static String FromDate(Calendar date) { String day = new Integer(date.get(Calendar.DAY_OF_MONTH)).toString(); String month = new Integer(date.get(Calendar.MONTH)+1).toString(); String year = new Integer(date.get(Calendar.YEAR)).toString(); String hour = new Integer(date.get(Calendar.HOUR_OF_DAY)).toString(); String minute = new Integer(date.get(Calendar.MINUTE)).toString(); String second = new Integer(date.get(Calendar.SECOND)).toString(); return day+'/'+month+'/'+year+' '+hour+':'+minute+':'+second; } public static void main(String args[]) throws Exception { Init(); Calendar temp = ParseDate("04/12/1991 10:02:33"); String date = FromDate(Calendar.getInstance()); System.out.println("Month" + temp.get(Calendar.MONTH)); System.out.println("Year" + temp.get(Calendar.YEAR)); System.out.println("Day" + temp.get(Calendar.DAY_OF_MONTH)); System.out.println("Hour" + temp.get(Calendar.HOUR)); System.out.println("Min" + temp.get(Calendar.MINUTE)); System.out.println("Sec" + temp.get(Calendar.SECOND)); System.out.println("Current Date is " + date); String testDate = "04/12/1991 10:02:33"; temp = ParseDate(testDate); date = FromDate(temp); System.out.println(testDate + " =? " + date); } }