]> git.mxchange.org Git - jcore.git/blobdiff - src/org/mxchange/jcore/BaseFrameworkSystem.java
Changed to SortedSet to keep ordering
[jcore.git] / src / org / mxchange / jcore / BaseFrameworkSystem.java
index 4b6d02567537cbb0ca4b302400f8a3942d0e1ca8..b48ad50e49d6a32faf7699efc7e860a0cfc2e47b 100644 (file)
@@ -33,6 +33,7 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.ResourceBundle;
 import java.util.StringTokenizer;
+import org.apache.commons.lang3.ArrayUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.mxchange.jcore.application.Application;
@@ -40,7 +41,7 @@ import org.mxchange.jcore.client.Client;
 import org.mxchange.jcore.contact.Contact;
 import org.mxchange.jcore.database.backend.DatabaseBackend;
 import org.mxchange.jcore.database.frontend.DatabaseFrontend;
-import org.mxchange.jcore.database.storage.Storeable;
+import org.mxchange.jcore.database.storage.Storable;
 import org.mxchange.jcore.manager.Manageable;
 
 /**
@@ -103,7 +104,12 @@ public class BaseFrameworkSystem implements FrameworkInterface {
        /**
         * DatabaseFrontend instance
         */
-       private DatabaseFrontend wrapper;
+       private DatabaseFrontend frontend;
+
+       /**
+        * Session id assigned with this basket, or any other unique identifier
+        */
+       private String sessionId;
 
        /**
         * Initialize object
@@ -215,11 +221,27 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                // Trace messahe
                this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
 
-               // Get target class instance
-               Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
 
-               // Init field instance
-               Method method = c.getDeclaredMethod(methodName, new Class<?>[0]);
+               // Init method instance
+               Method method = null;
+
+               // Try it from target class
+               try {
+                       // Get target class instance
+                       Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
+
+                       // Init field instance
+                       method = c.getDeclaredMethod(methodName, new Class<?>[0]);
+               } catch (final NoSuchMethodException e) {
+                       // Didn't found it
+                       this.getLogger().warn(e);
+
+                       // So try it from super class
+                       Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, "BaseFrameworkSystem"); //NOI18N
+
+                       // Init field instance
+                       method = c.getDeclaredMethod(methodName, new Class<?>[0]);
+               }
 
                // Assert on field
                assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
@@ -244,11 +266,26 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                // Trace messahe
                this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1},type={2}", targetClass, methodName, type)); //NOI18N
 
-               // Get target class instance
-               Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
+               // Init method instance
+               Method method = null;
+
+               // Try it from target class
+               try {
+                       // Get target class instance
+                       Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
 
-               // Init field instance
-               Method method = c.getDeclaredMethod(methodName, type);
+                       // Init field instance
+                       method = c.getDeclaredMethod(methodName, type);
+               } catch (final NoSuchMethodException e) {
+                       // Didn't found it
+                       this.getLogger().warn(e);
+
+                       // So try it from super class
+                       Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, "BaseFrameworkSystem"); //NOI18N
+
+                       // Init field instance
+                       method = c.getDeclaredMethod(methodName, type);
+               }
 
                // Assert on field
                assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
@@ -275,7 +312,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         */
        protected final void abortProgramWithException (final Throwable throwable) {
                // Log exception ...
-               this.getLogger().catching(throwable);
+               this.logException(throwable);
 
                // .. and exit
                System.exit(1);
@@ -309,6 +346,15 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                return BaseFrameworkSystem.bundle;
        }
 
+       /**
+        * Setter for bundle instance
+        *
+        * @param bundle the bundle to set
+        */
+       protected static void setBundle (final ResourceBundle bundle) {
+               BaseFrameworkSystem.bundle = bundle;
+       }
+
        /**
         * Client instance
         *
@@ -364,7 +410,8 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                // Init default values:
                // Default database backend
                BaseFrameworkSystem.properties.put("org.mxchange.database.backend.class", "org.mxchange.jcore.database.backend.base64.Base64CsvDatabaseBackend"); //NOI18N
-               BaseFrameworkSystem.properties.put("database.backend.storagepath", "data/"); //NOI18N
+               BaseFrameworkSystem.properties.put("org.mxchange.database.backend.storagepath", "data/"); //NOI18N
+               BaseFrameworkSystem.properties.put("org.mxchange.database.datasource.name", ""); //NOI18N
 
                // For MySQL backend
                BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.host", "localhost"); //NOI18N
@@ -396,15 +443,12 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         * @param columnName Column name to convert
         * @return Attribute name
         */
-       protected String convertColumnNameToAttribute (final String columnName) {
+       protected String convertColumnNameToFieldName (final String columnName) {
                // Trace message
                this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
 
-               // First all lower case
-               String lower = columnName.toLowerCase();
-
-               // Then split on "_"
-               StringTokenizer tokenizer = new StringTokenizer(lower, "_"); //NOI18N
+               // Split on "_"
+               StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
 
                // Resulting string
                StringBuilder builder = new StringBuilder(tokenizer.countTokens());
@@ -514,12 +558,15 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                        String token = tokenizer.nextToken();
 
                        // Debug message
-                       this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N
+                       this.getLogger().debug(MessageFormat.format("token={0} - BEFORE", token)); //NOI18N
 
                        // Make it upper-case
                        char c = token.charAt(0);
                        token = String.valueOf(c).toUpperCase() + token.substring(1);
 
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("token={0} - AFTER", token)); //NOI18N
+
                        // Add token
                        builder.append(token);
                }
@@ -536,19 +583,18 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         *
         * @param str String to tokenize and get array from
         * @param delimiter Delimiter
-        * @param size Size of array
         * @return Array from tokenized string
-        * @todo Get rid of size parameter
+        * TODO Get rid of size parameter
         */
-       protected String[] getArrayFromString (final String str, final String delimiter, final int size) {
+       protected String[] getArrayFromString (final String str, final String delimiter) {
                // Trace message
-               this.getLogger().trace(MessageFormat.format("str={0},delimiter={1},size={2} - CALLED!", str, delimiter, size)); //NOI18N
+               this.getLogger().trace(MessageFormat.format("str={0},delimiter={1} - CALLED!", str, delimiter)); //NOI18N
 
                // Get tokenizer
                StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
 
                // Init array and index
-               String[] tokens = new String[size];
+               String[] tokens = new String[tokenizer.countTokens()];
                int index = 0;
 
                // Run through all tokens
@@ -692,30 +738,39 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                // Init object
                Object object = value;
 
-               // Hard-coded "cast" again ... :-(
-               // @TODO Can't we get rid of this???
-               switch (type.getSimpleName()) {
-                       case "Long": // Long object //NOI18N
-                               object = Long.parseLong((String) value);
-                               break;
-
-                       case "Float": // Float object //NOI18N
-                               object = Float.parseFloat((String) value);
-                               break;
-
-                       case "Boolean": // Boolean object //NOI18N
-                               object = Boolean.parseBoolean((String) value);
-                               break;
-
-                       case "String": // String object //NOI18N
-                               break;
+               // Is the value null?
+               if ("null".equals(value)) { //NOI18N
+                       // Warning message
+                       this.getLogger().warn(MessageFormat.format("columnName={0} has null value.", columnName)); //NOI18N
 
-                       default: // Unsupported type
-                               throw new IllegalArgumentException(MessageFormat.format("value {0} has unsupported type {1}", value, type.getSimpleName())); //NOI18N
+                       // Set null
+                       object = null;
+               } else {
+                       // Hard-coded "cast" again ... :-(
+                       // TODO Can't we get rid of this???
+                       switch (type.getSimpleName()) {
+                               case "Long": // Long object //NOI18N
+                                       object = Long.parseLong((String) value);
+                                       break;
+
+                               case "Float": // Float object //NOI18N
+                                       object = Float.parseFloat((String) value);
+                                       break;
+
+                               case "Boolean": // Boolean object //NOI18N
+                                       object = Boolean.parseBoolean((String) value);
+                                       break;
+
+                               case "String": // String object //NOI18N
+                                       break;
+
+                               default: // Unsupported type
+                                       throw new IllegalArgumentException(MessageFormat.format("value {0} has unsupported type {1}", value, type.getSimpleName())); //NOI18N
+                       }
                }
 
                // Debug message
-               this.getLogger().debug(MessageFormat.format("object[{0}]={1}", object.getClass().getSimpleName(), object)); //NOI18N
+               this.getLogger().debug(MessageFormat.format("object={0}", object)); //NOI18N
 
                // Get method to call
                Method method = this.getMethodFromName(instance, targetClass, methodName, type);
@@ -760,7 +815,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                this.getLogger().trace(MessageFormat.format("str={0}", str)); //NOI18N
 
                // Is it null?
-               if (str == null) {
+               if (null == str) {
                        // Return empty string
                        return ""; //NOI18N
                }
@@ -782,7 +837,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         * @throws java.lang.IllegalAccessException If the method cannot be accessed
         * @throws java.lang.reflect.InvocationTargetException Any other problems?
         */
-       protected Iterator<Map.Entry<Field, Object>> fieldIterator (final Storeable instance, final String className) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+       protected Iterator<Map.Entry<Field, Object>> fieldIterator (final Storable instance, final String className) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
                // Trace message
                this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className)); //NOI18N
 
@@ -871,16 +926,46 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         * @return DatabaseFrontend instance
         */
        protected final DatabaseFrontend getFrontend () {
-               return this.wrapper;
+               return this.frontend;
        }
 
        /**
         * Setter for wrapper instance
         *
-        * @param wrapper A DatabaseFrontend instance
+        * @param frontend A DatabaseFrontend instance
+        */
+       protected final void setFrontend (final DatabaseFrontend frontend) {
+               this.frontend = frontend;
+       }
+
+       /**
+        * Setter for session id
+        *
+        * @param sessionId Session id
+        */
+       @Override
+       public final void setSessionId (final String sessionId) {
+               this.sessionId = sessionId;
+       }
+
+       /**
+        * Getter for session id
+        *
+        * @return Session id
+        */
+       @Override
+       public final String getSessionId () {
+               return this.sessionId;
+       }
+
+       /**
+        * Checks if the bundle is initialized
+        *
+        * @return Whether the bundle has been initialized
         */
-       protected final void setFrontend (final DatabaseFrontend wrapper) {
-               this.wrapper = wrapper;
+       protected boolean isBundledInitialized () {
+               // Check it
+               return (bundle instanceof ResourceBundle);
        }
 
        /**
@@ -891,13 +976,13 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                this.getLogger().trace("CALLED!"); //NOI18N
 
                // Is the bundle set?
-               if (bundle instanceof ResourceBundle) {
+               if (this.isBundledInitialized()) {
                        // Is already set
                        throw new IllegalStateException("called twice"); //NOI18N
                }
 
                // Set instance
-               bundle = ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE); // NOI18N
+               setBundle(ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE)); // NOI18N
 
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N
@@ -995,10 +1080,10 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); //NOI18N
 
                // Both should not be null
-               if (key == null) {
+               if (null == key) {
                        // key is null
                        throw new NullPointerException("key is null"); //NOI18N
-               } else if (value == null) {
+               } else if (null == value) {
                        // value is null
                        throw new NullPointerException("value is null"); //NOI18N
                }
@@ -1031,9 +1116,9 @@ public class BaseFrameworkSystem implements FrameworkInterface {
        }
 
        /**
-        * Some "setter" for a value in given Storeable instance and target class
+        * Some "setter" for a value in given Storable instance and target class
         * 
-        * @param instance An instance of a Storeable class
+        * @param instance An instance of a Storable class
         * @param targetClass The target class (where the field resides)
         * @param columnName Column name (= field name)
         * @param value Value to set
@@ -1041,7 +1126,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         * @throws java.lang.IllegalAccessException If the setter cannot be accessed
         * @throws java.lang.reflect.InvocationTargetException Any other problem?
         */
-       protected void setValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+       protected void setValueInStorableFromColumn (final Storable instance, final String targetClass, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
                // Trace message
                this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2},value={3} - CALLED!", instance, targetClass, columnName, value)); //NOI18N
 
@@ -1074,9 +1159,9 @@ public class BaseFrameworkSystem implements FrameworkInterface {
        }
 
        /**
-        * Some "getter" for a value from given Storeable instance and target class
+        * Some "getter" for a value from given Storable instance and target class
         *
-        * @param instance An instance of a Storeable class
+        * @param instance An instance of a Storable class
         * @param targetClass The target class (where the field resides)
         * @param columnName Column name (= field name)
         * @return  value Value to get
@@ -1084,7 +1169,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         * @throws java.lang.IllegalAccessException If the getter cannot be accessed
         * @throws java.lang.reflect.InvocationTargetException Some other problems?
         */
-       protected Object getValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+       protected Object getValueInStorableFromColumn (final Storable instance, final String targetClass, final String columnName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
                // Trace message
                this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
 
@@ -1134,27 +1219,52 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                // Init field tye
                Class<?> type = null;
 
+               // Get super class fields
+               Field[] superFields = this.getClassFromTarget(instance, "BaseFrameworkSystem").getDeclaredFields(); //NOI18N
+
                // Get all attributes from given instance
-               Field[] fields = this.getClassFromTarget(instance, targetClass).getDeclaredFields();
+               Field[] fields = ArrayUtils.addAll(superFields, this.getClassFromTarget(instance, targetClass).getDeclaredFields());
 
                // Debug message
-               this.getLogger().debug("fields()=" + fields.length); //NOI18N
+               this.getLogger().debug(MessageFormat.format("fields()={0}", fields.length)); //NOI18N
+
+               // Convert column_name to fieldName ;-)
+               String fieldName = this.convertColumnNameToFieldName(columnName);
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("fieldName={0}", fieldName)); //NOI18N
 
                // Search for proper field instance
                for (final Field field : fields) {
+                       // Is a dollar character there?
+                       if (field.getName().startsWith("$")) { //NOI18N
+                               // Debug message
+                               this.getLogger().debug(MessageFormat.format("Field name {0} starts with a dollar, skipped.", field.getName())); //NOI18N
+                               // Skip this
+                               continue;
+                       }
+
                        // Debug message
-                       this.getLogger().debug("field=" + field); //NOI18N
+                       this.getLogger().debug(MessageFormat.format("field.getName()={0},fieldName={1}", field.getName(), fieldName)); //NOI18N
 
                        // Does it match?
-                       if (field.getName().equals(columnName)) {
+                       if (field.getName().equals(fieldName)) {
                                // Found it
                                type = field.getType();
+
+                               // Debug message
+                               this.getLogger().debug(MessageFormat.format("Found fieldName={0}: setting type={1}", fieldName, type.getSimpleName())); //NOI18N
+
+                               // Don't continue with searching
                                break;
                        }
                }
 
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("type={0}", type)); //NOI18N
+
                // type should not be null
-               if (type == null) {
+               if (null == type) {
                        // No null allowed
                        throw new NullPointerException("type is null"); //NOI18N
                }