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;
112 LOG = LogManager.getLogger(this);
116 * No instances can be created of this class
118 protected BaseFrameworkSystem () {
120 this.setSelfInstance();
124 * Getter for this application
126 * @return Instance from this application
128 public static final FrameworkInterface getInstance () {
134 * Application instance
136 * @return the application
139 public final Application getApplication () {
140 return this.application;
149 public final Logger getLogger () {
156 * @return the contactManager
159 public final Manageable getManager () {
164 * Getter for human-readable string from given key
166 * @param key Key to return
167 * @return Human-readable message
170 public final String getMessageStringFromKey (final String key) {
172 return this.getBundle().getString(key);
176 * Some "getter" for target class instance from given name.
178 * @param instance Instance to iterate on
179 * @param targetClass Class name to look for
180 * @return Class instance
182 @SuppressWarnings("unchecked")
183 private Class<? extends FrameworkInterface> getClassFromTarget (final FrameworkInterface instance, final String targetClass) {
185 this.getLogger().debug(MessageFormat.format("instance={0},targetClass={1}", instance, targetClass)); //NOI18N
187 // Instance reflaction of this class
188 Class<? extends FrameworkInterface> c = instance.getClass();
191 while (!targetClass.equals(c.getSimpleName())) {
193 this.getLogger().debug(MessageFormat.format("c={0}", c.getSimpleName())); //NOI18N
195 // Get super class (causes unchecked warning)
196 c = (Class<? extends FrameworkInterface>) c.getSuperclass();
200 this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c)); //NOI18N
207 * Some "getter" for a Method instance from given method name
209 * @param instance Actual instance to call
210 * @param targetClass Target class name
211 * @param methodName Method name
212 * @return A Method instance
214 private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException {
216 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
218 // Get target class instance
219 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
221 // Init field instance
222 Method method = c.getDeclaredMethod(methodName, new Class<?>[0]);
225 assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
228 this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
235 * Some "getter" for a Method instance from given method name
237 * @param instance Actual instance to call
238 * @param targetClass Target class name
239 * @param methodName Method name
240 * @param type Type reflection to check type from
241 * @return A Method instance
243 private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName, final Class<?> type) throws NoSuchMethodException {
245 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1},type={2}", targetClass, methodName, type)); //NOI18N
247 // Get target class instance
248 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
250 // Init field instance
251 Method method = c.getDeclaredMethod(methodName, type);
254 assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
257 this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
264 * Setter for self instance
266 private void setSelfInstance () {
267 // Need to set it here
272 * Aborts program with given exception
274 * @param throwable Any type of Throwable
276 protected final void abortProgramWithException (final Throwable throwable) {
278 this.logException(throwable);
285 * Application instance
287 * @param application the application to set
289 protected final void setApplication (final Application application) {
290 this.application = application;
299 public final Client getClient () {
304 * Getter for bundle instance
306 * @return Resource bundle
308 protected final ResourceBundle getBundle () {
309 return BaseFrameworkSystem.bundle;
313 * Setter for bundle instance
315 * @param bundle the bundle to set
317 protected static void setBundle (final ResourceBundle bundle) {
318 BaseFrameworkSystem.bundle = bundle;
324 * @param client the client to set
326 protected final void setClient (final Client client) {
327 this.client = client;
331 * Name of used database table, handled over to backend
333 * @return the tableName
335 public final String getTableName () {
336 return this.tableName;
340 * Checks if given boolean field is available and set to same value
342 * @param columnName Column name to check
343 * @param bool Boolean value
344 * @return Whether all conditions are met
345 * @throws NoSuchMethodException May be thrown by some implementations
346 * @throws java.lang.IllegalAccessException If the method cannot be accessed
347 * @throws java.lang.reflect.InvocationTargetException Any other problems?
350 public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
352 throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0},bool={1}", columnName, bool)); //NOI18N
358 * @param exception Exception to log
361 public final void logException (final Throwable exception) {
362 // Log this exception
363 this.getLogger().catching(exception);
367 * Initializes properties with default values
369 private void initPropertiesWithDefault () {
371 this.getLogger().trace("CALLED!"); //NOI18N
373 // Init default values:
374 // Default database backend
375 BaseFrameworkSystem.properties.put("org.mxchange.database.backend.class", "org.mxchange.jcore.database.backend.base64.Base64CsvDatabaseBackend"); //NOI18N
376 BaseFrameworkSystem.properties.put("org.mxchange.database.backend.storagepath", "data/"); //NOI18N
377 BaseFrameworkSystem.properties.put("org.mxchange.database.datasource.name", ""); //NOI18N
380 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.host", "localhost"); //NOI18N
381 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.dbname", "test"); //NOI18N
382 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.login", ""); //NOI18N
383 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.password", ""); //NOI18N
386 this.getLogger().trace("EXIT!"); //NOI18N
390 * Writes the properties file to disk
392 private void writePropertiesFile () throws IOException {
394 this.getLogger().trace("CALLED!"); //NOI18N
397 BaseFrameworkSystem.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); //NOI18N
400 this.getLogger().trace("EXIT!"); //NOI18N
404 * Converts a column name like "foo_bar" to an attribute name like "fooBar"
406 * @param columnName Column name to convert
407 * @return Attribute name
409 protected String convertColumnNameToFieldName (final String columnName) {
411 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
413 // First all lower case
414 String lower = columnName.toLowerCase();
417 StringTokenizer tokenizer = new StringTokenizer(lower, "_"); //NOI18N
420 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
426 while (tokenizer.hasMoreTokens()) {
428 String token = tokenizer.nextToken();
430 // Is later than first element?
432 // Make first character upper-case
433 char c = token.charAt(0);
434 token = String.valueOf(c).toUpperCase() + token.substring(1);
438 builder.append(token);
445 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
448 return builder.toString();
452 * Converts a column name like "foo_bar" to a method name like "getFooBar"
453 * for non-booleans and to "isFooBar" for boolean fields.
455 * @param columnName Column name to convert
456 * @param isBool Whether the parameter is boolean
457 * @return Attribute name
459 protected String convertColumnNameToGetterMethod (final String columnName, boolean isBool) {
461 this.getLogger().trace(MessageFormat.format("columnName={0},isBool={1} - CALLED!", columnName, isBool)); //NOI18N
464 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
467 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
472 builder.append("is"); //NOI18N
475 builder.append("get"); //NOI18N
479 while (tokenizer.hasMoreTokens()) {
481 String token = tokenizer.nextToken();
484 this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N
486 // Make it upper-case
487 char c = token.charAt(0);
488 token = String.valueOf(c).toUpperCase() + token.substring(1);
491 builder.append(token);
495 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
498 return builder.toString();
502 * Converts a column name like "foo_bar" to a method name like "getFooBar"
503 * for non-booleans and to "isFooBar" for boolean fields.
505 * @param columnName Column name to convert
506 * @return Attribute name
508 protected String convertColumnNameToSetterMethod (final String columnName) {
510 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
513 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
516 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
519 builder.append("set"); //NOI18N
522 while (tokenizer.hasMoreTokens()) {
524 String token = tokenizer.nextToken();
527 this.getLogger().debug(MessageFormat.format("token={0} - BEFORE", token)); //NOI18N
529 // Make it upper-case
530 char c = token.charAt(0);
531 token = String.valueOf(c).toUpperCase() + token.substring(1);
534 this.getLogger().debug(MessageFormat.format("token={0} - AFTER", token)); //NOI18N
537 builder.append(token);
541 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
544 return builder.toString();
548 * Some "getter" for an array from given string and tokenizer
550 * @param str String to tokenize and get array from
551 * @param delimiter Delimiter
552 * @param size Size of array
553 * @return Array from tokenized string
554 * @todo Get rid of size parameter
556 protected String[] getArrayFromString (final String str, final String delimiter, final int size) {
558 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1},size={2} - CALLED!", str, delimiter, size)); //NOI18N
561 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
563 // Init array and index
564 String[] tokens = new String[size];
567 // Run through all tokens
568 while (tokenizer.hasMoreTokens()) {
569 // Get current token and add it
570 tokens[index] = tokenizer.nextToken();
573 this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
580 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
587 * Returns boolean field value from given method name by invoking it
589 * @param instance The instance to call
590 * @param targetClass Target class to look in
591 * @param methodName Method name to look for
592 * @return Boolean value from field
593 * @throws java.lang.NoSuchMethodException If the method was not found
594 * @throws java.lang.IllegalAccessException If the method cannot be accessed
595 * @throws java.lang.reflect.InvocationTargetException Some other problems?
597 protected boolean getBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
599 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
601 // Get method instance
602 Method method = this.getMethodFromName(instance, targetClass, methodName);
604 // Get value from field
605 Boolean value = (Boolean) method.invoke(instance);
608 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
615 * Sets boolean field value with given method name by invoking it
617 * @param instance The instance to call
618 * @param targetClass Target class to look in
619 * @param methodName Method name to look for
620 * @param columnName Column name
621 * @param value Boolean value from field
622 * @throws java.lang.NoSuchMethodException If the method was not found
623 * @throws java.lang.IllegalAccessException If the method cannot be accessed
624 * @throws java.lang.reflect.InvocationTargetException Some other problems?
626 protected void setBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Boolean value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
628 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
631 Class<?> type = this.getType(instance, targetClass, columnName);
633 // Get method instance
634 Method method = this.getMethodFromName(instance, targetClass, methodName, type);
636 // Try to get the value by invoking the method
637 method.invoke(instance, value);
640 this.getLogger().trace("EXIT!"); //NOI18N
646 * @param manager the manager instance to set
648 protected final void setContactManager (final Manageable manager) {
649 this.manager = manager;
653 * Returns any field value from given method name by invoking it
655 * @param instance The instance to call
656 * @param targetClass Target class to look in
657 * @param methodName Method name to look for
658 * @return Any value from field
659 * @throws java.lang.NoSuchMethodException If the method was not found
660 * @throws java.lang.IllegalAccessException If the method cannot be accessed
661 * @throws java.lang.reflect.InvocationTargetException Some other problems?
663 protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
665 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2}", instance, targetClass, methodName)); //NOI18N
667 // Get method to call
668 Method method = this.getMethodFromName(instance, targetClass, methodName);
671 this.getLogger().debug(MessageFormat.format("method={0},instance={1}", method, instance)); //NOI18N
673 // Get value from field
674 Object value = method.invoke(instance);
677 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
684 * Sets any field value with given method name by invoking it
686 * @param instance The instance to call
687 * @param targetClass Target class to look in
688 * @param methodName Method name to look for
689 * @param columnName Column name
690 * @param value Any value from field
691 * @throws java.lang.NoSuchMethodException If the method was not found
692 * @throws java.lang.IllegalAccessException If the method cannot be accessed
693 * @throws java.lang.reflect.InvocationTargetException Some other problems?
695 protected void setField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
697 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2},value={3}", instance, targetClass, methodName, value)); //NOI18N
700 Class<?> type = this.getType(instance, targetClass, columnName);
703 this.getLogger().debug(MessageFormat.format("type={0}", type)); //NOI18N
706 Object object = value;
708 // Is the value null?
709 if ("null".equals(value)) { //NOI18N
711 this.getLogger().warn(MessageFormat.format("columnName={0} has null value.", columnName)); //NOI18N
716 // Hard-coded "cast" again ... :-(
717 // @TODO Can't we get rid of this???
718 switch (type.getSimpleName()) {
719 case "Long": // Long object //NOI18N
720 object = Long.parseLong((String) value);
723 case "Float": // Float object //NOI18N
724 object = Float.parseFloat((String) value);
727 case "Boolean": // Boolean object //NOI18N
728 object = Boolean.parseBoolean((String) value);
731 case "String": // String object //NOI18N
734 default: // Unsupported type
735 throw new IllegalArgumentException(MessageFormat.format("value {0} has unsupported type {1}", value, type.getSimpleName())); //NOI18N
740 this.getLogger().debug(MessageFormat.format("object={0}", object)); //NOI18N
742 // Get method to call
743 Method method = this.getMethodFromName(instance, targetClass, methodName, type);
746 this.getLogger().debug(MessageFormat.format("method={0},instance={1},value[{2}]={3}", method, instance, value.getClass().getSimpleName(), value)); //NOI18N
748 // Get value from field
749 method.invoke(instance, object);
752 this.getLogger().trace("EXIT!"); //NOI18N
756 * Getter for property which must exist
758 * @param key Key to get
759 * @return Propety value
761 protected final String getProperty (final String key) {
762 return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.%s", key)); //NOI18N
766 * Name of used database table, handled over to backend
768 * @param tableName the tableName to set
770 protected final void setTableName (final String tableName) {
771 this.tableName = tableName;
775 * Converts null to empty string or leaves original string.
777 * @param str Any string
778 * @return Empty string if null or original string
780 protected Object convertNullToEmpty (final Object str) {
782 this.getLogger().trace(MessageFormat.format("str={0}", str)); //NOI18N
786 // Return empty string
791 this.getLogger().trace(MessageFormat.format("str={0} - EXIT!", str)); //NOI18N
798 * Creates an iterator from given instance and class name.
800 * @param instance Instance to run getter calls on
801 * @param className Class name to iterate over
802 * @return An iterator over all object's fields
803 * @throws java.lang.NoSuchMethodException If the called method does not exist
804 * @throws java.lang.IllegalAccessException If the method cannot be accessed
805 * @throws java.lang.reflect.InvocationTargetException Any other problems?
807 protected Iterator<Map.Entry<Field, Object>> fieldIterator (final Storeable instance, final String className) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
809 this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className)); //NOI18N
811 // Get all attributes from given instance
812 Field[] fields = this.getClassFromTarget(instance, className).getDeclaredFields();
815 this.getLogger().debug(MessageFormat.format("Found {0} fields.", fields.length)); //NOI18N
817 // A simple map with K=fieldName and V=Value is fine
818 Map<Field, Object> map = new HashMap<>(fields.length);
821 for (final Field field : fields) {
823 this.getLogger().debug(MessageFormat.format("field={0}", field.getName())); //NOI18N
825 // Does the field start with "$"?
826 if (field.getName().startsWith("$")) { //NOI18N
828 this.getLogger().debug(MessageFormat.format("Skipping field={0} as it starts with a dollar character.", field.getName())); //NOI18N
835 this.getLogger().debug(MessageFormat.format("Calling getValueFromColumn({0}) on instance {1} ...", field.getName(), instance)); //NOI18N
838 Object value = instance.getValueFromColumn(field.getName());
841 this.getLogger().debug(MessageFormat.format("Adding field={0},value={1}", field.getName(), value)); //NOI18N
844 map.put(field, value);
848 this.getLogger().debug(MessageFormat.format("Returning iterator for {0} entries ...", map.size())); //NOI18N
850 // Return list iterator
851 return map.entrySet().iterator();
855 * Instance for database backend
857 * @return the backend
859 protected final DatabaseBackend getBackend () {
864 * Instance for database backend
866 * @param backend the backend to set
868 protected final void setBackend (final DatabaseBackend backend) {
869 this.backend = backend;
873 * Getter for Contact instance
875 * @return Contact instance
877 protected final Contact getContact () {
882 * Setter for Contact instance
884 * @param contact A Contact instance
886 protected final void setContact (final Contact contact) {
887 this.contact = contact;
891 * Getter for DatabaseFrontend instance
893 * @return DatabaseFrontend instance
895 protected final DatabaseFrontend getFrontend () {
896 return this.frontend;
900 * Setter for wrapper instance
902 * @param frontend A DatabaseFrontend instance
904 protected final void setFrontend (final DatabaseFrontend frontend) {
905 this.frontend = frontend;
909 * Initializes i18n bundles
911 protected void initBundle () {
913 this.getLogger().trace("CALLED!"); //NOI18N
915 // Is the bundle set?
916 if (bundle instanceof ResourceBundle) {
918 throw new IllegalStateException("called twice"); //NOI18N
922 setBundle(ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE)); // NOI18N
925 this.getLogger().trace("EXIT!"); //NOI18N
929 * Prepares all properties, the file is written if it is not found
931 * @throws java.io.IOException If any IO problem occurs
933 protected void initProperties () throws IOException {
935 this.getLogger().trace("CALLED!"); //NOI18N
938 this.getLogger().debug(MessageFormat.format("{0} properties are loaded already.", BaseFrameworkSystem.properties.size())); //NOI18N
940 // Are some properties loaded?
941 if (!BaseFrameworkSystem.properties.isEmpty()) {
942 // Some are already loaded, abort here
948 BaseFrameworkSystem.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
951 this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", BaseFrameworkSystem.properties.size())); //NOI18N
952 } catch (final FileNotFoundException ex) {
954 this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N
957 * The file is not found which is normal for first run, so
958 * initialize default values.
960 this.initPropertiesWithDefault();
963 this.writePropertiesFile();
967 this.getLogger().trace("EXIT!"); //NOI18N
971 * Checks whether the given field is a boolean field by probing it.
973 * @param instance Instance to call
974 * @param targetClass Target class
975 * @param columnName Column name to check
976 * @return Whether the given column name represents a boolean field
978 protected boolean isBooleanField (final FrameworkInterface instance, final String targetClass, final String columnName) {
980 this.getLogger().trace(MessageFormat.format("instance={0},targetCLass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
982 // Convert column name to getter name (boolean)
983 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
985 // Get class instance
986 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
988 // Defauzlt is boolean
989 boolean isBool = true;
992 // Now try to instance the method
993 Method method = c.getDeclaredMethod(methodName, new Class<?>[0]);
994 } catch (final NoSuchMethodException ex) {
996 this.getLogger().debug(MessageFormat.format("Method {0} does not exist, field {1} cannot be boolean: {2}", methodName, columnName, ex)); //NOI18N
1003 this.getLogger().trace(MessageFormat.format("isBool={0} - EXIT!", isBool)); //NOI18N
1010 * Sets value in properties instance.
1012 * @param key Property key (part) to set
1013 * @param value Property value to set
1015 protected void setProperty (final String key, final String value) {
1017 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); //NOI18N
1019 // Both should not be null
1022 throw new NullPointerException("key is null"); //NOI18N
1023 } else if (null == value) {
1025 throw new NullPointerException("value is null"); //NOI18N
1029 properties.setProperty(String.format("org.mxchange.%s", key), value); //NOI18N
1032 this.getLogger().trace("EXIT!"); //NOI18N
1036 * Some getter for properties names (without org.mxchange)
1038 * @return An array with property names
1040 protected String[] getPropertyNames () {
1043 "database.backend.class", //NOI18N
1044 "database.backend.storagepath", //NOI18N
1045 "database.mysql.login", //NOI18N
1046 "database.mysql.host", //NOI18N
1047 "database.mysql.password", //NOI18N
1048 "database.mysql.dbname", //NOI18N
1056 * Some "setter" for a value in given Storeable instance and target class
1058 * @param instance An instance of a Storeable class
1059 * @param targetClass The target class (where the field resides)
1060 * @param columnName Column name (= field name)
1061 * @param value Value to set
1062 * @throws java.lang.NoSuchMethodException If the setter is not found
1063 * @throws java.lang.IllegalAccessException If the setter cannot be accessed
1064 * @throws java.lang.reflect.InvocationTargetException Any other problem?
1066 protected void setValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1068 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2},value={3} - CALLED!", instance, targetClass, columnName, value)); //NOI18N
1070 // A '$' means not our field
1071 if (columnName.startsWith("$")) { //NOI18N
1072 // Don't handle these
1073 throw new IllegalArgumentException("columnsName contains $"); //NOI18N
1076 // Determine if the given column is boolean
1077 if (this.isBooleanField(instance, targetClass, columnName)) {
1079 this.getLogger().debug(MessageFormat.format("Column {0} represents a boolean field.", columnName)); //NOI18N
1081 // Yes, then call other method
1082 this.setBooleanField(instance, targetClass, this.convertColumnNameToSetterMethod(columnName), columnName, (Boolean) value);
1085 // Convert column name to field name
1086 String methodName = this.convertColumnNameToSetterMethod(columnName);
1089 this.getLogger().debug(MessageFormat.format("methodName={0}", methodName)); //NOI18N
1092 this.setField(instance, targetClass, methodName, columnName, value);
1095 this.getLogger().trace("EXIT!"); //NOI18N
1099 * Some "getter" for a value from given Storeable instance and target class
1101 * @param instance An instance of a Storeable class
1102 * @param targetClass The target class (where the field resides)
1103 * @param columnName Column name (= field name)
1104 * @return value Value to get
1105 * @throws java.lang.NoSuchMethodException If the getter was not found
1106 * @throws java.lang.IllegalAccessException If the getter cannot be accessed
1107 * @throws java.lang.reflect.InvocationTargetException Some other problems?
1109 protected Object getValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1111 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
1113 // A '$' means not our field
1114 if (columnName.startsWith("$")) { //NOI18N
1115 // Don't handle these
1116 throw new IllegalArgumentException("columnsName contains $"); //NOI18N
1119 // Determine if the given column is boolean
1120 if (this.isBooleanField(instance, targetClass, columnName)) {
1122 this.getLogger().debug(MessageFormat.format("Column {0} represents a boolean field.", columnName)); //NOI18N
1124 // Yes, then call other method
1125 return this.getBooleanField(instance, targetClass, this.convertColumnNameToGetterMethod(columnName, true));
1128 // Convert column name to field name
1129 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
1132 this.getLogger().debug(MessageFormat.format("methodName={0}", methodName)); //NOI18N
1135 Object value = this.getField(instance, targetClass, methodName);
1138 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1145 * Some getter for type reflection of given column name
1147 * @param instance The instance to check
1148 * @param targetClass Target class to check
1149 * @param columnName Column name
1150 * @return Type reflection of value
1152 private Class<?> getType (final FrameworkInterface instance, final String targetClass, final String columnName) {
1154 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
1157 Class<?> type = null;
1159 // Get all attributes from given instance
1160 Field[] fields = this.getClassFromTarget(instance, targetClass).getDeclaredFields();
1163 this.getLogger().debug(MessageFormat.format("fields()={0}", fields.length)); //NOI18N
1165 // Convert column_name to fieldName ;-)
1166 String fieldName = this.convertColumnNameToFieldName(columnName);
1169 this.getLogger().debug(MessageFormat.format("fieldName={0}", fieldName)); //NOI18N
1171 // Search for proper field instance
1172 for (final Field field : fields) {
1174 this.getLogger().debug(MessageFormat.format("field.getName()={0},fieldName={1}", field.getName(), fieldName)); //NOI18N
1177 if (field.getName().equals(fieldName)) {
1179 type = field.getType();
1182 this.getLogger().debug(MessageFormat.format("Found fieldName={0}: setting type={1}", fieldName, type.getSimpleName())); //NOI18N
1184 // Don't continue with searching
1190 this.getLogger().debug(MessageFormat.format("type={0}", type));
1192 // type should not be null
1195 throw new NullPointerException("type is null"); //NOI18N
1199 this.getLogger().debug(MessageFormat.format("type={0} - EXIT!", type)); //NOI18N