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 org.apache.logging.log4j.LogManager;
37 import org.apache.logging.log4j.Logger;
38 import org.mxchange.jcore.application.Application;
39 import org.mxchange.jcore.client.Client;
40 import org.mxchange.jcore.contact.Contact;
41 import org.mxchange.jcore.database.backend.DatabaseBackend;
42 import org.mxchange.jcore.database.frontend.DatabaseFrontend;
43 import org.mxchange.jcore.database.storage.Storeable;
44 import org.mxchange.jcore.manager.Manageable;
49 * @author Roland Haeder
51 public class BaseFrameworkSystem implements FrameworkInterface {
56 private static ResourceBundle bundle;
59 * Instance for own properties
61 private static final Properties properties = new Properties(System.getProperties());
66 private static FrameworkInterface selfInstance;
71 private final Logger LOG;
74 * Application instance
76 private Application application;
79 * Instance for database backend
81 private DatabaseBackend backend;
86 private Client client;
91 private Contact contact;
96 private Manageable manager;
99 * Name of used database table, handled over to backend
101 private String tableName;
104 * DatabaseFrontend instance
106 private DatabaseFrontend frontend;
109 * Session id assigned with this basket, or any other unique identifier
111 private String sessionId;
117 LOG = LogManager.getLogger(this);
121 * No instances can be created of this class
123 protected BaseFrameworkSystem () {
125 this.setSelfInstance();
129 * Getter for this application
131 * @return Instance from this application
133 public static final FrameworkInterface getInstance () {
139 * Application instance
141 * @return the application
144 public final Application getApplication () {
145 return this.application;
154 public final Logger getLogger () {
161 * @return the contactManager
164 public final Manageable getManager () {
169 * Getter for human-readable string from given key
171 * @param key Key to return
172 * @return Human-readable message
175 public final String getMessageStringFromKey (final String key) {
177 return this.getBundle().getString(key);
181 * Some "getter" for target class instance from given name.
183 * @param instance Instance to iterate on
184 * @param targetClass Class name to look for
185 * @return Class instance
187 @SuppressWarnings("unchecked")
188 private Class<? extends FrameworkInterface> getClassFromTarget (final FrameworkInterface instance, final String targetClass) {
190 this.getLogger().debug(MessageFormat.format("instance={0},targetClass={1}", instance, targetClass)); //NOI18N
192 // Instance reflaction of this class
193 Class<? extends FrameworkInterface> c = instance.getClass();
196 while (!targetClass.equals(c.getSimpleName())) {
198 this.getLogger().debug(MessageFormat.format("c={0}", c.getSimpleName())); //NOI18N
200 // Get super class (causes unchecked warning)
201 c = (Class<? extends FrameworkInterface>) c.getSuperclass();
205 this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c)); //NOI18N
212 * Some "getter" for a Method instance from given method name
214 * @param instance Actual instance to call
215 * @param targetClass Target class name
216 * @param methodName Method name
217 * @return A Method instance
219 private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException {
221 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
223 // Get target class instance
224 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
226 // Init field instance
227 Method method = c.getDeclaredMethod(methodName, new Class<?>[0]);
230 assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
233 this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
240 * Some "getter" for a Method instance from given method name
242 * @param instance Actual instance to call
243 * @param targetClass Target class name
244 * @param methodName Method name
245 * @param type Type reflection to check type from
246 * @return A Method instance
248 private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName, final Class<?> type) throws NoSuchMethodException {
250 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1},type={2}", targetClass, methodName, type)); //NOI18N
252 // Get target class instance
253 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
255 // Init field instance
256 Method method = c.getDeclaredMethod(methodName, type);
259 assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
262 this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
269 * Setter for self instance
271 private void setSelfInstance () {
272 // Need to set it here
277 * Aborts program with given exception
279 * @param throwable Any type of Throwable
281 protected final void abortProgramWithException (final Throwable throwable) {
283 this.logException(throwable);
290 * Application instance
292 * @param application the application to set
294 protected final void setApplication (final Application application) {
295 this.application = application;
304 public final Client getClient () {
309 * Getter for bundle instance
311 * @return Resource bundle
313 protected final ResourceBundle getBundle () {
314 return BaseFrameworkSystem.bundle;
318 * Setter for bundle instance
320 * @param bundle the bundle to set
322 protected static void setBundle (final ResourceBundle bundle) {
323 BaseFrameworkSystem.bundle = bundle;
329 * @param client the client to set
331 protected final void setClient (final Client client) {
332 this.client = client;
336 * Name of used database table, handled over to backend
338 * @return the tableName
340 public final String getTableName () {
341 return this.tableName;
345 * Checks if given boolean field is available and set to same value
347 * @param columnName Column name to check
348 * @param bool Boolean value
349 * @return Whether all conditions are met
350 * @throws NoSuchMethodException May be thrown by some implementations
351 * @throws java.lang.IllegalAccessException If the method cannot be accessed
352 * @throws java.lang.reflect.InvocationTargetException Any other problems?
355 public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
357 throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0},bool={1}", columnName, bool)); //NOI18N
363 * @param exception Exception to log
366 public final void logException (final Throwable exception) {
367 // Log this exception
368 this.getLogger().catching(exception);
372 * Initializes properties with default values
374 private void initPropertiesWithDefault () {
376 this.getLogger().trace("CALLED!"); //NOI18N
378 // Init default values:
379 // Default database backend
380 BaseFrameworkSystem.properties.put("org.mxchange.database.backend.class", "org.mxchange.jcore.database.backend.base64.Base64CsvDatabaseBackend"); //NOI18N
381 BaseFrameworkSystem.properties.put("org.mxchange.database.backend.storagepath", "data/"); //NOI18N
382 BaseFrameworkSystem.properties.put("org.mxchange.database.datasource.name", ""); //NOI18N
385 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.host", "localhost"); //NOI18N
386 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.dbname", "test"); //NOI18N
387 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.login", ""); //NOI18N
388 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.password", ""); //NOI18N
391 this.getLogger().trace("EXIT!"); //NOI18N
395 * Writes the properties file to disk
397 private void writePropertiesFile () throws IOException {
399 this.getLogger().trace("CALLED!"); //NOI18N
402 BaseFrameworkSystem.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); //NOI18N
405 this.getLogger().trace("EXIT!"); //NOI18N
409 * Converts a column name like "foo_bar" to an attribute name like "fooBar"
411 * @param columnName Column name to convert
412 * @return Attribute name
414 protected String convertColumnNameToFieldName (final String columnName) {
416 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
418 // First all lower case
419 String lower = columnName.toLowerCase();
422 StringTokenizer tokenizer = new StringTokenizer(lower, "_"); //NOI18N
425 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
431 while (tokenizer.hasMoreTokens()) {
433 String token = tokenizer.nextToken();
435 // Is later than first element?
437 // Make first character upper-case
438 char c = token.charAt(0);
439 token = String.valueOf(c).toUpperCase() + token.substring(1);
443 builder.append(token);
450 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
453 return builder.toString();
457 * Converts a column name like "foo_bar" to a method name like "getFooBar"
458 * for non-booleans and to "isFooBar" for boolean fields.
460 * @param columnName Column name to convert
461 * @param isBool Whether the parameter is boolean
462 * @return Attribute name
464 protected String convertColumnNameToGetterMethod (final String columnName, boolean isBool) {
466 this.getLogger().trace(MessageFormat.format("columnName={0},isBool={1} - CALLED!", columnName, isBool)); //NOI18N
469 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
472 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
477 builder.append("is"); //NOI18N
480 builder.append("get"); //NOI18N
484 while (tokenizer.hasMoreTokens()) {
486 String token = tokenizer.nextToken();
489 this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N
491 // Make it upper-case
492 char c = token.charAt(0);
493 token = String.valueOf(c).toUpperCase() + token.substring(1);
496 builder.append(token);
500 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
503 return builder.toString();
507 * Converts a column name like "foo_bar" to a method name like "getFooBar"
508 * for non-booleans and to "isFooBar" for boolean fields.
510 * @param columnName Column name to convert
511 * @return Attribute name
513 protected String convertColumnNameToSetterMethod (final String columnName) {
515 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
518 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
521 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
524 builder.append("set"); //NOI18N
527 while (tokenizer.hasMoreTokens()) {
529 String token = tokenizer.nextToken();
532 this.getLogger().debug(MessageFormat.format("token={0} - BEFORE", token)); //NOI18N
534 // Make it upper-case
535 char c = token.charAt(0);
536 token = String.valueOf(c).toUpperCase() + token.substring(1);
539 this.getLogger().debug(MessageFormat.format("token={0} - AFTER", token)); //NOI18N
542 builder.append(token);
546 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
549 return builder.toString();
553 * Some "getter" for an array from given string and tokenizer
555 * @param str String to tokenize and get array from
556 * @param delimiter Delimiter
557 * @param size Size of array
558 * @return Array from tokenized string
559 * @todo Get rid of size parameter
561 protected String[] getArrayFromString (final String str, final String delimiter, final int size) {
563 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1},size={2} - CALLED!", str, delimiter, size)); //NOI18N
566 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
568 // Init array and index
569 String[] tokens = new String[size];
572 // Run through all tokens
573 while (tokenizer.hasMoreTokens()) {
574 // Get current token and add it
575 tokens[index] = tokenizer.nextToken();
578 this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
585 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
592 * Returns boolean field value from given method name by invoking it
594 * @param instance The instance to call
595 * @param targetClass Target class to look in
596 * @param methodName Method name to look for
597 * @return Boolean value from field
598 * @throws java.lang.NoSuchMethodException If the method was not found
599 * @throws java.lang.IllegalAccessException If the method cannot be accessed
600 * @throws java.lang.reflect.InvocationTargetException Some other problems?
602 protected boolean getBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
604 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
606 // Get method instance
607 Method method = this.getMethodFromName(instance, targetClass, methodName);
609 // Get value from field
610 Boolean value = (Boolean) method.invoke(instance);
613 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
620 * Sets boolean field value with given method name by invoking it
622 * @param instance The instance to call
623 * @param targetClass Target class to look in
624 * @param methodName Method name to look for
625 * @param columnName Column name
626 * @param value Boolean value from field
627 * @throws java.lang.NoSuchMethodException If the method was not found
628 * @throws java.lang.IllegalAccessException If the method cannot be accessed
629 * @throws java.lang.reflect.InvocationTargetException Some other problems?
631 protected void setBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Boolean value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
633 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
636 Class<?> type = this.getType(instance, targetClass, columnName);
638 // Get method instance
639 Method method = this.getMethodFromName(instance, targetClass, methodName, type);
641 // Try to get the value by invoking the method
642 method.invoke(instance, value);
645 this.getLogger().trace("EXIT!"); //NOI18N
651 * @param manager the manager instance to set
653 protected final void setContactManager (final Manageable manager) {
654 this.manager = manager;
658 * Returns any field value from given method name by invoking it
660 * @param instance The instance to call
661 * @param targetClass Target class to look in
662 * @param methodName Method name to look for
663 * @return Any value from field
664 * @throws java.lang.NoSuchMethodException If the method was not found
665 * @throws java.lang.IllegalAccessException If the method cannot be accessed
666 * @throws java.lang.reflect.InvocationTargetException Some other problems?
668 protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
670 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2}", instance, targetClass, methodName)); //NOI18N
672 // Get method to call
673 Method method = this.getMethodFromName(instance, targetClass, methodName);
676 this.getLogger().debug(MessageFormat.format("method={0},instance={1}", method, instance)); //NOI18N
678 // Get value from field
679 Object value = method.invoke(instance);
682 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
689 * Sets any field value with given method name by invoking it
691 * @param instance The instance to call
692 * @param targetClass Target class to look in
693 * @param methodName Method name to look for
694 * @param columnName Column name
695 * @param value Any value from field
696 * @throws java.lang.NoSuchMethodException If the method was not found
697 * @throws java.lang.IllegalAccessException If the method cannot be accessed
698 * @throws java.lang.reflect.InvocationTargetException Some other problems?
700 protected void setField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
702 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2},value={3}", instance, targetClass, methodName, value)); //NOI18N
705 Class<?> type = this.getType(instance, targetClass, columnName);
708 this.getLogger().debug(MessageFormat.format("type={0}", type)); //NOI18N
711 Object object = value;
713 // Is the value null?
714 if ("null".equals(value)) { //NOI18N
716 this.getLogger().warn(MessageFormat.format("columnName={0} has null value.", columnName)); //NOI18N
721 // Hard-coded "cast" again ... :-(
722 // @TODO Can't we get rid of this???
723 switch (type.getSimpleName()) {
724 case "Long": // Long object //NOI18N
725 object = Long.parseLong((String) value);
728 case "Float": // Float object //NOI18N
729 object = Float.parseFloat((String) value);
732 case "Boolean": // Boolean object //NOI18N
733 object = Boolean.parseBoolean((String) value);
736 case "String": // String object //NOI18N
739 default: // Unsupported type
740 throw new IllegalArgumentException(MessageFormat.format("value {0} has unsupported type {1}", value, type.getSimpleName())); //NOI18N
745 this.getLogger().debug(MessageFormat.format("object={0}", object)); //NOI18N
747 // Get method to call
748 Method method = this.getMethodFromName(instance, targetClass, methodName, type);
751 this.getLogger().debug(MessageFormat.format("method={0},instance={1},value[{2}]={3}", method, instance, value.getClass().getSimpleName(), value)); //NOI18N
753 // Get value from field
754 method.invoke(instance, object);
757 this.getLogger().trace("EXIT!"); //NOI18N
761 * Getter for property which must exist
763 * @param key Key to get
764 * @return Propety value
766 protected final String getProperty (final String key) {
767 return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.%s", key)); //NOI18N
771 * Name of used database table, handled over to backend
773 * @param tableName the tableName to set
775 protected final void setTableName (final String tableName) {
776 this.tableName = tableName;
780 * Converts null to empty string or leaves original string.
782 * @param str Any string
783 * @return Empty string if null or original string
785 protected Object convertNullToEmpty (final Object str) {
787 this.getLogger().trace(MessageFormat.format("str={0}", str)); //NOI18N
791 // Return empty string
796 this.getLogger().trace(MessageFormat.format("str={0} - EXIT!", str)); //NOI18N
803 * Creates an iterator from given instance and class name.
805 * @param instance Instance to run getter calls on
806 * @param className Class name to iterate over
807 * @return An iterator over all object's fields
808 * @throws java.lang.NoSuchMethodException If the called method does not exist
809 * @throws java.lang.IllegalAccessException If the method cannot be accessed
810 * @throws java.lang.reflect.InvocationTargetException Any other problems?
812 protected Iterator<Map.Entry<Field, Object>> fieldIterator (final Storeable instance, final String className) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
814 this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className)); //NOI18N
816 // Get all attributes from given instance
817 Field[] fields = this.getClassFromTarget(instance, className).getDeclaredFields();
820 this.getLogger().debug(MessageFormat.format("Found {0} fields.", fields.length)); //NOI18N
822 // A simple map with K=fieldName and V=Value is fine
823 Map<Field, Object> map = new HashMap<>(fields.length);
826 for (final Field field : fields) {
828 this.getLogger().debug(MessageFormat.format("field={0}", field.getName())); //NOI18N
830 // Does the field start with "$"?
831 if (field.getName().startsWith("$")) { //NOI18N
833 this.getLogger().debug(MessageFormat.format("Skipping field={0} as it starts with a dollar character.", field.getName())); //NOI18N
840 this.getLogger().debug(MessageFormat.format("Calling getValueFromColumn({0}) on instance {1} ...", field.getName(), instance)); //NOI18N
843 Object value = instance.getValueFromColumn(field.getName());
846 this.getLogger().debug(MessageFormat.format("Adding field={0},value={1}", field.getName(), value)); //NOI18N
849 map.put(field, value);
853 this.getLogger().debug(MessageFormat.format("Returning iterator for {0} entries ...", map.size())); //NOI18N
855 // Return list iterator
856 return map.entrySet().iterator();
860 * Instance for database backend
862 * @return the backend
864 protected final DatabaseBackend getBackend () {
869 * Instance for database backend
871 * @param backend the backend to set
873 protected final void setBackend (final DatabaseBackend backend) {
874 this.backend = backend;
878 * Getter for Contact instance
880 * @return Contact instance
882 protected final Contact getContact () {
887 * Setter for Contact instance
889 * @param contact A Contact instance
891 protected final void setContact (final Contact contact) {
892 this.contact = contact;
896 * Getter for DatabaseFrontend instance
898 * @return DatabaseFrontend instance
900 protected final DatabaseFrontend getFrontend () {
901 return this.frontend;
905 * Setter for wrapper instance
907 * @param frontend A DatabaseFrontend instance
909 protected final void setFrontend (final DatabaseFrontend frontend) {
910 this.frontend = frontend;
914 * Setter for session id
916 * @param sessionId Session id
919 public final void setSessionId (final String sessionId) {
920 this.sessionId = sessionId;
924 * Getter for session id
929 public final String getSessionId () {
930 return this.sessionId;
934 * Checks if the bundle is initialized
936 * @return Whether the bundle has been initialized
938 protected boolean isBundledInitialized () {
940 return (bundle instanceof ResourceBundle);
944 * Initializes i18n bundles
946 protected void initBundle () {
948 this.getLogger().trace("CALLED!"); //NOI18N
950 // Is the bundle set?
951 if (this.isBundledInitialized()) {
953 throw new IllegalStateException("called twice"); //NOI18N
957 setBundle(ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE)); // NOI18N
960 this.getLogger().trace("EXIT!"); //NOI18N
964 * Prepares all properties, the file is written if it is not found
966 * @throws java.io.IOException If any IO problem occurs
968 protected void initProperties () throws IOException {
970 this.getLogger().trace("CALLED!"); //NOI18N
973 this.getLogger().debug(MessageFormat.format("{0} properties are loaded already.", BaseFrameworkSystem.properties.size())); //NOI18N
975 // Are some properties loaded?
976 if (!BaseFrameworkSystem.properties.isEmpty()) {
977 // Some are already loaded, abort here
983 BaseFrameworkSystem.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
986 this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", BaseFrameworkSystem.properties.size())); //NOI18N
987 } catch (final FileNotFoundException ex) {
989 this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N
992 * The file is not found which is normal for first run, so
993 * initialize default values.
995 this.initPropertiesWithDefault();
998 this.writePropertiesFile();
1002 this.getLogger().trace("EXIT!"); //NOI18N
1006 * Checks whether the given field is a boolean field by probing it.
1008 * @param instance Instance to call
1009 * @param targetClass Target class
1010 * @param columnName Column name to check
1011 * @return Whether the given column name represents a boolean field
1013 protected boolean isBooleanField (final FrameworkInterface instance, final String targetClass, final String columnName) {
1015 this.getLogger().trace(MessageFormat.format("instance={0},targetCLass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
1017 // Convert column name to getter name (boolean)
1018 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
1020 // Get class instance
1021 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
1023 // Defauzlt is boolean
1024 boolean isBool = true;
1027 // Now try to instance the method
1028 Method method = c.getDeclaredMethod(methodName, new Class<?>[0]);
1029 } catch (final NoSuchMethodException ex) {
1031 this.getLogger().debug(MessageFormat.format("Method {0} does not exist, field {1} cannot be boolean: {2}", methodName, columnName, ex)); //NOI18N
1038 this.getLogger().trace(MessageFormat.format("isBool={0} - EXIT!", isBool)); //NOI18N
1045 * Sets value in properties instance.
1047 * @param key Property key (part) to set
1048 * @param value Property value to set
1050 protected void setProperty (final String key, final String value) {
1052 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); //NOI18N
1054 // Both should not be null
1057 throw new NullPointerException("key is null"); //NOI18N
1058 } else if (null == value) {
1060 throw new NullPointerException("value is null"); //NOI18N
1064 properties.setProperty(String.format("org.mxchange.%s", key), value); //NOI18N
1067 this.getLogger().trace("EXIT!"); //NOI18N
1071 * Some getter for properties names (without org.mxchange)
1073 * @return An array with property names
1075 protected String[] getPropertyNames () {
1078 "database.backend.class", //NOI18N
1079 "database.backend.storagepath", //NOI18N
1080 "database.mysql.login", //NOI18N
1081 "database.mysql.host", //NOI18N
1082 "database.mysql.password", //NOI18N
1083 "database.mysql.dbname", //NOI18N
1091 * Some "setter" for a value in given Storeable instance and target class
1093 * @param instance An instance of a Storeable class
1094 * @param targetClass The target class (where the field resides)
1095 * @param columnName Column name (= field name)
1096 * @param value Value to set
1097 * @throws java.lang.NoSuchMethodException If the setter is not found
1098 * @throws java.lang.IllegalAccessException If the setter cannot be accessed
1099 * @throws java.lang.reflect.InvocationTargetException Any other problem?
1101 protected void setValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1103 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2},value={3} - CALLED!", instance, targetClass, columnName, value)); //NOI18N
1105 // A '$' means not our field
1106 if (columnName.startsWith("$")) { //NOI18N
1107 // Don't handle these
1108 throw new IllegalArgumentException("columnsName contains $"); //NOI18N
1111 // Determine if the given column is boolean
1112 if (this.isBooleanField(instance, targetClass, columnName)) {
1114 this.getLogger().debug(MessageFormat.format("Column {0} represents a boolean field.", columnName)); //NOI18N
1116 // Yes, then call other method
1117 this.setBooleanField(instance, targetClass, this.convertColumnNameToSetterMethod(columnName), columnName, (Boolean) value);
1120 // Convert column name to field name
1121 String methodName = this.convertColumnNameToSetterMethod(columnName);
1124 this.getLogger().debug(MessageFormat.format("methodName={0}", methodName)); //NOI18N
1127 this.setField(instance, targetClass, methodName, columnName, value);
1130 this.getLogger().trace("EXIT!"); //NOI18N
1134 * Some "getter" for a value from given Storeable instance and target class
1136 * @param instance An instance of a Storeable class
1137 * @param targetClass The target class (where the field resides)
1138 * @param columnName Column name (= field name)
1139 * @return value Value to get
1140 * @throws java.lang.NoSuchMethodException If the getter was not found
1141 * @throws java.lang.IllegalAccessException If the getter cannot be accessed
1142 * @throws java.lang.reflect.InvocationTargetException Some other problems?
1144 protected Object getValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1146 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
1148 // A '$' means not our field
1149 if (columnName.startsWith("$")) { //NOI18N
1150 // Don't handle these
1151 throw new IllegalArgumentException("columnsName contains $"); //NOI18N
1154 // Determine if the given column is boolean
1155 if (this.isBooleanField(instance, targetClass, columnName)) {
1157 this.getLogger().debug(MessageFormat.format("Column {0} represents a boolean field.", columnName)); //NOI18N
1159 // Yes, then call other method
1160 return this.getBooleanField(instance, targetClass, this.convertColumnNameToGetterMethod(columnName, true));
1163 // Convert column name to field name
1164 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
1167 this.getLogger().debug(MessageFormat.format("methodName={0}", methodName)); //NOI18N
1170 Object value = this.getField(instance, targetClass, methodName);
1173 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1180 * Some getter for type reflection of given column name
1182 * @param instance The instance to check
1183 * @param targetClass Target class to check
1184 * @param columnName Column name
1185 * @return Type reflection of value
1187 private Class<?> getType (final FrameworkInterface instance, final String targetClass, final String columnName) {
1189 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
1192 Class<?> type = null;
1194 // Get all attributes from given instance
1195 Field[] fields = this.getClassFromTarget(instance, targetClass).getDeclaredFields();
1198 this.getLogger().debug(MessageFormat.format("fields()={0}", fields.length)); //NOI18N
1200 // Convert column_name to fieldName ;-)
1201 String fieldName = this.convertColumnNameToFieldName(columnName);
1204 this.getLogger().debug(MessageFormat.format("fieldName={0}", fieldName)); //NOI18N
1206 // Search for proper field instance
1207 for (final Field field : fields) {
1209 this.getLogger().debug(MessageFormat.format("field.getName()={0},fieldName={1}", field.getName(), fieldName)); //NOI18N
1212 if (field.getName().equals(fieldName)) {
1214 type = field.getType();
1217 this.getLogger().debug(MessageFormat.format("Found fieldName={0}: setting type={1}", fieldName, type.getSimpleName())); //NOI18N
1219 // Don't continue with searching
1225 this.getLogger().debug(MessageFormat.format("type={0}", type));
1227 // type should not be null
1230 throw new NullPointerException("type is null"); //NOI18N
1234 this.getLogger().debug(MessageFormat.format("type={0} - EXIT!", type)); //NOI18N