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