X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=Addressbook%2Fsrc%2Forg%2Fmxchange%2Faddressbook%2Fdatabase%2Ffrontend%2Fcontact%2FAddressbookContactDatabaseFrontend.java;h=89802a8b5198ea3bc1f789da99e5dd1a21cc8856;hb=a67280d037eb9f783695157780cbbc676e84b26f;hp=912ad5b77201919182bb2f63a75c79937bae7eb6;hpb=2747e0cd126be93b127a4bb9508178d74d9df39c;p=addressbook-swing.git diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java index 912ad5b..89802a8 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java @@ -17,9 +17,11 @@ package org.mxchange.addressbook.database.frontend.contact; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; import java.text.MessageFormat; import java.util.Iterator; +import java.util.Map; import java.util.StringTokenizer; import org.mxchange.addressbook.contact.book.BookContact; import org.mxchange.addressbook.contact.user.UserContact; @@ -29,11 +31,12 @@ import org.mxchange.addressbook.manager.contact.AddressbookContactManager; import org.mxchange.jcore.contact.Contact; import org.mxchange.jcore.contact.Gender; import org.mxchange.jcore.criteria.searchable.SearchCriteria; -import org.mxchange.jcore.criteria.searchable.SearchableCritera; +import org.mxchange.jcore.criteria.searchable.SearchableCriteria; import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend; import org.mxchange.jcore.database.result.Result; import org.mxchange.jcore.database.storage.Storeable; import org.mxchange.jcore.exceptions.BadTokenException; +import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException; import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; /** @@ -133,6 +136,11 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp this.getLogger().trace("EXIT!"); //NOI18N } + @Override + public Object emptyStringToNull (final String key, final Object value) { + throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: key={0},value={1}", key, value)); + } + /** * Some "getter" for total contact count * @@ -155,7 +163,7 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp this.getLogger().trace("CALLED!"); //NOI18N // Get row index back from backend - int rowIndex = this.getBackend().getRowIndexFromColumn(AddressbookContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true); + int rowIndex = this.getBackend().getRowIndexFromColumn(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true); // Debug message this.getLogger().debug(MessageFormat.format("rowIndex={0}", rowIndex)); @@ -228,16 +236,19 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp * Checks whether own contact is found in database * * @return Whether own contact is found - * @throws java.io.IOException If any IO error occurs - * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws org.mxchange.jcore.exceptions.BadTokenException Continued throw + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged + * @throws java.lang.NoSuchMethodException If a method cannot be found + * @throws java.lang.IllegalAccessException If a method is not accessible + * @throws java.lang.reflect.InvocationTargetException Any other problems? */ @Override - public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException { + public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { // Get search criteria instance - SearchableCritera critera = new SearchCriteria(); + SearchableCriteria critera = new SearchCriteria(); // Add condition - critera.addCriteria(AddressbookContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true); + critera.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true); // Get result Result result = this.getBackend().doSelectByCriteria(critera); @@ -246,28 +257,6 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp return result.hasNext(); } - /** - * 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 * @@ -288,250 +277,14 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp 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) && (Gender.valueOf(strippedToken) instanceof Gender)) { //NOI18N - // Get first character - gender = Gender.valueOf(strippedToken); - - // 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 + @Override + public Storeable toStoreable (final Map map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + throw new UnsupportedOperationException("Not supported yet: map=" + map); + } - // Return finished instance - return contact; + @Override + public String getIdName () { + // Return id column + return AddressbookContactDatabaseConstants.COLUMN_ID; } }