]> git.mxchange.org Git - jbonuscard-lib.git/blob - Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java
Rewrite start of storing contacts in database as the previous was just for demo ...
[jbonuscard-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.contact.Contact;\r
23 import org.mxchange.addressbook.database.backend.csv.CsvBackend;\r
24 import org.mxchange.addressbook.database.frontend.BaseDatabaseFrontend;\r
25 import org.mxchange.addressbook.database.storage.Storeable;\r
26 import org.mxchange.addressbook.manager.contact.ManageableContact;\r
27 \r
28 /**\r
29  * Stores and retrieves Contact instances\r
30  *\r
31  * @author Roland Haeder\r
32  */\r
33 public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper {\r
34     /**\r
35      * Basic constrcutor\r
36      */\r
37     public ContactDatabaseFrontend () {\r
38         super();\r
39 \r
40         // Set "table" name\r
41         this.setTableName("contacts");\r
42 \r
43         // Initalize backend\r
44         this.initBackend();\r
45     }\r
46 \r
47     /**\r
48      * Flushes all contact entries to database\r
49      * @param contactManager An instance of a MangeableContact class\r
50      */\r
51     @Override\r
52     public void flushAllContacts (final ManageableContact contactManager) {\r
53         // Get full list\r
54         List<Contact> contacts = contactManager.getList();\r
55 \r
56         // Get iterator\r
57         Iterator<Contact> iterator = contacts.iterator();\r
58 \r
59         // Rewind backend\r
60         this.getBackend().rewind();\r
61 \r
62         // Get all entries\r
63         while (iterator.hasNext()) {\r
64             // Get next entry\r
65             Contact contact = iterator.next();\r
66 \r
67             try {\r
68                 // Store this entry\r
69                 this.getBackend().store((Storeable) contact);\r
70             } catch (final IOException ex) {\r
71                 // Should not happen?\r
72                 this.getLogger().catching(ex);\r
73                 System.exit(1);\r
74             }\r
75         }\r
76     }\r
77 \r
78     /**\r
79      * Reads all contacts from database backend and handles them over to the\r
80      * contact manager\r
81      * \r
82      * @param contactManager Contact manager to handle loaded contacts\r
83      */\r
84     @Override\r
85     public void readAllContacts (final ManageableContact contactManager) {\r
86         // Get iterator and case it\r
87         CsvBackend backend = (CsvBackend) this.getBackend();\r
88         \r
89         // First rewind to beginning\r
90         this.getBackend().rewind();\r
91 \r
92         // Get backend iterator\r
93         Iterator<Contact> iterator = .contactIterator();\r
94 \r
95         // Read all entries\r
96         while (iterator.hasNext()) {\r
97             // Get next entry\r
98             Contact contact = iterator.next();\r
99         }\r
100     }\r
101 }\r