]> git.mxchange.org Git - addressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java
85dcf153689a00b5c78318bca34fe6504e1793c8
[addressbook-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.frontend.BaseDatabaseFrontend;
25 import org.mxchange.addressbook.database.storage.Storeable;
26 import org.mxchange.addressbook.exceptions.BadTokenException;
27 import org.mxchange.addressbook.exceptions.UnsupportedDatabaseBackendException;
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                 try {
72                         // Initalize backend
73                         this.initBackend();
74                 } catch (final UnsupportedDatabaseBackendException ex) {
75                         // Abort program
76                         this.abortProgramWithException(ex);
77                 }
78         }
79
80         /**
81          * Shuts down the database layer
82          */
83         @Override
84         public void doShutdown () {
85                 // Trace message
86                 this.getLogger().trace("CALLED!"); //NOI18N
87
88                 // Shutdown backend
89                 this.getBackend().doShutdown();
90
91                 // Trace message
92                 this.getLogger().trace("EXIT!"); //NOI18N
93         }
94
95         /**
96          * Flushes all contact entries to database
97          */
98         @Override
99         public void flushAllContacts () {
100                 // Trace message
101                 this.getLogger().trace("CALLED!"); //NOI18N
102
103                 // Get full list
104                 List<Contact> contacts = this.getContactManager().getList();
105
106                 // Get iterator
107                 Iterator<Contact> iterator = contacts.iterator();
108
109                 // Rewind backend
110                 this.getBackend().rewind();
111
112                 // Get all entries
113                 while (iterator.hasNext()) {
114                         // Get next entry
115                         Contact contact = iterator.next();
116
117                         try {
118                                 // Store this entry
119                                 this.getBackend().store((Storeable) contact);
120                         } catch (final IOException ex) {
121                                 // Should not happen?
122                                 this.abortProgramWithException(ex);
123                         }
124                 }
125
126                 // Trace message
127                 this.getLogger().trace("EXIT!"); //NOI18N
128         }
129
130         /**
131          * Reads all contacts from database backend and handles them over to the
132          * contact manager
133          */
134         @Override
135         public void readAllContacts () {
136                 // Trace message
137                 this.getLogger().trace("CALLED!"); //NOI18N
138
139                 // First rewind to beginning
140                 this.getBackend().rewind();
141
142                 // Get backend iterator
143                 Iterator<Contact> iterator = null;
144                 try {
145                         iterator = this.getBackend().contactIterator();
146                 } catch (final BadTokenException ex) {
147                         // Abort program
148                         this.abortProgramWithException(ex);
149                 }
150
151                 // Read all entries
152                 while (iterator.hasNext()) {
153                         // Get next entry
154                         Contact contact = iterator.next();
155
156                         // Add contact instance to manager
157                         this.getContactManager().addContact(contact);
158                 }
159
160                 // Trace message
161                 this.getLogger().trace("EXIT!"); //NOI18N
162         }
163 }