2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jcore;
19 import java.io.BufferedReader;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.PrintWriter;
25 import java.lang.reflect.Field;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.text.MessageFormat;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.Iterator;
33 import java.util.Properties;
34 import java.util.ResourceBundle;
35 import java.util.StringTokenizer;
36 import java.util.logging.Level;
37 import org.apache.logging.log4j.LogManager;
38 import org.apache.logging.log4j.Logger;
39 import org.mxchange.jcore.application.Application;
40 import org.mxchange.jcore.client.Client;
41 import org.mxchange.jcore.contact.Contact;
42 import org.mxchange.jcore.database.backend.DatabaseBackend;
43 import org.mxchange.jcore.database.frontend.DatabaseFrontend;
44 import org.mxchange.jcore.database.storage.Storeable;
45 import org.mxchange.jcore.manager.Manageable;
50 * @author Roland Haeder
52 public class BaseFrameworkSystem implements FrameworkInterface {
57 private static ResourceBundle bundle;
60 * Instance for own properties
62 private static final Properties properties = new Properties(System.getProperties());
67 private static FrameworkInterface selfInstance;
72 private final Logger LOG;
75 * Application instance
77 private Application application;
80 * Instance for database backend
82 private DatabaseBackend backend;
87 private Client client;
92 private Contact contact;
97 private Manageable manager;
100 * Name of used database table, handled over to backend
102 private String tableName;
105 * DatabaseFrontend instance
107 private DatabaseFrontend wrapper;
113 LOG = LogManager.getLogger(this);
117 * No instances can be created of this class
119 protected BaseFrameworkSystem () {
121 this.setSelfInstance();
125 * Getter for this application
127 * @return Instance from this application
129 public static final FrameworkInterface getInstance () {
135 * Application instance
137 * @return the application
140 public final Application getApplication () {
141 return this.application;
150 public final Logger getLogger () {
157 * @return the contactManager
160 public final Manageable getManager () {
165 * Getter for human-readable string from given key
167 * @param key Key to return
168 * @return Human-readable message
171 public final String getMessageStringFromKey (final String key) {
173 return this.getBundle().getString(key);
177 * Some "getter" for target class instance from given name.
179 * @param instance Instance to iterate on
180 * @param targetClass Class name to look for
181 * @return Class instance
183 @SuppressWarnings("unchecked")
184 private Class<? extends FrameworkInterface> getClassFromTarget (final FrameworkInterface instance, final String targetClass) {
186 this.getLogger().debug(MessageFormat.format("instance={0},targetClass={1}", instance, targetClass)); //NOI18N
188 // Instance reflaction of this class
189 Class<? extends FrameworkInterface> c = instance.getClass();
192 while (!targetClass.equals(c.getSimpleName())) {
194 this.getLogger().debug(MessageFormat.format("c={0}", c.getSimpleName())); //NOI18N
196 // Get super class (causes unchecked warning)
197 c = (Class<? extends FrameworkInterface>) c.getSuperclass();
201 this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c)); //NOI18N
208 * Some "getter" for a Method instance from given method name
210 * @param instance Actual instance to call
211 * @param targetClass Target class name
212 * @param methodName Method name
213 * @return A Method instance
215 private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException {
217 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
219 // Get target class instance
220 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
222 // Init field instance
223 Method method = c.getDeclaredMethod(methodName, new Class<?>[0]);
226 assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
229 this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
236 * Some "getter" for a Method instance from given method name
238 * @param instance Actual instance to call
239 * @param targetClass Target class name
240 * @param methodName Method name
241 * @param type Type reflection to check type from
242 * @return A Method instance
244 private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName, final Class<?> type) throws NoSuchMethodException {
246 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1},type={2}", targetClass, methodName, type)); //NOI18N
248 // Get target class instance
249 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
251 // Init field instance
252 Method method = c.getDeclaredMethod(methodName, type);
255 assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
258 this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
265 * Setter for self instance
267 private void setSelfInstance () {
268 // Need to set it here
273 * Aborts program with given exception
275 * @param throwable Any type of Throwable
277 protected final void abortProgramWithException (final Throwable throwable) {
279 this.getLogger().catching(throwable);
286 * Application instance
288 * @param application the application to set
290 protected final void setApplication (final Application application) {
291 this.application = application;
300 public final Client getClient () {
305 * Getter for bundle instance
307 * @return Resource bundle
309 protected final ResourceBundle getBundle () {
310 return BaseFrameworkSystem.bundle;
316 * @param client the client to set
318 protected final void setClient (final Client client) {
319 this.client = client;
323 * Name of used database table, handled over to backend
325 * @return the tableName
327 public final String getTableName () {
328 return this.tableName;
332 * Checks if given boolean field is available and set to same value
334 * @param columnName Column name to check
335 * @param bool Boolean value
336 * @return Whether all conditions are met
337 * @throws NoSuchMethodException May be thrown by some implementations
338 * @throws java.lang.IllegalAccessException If the method cannot be accessed
339 * @throws java.lang.reflect.InvocationTargetException Any other problems?
342 public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
344 throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0},bool={1}", columnName, bool)); //NOI18N
350 * @param exception Exception to log
353 public final void logException (final Throwable exception) {
354 // Log this exception
355 this.getLogger().catching(exception);
359 * Initializes properties with default values
361 private void initPropertiesWithDefault () {
363 this.getLogger().trace("CALLED!"); //NOI18N
365 // Init default values:
366 // Default database backend
367 BaseFrameworkSystem.properties.put("org.mxchange.database.backend.class", "org.mxchange.jcore.database.backend.base64.Base64CsvDatabaseBackend"); //NOI18N
368 BaseFrameworkSystem.properties.put("database.backend.storagepath", "data/"); //NOI18N
371 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.host", "localhost"); //NOI18N
372 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.dbname", "test"); //NOI18N
373 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.login", ""); //NOI18N
374 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.password", ""); //NOI18N
377 this.getLogger().trace("EXIT!"); //NOI18N
381 * Writes the properties file to disk
383 private void writePropertiesFile () throws IOException {
385 this.getLogger().trace("CALLED!"); //NOI18N
388 BaseFrameworkSystem.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); //NOI18N
391 this.getLogger().trace("EXIT!"); //NOI18N
395 * Converts a column name like "foo_bar" to an attribute name like "fooBar"
397 * @param columnName Column name to convert
398 * @return Attribute name
400 protected String convertColumnNameToAttribute (final String columnName) {
402 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
404 // First all lower case
405 String lower = columnName.toLowerCase();
408 StringTokenizer tokenizer = new StringTokenizer(lower, "_"); //NOI18N
411 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
417 while (tokenizer.hasMoreTokens()) {
419 String token = tokenizer.nextToken();
421 // Is later than first element?
423 // Make first character upper-case
424 char c = token.charAt(0);
425 token = String.valueOf(c).toUpperCase() + token.substring(1);
429 builder.append(token);
436 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
439 return builder.toString();
443 * Converts a column name like "foo_bar" to a method name like "getFooBar"
444 * for non-booleans and to "isFooBar" for boolean fields.
446 * @param columnName Column name to convert
447 * @param isBool Whether the parameter is boolean
448 * @return Attribute name
450 protected String convertColumnNameToGetterMethod (final String columnName, boolean isBool) {
452 this.getLogger().trace(MessageFormat.format("columnName={0},isBool={1} - CALLED!", columnName, isBool)); //NOI18N
455 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
458 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
463 builder.append("is"); //NOI18N
466 builder.append("get"); //NOI18N
470 while (tokenizer.hasMoreTokens()) {
472 String token = tokenizer.nextToken();
475 this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N
477 // Make it upper-case
478 char c = token.charAt(0);
479 token = String.valueOf(c).toUpperCase() + token.substring(1);
482 builder.append(token);
486 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
489 return builder.toString();
493 * Converts a column name like "foo_bar" to a method name like "getFooBar"
494 * for non-booleans and to "isFooBar" for boolean fields.
496 * @param columnName Column name to convert
497 * @return Attribute name
499 protected String convertColumnNameToSetterMethod (final String columnName) {
501 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
504 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
507 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
510 builder.append("set"); //NOI18N
513 while (tokenizer.hasMoreTokens()) {
515 String token = tokenizer.nextToken();
518 this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N
520 // Make it upper-case
521 char c = token.charAt(0);
522 token = String.valueOf(c).toUpperCase() + token.substring(1);
525 builder.append(token);
529 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
532 return builder.toString();
536 * Some "getter" for an array from given string and tokenizer
538 * @param str String to tokenize and get array from
539 * @param delimiter Delimiter
540 * @param size Size of array
541 * @return Array from tokenized string
542 * @todo Get rid of size parameter
544 protected String[] getArrayFromString (final String str, final String delimiter, final int size) {
546 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1},size={2} - CALLED!", str, delimiter, size)); //NOI18N
549 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
551 // Init array and index
552 String[] tokens = new String[size];
555 // Run through all tokens
556 while (tokenizer.hasMoreTokens()) {
557 // Get current token and add it
558 tokens[index] = tokenizer.nextToken();
561 this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
568 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
575 * Returns boolean field value from given method name by invoking it
577 * @param instance The instance to call
578 * @param targetClass Target class to look in
579 * @param methodName Method name to look for
580 * @return Boolean value from field
581 * @throws java.lang.NoSuchMethodException If the method was not found
582 * @throws java.lang.IllegalAccessException If the method cannot be accessed
583 * @throws java.lang.reflect.InvocationTargetException Some other problems?
585 protected boolean getBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
587 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
589 // Get method instance
590 Method method = this.getMethodFromName(instance, targetClass, methodName);
592 // Get value from field
593 Boolean value = (Boolean) method.invoke(instance);
596 this.getLogger().trace("value=" + value + " - EXIT!");
603 * Sets boolean field value with given method name by invoking it
605 * @param instance The instance to call
606 * @param targetClass Target class to look in
607 * @param methodName Method name to look for
608 * @param columnName Column name
609 * @param value Boolean value from field
610 * @throws java.lang.NoSuchMethodException If the method was not found
611 * @throws java.lang.IllegalAccessException If the method cannot be accessed
612 * @throws java.lang.reflect.InvocationTargetException Some other problems?
614 protected void setBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Boolean value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
616 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
619 Class<?> type = this.getType(instance, targetClass, columnName);
621 // Get method instance
622 Method method = this.getMethodFromName(instance, targetClass, methodName, type);
624 // Try to get the value by invoking the method
625 method.invoke(instance, value);
628 this.getLogger().trace("EXIT!");
634 * @param manager the manager instance to set
636 protected final void setContactManager (final Manageable manager) {
637 this.manager = manager;
641 * Returns any field value from given method name by invoking it
643 * @param instance The instance to call
644 * @param targetClass Target class to look in
645 * @param methodName Method name to look for
646 * @return Any value from field
647 * @throws java.lang.NoSuchMethodException If the method was not found
648 * @throws java.lang.IllegalAccessException If the method cannot be accessed
649 * @throws java.lang.reflect.InvocationTargetException Some other problems?
651 protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
653 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2}", instance, targetClass, methodName)); //NOI18N
655 // Get method to call
656 Method method = this.getMethodFromName(instance, targetClass, methodName);
659 this.getLogger().debug("method=" + method + ",instance=" + instance);
661 // Get value from field
662 Object value = method.invoke(instance);
665 this.getLogger().trace("value=" + value + " - EXIT!");
672 * Sets any field value with given method name by invoking it
674 * @param instance The instance to call
675 * @param targetClass Target class to look in
676 * @param methodName Method name to look for
677 * @param columnName Column name
678 * @param value Any value from field
679 * @throws java.lang.NoSuchMethodException If the method was not found
680 * @throws java.lang.IllegalAccessException If the method cannot be accessed
681 * @throws java.lang.reflect.InvocationTargetException Some other problems?
683 protected void setField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
685 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2},value={3}", instance, targetClass, methodName, value)); //NOI18N
688 Class<?> type = this.getType(instance, targetClass, columnName);
691 this.getLogger().debug("type=" + type);
694 Object object = value;
696 // Hard-coded "cast" again ... :-(
697 // @TODO Can't we get rid of this???
698 switch (type.getSimpleName()) {
699 case "Long": // Long object
700 object = Long.parseLong((String) value);
703 case "Float": // Float object
704 object = Float.parseFloat((String) value);
707 case "Object": // General object
708 case "String": // String object
711 default: // Unsupported type
712 throw new IllegalArgumentException("value " + value + " has unsupported type " + type.getSimpleName());
716 this.getLogger().debug("object[" + object.getClass().getSimpleName() + "]=" + object);
718 // Get method to call
719 Method method = this.getMethodFromName(instance, targetClass, methodName, type);
722 this.getLogger().debug("method=" + method + ",instance=" + instance + ",value[" + value.getClass().getSimpleName() + "]=" + value);
724 // Get value from field
725 method.invoke(instance, object);
728 this.getLogger().trace("EXIT!");
732 * Getter for property which must exist
734 * @param key Key to get
735 * @return Propety value
737 protected final String getProperty (final String key) {
738 return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.%s", key)); //NOI18N
742 * Name of used database table, handled over to backend
744 * @param tableName the tableName to set
746 protected final void setTableName (final String tableName) {
747 this.tableName = tableName;
751 * Converts null to empty string or leaves original string.
753 * @param str Any string
754 * @return Empty string if null or original string
756 protected Object convertNullToEmpty (final Object str) {
758 this.getLogger().trace(MessageFormat.format("str={0}", str)); //NOI18N
762 // Return empty string
767 this.getLogger().trace(MessageFormat.format("str={0} - EXIT!", str)); //NOI18N
774 * Creates an iterator from given instance and class name.
776 * @param instance Instance to run getter calls on
777 * @param className Class name to iterate over
778 * @return An iterator over all object's fields
779 * @throws java.lang.NoSuchMethodException If the called method does not exist
780 * @throws java.lang.IllegalAccessException If the method cannot be accessed
781 * @throws java.lang.reflect.InvocationTargetException Any other problems?
783 protected Iterator<Map.Entry<Field, Object>> fieldIterator (final Storeable instance, final String className) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
785 this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className)); //NOI18N
787 // Get all attributes from given instance
788 Field[] fields = this.getClassFromTarget(instance, className).getDeclaredFields();
791 this.getLogger().debug(MessageFormat.format("Found {0} fields.", fields.length)); //NOI18N
793 // A simple map with K=fieldName and V=Value is fine
794 Map<Field, Object> map = new HashMap<>(fields.length);
797 for (final Field field : fields) {
799 this.getLogger().debug(MessageFormat.format("field={0}", field.getName())); //NOI18N
801 // Does the field start with "$"?
802 if (field.getName().startsWith("$")) { //NOI18N
804 this.getLogger().debug(MessageFormat.format("Skipping field={0} as it starts with a dollar character.", field.getName())); //NOI18N
811 this.getLogger().debug(MessageFormat.format("Calling getValueFromColumn({0}) on instance {1} ...", field.getName(), instance)); //NOI18N
814 Object value = instance.getValueFromColumn(field.getName());
817 this.getLogger().debug(MessageFormat.format("Adding field={0},value={1}", field.getName(), value)); //NOI18N
820 map.put(field, value);
824 this.getLogger().debug(MessageFormat.format("Returning iterator for {0} entries ...", map.size())); //NOI18N
826 // Return list iterator
827 return map.entrySet().iterator();
831 * Instance for database backend
833 * @return the backend
835 protected final DatabaseBackend getBackend () {
840 * Instance for database backend
842 * @param backend the backend to set
844 protected final void setBackend (final DatabaseBackend backend) {
845 this.backend = backend;
849 * Getter for Contact instance
851 * @return Contact instance
853 protected final Contact getContact () {
858 * Setter for Contact instance
860 * @param contact A Contact instance
862 protected final void setContact (final Contact contact) {
863 this.contact = contact;
867 * Getter for DatabaseFrontend instance
869 * @return DatabaseFrontend instance
871 protected final DatabaseFrontend getFrontend () {
876 * Setter for wrapper instance
878 * @param wrapper A DatabaseFrontend instance
880 protected final void setFrontend (final DatabaseFrontend wrapper) {
881 this.wrapper = wrapper;
885 * Initializes i18n bundles
887 protected void initBundle () {
889 this.getLogger().trace("CALLED!");
891 // Is the bundle set?
892 if (bundle instanceof ResourceBundle) {
894 throw new IllegalStateException("called twice"); //NOI18N
898 bundle = ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE); // NOI18N
901 this.getLogger().trace("EXIT!");
905 * Prepares all properties, the file is written if it is not found
907 * @throws java.io.IOException If any IO problem occurs
909 protected void initProperties () throws IOException {
911 this.getLogger().trace("CALLED!"); //NOI18N
914 this.getLogger().debug(MessageFormat.format("{0} properties are loaded already.", BaseFrameworkSystem.properties.size())); //NOI18N
916 // Are some properties loaded?
917 if (!BaseFrameworkSystem.properties.isEmpty()) {
918 // Some are already loaded, abort here
924 BaseFrameworkSystem.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
927 this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", BaseFrameworkSystem.properties.size())); //NOI18N
928 } catch (final FileNotFoundException ex) {
930 this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N
933 * The file is not found which is normal for first run, so
934 * initialize default values.
936 this.initPropertiesWithDefault();
939 this.writePropertiesFile();
943 this.getLogger().trace("EXIT!"); //NOI18N
947 * Checks whether the given field is a boolean field by probing it.
949 * @param instance Instance to call
950 * @param targetClass Target class
951 * @param columnName Column name to check
952 * @return Whether the given column name represents a boolean field
954 protected boolean isBooleanField (final FrameworkInterface instance, final String targetClass, final String columnName) {
956 this.getLogger().trace(MessageFormat.format("instance={0},targetCLass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
958 // Convert column name to getter name (boolean)
959 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
961 // Get class instance
962 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
964 // Defauzlt is boolean
965 boolean isBool = true;
968 // Now try to instance the method
969 Method method = c.getDeclaredMethod(methodName, new Class<?>[0]);
970 } catch (final NoSuchMethodException ex) {
972 this.getLogger().debug(MessageFormat.format("Method {0} does not exist, field {1} cannot be boolean: {2}", methodName, columnName, ex)); //NOI18N
979 this.getLogger().trace(MessageFormat.format("isBool={0} - EXIT!", isBool)); //NOI18N
986 * Sets value in properties instance.
988 * @param key Property key (part) to set
989 * @param value Property value to set
991 protected void setProperty (final String key, final String value) {
993 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); //NOI18N
995 // Both should not be null
998 throw new NullPointerException("key is null");
999 } else if (value == null) {
1001 throw new NullPointerException("value is null");
1005 properties.setProperty(String.format("org.mxchange.%s", key), value); //NOI18N
1008 this.getLogger().trace("EXIT!"); //NOI18N
1012 * Some getter for properties names (without org.mxchange)
1014 * @return An array with property names
1016 protected String[] getPropertyNames () {
1019 "database.backend.class", //NOI18N
1020 "database.backend.storagepath", //NOI18N
1021 "database.mysql.login", //NOI18N
1022 "database.mysql.host", //NOI18N
1023 "database.mysql.password", //NOI18N
1024 "database.mysql.dbname", //NOI18N
1032 * Some "setter" for a value in given Storeable instance and target class
1034 * @param instance An instance of a Storeable class
1035 * @param targetClass The target class (where the field resides)
1036 * @param columnName Column name (= field name)
1037 * @param value Value to set
1038 * @throws java.lang.NoSuchMethodException If the setter is not found
1039 * @throws java.lang.IllegalAccessException If the setter cannot be accessed
1040 * @throws java.lang.reflect.InvocationTargetException Any other problem?
1042 protected void setValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1044 this.getLogger().trace("instance=" + instance + ",targetClass=" + targetClass + ",columnName=" + columnName + ",value=" + value + " - CALLED!");
1046 // A '$' means not our field
1047 if (columnName.startsWith("$")) {
1048 // Don't handle these
1049 throw new IllegalArgumentException("columnsName contains $");
1052 // Determine if the given column is boolean
1053 if (this.isBooleanField(instance, targetClass, columnName)) {
1055 this.getLogger().debug("Column " + columnName + " represents a boolean field.");
1057 // Yes, then call other method
1058 this.setBooleanField(instance, targetClass, this.convertColumnNameToSetterMethod(columnName), columnName, (Boolean) value);
1061 // Convert column name to field name
1062 String methodName = this.convertColumnNameToSetterMethod(columnName);
1065 this.getLogger().debug(MessageFormat.format("methodName={0}", methodName));
1068 this.setField(instance, targetClass, methodName, columnName, value);
1071 this.getLogger().trace("EXIT!");
1075 * Some "getter" for a value from given Storeable instance and target class
1077 * @param instance An instance of a Storeable class
1078 * @param targetClass The target class (where the field resides)
1079 * @param columnName Column name (= field name)
1080 * @return value Value to get
1081 * @throws java.lang.NoSuchMethodException If the getter was not found
1082 * @throws java.lang.IllegalAccessException If the getter cannot be accessed
1083 * @throws java.lang.reflect.InvocationTargetException Some other problems?
1085 protected Object getValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1087 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2} - CALLED!", instance, targetClass, columnName));
1089 // A '$' means not our field
1090 if (columnName.startsWith("$")) {
1091 // Don't handle these
1092 throw new IllegalArgumentException("columnsName contains $");
1095 // Determine if the given column is boolean
1096 if (this.isBooleanField(instance, targetClass, columnName)) {
1098 this.getLogger().debug("Column " + columnName + " represents a boolean field.");
1100 // Yes, then call other method
1101 return this.getBooleanField(instance, targetClass, this.convertColumnNameToGetterMethod(columnName, true));
1104 // Convert column name to field name
1105 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
1108 this.getLogger().debug(MessageFormat.format("methodName={0}", methodName));
1111 Object value = this.getField(instance, targetClass, methodName);
1114 this.getLogger().trace("value=" + value + " - EXIT!");
1121 * Some getter for type reflection of given column name
1123 * @param instance The instance to check
1124 * @param targetClass Target class to check
1125 * @param columnName Column name
1126 * @return Type reflection of value
1128 private Class<?> getType (final FrameworkInterface instance, final String targetClass, final String columnName) {
1130 this.getLogger().trace("instance=" + instance + ",targetClass=" + targetClass + ",columnName=" + columnName + " - CALLED!");
1133 Class<?> type = null;
1135 // Get all attributes from given instance
1136 Field[] fields = this.getClassFromTarget(instance, targetClass).getDeclaredFields();
1139 this.getLogger().debug("fields()=" + fields.length);
1141 // Search for proper field instance
1142 for (final Field field : fields) {
1144 this.getLogger().debug("field=" + field);
1147 if (field.getName().equals(columnName)) {
1149 type = field.getType();
1154 // type should not be null
1157 throw new NullPointerException("type is null");
1161 this.getLogger().debug("type=" + type + " - EXIT!");