]> git.mxchange.org Git - jaddressbook-lib.git/commitdiff
Added a lot database stuff, which needs heavy rewrite
authorRoland Haeder <roland@mxchange.org>
Thu, 16 Jul 2015 13:24:26 +0000 (15:24 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 16 Jul 2015 13:24:26 +0000 (15:24 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

15 files changed:
Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java
Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java
Addressbook/src/org/mxchange/addressbook/contact/Contact.java
Addressbook/src/org/mxchange/addressbook/contact/book/BookContact.java
Addressbook/src/org/mxchange/addressbook/contact/user/UserContact.java
Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java [new file with mode: 0644]
Addressbook/src/org/mxchange/addressbook/database/backend/csv/BaseDatabaseBackend.java [new file with mode: 0644]
Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java [new file with mode: 0644]
Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java [new file with mode: 0644]
Addressbook/src/org/mxchange/addressbook/database/frontend/DatabaseWrapper.java [new file with mode: 0644]
Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java [new file with mode: 0644]
Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java [new file with mode: 0644]
Addressbook/src/org/mxchange/addressbook/database/storage/Storeable.java [new file with mode: 0644]
Addressbook/src/org/mxchange/addressbook/database/storage/csv/StoreableCsv.java [new file with mode: 0644]
Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java

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