]> git.mxchange.org Git - jbonuscard-lib.git/blob - Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java
Added a lot trace messages + sanity checks for null references and such things
[jbonuscard-lib.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.text.MessageFormat;
21 import java.util.Iterator;
22 import java.util.List;
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.exceptions.BadTokenException;
28 import org.mxchange.addressbook.manager.contact.ContactManager;
29
30 /**
31  * Stores and retrieves Contact instances
32  *
33  * @author Roland Haeder
34  */
35 public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper {
36
37         /**
38          * Constructor which accepts a contact manager
39          *
40          * @param manager
41          */
42         public ContactDatabaseFrontend (final ContactManager manager) {
43                 // Call own constructor
44                 this();
45
46                 // Trace message
47                 this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
48
49                 // Manager instance must not be null
50                 if (manager == null) {
51                         // Abort here
52                         throw new NullPointerException("manager is null"); //NOI18N
53                 }
54
55                 // Set contact manager
56                 this.setContactManager(manager);
57         }
58
59         /**
60          * Basic constrcutor
61          */
62         protected ContactDatabaseFrontend () {
63                 super();
64
65                 // Trace message
66                 this.getLogger().trace("CALLED!"); //NOI18N
67
68                 // Set "table" name
69                 this.setTableName("contacts"); //NOI18N
70
71                 // Initalize backend
72                 this.initBackend();
73         }
74
75         /**
76          * Shuts down the database layer
77          */
78         @Override
79         public void doShutdown () {
80                 // Trace message
81                 this.getLogger().trace("CALLED!"); //NOI18N
82
83                 // Shutdown backend
84                 this.getBackend().doShutdown();
85
86                 // Trace message
87                 this.getLogger().trace("EXIT!"); //NOI18N
88         }
89
90         /**
91          * Flushes all contact entries to database
92          */
93         @Override
94         public void flushAllContacts () {
95                 // Trace message
96                 this.getLogger().trace("CALLED!"); //NOI18N
97
98                 // Get full list
99                 List<Contact> contacts = this.getContactManager().getList();
100
101                 // Get iterator
102                 Iterator<Contact> iterator = contacts.iterator();
103
104                 // Rewind backend
105                 this.getBackend().rewind();
106
107                 // Get all entries
108                 while (iterator.hasNext()) {
109                         // Get next entry
110                         Contact contact = iterator.next();
111
112                         try {
113                                 // Store this entry
114                                 this.getBackend().store((Storeable) contact);
115                         } catch (final IOException ex) {
116                                 // Should not happen?
117                                 this.getLogger().catching(ex);
118                                 System.exit(1);
119                         }
120                 }
121
122                 // Trace message
123                 this.getLogger().trace("EXIT!"); //NOI18N
124         }
125
126         /**
127          * Reads all contacts from database backend and handles them over to the
128          * contact manager
129          */
130         @Override
131         public void readAllContacts () {
132                 // Trace message
133                 this.getLogger().trace("CALLED!"); //NOI18N
134
135                 // Get iterator and case it
136                 CsvBackend backend = (CsvBackend) this.getBackend();
137
138                 // First rewind to beginning
139                 this.getBackend().rewind();
140
141                 // Get backend iterator
142                 Iterator<Contact> iterator = null;
143                 try {
144                         iterator = backend.contactIterator();
145                 } catch (final BadTokenException ex) {
146                         this.getLogger().catching(ex);
147                         System.exit(1);
148                 }
149
150                 // Read all entries
151                 while (iterator.hasNext()) {
152                         // Get next entry
153                         Contact contact = iterator.next();
154
155                         // Add contact instance to manager
156                         this.getContactManager().addContact(contact);
157                 }
158
159                 // Trace message
160                 this.getLogger().trace("EXIT!"); //NOI18N
161         }
162 }