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