]> git.mxchange.org Git - jaddressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java
Bah, bad bad NetBeans. Why the f**k does it need to have a sub directory??? Why not...
[jaddressbook-lib.git] / Addressbook / src / org / mxchange / addressbook / database / frontend / contact / ContactDatabaseFrontend.java
1 /*\r
2  * Copyright (C) 2015 Roland Haeder\r
3  *\r
4  * This program is free software: you can redistribute it and/or modify\r
5  * it under the terms of the GNU General Public License as published by\r
6  * the Free Software Foundation, either version 3 of the License, or\r
7  * (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
16  */\r
17 package org.mxchange.addressbook.database.frontend.contact;\r
18 \r
19 import java.io.IOException;\r
20 import java.util.Iterator;\r
21 import java.util.List;\r
22 import org.mxchange.addressbook.BadTokenException;\r
23 import org.mxchange.addressbook.contact.Contact;\r
24 import org.mxchange.addressbook.database.backend.csv.CsvBackend;\r
25 import org.mxchange.addressbook.database.frontend.BaseDatabaseFrontend;\r
26 import org.mxchange.addressbook.database.storage.Storeable;\r
27 import org.mxchange.addressbook.manager.contact.ContactManager;\r
28 \r
29 /**\r
30  * Stores and retrieves Contact instances\r
31  *\r
32  * @author Roland Haeder\r
33  */\r
34 public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper {\r
35     /**\r
36      * Constructor which accepts a contact manager\r
37      * @param manager\r
38      */\r
39     public ContactDatabaseFrontend (final ContactManager manager) {\r
40         // Call own constructor\r
41         this();\r
42         \r
43         // Set contact manager\r
44         this.setContactManager(manager);\r
45     }\r
46 \r
47     /**\r
48      * Basic constrcutor\r
49      */\r
50     protected ContactDatabaseFrontend () {\r
51         super();\r
52 \r
53         // Set "table" name\r
54         this.setTableName("contacts");\r
55 \r
56         // Initalize backend\r
57         this.initBackend();\r
58     }\r
59 \r
60     /**\r
61      * Shuts down the database layer\r
62      */\r
63     @Override\r
64     public void doShutdown () {\r
65         // Shutdown backend\r
66         this.getBackend().doShutdown();\r
67     }\r
68 \r
69     /**\r
70      * Flushes all contact entries to database\r
71      */\r
72     @Override\r
73     public void flushAllContacts () {\r
74         // Get full list\r
75         List<Contact> contacts = this.getContactManager().getList();\r
76 \r
77         // Get iterator\r
78         Iterator<Contact> iterator = contacts.iterator();\r
79 \r
80         // Rewind backend\r
81         this.getBackend().rewind();\r
82 \r
83         // Get all entries\r
84         while (iterator.hasNext()) {\r
85             // Get next entry\r
86             Contact contact = iterator.next();\r
87 \r
88             try {\r
89                 // Store this entry\r
90                 this.getBackend().store((Storeable) contact);\r
91             } catch (final IOException ex) {\r
92                 // Should not happen?\r
93                 this.getLogger().catching(ex);\r
94                 System.exit(1);\r
95             }\r
96         }\r
97     }\r
98 \r
99     /**\r
100      * Reads all contacts from database backend and handles them over to the\r
101      * contact manager\r
102      */\r
103     @Override\r
104     public void readAllContacts () {\r
105         // Get iterator and case it\r
106         CsvBackend backend = (CsvBackend) this.getBackend();\r
107         \r
108         // First rewind to beginning\r
109         this.getBackend().rewind();\r
110 \r
111         // Get backend iterator\r
112         Iterator<Contact> iterator = null;\r
113         try {\r
114             iterator = backend.contactIterator();\r
115         } catch (final BadTokenException ex) {\r
116             this.getLogger().catching(ex);\r
117             System.exit(1);\r
118         }\r
119 \r
120         // Read all entries\r
121         while (iterator.hasNext()) {\r
122             // Get next entry\r
123             Contact contact = iterator.next();\r
124 \r
125             // Add contact instance to manager\r
126             this.getContactManager().addContact(contact);\r
127         }\r
128     }\r
129 }\r