@@ -0,0 +1,2607 @@
+/*%0A * Copyright 2014 Rico Antonio Felix%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage com.rico.felix.data;%0A%0A/*%0A * Platform Dependencies%0A */%0Aimport java.io.BufferedInputStream;%0Aimport java.io.BufferedOutputStream;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.ObjectInputStream;%0Aimport java.io.ObjectOutputStream;%0Aimport java.util.TreeMap;%0A%0A/*%0A * Local Dependency%0A */%0Aimport com.rico.felix.models.Person;%0A%0A/**%0A * This class is used to persist and restore runtime data.%0A */%0Apublic class Database%0A%7B%0A%09/*%0A%09 * Prevent instantiation%0A%09 */%0A%09private Database()%0A%09%7B%7D%0A%0A%09/**%0A%09 * This method is used to persist runtime data in a file on disk%0A%09 */%0A%09public static void save()%0A%09%7B%0A%09%09try (BufferedOutputStream bos = new BufferedOutputStream(%0A%09%09%09%09new FileOutputStream(%22directory.dat%22));%0A%09%09%09 ObjectOutputStream oos = new ObjectOutputStream(bos);%0A%09%09%09)%0A%09%09%7B%0A%09%09%09oos.writeObject(Directory.getFilingSystem());%0A%09%09%7D%0A%09%09catch (IOException ioe)%0A%09%09%7B%0A%09%09%09ioErrorMessage();%0A%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09 * This method is used to restore persisted data if any%0A%09 *%0A%09 * @return Whether restoration succeeded or not%0A%09 */%0A%09@SuppressWarnings(%22unchecked%22)%0A%09public static boolean restore()%0A%09%7B%0A%09%09try (BufferedInputStream bis = new BufferedInputStream(%0A%09%09%09%09new FileInputStream(%22directory.dat%22));%0A%09%09%09 ObjectInputStream ois = new ObjectInputStream(bis);%0A%09%09%09)%0A%09%09%7B%0A%09%09%09return Directory.setFilingSystem((TreeMap%3CString, Person%3E)ois.readObject());%0A%09%09%7D%0A%09%09catch (IOException ioe)%0A%09%09%7B%0A%09%09%09if (Directory.getFilingSystem().isEmpty())%0A%09%09%09%09; // Application has no records in the database which is okay%0A%09%09%09else%0A%09%09%09%09ioErrorMessage();%0A%09%09%7D%0A%09%09catch (ClassNotFoundException cnfe)%0A%09%09%7B%0A%09%09%09System.err.println(%22Internal Error: %22);%0A%09%09%09cnfe.printStackTrace();%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%0A%09/**%0A%09 * This method is used to stream an error message upon i/o error%0A%09 * and terminate the application%0A%09 */%0A%09private static void ioErrorMessage()%0A%09%7B%0A%09%09System.err.println(%22I/O Error Occurred Unexpectedly%22);%0A%09%09System.err.println(%22Application will now terminate%22);%0A%09%09System.exit(-0x1);%0A%09%7D%0A%0A%7D%0A

Create Database.java