import org.apache.logging.log4j.Logger;
import org.mxchange.addressbook.application.Application;
import org.mxchange.addressbook.client.Client;
+import org.mxchange.addressbook.database.frontend.DatabaseWrapper;
import org.mxchange.addressbook.manager.contact.ManageableContact;
/**
*/
private String tableName;
+ /**
+ * DatabaseWrapper instance
+ */
+ private DatabaseWrapper wrapper;
+
/**
* Initialize object
*/
return this.getBundle().getString(key);
}
+ /**
+ * Some "getter for a value from given column name. This name will be
+ * translated into a method name and then this method is called.
+ *
+ * @param columnName Column name
+ * @return Value from field
+ */
+ @Override
+ public Object getValueFromColumn (final String columnName) {
+ throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0}", columnName));
+ }
+
+ /**
+ * Some "getter" for a Method instance from given method name
+ *
+ * @param instance Actual instance to call
+ * @param targetClass Target class name
+ * @param methodName Method name
+ * @return A Method instance
+ */
+ @SuppressWarnings ("unchecked")
+ private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) {
+ // Trace messahe
+ this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName));
+
+ // Instance reflaction of this class
+ Class<? extends FrameworkInterface> c = instance.getClass();
+
+ // Analyze class
+ while (!targetClass.equals(c.getSimpleName())) {
+ // Debug message
+ this.getLogger().debug("c=" + c.getSimpleName());
+
+ // Get super class (causes unchecked warning)
+ c = (Class<? extends FrameworkInterface>) c.getSuperclass();
+ }
+
+ // Init field instance
+ Method method = null;
+
+ // Use reflection to get all attributes
+ try {
+ method = c.getDeclaredMethod(methodName, new Class<?>[0]);
+ } catch (final SecurityException ex) {
+ // Security problem
+ this.abortProgramWithException(ex);
+ } catch (final NoSuchMethodException ex) {
+ // Method not found
+ this.abortProgramWithException(ex);
+ }
+
+ // Assert on field
+ assert(method instanceof Method) : "method is not a Method instance";
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method));
+
+ // Return it
+ return method;
+ }
+
/**
* Aborts program with given exception
*
protected final void abortProgramWithException (final Throwable throwable) {
// Log exception ...
this.getLogger().catching(throwable);
-
+
// .. and exit
System.exit(1);
this.contactManager = contactManager;
}
+ /**
+ * Checks if given boolean field is available and set to same value
+ *
+ * @param columnName Column name to check
+ * @param bool Boolean value
+ * @return Whether all conditions are met
+ */
+ @Override
+ public boolean isValueEqual (final String columnName, final boolean bool) {
+ // Not implemented
+ throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0},bool={1}", columnName, bool));
+ }
+
/**
* Log exception
*
return builder.toString();
}
+ /**
+ * Returns boolean field value from given method call
+ *
+ * @param instance The instance to call
+ * @param targetClass Target class to look in
+ * @param methodName Method name to look for
+ * @return Boolean value from field
+ */
+ protected boolean getBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName) {
+ // Trace messahe
+ this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName));
+
+ // Get method instance
+ Method method = this.getMethodFromName(instance, targetClass, methodName);
+
+ // Get value from field
+ Boolean value = false;
+
+ try {
+ value = (Boolean) method.invoke(instance);
+ } catch (final IllegalArgumentException ex) {
+ // Other problem
+ this.abortProgramWithException(ex);
+ } catch (final IllegalAccessException ex) {
+ // Other problem
+ this.abortProgramWithException(ex);
+ } catch (final InvocationTargetException ex) {
+ // Other problem
+ this.abortProgramWithException(ex);
+ }
+
+ // Return value
+ return value;
+ }
+
+ /**
+ * Returns any field value from given method call
+ *
+ * @param instance The instance to call
+ * @param targetClass Target class to look in
+ * @param methodName Method name to look for
+ * @return Any value from field
+ */
+ protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) {
+ // Trace messahe
+ this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName));
+
+ // Get method to call
+ Method method = this.getMethodFromName(instance, targetClass, methodName);
+
+ // Get value from field
+ Object object = null;
+
+ try {
+ object = method.invoke(instance);
+ } catch (final IllegalArgumentException ex) {
+ // Other problem
+ this.abortProgramWithException(ex);
+ } catch (final IllegalAccessException ex) {
+ // Other problem
+ this.abortProgramWithException(ex);
+ } catch (final InvocationTargetException ex) {
+ // Other problem
+ this.abortProgramWithException(ex);
+ }
+
+ // Return value
+ return object;
+ }
+
/**
* Getter for logger
*
}
/**
- * Returns boolean field value from given method call
+ * Getter for DatabaseWrapper instance
*
- * @param instance The instance to call
- * @param targetClass Target class to look in
- * @param methodName Method name to look for
- * @return Boolean value from field
+ * @return DatabaseWrapper instance
*/
- @SuppressWarnings ("unchecked")
- protected boolean getBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName) {
- // Trace messahe
- this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName));
-
- // Instance reflaction of this class
- Class<? extends FrameworkInterface> c = instance.getClass();
-
- // Analyze class
- while (!targetClass.equals(c.getSimpleName())) {
- // Debug message
- this.getLogger().debug("c=" + c.getSimpleName());
-
- // Get super class (causes unchecked warning)
- c = (Class<? extends FrameworkInterface>) c.getSuperclass();
- }
-
- // Init field instance
- Method method = null;
-
- // Use reflection to get all attributes
- try {
- method = c.getDeclaredMethod(methodName, new Class<?>[0]);
- } catch (final SecurityException ex) {
- // Security problem
- this.abortProgramWithException(ex);
- } catch (final NoSuchMethodException ex) {
- // Method not found
- this.abortProgramWithException(ex);
- }
-
- // Assert on field
- assert(method instanceof Method) : "method is not a Method instance";
-
- // Get value from field
- boolean value = false;
-
- try {
- value = (boolean) method.invoke(instance);
- } catch (final IllegalArgumentException ex) {
- // Other problem
- this.abortProgramWithException(ex);
- } catch (final IllegalAccessException ex) {
- // Other problem
- this.abortProgramWithException(ex);
- } catch (final InvocationTargetException ex) {
- // Other problem
- this.abortProgramWithException(ex);
- }
+ protected DatabaseWrapper getWrapper () {
+ return this.wrapper;
+ }
- // Return value
- return value;
+ /**
+ * Setter for wrapper instance
+ *
+ * @param wrapper A DatabaseWrapper instance
+ */
+ protected void setWrapper (final DatabaseWrapper wrapper) {
+ this.wrapper = wrapper;
}
}
* @param exception Exception to log
*/
public void logException (final Throwable exception);
+
+ /**
+ * Checks if given boolean field is available and set to same value
+ *
+ * @param columnName Column name to check
+ * @param bool Boolean value
+ * @return Whether all conditions are met
+ */
+ public boolean isValueEqual (final String columnName, final boolean bool);
+
+ /**
+ * Some "getter for a value from given column name. This name will be
+ * translated into a method name and then this method is called.
+ *
+ * @param columnName Column name
+ * @return Value from field
+ */
+ public Object getValueFromColumn (final String columnName);
}
// Get all together
String csvString = String.format(
- "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"",
+ "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"", //NOI18N
this.isOwnContact(),
this.getGender().getDatabaseValue(),
this.getSurname(),
* @return gender Human-readable gender
*/
public String getTranslatedGender () {
- // Default init
- String translated = null;
-
// "Translate" it
- switch (this.getGender()) {
- case MALE: // Mr.
- translated = "Herr";
- break;
-
- case FEMALE: // Mrs.
- translated = "Frau";
- break;
-
- case COMPANY: // "Company"
- translated = "Firma";
- break;
-
- default: // Unsupported
- this.getLogger().error("Gender " + this.getGender() + " not supported.");
- break;
- }
+ String translated = this.getBundle().getString(this.getGender().getMessageKey());
// Return it
return translated;
* @param bool Boolean value
* @return Whether all conditions are met
*/
+ @Override
public boolean isValueEqual (final String columnName, final boolean bool) {
// Trace message
this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
boolean isFound = (bool == value);
// Trace message
- this.getLogger().trace("isFound=" + isFound + " - EXIT!");
+ this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
// Return result
return isFound;
}
+
+ /**
+ * Some "getter for a value from given column name. This name will be
+ * translated into a method name and then this method is called.
+ *
+ * @param columnName Column name
+ * @return Value from field
+ */
+ @Override
+ public Object getValueFromColumn (final String columnName) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
+
+ // Convert column name to field name
+ String methodName = this.convertColumnNameToGetterMethod(columnName, true);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("field={0}", methodName));
+
+ // Get field
+ Object value = this.getField(this, "BaseContact", methodName);
+
+ // Trace message
+ this.getLogger().trace("value=" + value + " - EXIT!");
+
+ // Return it
+ return value;
+ }
}
* @throws org.mxchange.addressbook.exceptions.BadTokenException If the CSV token is badly formulated
*/
public Iterator<? extends Storeable> iterator () throws BadTokenException;
+
+ /**
+ * Reads a single row from database.
+ *
+ * @param rowIndex Row index (or how much to skip)
+ * @return A Storeable instance
+ */
+ public Storeable readRow (final int rowIndex);
}
import org.mxchange.addressbook.contact.user.UserContact;
import org.mxchange.addressbook.database.backend.BaseDatabaseBackend;
import org.mxchange.addressbook.database.backend.DatabaseBackend;
+import org.mxchange.addressbook.database.frontend.DatabaseWrapper;
import org.mxchange.addressbook.database.storage.Storeable;
import org.mxchange.addressbook.database.storage.csv.StoreableCsv;
import org.mxchange.addressbook.exceptions.BadTokenException;
* Constructor with table name
*
* @param tableName Name of "table"
+ * @param wrapper Wrapper instance to call back
*/
- public Base64CsvDatabaseBackend (final String tableName) {
+ public Base64CsvDatabaseBackend (final String tableName, final DatabaseWrapper wrapper) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("tableName={0},wrapper={1}", tableName, wrapper)); //NOI18N
+
// Debug message
this.getLogger().debug(MessageFormat.format("Trying to initialize table {0} ...", tableName)); //NOI18N
// Set table name here, too
this.setTableName(tableName);
+ // Set wrapper here
+ this.setWrapper(wrapper);
+
// Construct file name
String fileName = String.format("data/table_%s.b64", tableName); //NOI18N
return length;
}
+ /**
+ * Reads a single row from database.
+ *
+ * @param rowIndex Row index (or how much to skip)
+ * @return A Storeable instance
+ */
+ @Override
+ public Storeable readRow (final int rowIndex) {
+ // First rewind
+ this.rewind();
+
+ // Intialize variables
+ int count = 0;
+ Storeable storeable = null;
+
+ // Read all rows
+ do {
+ // Read row
+ String line = this.readLine();
+
+ // Callback the wrapper to handle parsing
+ storeable = this.getWrapper().parseLineToStoreable(line);
+
+ // Increment counter
+ count++;
+ } while (!this.isEndOfFile() || (count > rowIndex));
+
+ // Return found element
+ return storeable;
+ }
+
/**
* Rewinds backend
*/
throw new UnsupportedOperationException("Not implemented for this backend."); //NOI18N
}
+ @Override
+ public Storeable readRow (final int rowIndex) {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
@Override
public void rewind () {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
*
* @author Roland Haeder
*/
-public class BaseDatabaseFrontend extends BaseFrameworkSystem {
+public abstract class BaseDatabaseFrontend extends BaseFrameworkSystem implements DatabaseWrapper {
/**
* Instance for database backend
break;
case "base64csv": // BASE64-encoded CSV rows //NOI18N
- this.backend = new Base64CsvDatabaseBackend(this.getTableName());
+ this.backend = new Base64CsvDatabaseBackend(this.getTableName(), this);
break;
default: // Unsupported
package org.mxchange.addressbook.database.frontend;
import org.mxchange.addressbook.FrameworkInterface;
+import org.mxchange.addressbook.database.storage.Storeable;
/**
* A generic interface for database frontends
* @author Roland Haeder
*/
public interface DatabaseWrapper extends FrameworkInterface {
+
+ /**
+ * 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
+ */
+ public Storeable parseLineToStoreable (final String line);
}
// 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) {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ /**
+ * 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) {
+ // Deligate this to backend instance
+ return (Contact) this.getBackend().readRow(rowIndex);
+
+ }
}
* @throws java.sql.SQLException If any SQL error occurs
*/
public boolean isOwnContactFound () throws SQLException;
+
+ /**
+ * Reads a single row and parses it to a contact instance
+ *
+ * @param rowIndex Row index (also how much to skip)
+ * @return Contact instance
+ */
+ public Contact readSingleContact (final int rowIndex);
}
return this.columnNames.get(columnIndex);
}
+ /**
+ * Somewhat "getter" for value from given row and column index
+ *
+ * @param rowIndex Row index
+ * @param columnIndex Column index
+ * @return Value from given row/column
+ */
+ @Override
+ public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex));
+
+ // Then get specific row from database which is a Contact instance
+ Contact contact = this.getContactDatabase().readSingleContact(rowIndex);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("contact={0}", contact));
+
+ // It may return null
+ if (contact == null) {
+ // Nothing found
+ this.getLogger().warn("contact is null - returning null ...");
+ return null;
+ }
+
+ // Convert column index -> name
+ String columnName = this.getColumnName(columnIndex);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("columnName={0}", columnName));
+
+ // Now get that column
+ Object value = contact.getValueFromColumn(columnName);
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value));
+
+ // Return it
+ return value;
+ }
+
/**
* Checks whether own contact is already added by checking all entries for
* isOwnContact flag
*/
public String getColumnName (final int columnIndex);
+ /**
+ * Somewhat "getter" for value from given row and column index
+ *
+ * @param rowIndex Row index
+ * @param columnIndex Column index
+ * @return Value from given row/column
+ */
+ public Object getValueFromRowColumn (final int rowIndex, final int columnIndex);
+
/**
* Adds given contact to address book
*
@Override
public Class<?> getColumnClass (final int columnIndex) {
- throw new UnsupportedOperationException("Not supported yet. columnIndex=" + columnIndex); //To change body of generated methods, choose Tools | Templates.
+ // All is the same
+ return Object.class;
}
@Override
@Override
public Object getValueAt (final int rowIndex, final int columnIndex) {
- throw new UnsupportedOperationException("Not supported yet. rowIndex=" + rowIndex + ",columnIndex=" + columnIndex); //To change body of generated methods, choose Tools | Templates.
+ // Let the manager do this job for us
+ return this.getClient().getContactManager().getValueFromRowColumn(rowIndex, columnIndex);
}
@Override