From: Roland Haeder Date: Fri, 14 Aug 2015 19:18:47 +0000 (+0200) Subject: Continued with jcore: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=686dfbe21ae67f084f124cdf20fef0e60e3314c2;p=jcore.git Continued with jcore: - Added convertColumnNameToSetterMethod() - Added setBooleanField() - Added setField() - Added setValueInStoreableFromColumn() - Added getValueInStoreableFromColumn() - Added missing trace message Signed-off-by:Roland Häder --- diff --git a/src/org/mxchange/jcore/BaseFrameworkSystem.java b/src/org/mxchange/jcore/BaseFrameworkSystem.java index 06d502f..6cc490e 100644 --- a/src/org/mxchange/jcore/BaseFrameworkSystem.java +++ b/src/org/mxchange/jcore/BaseFrameworkSystem.java @@ -422,7 +422,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { */ protected String convertColumnNameToGetterMethod (final String columnName, boolean isBool) { // Trace message - this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N + this.getLogger().trace(MessageFormat.format("columnName={0},isBool={1} - CALLED!", columnName, isBool)); //NOI18N // Then split on "_" StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N @@ -462,6 +462,49 @@ public class BaseFrameworkSystem implements FrameworkInterface { return builder.toString(); } + /** + * Converts a column name like "foo_bar" to a method name like "getFooBar" + * for non-booleans and to "isFooBar" for boolean fields. + * + * @param columnName Column name to convert + * @return Attribute name + */ + protected String convertColumnNameToSetterMethod (final String columnName) { + // Trace message + this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N + + // Then split on "_" + StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N + + // Resulting string + StringBuilder builder = new StringBuilder(tokenizer.countTokens()); + + // Append "set" + builder.append("set"); //NOI18N + + // Walk through all + while (tokenizer.hasMoreTokens()) { + // Get token + String token = tokenizer.nextToken(); + + // Debug message + this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N + + // Make it upper-case + char c = token.charAt(0); + token = String.valueOf(c).toUpperCase() + token.substring(1); + + // Add token + builder.append(token); + } + + // Trace message + this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N + + // Return result + return builder.toString(); + } + /** * Some "getter" for an array from given string and tokenizer * @@ -525,10 +568,38 @@ public class BaseFrameworkSystem implements FrameworkInterface { // Try to get the value by invoking the method value = (Boolean) method.invoke(instance); + // Trace message + this.getLogger().trace("value=" + value + " - EXIT!"); + // Return value return value; } + /** + * Sets boolean field value with given method name by invoking it + * + * @param instance The instance to call + * @param targetClass Target class to look in + * @param methodName Method name to look for + * @param value Boolean value from field + * @throws java.lang.NoSuchMethodException If the method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Some other problems? + */ + protected void setBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName, final Boolean value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + // Trace messahe + this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N + + // Get method instance + Method method = this.getMethodFromName(instance, targetClass, methodName); + + // Try to get the value by invoking the method + method.invoke(instance, value); + + // Trace message + this.getLogger().trace("EXIT!"); + } + /** * Manager instance * @@ -557,10 +628,38 @@ public class BaseFrameworkSystem implements FrameworkInterface { Method method = this.getMethodFromName(instance, targetClass, methodName); // Get value from field - Object object = method.invoke(instance); + Object value = method.invoke(instance); + + // Trace messahe + this.getLogger().trace("value=" + value + " - EXIT!"); // Return value - return object; + return value; + } + + /** + * Sets any field value with given method name by invoking it + * + * @param instance The instance to call + * @param targetClass Target class to look in + * @param methodName Method name to look for + * @param value Any value from field + * @throws java.lang.NoSuchMethodException If the method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Some other problems? + */ + protected void setField (final FrameworkInterface instance, final String targetClass, final String methodName, final String value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + // Trace messahe + this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1},value={2}", targetClass, methodName, value)); //NOI18N + + // Get method to call + Method method = this.getMethodFromName(instance, targetClass, methodName); + + // Get value from field + method.invoke(instance, value); + + // Trace messahe + this.getLogger().trace("EXIT!"); } /** @@ -862,4 +961,93 @@ public class BaseFrameworkSystem implements FrameworkInterface { // Return it return names; } + + /** + * Some "setter" for a value in given Storeable instance and target class + * + * @param instance An instance of a Storeable class + * @param targetClass The target class (where the field resides) + * @param columnName Column name (= field name) + * @param value Value to set + * @throws java.lang.NoSuchMethodException If the setter is not found + * @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 String value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + // Trace message + this.getLogger().trace("instance=" + instance + ",targetClass=" + targetClass + ",columnName=" + columnName + ",value=" + value + " - CALLED!"); + + // A '$' means not our field + if (columnName.startsWith("$")) { + // Don't handle these + throw new IllegalArgumentException("columnsName contains $"); + } + + // Determine if the given column is boolean + if (this.isBooleanField(instance, targetClass, columnName)) { + // Debug message + this.getLogger().debug("Column " + columnName + " represents a boolean field."); + + // Yes, then call other method + this.setBooleanField(instance, targetClass, this.convertColumnNameToSetterMethod(columnName), Boolean.parseBoolean(value)); + } + + // Convert column name to field name + String methodName = this.convertColumnNameToSetterMethod(columnName); + + // Debug message + this.getLogger().debug(MessageFormat.format("methodName={0}", methodName)); + + // Get field + this.setField(instance, targetClass, methodName, value); + + // Trace message + this.getLogger().trace("EXIT!"); + } + + /** + * Some "getter" for a value from given Storeable instance and target class + * + * @param instance An instance of a Storeable class + * @param targetClass The target class (where the field resides) + * @param columnName Column name (= field name) + * @return value Value to get + * @throws java.lang.NoSuchMethodException If the getter was not found + * @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 { + // Trace message + this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); + + // A '$' means not our field + if (columnName.startsWith("$")) { + // Don't handle these + throw new IllegalArgumentException("columnsName contains $"); + } + + // Determine if the given column is boolean + if (this.isBooleanField(instance, targetClass, columnName)) { + // Debug message + this.getLogger().debug("Column " + columnName + " represents a boolean field."); + + // Yes, then call other method + return this.getBooleanField(instance, targetClass, this.convertColumnNameToGetterMethod(columnName, true)); + } + + // Convert column name to field name + String methodName = this.convertColumnNameToGetterMethod(columnName, false); + + // Debug message + this.getLogger().debug(MessageFormat.format("methodName={0}", methodName)); + + // Get field + Object value = this.getField(instance, targetClass, methodName); + + // Trace message + this.getLogger().trace("value=" + value + " - EXIT!"); + + // Return value + return value; + } } diff --git a/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java b/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java index 6b2cc3a..57d15ba 100644 --- a/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java +++ b/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java @@ -17,6 +17,7 @@ package org.mxchange.jcore.database.frontend; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; @@ -69,6 +70,10 @@ public interface DatabaseFrontend extends FrameworkInterface { * * @param map Map instance to convert to Storeable * @return An instance of a Storeable implementation + * @throws IllegalArgumentException Some implementations may throw this + * @throws java.lang.NoSuchMethodException If the invoked method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Any other problems? */ - public Storeable toStoreable (final Map map); + public Storeable toStoreable (final Map map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException; } diff --git a/src/org/mxchange/jcore/database/storage/Storeable.java b/src/org/mxchange/jcore/database/storage/Storeable.java index 71b289f..42d7b43 100644 --- a/src/org/mxchange/jcore/database/storage/Storeable.java +++ b/src/org/mxchange/jcore/database/storage/Storeable.java @@ -44,6 +44,10 @@ public interface Storeable extends FrameworkInterface { * * @param columnName Column name * @param value Value to set in object's field + * @throws IllegalArgumentException Some implementations may throw this + * @throws java.lang.NoSuchMethodException If the invoked method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Any other problems? */ - public void setValueFromColumn (final String columnName, final String value); + public void setValueFromColumn (final String columnName, final String value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException; }