]> git.mxchange.org Git - jbonuscard-lib.git/blob - Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java
Moved exceptions in own package
[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.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      * Constructor which accepts a contact manager
37      * @param manager
38      */
39     public ContactDatabaseFrontend (final ContactManager manager) {
40         // Call own constructor
41         this();
42         
43         // Set contact manager
44         this.setContactManager(manager);
45     }
46
47     /**
48      * Basic constrcutor
49      */
50     protected ContactDatabaseFrontend () {
51         super();
52
53         // Set "table" name
54         this.setTableName("contacts");
55
56         // Initalize backend
57         this.initBackend();
58     }
59
60     /**
61      * Shuts down the database layer
62      */
63     @Override
64     public void doShutdown () {
65         // Shutdown backend
66         this.getBackend().doShutdown();
67     }
68
69     /**
70      * Flushes all contact entries to database
71      */
72     @Override
73     public void flushAllContacts () {
74         // Get full list
75         List<Contact> contacts = this.getContactManager().getList();
76
77         // Get iterator
78         Iterator<Contact> iterator = contacts.iterator();
79
80         // Rewind backend
81         this.getBackend().rewind();
82
83         // Get all entries
84         while (iterator.hasNext()) {
85             // Get next entry
86             Contact contact = iterator.next();
87
88             try {
89                 // Store this entry
90                 this.getBackend().store((Storeable) contact);
91             } catch (final IOException ex) {
92                 // Should not happen?
93                 this.getLogger().catching(ex);
94                 System.exit(1);
95             }
96         }
97     }
98
99     /**
100      * Reads all contacts from database backend and handles them over to the
101      * contact manager
102      */
103     @Override
104     public void readAllContacts () {
105         // Get iterator and case it
106         CsvBackend backend = (CsvBackend) this.getBackend();
107         
108         // First rewind to beginning
109         this.getBackend().rewind();
110
111         // Get backend iterator
112         Iterator<Contact> iterator = null;
113         try {
114             iterator = backend.contactIterator();
115         } catch (final BadTokenException ex) {
116             this.getLogger().catching(ex);
117             System.exit(1);
118         }
119
120         // Read all entries
121         while (iterator.hasNext()) {
122             // Get next entry
123             Contact contact = iterator.next();
124
125             // Add contact instance to manager
126             this.getContactManager().addContact(contact);
127         }
128     }
129 }