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;
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;
return m_contacts;
}
return m_events;
}
return m_settings;
}
return ++m_max_event_id;
}
return ++m_max_contact_id;
}
return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
return Calendar.getInstance().get(Calendar.MONTH);
}
return Calendar.getInstance().get(Calendar.YEAR);
}
public static boolean AddSetting(String name, String value,
int type)
throws Exception {
m_settings.Add(new Setting(name, value, type));
writeSettings();
return true;
}
int idx = m_contacts.GetIndexById(id);
m_contacts.Remove(idx);
writeContacts();
return true;
}
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;
}
}
public static boolean RemoveEvent(
int id)
throws Exception {
int idx = m_events.GetEventIndexById(id);
m_events.Remove(idx);
writeEvents();
return true;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
String data = m_settings.ToCSV();
FileWriter fw = new FileWriter("data/settings.csv");
BufferedWriter writer = new BufferedWriter(fw);
writer.write(data);
writer.close();
return true;
}
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;
}
String data = m_contacts.ToCSV();
FileWriter fw = new FileWriter("data/contacts.csv");
BufferedWriter writer = new BufferedWriter(fw);
writer.write(data);
writer.close();
return true;
}
public static boolean Init()
throws Exception {
m_settings = new SettingsCollection();
m_events = new EventsCollection();
m_contacts = new ContactsCollection();
testDir();
initSettings();
initContacts();
initEvents();
return true;
}
File data = new File("data");
if(!data.isDirectory() || !data.exists()) {
data.mkdir();
}
return true;
}
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;
}
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_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;
}
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]))
);
}
}
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;
}
public static int ParseInt(String value) {
return new Integer(value).intValue();
}
return new Double(value).doubleValue();
}
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;
}
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);
}
}