]> git.mxchange.org Git - addressbook-swing.git/blobdiff - Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java
Added a lot more generic methods for columnName lookup (a field must be there) +...
[addressbook-swing.git] / Addressbook / src / org / mxchange / addressbook / BaseFrameworkSystem.java
index f0fd4d4e3eed45ac6f1be3245fdbdb8153b19669..92fe2587e1cc0a9671d5ad178baff8439752ab46 100644 (file)
@@ -25,6 +25,7 @@ import java.io.PrintWriter;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.text.MessageFormat;
+import java.util.Arrays;
 import java.util.Properties;
 import java.util.ResourceBundle;
 import java.util.StringTokenizer;
@@ -32,7 +33,7 @@ import org.apache.logging.log4j.LogManager;
 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.database.frontend.DatabaseFrontend;
 import org.mxchange.addressbook.manager.contact.ManageableContact;
 
 /**
@@ -41,6 +42,7 @@ import org.mxchange.addressbook.manager.contact.ManageableContact;
  * @author Roland Haeder
  */
 public class BaseFrameworkSystem implements FrameworkInterface {
+
        /**
         * Instance for own properties
         */
@@ -71,16 +73,15 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         */
        private ManageableContact contactManager;
 
-
        /**
         * Name of used database table, handled over to backend
         */
        private String tableName;
 
        /**
-        * DatabaseWrapper instance
+        * DatabaseFrontend instance
         */
-       private DatabaseWrapper wrapper;
+       private DatabaseFrontend wrapper;
 
        /**
         * Initialize object
@@ -133,21 +134,20 @@ public class BaseFrameworkSystem implements FrameworkInterface {
        }
 
        /**
-        * Some "getter" for a Method instance from given method name
+        * Some "getter" for target class instance from given name.
         *
-        * @param instance Actual instance to call
-        * @param targetClass Target class name
-        * @param methodName Method name
-        * @return A Method instance
+        * @param instance Instance to iterate on
+        * @param targetClass Class name to look for
+        * @return Class 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));
+       private Class<? extends FrameworkInterface> getClassFromTarget (final FrameworkInterface instance, final String targetClass) {
+               // Trace message
+               this.getLogger().debug(MessageFormat.format("instance={0},targetClass={1}", instance, targetClass));
 
                // Instance reflaction of this class
                Class<? extends FrameworkInterface> c = instance.getClass();
-               
+
                // Analyze class
                while (!targetClass.equals(c.getSimpleName())) {
                        // Debug message
@@ -157,6 +157,28 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                        c = (Class<? extends FrameworkInterface>) c.getSuperclass();
                }
 
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c));
+
+               // Return it
+               return c;
+       }
+
+       /**
+        * 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
+        */
+       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));
+
+               // Get target class instance
+               Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
+
                // Init field instance
                Method method = null;
 
@@ -172,7 +194,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                }
 
                // Assert on field
-               assert(method instanceof Method) : "method is not a Method instance";
+               assert (method instanceof Method) : "method is not a Method instance";
 
                // Trace message
                this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method));
@@ -192,7 +214,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
 
                // .. and exit
                System.exit(1);
-               
+
        }
 
        /**
@@ -397,7 +419,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                        // Increment counter
                        count++;
                }
-       
+
                // Trace message
                this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder));
 
@@ -417,11 +439,8 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                // Trace message
                this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
 
-               // First all lower case
-               String lower = columnName.toLowerCase();
-
                // Then split on "_"
-               StringTokenizer tokenizer = new StringTokenizer(lower, "_");
+               StringTokenizer tokenizer = new StringTokenizer(columnName, "_");
 
                // Resulting string
                StringBuilder builder = new StringBuilder(tokenizer.countTokens());
@@ -450,7 +469,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                        // Add token
                        builder.append(token);
                }
-       
+
                // Trace message
                this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder));
 
@@ -527,7 +546,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                // Return value
                return object;
        }
-       
+
        /**
         * Getter for logger
         *
@@ -566,20 +585,100 @@ public class BaseFrameworkSystem implements FrameworkInterface {
        }
 
        /**
-        * Getter for DatabaseWrapper instance
+        * Getter for DatabaseFrontend instance
         *
-        * @return DatabaseWrapper instance
+        * @return DatabaseFrontend instance
         */
-       protected DatabaseWrapper getWrapper () {
+       protected DatabaseFrontend getWrapper () {
                return this.wrapper;
        }
 
        /**
         * Setter for wrapper instance
         *
-        * @param wrapper A DatabaseWrapper instance
+        * @param wrapper A DatabaseFrontend instance
         */
-       protected void setWrapper (final DatabaseWrapper wrapper) {
+       protected void setWrapper (final DatabaseFrontend wrapper) {
                this.wrapper = wrapper;
        }
+
+       /**
+        * Some "getter" for an array from given string and tokenizer
+        *
+        * @param str String to tokenize and get array from
+        * @param delimiter Delimiter
+        * @param size Size of array
+        * @return Array from tokenized string
+        */
+       protected String[] getArrayFromString (final String str, final String delimiter, final int size) {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("str={0},delimiter={1},size={2} - CALLED!", str, delimiter, size));
+
+               // Get tokenizer
+               StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
+
+               // Init array and index
+               String[] tokens = new String[size];
+               int index = 0;
+
+               // Run through all tokens
+               while (tokenizer.hasMoreTokens()) {
+                       // Get current token and add it
+                       tokens[index] = tokenizer.nextToken();
+
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1]));
+
+                       // Increment index
+                       index++;
+               }
+       
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens)));
+
+               // Return it
+               return tokens;
+       }
+
+       /**
+        * Checks whether the given field is a boolean field by probing it.
+        * 
+        * @param instance Instance to call
+        * @param targetClass Target class
+        * @param columnName Column name to check
+        * @return Whether the given column name represents a boolean field
+        */
+       protected boolean isBooleanField (final FrameworkInterface instance, final String targetClass, final String columnName) {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("instance={0},targetCLass={1},columnName={2} - CALLED!", instance, targetClass, columnName));
+
+               // Convert column name to getter name (boolean)
+               String methodName = this.convertColumnNameToGetterMethod(columnName, true);
+
+               // Get class instance
+               Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
+
+               // Defauzlt is boolean
+               boolean isBool = true;
+
+               try {
+                       // Now try to instance the method
+                       Method method = c.getDeclaredMethod(methodName, new Class<?>[0]);
+               } catch (final NoSuchMethodException ex) {
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("Method {0} does not exist, field {1} cannot be boolean: {2}", methodName, columnName, ex));
+
+                       // Not found
+                       isBool = false;
+               } catch (final SecurityException ex) {
+                       // Really bad?
+                       this.abortProgramWithException(ex);
+               }
+       
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("isBool={0} - EXIT!", isBool));
+
+               // Return result
+               return isBool;
+       }
 }