-/*\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 java.util.Iterator;\r
-import java.util.List;\r
-import org.mxchange.addressbook.BadTokenException;\r
-import org.mxchange.addressbook.contact.Contact;\r
-import org.mxchange.addressbook.database.backend.csv.CsvBackend;\r
-import org.mxchange.addressbook.database.frontend.BaseDatabaseFrontend;\r
-import org.mxchange.addressbook.database.storage.Storeable;\r
-import org.mxchange.addressbook.manager.contact.ManageableContact;\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
- /**\r
- * Flushes all contact entries to database\r
- * @param contactManager An instance of a MangeableContact class\r
- */\r
- @Override\r
- public void flushAllContacts (final ManageableContact contactManager) {\r
- // Get full list\r
- List<Contact> contacts = contactManager.getList();\r
-\r
- // Get iterator\r
- Iterator<Contact> iterator = contacts.iterator();\r
-\r
- // Rewind backend\r
- this.getBackend().rewind();\r
-\r
- // Get all entries\r
- while (iterator.hasNext()) {\r
- // Get next entry\r
- Contact contact = iterator.next();\r
-\r
- try {\r
- // Store this entry\r
- this.getBackend().store((Storeable) contact);\r
- } catch (final IOException ex) {\r
- // Should not happen?\r
- this.getLogger().catching(ex);\r
- System.exit(1);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Reads all contacts from database backend and handles them over to the\r
- * contact manager\r
- * \r
- * @param contactManager Contact manager to handle loaded contacts\r
- */\r
- @Override\r
- public void readAllContacts (final ManageableContact contactManager) {\r
- // Get iterator and case it\r
- CsvBackend backend = (CsvBackend) this.getBackend();\r
- \r
- // First rewind to beginning\r
- this.getBackend().rewind();\r
-\r
- // Get backend iterator\r
- Iterator<Contact> iterator = null;\r
- try {\r
- iterator = backend.contactIterator();\r
- } catch (final BadTokenException ex) {\r
- this.getLogger().catching(ex);\r
- System.exit(1);\r
- }\r
-\r
- // Read all entries\r
- while (iterator.hasNext()) {\r
- // Get next entry\r
- Contact contact = iterator.next();\r
-\r
- // Add contact instance to manager\r
- contactManager.addContact(contact);\r
- }\r
- }\r
-}\r
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook.database.frontend.contact;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.text.MessageFormat;
+import java.util.Iterator;
+import java.util.StringTokenizer;
+import org.mxchange.jcore.contact.Contact;
+import org.mxchange.jcore.contact.Gender;
+import org.mxchange.addressbook.contact.book.BookContact;
+import org.mxchange.addressbook.contact.user.UserContact;
+import org.mxchange.addressbook.database.contact.ContactDatabaseConstants;
+import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
+import org.mxchange.addressbook.manager.contact.ContactManager;
+import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
+import org.mxchange.jcore.database.storage.Storeable;
+import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
+
+/**
+ * Stores and retrieves Contact instances
+ *
+ * @author Roland Haeder
+ */
+public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactFrontend {
+
+ /**
+ * Constructor which accepts a contact manager
+ *
+ * @param manager Manager instance
+ */
+ public ContactDatabaseFrontend (final ContactManager manager) {
+ // Call own constructor
+ this();
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
+
+ // Manager instance must not be null
+ if (manager == null) {
+ // Abort here
+ throw new NullPointerException("manager is null"); //NOI18N
+ }
+
+ // Set contact manager
+ this.setContactManager(manager);
+ }
+
+ /**
+ * Basic constrcutor
+ */
+ protected ContactDatabaseFrontend () {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Set "table" name
+ this.setTableName("contacts"); //NOI18N
+
+ try {
+ // Initalize backend
+ this.initBackend();
+ } catch (final UnsupportedDatabaseBackendException ex) {
+ // Abort program
+ this.abortProgramWithException(ex);
+ } catch (final SQLException ex) {
+ // Abort here
+ this.abortProgramWithException(ex);
+ }
+ }
+
+ /**
+ * Adds given contact instance to database
+ *
+ * @param contact Contact instance
+ * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact instance is already found
+ */
+ @Override
+ public void addContact (final Contact contact) throws ContactAlreadyAddedException {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Make sure the contact is set
+ if (contact == null) {
+ // Abort here
+ throw new NullPointerException("contact is null"); //NOI18N
+ }
+
+ try {
+ // First check if the contact is there
+ if (this.isContactFound(contact)) {
+ // Already there
+ throw new ContactAlreadyAddedException(contact);
+ }
+
+ // Then add it
+ this.getBackend().store((Storeable) contact);
+ } catch (final IOException ex) {
+ // Abort here
+ this.abortProgramWithException(ex);
+ } catch (final BadTokenException ex) {
+ // Abort here
+ this.abortProgramWithException(ex);
+ }
+
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+ }
+
+ /**
+ * Shuts down the database layer
+ */
+ @Override
+ public void doShutdown () {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Shutdown backend
+ this.getBackend().doShutdown();
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
+
+ /**
+ * Some "getter" for total contact count
+ *
+ * @return Total contact count
+ */
+ @Override
+ public int getContactsCount () throws SQLException {
+ // And deligate to backend
+ return this.getBackend().getTotalCount(); //NOI18N
+ }
+
+ /**
+ * Some "getter" for own contact instance
+ *
+ * @return Own contact instance
+ */
+ @Override
+ public Contact getOwnContact () {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Get row index back from backend
+ int rowIndex = this.getBackend().getRowIndexFromColumn(ContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("rowIndex={0}", rowIndex));
+
+ // Init instance
+ Contact contact = null;
+
+ try {
+ // Now simply read the row
+ contact = (Contact) this.getBackend().readRow(rowIndex);
+ } catch (final BadTokenException ex) {
+ // Bad token found
+ this.abortProgramWithException(ex);
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact));
+
+ // Return it
+ return contact;
+ }
+
+ /**
+ * Checks if given Contact is found
+ *
+ * @param contact Contact instance to check
+ * @return Whether the given Contact instance is found
+ */
+ @Override
+ public boolean isContactFound (final Contact contact) throws BadTokenException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
+
+ // contact should not be null
+ if (contact == null) {
+ // Abort here
+ throw new NullPointerException("contact is null"); //NOI18N
+ }
+
+ // Default is not found
+ boolean isFound = false;
+
+ // Start iteration
+ Iterator<? extends Storeable> iterator = this.getBackend().iterator();
+
+ // Check all entries
+ while (iterator.hasNext()) {
+ // Get next element
+ Contact c = (Contact) iterator.next();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
+
+ // Is it added?
+ if (c.equals(contact)) {
+ // Is found
+ isFound = true;
+ break;
+ }
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
+
+ // Return it
+ return isFound;
+ }
+
+ /**
+ * Checks whether own contact is found in database
+ *
+ * @return Whether own contact is found
+ */
+ @Override
+ public boolean isOwnContactFound () throws SQLException {
+ // Deligate this call to backend
+ return this.getBackend().isRowFound(ContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);
+ }
+
+ /**
+ * Parses given line from database backend into a Storeable instance. Please
+ * note that not all backends need this.
+ *
+ * @param line Line from database backend
+ * @return A Storeable instance
+ */
+ @Override
+ public Storeable parseLineToStoreable (final String line) throws BadTokenException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
+
+ // Call inner method
+ Contact contact = this.parseLineToContact(line);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("contact={0}", contact));
+
+ // Return it
+ return (Storeable) contact;
+ }
+
+ /**
+ * Reads a single row and parses it to a contact instance
+ *
+ * @param rowIndex Row index (also how much to skip)
+ * @return Contact instance
+ */
+ @Override
+ public Contact readSingleContact (final int rowIndex) {
+ try {
+ // Deligate this to backend instance
+ return (Contact) this.getBackend().readRow(rowIndex);
+ } catch (final BadTokenException ex) {
+ // Bad token found
+ this.abortProgramWithException(ex);
+ }
+
+ // Bad state, should not be reached
+ throw new IllegalStateException("This should not be reached");
+ }
+
+ /**
+ * Parses given line and creates a Contact instance
+ *
+ * @param line Raw line to parse
+ *
+ * @return Contact instance
+ */
+ private Contact parseLineToContact (final String line) throws BadTokenException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
+
+ // Init A lot variables
+ Long num = null;
+ Boolean bool = null;
+ Gender gender = null;
+ int count = 0;
+ Contact contact = null;
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("line={0}", line)); //NOI18N
+
+ // Then tokenize it
+ // @TODO Move this into separate method
+ StringTokenizer tokenizer = new StringTokenizer(line, ";"); //NOI18N
+
+ // Reset variables
+ count = 0;
+ contact = null;
+
+ // The tokens are now available, so get all
+ while (tokenizer.hasMoreElements()) {
+ // Reset variables
+ num = null;
+ bool = null;
+
+ // Get next token
+ String token = tokenizer.nextToken();
+
+ // If char " is at pos 2 (0,1,2), then cut it of there
+ if ((token.charAt(0) != '"') && (token.charAt(2) == '"')) {
+ // UTF-8 writer characters found
+ token = token.substring(2);
+ }
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N
+
+ // Verify token, it must have double-quotes on each side
+ if ((!token.startsWith("\"")) || (!token.endsWith("\""))) { //NOI18N
+ // Something bad was read
+ throw new BadTokenException(token, count); //NOI18N
+ }
+
+ // All fine, so remove it
+ String strippedToken = token.substring(1, token.length() - 1);
+
+ // Is the string's content "null"?
+ if (strippedToken.equals("null")) { //NOI18N
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("strippedToken={0} - NULL!", strippedToken)); //NOI18N
+
+ // This needs to be set to null
+ strippedToken = null;
+ }
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("strippedToken={0}", strippedToken)); //NOI18N
+
+ // Now, let's try a number check, if no null
+ if (strippedToken != null) {
+ // Okay, no null, maybe the string bears a decimal number?
+ try {
+ num = Long.valueOf(strippedToken);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("strippedToken={0} - NUMBER!", strippedToken)); //NOI18N
+ } catch (final NumberFormatException ex) {
+ // No number, then set default
+ num = null;
+ }
+ }
+
+ // Now, let's try a boolean check, if no null
+ if ((strippedToken != null) && (num == null) && ((strippedToken.equals("true")) || (strippedToken.equals("false")))) { //NOI18N
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("strippedToken={0} - BOOLEAN!", strippedToken)); //NOI18N
+
+ // parseBoolean() is relaxed, so no exceptions
+ bool = Boolean.valueOf(strippedToken);
+ }
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("strippedToken={0},num={1},bool={2}", strippedToken, num, bool)); //NOI18N
+
+ // Now, let's try a gender check, if no null
+ if ((strippedToken != null) && (num == null) && (bool == null) && ((strippedToken.equals("M")) || (strippedToken.equals("F")) || (strippedToken.equals("C")))) { //NOI18N
+ // Get first character
+ gender = Gender.fromChar(strippedToken.charAt(0));
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("strippedToken={0},gender={1}", strippedToken, gender)); //NOI18N
+
+ // This instance must be there
+ assert (gender instanceof Gender) : MessageFormat.format("gender is not set by Gender.fromChar({0})", strippedToken); //NOI18N
+ }
+
+ // Now it depends on the counter which position we need to check
+ switch (count) {
+ case 0: // isOwnContact
+ assert ((bool instanceof Boolean));
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("bool={0}", bool)); //NOI18N
+
+ // Is it own contact?
+ if (true == bool) {
+ // Debug message
+ this.getLogger().debug("Creating UserContact object ..."); //NOI18N
+
+ // Own entry
+ contact = new UserContact();
+ } else {
+ // Debug message
+ this.getLogger().debug("Creating BookContact object ..."); //NOI18N
+
+ // Other contact
+ contact = new BookContact();
+ }
+ break;
+
+ case 1: // Gender
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+
+ // Update data
+ contact.setGender(gender);
+ break;
+
+ case 2: // Surname
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+ assert (gender instanceof Gender) : "gender instance is not set"; //NOI18N
+
+ // Update data
+ contact.setSurname(strippedToken);
+ break;
+
+ case 3: // Family name
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+ assert (gender instanceof Gender) : "gender instance is not set"; //NOI18N
+
+ // Update data
+ contact.setFamilyName(strippedToken);
+ break;
+
+ case 4: // Company name
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+ assert (gender instanceof Gender) : "gender instance is not set"; //NOI18N
+
+ // Update data
+ contact.setCompanyName(strippedToken);
+ break;
+
+ case 5: // Street number
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+
+ // Update data
+ contact.setHouseNumber(num);
+ break;
+
+ case 6: // ZIP code
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+
+ // Update data
+ contact.setZipCode(num);
+ break;
+
+ case 7: // City name
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+
+ // Update data
+ contact.setCity(strippedToken);
+ break;
+
+ case 8: // Country code
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+
+ // Update data
+ contact.setCountryCode(strippedToken);
+ break;
+
+ case 9: // Phone number
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+
+ // Update data
+ contact.setPhoneNumber(strippedToken);
+ break;
+
+ case 10: // Fax number
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+
+ // Update data
+ contact.setFaxNumber(strippedToken);
+ break;
+
+ case 11: // Cellphone number
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+
+ // Update data
+ contact.setCellphoneNumber(strippedToken);
+ break;
+
+ case 12: // Email address
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+
+ // Update data
+ contact.setEmailAddress(strippedToken);
+ break;
+
+ case 13: // Birthday
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+
+ // Update data
+ contact.setBirthday(strippedToken);
+ break;
+
+ case 14: // Comment
+ assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
+
+ // Update data
+ contact.setComment(strippedToken);
+ break;
+
+ default: // New data entry
+ this.getLogger().warn(MessageFormat.format("Will not handle unknown data {0} at index {1}", strippedToken, count)); //NOI18N
+ break;
+ }
+
+ // Increment counter for next round
+ count++;
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
+
+ // Return finished instance
+ return contact;
+ }
+}