From: Roland Haeder Date: Thu, 16 Jul 2015 13:24:26 +0000 (+0200) Subject: Added a lot database stuff, which needs heavy rewrite X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b868f84e773bc3ab97540e8f1979d8ed559955bb;p=jaddressbook-lib.git Added a lot database stuff, which needs heavy rewrite Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java index 093797a0..a9d6dff1 100644 --- a/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java +++ b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java @@ -49,6 +49,11 @@ public class BaseFrameworkSystem { */ private ManageableContact contactManager; + /** + * Name of used database table, handled over to backend + */ + private String tableName; + /** * Initialize object */ @@ -64,36 +69,22 @@ public class BaseFrameworkSystem { /** * Application instance + * * @return the application */ public Application getApplication () { return this.application; } - /** - * Application instance - * @param application the application to set - */ - protected void setApplication (final Application application) { - this.application = application; - } - /** * Client instance + * * @return the client */ public Client getClient () { return this.client; } - /** - * Client instance - * @param client the client to set - */ - protected void setClient (final Client client) { - this.client = client; - } - /** * Contact manager instance * @return the contactManager @@ -106,10 +97,27 @@ public class BaseFrameworkSystem { * Contact manager instance * @param contactManager the contactManager to set */ - protected void setContactManager(final ManageableContact contactManager) { + protected void setContactManager (final ManageableContact contactManager) { this.contactManager = contactManager; } + /** + * Client instance + * @param client the client to set + */ + protected void setClient (final Client client) { + this.client = client; + } + + /** + * Application instance + * + * @param application the application to set + */ + protected void setApplication(final Application application) { + this.application = application; + } + /** * Getter for logger * @@ -119,6 +127,24 @@ public class BaseFrameworkSystem { return this.LOG; } + /** + * Name of used database table, handled over to backend + * + * @return the tableName + */ + protected String getTableName () { + return this.tableName; + } + + /** + * Name of used database table, handled over to backend + * + * @param tableName the tableName to set + */ + protected void setTableName (final String tableName) { + this.tableName = tableName; + } + /** * Initializes contact manager * @param client Client instance @@ -126,14 +152,14 @@ public class BaseFrameworkSystem { protected void initContactManager (final Client client) { // Debug message this.getLogger().debug("Initializing contact manager ..."); - + // Init contact manager with console client // @TODO Static initial amount of contacts ManageableContact manager = new ContactManager (100, client); - + // Set it here this.setContactManager(manager); - + // Debug message this.getLogger().debug("Contact manager has been initialized."); } diff --git a/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java index da93b3dd..ef465f72 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java @@ -16,6 +16,7 @@ */ package org.mxchange.addressbook.contact; +import java.util.Objects; import org.mxchange.addressbook.BaseFrameworkSystem; import org.mxchange.addressbook.client.Client; @@ -128,18 +129,26 @@ public class BaseContact extends BaseFrameworkSystem { * * @param object Other possible contact class * @return Whether both contacts are same + * @todo Needs a lot improvements */ @Override public boolean equals (Object object) { + // Is it same type? + if (!(object instanceof BaseContact)) { + // Not equal types + return false; + } else if (!(object instanceof Contact)) { + // Not correct interface + return false; + } + // Try to cast - BaseContact c = (BaseContact) object; + Contact contact = (Contact) object; - /* - * Now test some data @todo Definedly needs improvement - */ - return ((this.getGender() == c.getGender()) - && (this.getSurname().toLowerCase().equals(c.getSurname().toLowerCase())) - && (this.getFamilyName().toLowerCase().equals(c.getFamilyName().toLowerCase()))); + // Now test some data @todo Definedly needs improvement + return ((this.getGender() == contact.getGender()) + && (this.getSurname().toLowerCase().equals(contact.getSurname().toLowerCase())) + && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase()))); } /** @@ -443,6 +452,15 @@ public class BaseContact extends BaseFrameworkSystem { this.zipCode = zipCode; } + @Override + public int hashCode () { + int hash = 7; + hash = 79 * hash + Objects.hashCode(this.getFamilyName()); + hash = 79 * hash + this.getGender(); + hash = 79 * hash + Objects.hashCode(this.getSurname()); + return hash; + } + /** * Checks whether the contact is user's own data * @@ -498,4 +516,51 @@ public class BaseContact extends BaseFrameworkSystem { this.setFamilyName(familyName); this.setCompanyName(companyName); } + + /** + * Updates other data in this Contact instance + * + * @param phoneNumber Phone number + * @param cellphoneNumber Cellphone number + * @param faxNumber Fax number + * @param emailAddress Email address + * @param comment Comments + */ + public void updateOtherData (final String phoneNumber, final String cellphoneNumber, final String faxNumber, final String emailAddress, final String comment) { + // Set all + this.setPhoneNumber(phoneNumber); + this.setCellphoneNumber(cellphoneNumber); + this.setFaxNumber(faxNumber); + this.setEmailAddress(emailAddress); + this.setComment(comment); + } + + /** + * "Serializes" this object into a CSV string (this time with semicolons) + * + * @return "CSV-serialized" version of the stored data + */ + public String getCsvStringFromStoreableObject () { + // Get all together + String csvString = String.format( + "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"\n", + this.getGender(), + this.getSurname(), + this.getFamilyName(), + this.getCompanyName(), + this.getStreet(), + this.getZipCode(), + this.getCity(), + this.getCountryCode(), + this.getPhoneNumber(), + this.getFaxNumber(), + this.getCellphoneNumber(), + this.getEmailAddress(), + this.getBirthday(), + this.getComment() + ); + + // Then return it + return csvString; + } } diff --git a/Addressbook/src/org/mxchange/addressbook/contact/Contact.java b/Addressbook/src/org/mxchange/addressbook/contact/Contact.java index 21044cfa..fb75fdb2 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/Contact.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/Contact.java @@ -175,4 +175,15 @@ public interface Contact extends FrameworkInterface { * @param companyName Company name */ public void updateNameData (final char gender, final String surname, final String familyName, final String companyName); + + /** + * Updates other data in this Contact instance + * + * @param phoneNumber Phone number + * @param cellNumber Cellphone number + * @param faxNumber Fax number + * @param email Email address + * @param comment Comments + */ + public void updateOtherData (final String phoneNumber, final String cellNumber, final String faxNumber, final String email, final String comment); } diff --git a/Addressbook/src/org/mxchange/addressbook/contact/book/BookContact.java b/Addressbook/src/org/mxchange/addressbook/contact/book/BookContact.java index ae49d5c2..c7a26572 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/book/BookContact.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/book/BookContact.java @@ -18,6 +18,7 @@ package org.mxchange.addressbook.contact.book; import org.mxchange.addressbook.contact.BaseContact; import org.mxchange.addressbook.contact.Contact; +import org.mxchange.addressbook.database.storage.csv.StoreableCsv; /** * A contact that can be placed into "contact books" @@ -26,6 +27,6 @@ import org.mxchange.addressbook.contact.Contact; * @version 0.0 * @since 0.0 */ -public class BookContact extends BaseContact implements Contact { +public class BookContact extends BaseContact implements Contact, StoreableCsv { } diff --git a/Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java b/Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java index 4cf38b14..81a5d303 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java @@ -18,13 +18,14 @@ package org.mxchange.addressbook.contact.user; import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.contact.book.BookContact; +import org.mxchange.addressbook.database.storage.csv.StoreableCsv; /** * * @author Roland Haeder * @todo After a Collection has been used in ContactManager, change to BaseContact */ -public class UserContact extends BookContact implements Contact { +public class UserContact extends BookContact implements Contact, StoreableCsv { /** * Creates own contact entry diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java new file mode 100644 index 00000000..a9d71f9f --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.database.backend; + +import java.io.IOException; +import org.mxchange.addressbook.FrameworkInterface; +import org.mxchange.addressbook.database.storage.Storeable; + +/** + * A generic interface for database frontends + * + * @author Roland Haeder + */ +public interface DatabaseBackend extends FrameworkInterface { + /** + * Stores an object in the database. + * + * @param object Object to store in database + * @throws java.io.IOException From inner class + */ + public void store (final Storeable object) throws IOException; +} diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/BaseDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/BaseDatabaseBackend.java new file mode 100644 index 00000000..59fe8701 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/BaseDatabaseBackend.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.database.backend.csv; + +import org.mxchange.addressbook.BaseFrameworkSystem; + +/** + * + * @author Roland Haeder + */ +class BaseDatabaseBackend extends BaseFrameworkSystem { + /** + * No instances from this class + */ + protected BaseDatabaseBackend () { + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java new file mode 100644 index 00000000..c2caeb3e --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.database.backend.csv; + +import java.io.DataOutput; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.text.MessageFormat; +import org.mxchange.addressbook.database.storage.Storeable; +import org.mxchange.addressbook.database.backend.DatabaseBackend; +import org.mxchange.addressbook.database.storage.csv.StoreableCsv; + +/** + * A database backend with CSV file as storage implementation + * + * @author Roland Haeder + */ +public class CsvDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend { + /** + * Output stream for this storage engine + */ + private RandomAccessFile storageFile; + + /** + * Constructor with table name + * + * @param tableName Name of "table" + */ + public CsvDatabaseBackend (final String tableName) { + // Debug message + this.getLogger().debug(MessageFormat.format("Trying to initialize table {0} ...", tableName)); + + // Set table name here, too + this.setTableName(tableName); + + // Construct file name + String fileName = String.format("data/table_%s.csv", tableName); + + // Debug message + this.getLogger().debug(MessageFormat.format("Trying to open file {0} ...", fileName)); + + try { + // Try to initialize the storage (file instance) + this.storageFile = new RandomAccessFile(fileName, "rw"); + } catch (final FileNotFoundException ex) { + // Did not work + this.getLogger().error(MessageFormat.format("File {0} cannot be opened: {1}", fileName, ex.toString())); + System.exit(1); + } + + // Output message + this.getLogger().debug(MessageFormat.format("Database for {0} has been initialized.", tableName)); + } + + /** + * Stores given object by "visiting" it + * + * @param object An object implementing Storeable + * @throws java.io.IOException From "inner" class + */ + @Override + public void store (final Storeable object) throws IOException{ + // Make sure the instance is there (DataOutput flawor) + assert(this.storageFile instanceof DataOutput); + + // Try to cast it, this will fail if the interface is not implemented + StoreableCsv csv = (StoreableCsv) object; + + // Now get the object that needs to be stored + String str = csv.getCsvStringFromStoreableObject(); + + // Debug message + this.getLogger().debug(MessageFormat.format("str({0})={1}", str.length(), str)); + + // The string is now a valid CSV string + this.storageFile.writeUTF(str); + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java new file mode 100644 index 00000000..de208ba5 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.database.frontend; + +import org.mxchange.addressbook.BaseFrameworkSystem; +import org.mxchange.addressbook.database.backend.DatabaseBackend; +import org.mxchange.addressbook.database.backend.csv.CsvDatabaseBackend; + +/** + * General database frontend class + * + * @author Roland Haeder + */ +public class BaseDatabaseFrontend extends BaseFrameworkSystem { + + /** + * Instance for database backend + */ + private DatabaseBackend backend; + + /** + * No instances from this class + */ + protected BaseDatabaseFrontend () { + } + + /** + * Instance for database backend + * + * @return the backend + */ + protected DatabaseBackend getBackend () { + return this.backend; + } + + /** + * Instance for database backend + * + * @param backend the backend to set + */ + protected void setBackend (final DatabaseBackend backend) { + this.backend = backend; + } + + /** + * Initialize backend + */ + protected void initBackend () { + // Instance backend + this.backend = new CsvDatabaseBackend(this.getTableName()); + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/DatabaseWrapper.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/DatabaseWrapper.java new file mode 100644 index 00000000..0bf6cc32 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/DatabaseWrapper.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.database.frontend; + +import org.mxchange.addressbook.FrameworkInterface; + +/** + * A generic interface for database frontends + * + * @author Roland Haeder + */ +public interface DatabaseWrapper extends FrameworkInterface { +} diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java new file mode 100644 index 00000000..a67be31b --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.database.frontend.contact; + +import java.io.IOException; +import org.mxchange.addressbook.contact.Contact; +import org.mxchange.addressbook.database.storage.Storeable; +import org.mxchange.addressbook.database.frontend.BaseDatabaseFrontend; + +/** + * Stores and retrieves Contact instances + * + * @author Roland Haeder + */ +public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper { + /** + * Basic constrcutor + */ + public ContactDatabaseFrontend () { + super(); + + // Set "table" name + this.setTableName("contacts"); + + // Initalize backend + this.initBackend(); + } + + @Override + public void addContact (final Contact contact) { + try { + // Try to cast the object and handle it over to the backend + this.getBackend().store((Storeable) contact); + } catch (final IOException ex) { + this.getLogger().error("Cannot write contact to storage: " + ex.getMessage()); + System.exit(1); + } + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java new file mode 100644 index 00000000..e5d5d8fc --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2015 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.database.frontend.contact; + +import org.mxchange.addressbook.contact.Contact; +import org.mxchange.addressbook.database.frontend.DatabaseWrapper; + +/** + * + * @author Roland Häder + */ +public interface ContactWrapper extends DatabaseWrapper { + /** + * Adds a Contact instance to database + * + * @param contact Contact instance to add(store) to database + */ + public void addContact (final Contact contact); +} diff --git a/Addressbook/src/org/mxchange/addressbook/database/storage/Storeable.java b/Addressbook/src/org/mxchange/addressbook/database/storage/Storeable.java new file mode 100644 index 00000000..cd680489 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/database/storage/Storeable.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.database.storage; + +import org.mxchange.addressbook.FrameworkInterface; + +/** + *An interface for objects being stored in databases + * + * @author Roland Haeder + */ +public interface Storeable extends FrameworkInterface { +} diff --git a/Addressbook/src/org/mxchange/addressbook/database/storage/csv/StoreableCsv.java b/Addressbook/src/org/mxchange/addressbook/database/storage/csv/StoreableCsv.java new file mode 100644 index 00000000..6a5915e1 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/database/storage/csv/StoreableCsv.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2015 KLC + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.database.storage.csv; + +import org.mxchange.addressbook.database.storage.Storeable; + +/** + * + * @author KLC + */ +public interface StoreableCsv extends Storeable { + /** + * Getter for a CSV-formated string from object + * @return + */ + public String getCsvStringFromStoreableObject (); +} diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java index 32706909..73c095b2 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java @@ -23,10 +23,13 @@ import org.mxchange.addressbook.UnhandledUserChoiceException; import org.mxchange.addressbook.client.Client; import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.contact.user.UserContact; +import org.mxchange.addressbook.database.frontend.contact.ContactDatabaseFrontend; +import org.mxchange.addressbook.database.frontend.contact.ContactWrapper; import org.mxchange.addressbook.manager.BaseManager; /** - * A manager for contacts + * A manager for contacts, please note that this implementation loads the whole + * list into RAM. * * @author Roland Haeder * @version 0.0 @@ -35,7 +38,12 @@ import org.mxchange.addressbook.manager.BaseManager; public class ContactManager extends BaseManager implements ManageableContact { /** - * All contacts + * A ContactWrapper instance + */ + private final ContactWrapper contactDatabase; + + /** + * A list of all contacts */ private final List contacts; @@ -50,6 +58,9 @@ public class ContactManager extends BaseManager implements ManageableContact { // Init contacts this.contacts = new ArrayList<>(maxContacts); + // Init database connection + this.contactDatabase = new ContactDatabaseFrontend(); + // Debug message //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client); @@ -77,8 +88,18 @@ public class ContactManager extends BaseManager implements ManageableContact { // Debug message /* NOISY-DEBUG: */ this.getLogger().debug("Adding '" + contact.getSurname() + "' '" + contact.getFamilyName() + "' at pos '" + this.size () + "' ..."); - // Add contact + // Add contact to internal list this.contacts.add(contact); + + /* + * @TODO This call doesn't make sense, it would cause that only one + * entry exist in storage. A better solution is to iterate over all + * contacts and add them. This may also require to rewrite the database + * layer a little. For demonstrational purposes, this should be fine. + */ + + // Add object to database + this.getContactDatabase().addContact(contact); } /** @@ -196,7 +217,7 @@ public class ContactManager extends BaseManager implements ManageableContact { String familyName = this.enterOwnFamilyName(); // And company - String companyName = this.enterCompanyName(); + String companyName = this.enterOwnCompanyName(); // Update contact instance contact.updateNameData(gender, surname, familyName, companyName); @@ -214,7 +235,33 @@ public class ContactManager extends BaseManager implements ManageableContact { */ @Override public void doChangeOtherData (final Contact contact, final Client client) { - throw new UnsupportedOperationException("Not supported yet."); + // First display them again + client.displayOtherDataBox(contact); + + // Is this own data? + if (contact.isOwnContact()) { + // Re-ask own data + // Phone number + String phoneNumber = this.enterOwnPhoneNumber(); + + // Phone number + String cellNumber = this.enterOwnCellNumber(); + + // Fax number + String faxNumber = this.enterOwnFaxNumber(); + + // Email address + String email = this.enterOwnEmailAddress(); + + // Comment + String comment = this.enterOwnComment(); + + // Update contact instance + contact.updateOtherData(phoneNumber, cellNumber, faxNumber, email, comment); + } else { + // Then re-ask them ... + throw new UnsupportedOperationException("Changing contact entries not finished."); + } } /** @@ -232,7 +279,7 @@ public class ContactManager extends BaseManager implements ManageableContact { String familyName = this.enterOwnFamilyName(); // Company name ... - String companyName = this.enterCompanyName(); + String companyName = this.enterOwnCompanyName(); // Construct UserContact instance Contact contact = new UserContact(gender, surname, familyName, companyName); @@ -255,22 +302,41 @@ public class ContactManager extends BaseManager implements ManageableContact { } /** - * Asks the user for his/her company name - * @return + * Asks the user for his/her cellphone number + * + * @return User's cellphone number */ - private String enterCompanyName () { - return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true); + private String enterOwnCellNumber () { + return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true); } /** * Asks the user for his/her city's name - * + * * @return City's name of the user */ private String enterOwnCity () { return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false); } + /** + * Asks the user for his/her city's name + * + * @return City's name of the user + */ + private String enterOwnComment () { + return this.getClient().enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true); + } + + /** + * Asks the user for his/her company name + * + * @return User's company name + */ + private String enterOwnCompanyName () { + return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true); + } + /** * Asks user for his/her own country code * @@ -280,22 +346,51 @@ public class ContactManager extends BaseManager implements ManageableContact { return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase(); } + /** + * Asks user for his/her own country code + * + * @return User's own country code + */ + private String enterOwnEmailAddress () { + return this.getClient().enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true); + } + /** * Asks the user for family name + * * @return Family name of the user */ private String enterOwnFamilyName () { return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false); } + /** + * Asks the user for family name + * + * @return Family name of the user + */ + private String enterOwnFaxNumber () { + return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true); + } + /** * Asks the user for gender, until a valid has been entered + * * @return Gender of the user */ private char enterOwnGender () { return this.getClient().enterChar(new char[] {'M', 'F', 'C'}, "Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): "); } + /** + * Asks the user for phone number + * + * @return Phone number of the user + */ + private String enterOwnPhoneNumber () { + return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true); + } + /** * Asks the user for own street (including number) */ @@ -416,4 +511,13 @@ public class ContactManager extends BaseManager implements ManageableContact { // Return result return isAdded; } + + /** + * A ContactWrapper instance + * + * @return the database + */ + private ContactWrapper getContactDatabase () { + return this.contactDatabase; + } }