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.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.text.MessageFormat;
22 import java.util.Arrays;
23 import java.util.ResourceBundle;
24 import java.util.StringTokenizer;
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.mxchange.jcore.application.Application;
28 import org.mxchange.jcore.client.Client;
29 import org.mxchange.jcore.contact.Contact;
30 import org.mxchange.jcore.manager.Manageable;
35 * @author Roland Haeder
37 public class BaseFrameworkSystem implements FrameworkInterface {
42 private static ResourceBundle bundle;
47 private static FrameworkInterface selfInstance;
52 private final Logger LOG;
55 * Application instance
57 private Application application;
62 private Client client;
67 private Contact contact;
72 private Manageable manager;
75 * Name of used database table, handled over to backend
77 private String tableName;
84 this.LOG = LogManager.getLogger(this);
86 // Need to set it here
91 * No instances can be created of this class
93 protected BaseFrameworkSystem () {
97 * Getter for this application
99 * @return Instance from this application
101 public static final FrameworkInterface getInstance () {
107 public final Application getApplication () {
108 return this.application;
112 public final Logger getLogger () {
117 public final Manageable getManager () {
122 public final String getMessageStringFromKey (final String key) {
124 return this.getBundle().getString(key);
128 * Some "getter" for target class instance from given name.
130 * @param instance Instance to iterate on
131 * @param targetClass Class name to look for
132 * @return Class instance
134 @SuppressWarnings ("unchecked")
135 private Class<? extends FrameworkInterface> getClassFromTarget (final FrameworkInterface instance, final String targetClass) {
137 this.getLogger().debug(MessageFormat.format("instance={0},targetClass={1}", instance, targetClass)); //NOI18N
139 // Instance reflaction of this class
140 Class<? extends FrameworkInterface> c = instance.getClass();
143 while (!targetClass.equals(c.getSimpleName())) {
145 this.getLogger().debug(MessageFormat.format("c={0}", c.getSimpleName())); //NOI18N
147 // Get super class (causes unchecked warning)
148 c = (Class<? extends FrameworkInterface>) c.getSuperclass();
152 this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c)); //NOI18N
159 * Some "getter" for a Method instance from given method name
161 * @param instance Actual instance to call
162 * @param targetClass Target class name
163 * @param methodName Method name
164 * @return A Method instance
166 private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException {
168 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
170 // Init method instance
171 Method method = null;
173 // Try it from target class
175 // Get target class instance
176 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
178 // Init field instance
179 method = c.getDeclaredMethod(methodName, new Class<?>[0]);
180 } catch (final NoSuchMethodException e) {
182 this.getLogger().debug(e);
184 // So try it from super class
185 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, "BaseFrameworkSystem"); //NOI18N
187 // Init field instance
188 method = c.getDeclaredMethod(methodName, new Class<?>[0]);
192 assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
195 this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
202 * Some "getter" for a Method instance from given method name
204 * @param instance Actual instance to call
205 * @param targetClass Target class name
206 * @param methodName Method name
207 * @param type Type reflection to check type from
208 * @return A Method instance
210 private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName, final Class<?> type) throws NoSuchMethodException {
212 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1},type={2}", targetClass, methodName, type)); //NOI18N
214 // Init method instance
215 Method method = null;
217 // Try it from target class
219 // Get target class instance
220 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
222 // Init field instance
223 method = c.getDeclaredMethod(methodName, type);
224 } catch (final NoSuchMethodException e) {
226 this.getLogger().debug(e);
228 // So try it from super class
229 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, "BaseFrameworkSystem"); //NOI18N
231 // Init field instance
232 method = c.getDeclaredMethod(methodName, type);
236 assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
239 this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
246 * Aborts program with given exception
248 * @param throwable Any type of Throwable
250 protected final void abortProgramWithException (final Throwable throwable) {
252 this.logException(throwable);
259 * Application instance
261 * @param application the application to set
263 protected final void setApplication (final Application application) {
264 this.application = application;
268 public final Client getClient () {
273 * Getter for bundle instance
275 * @return Resource bundle
277 protected final ResourceBundle getBundle () {
278 return BaseFrameworkSystem.bundle;
282 * Setter for bundle instance
284 * @param bundle the bundle to set
286 protected static void setBundle (final ResourceBundle bundle) {
287 BaseFrameworkSystem.bundle = bundle;
293 * @param client the client to set
295 protected final void setClient (final Client client) {
296 this.client = client;
300 * Name of used database table, handled over to backend
302 * @return the tableName
304 public final String getTableName () {
305 return this.tableName;
309 public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
311 throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0},bool={1}", columnName, bool)); //NOI18N
315 public final void logException (final Throwable exception) {
316 // Log this exception
317 this.getLogger().catching(exception);
321 * Converts a column name like "foo_bar" to an attribute name like "fooBar"
323 * @param columnName Column name to convert
324 * @return Attribute name
326 protected String convertColumnNameToFieldName (final String columnName) {
328 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
331 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
334 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
340 while (tokenizer.hasMoreTokens()) {
342 String token = tokenizer.nextToken();
344 // Is later than first element?
346 // Make first character upper-case
347 char c = token.charAt(0);
348 token = String.valueOf(c).toUpperCase() + token.substring(1);
352 builder.append(token);
359 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
362 return builder.toString();
366 * Converts a column name like "foo_bar" to a method name like "getFooBar"
367 * for non-booleans and to "isFooBar" for boolean fields.
369 * @param columnName Column name to convert
370 * @param isBool Whether the parameter is boolean
371 * @return Attribute name
373 protected String convertColumnNameToGetterMethod (final String columnName, boolean isBool) {
375 this.getLogger().trace(MessageFormat.format("columnName={0},isBool={1} - CALLED!", columnName, isBool)); //NOI18N
378 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
381 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
386 builder.append("is"); //NOI18N
389 builder.append("get"); //NOI18N
393 while (tokenizer.hasMoreTokens()) {
395 String token = tokenizer.nextToken();
398 this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N
400 // Make it upper-case
401 char c = token.charAt(0);
402 token = String.valueOf(c).toUpperCase() + token.substring(1);
405 builder.append(token);
409 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
412 return builder.toString();
416 * Converts a column name like "foo_bar" to a method name like "getFooBar"
417 * for non-booleans and to "isFooBar" for boolean fields.
419 * @param columnName Column name to convert
420 * @return Attribute name
422 protected String convertColumnNameToSetterMethod (final String columnName) {
424 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
427 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
430 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
433 builder.append("set"); //NOI18N
436 while (tokenizer.hasMoreTokens()) {
438 String token = tokenizer.nextToken();
441 this.getLogger().debug(MessageFormat.format("token={0} - BEFORE", token)); //NOI18N
443 // Make it upper-case
444 char c = token.charAt(0);
445 token = String.valueOf(c).toUpperCase() + token.substring(1);
448 this.getLogger().debug(MessageFormat.format("token={0} - AFTER", token)); //NOI18N
451 builder.append(token);
455 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
458 return builder.toString();
462 * Some "getter" for an array from given string and tokenizer
464 * @param str String to tokenize and get array from
465 * @param delimiter Delimiter
466 * @return Array from tokenized string TODO Get rid of size parameter
468 protected String[] getArrayFromString (final String str, final String delimiter) {
470 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1} - CALLED!", str, delimiter)); //NOI18N
473 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
475 // Init array and index
476 String[] tokens = new String[tokenizer.countTokens()];
479 // Run through all tokens
480 while (tokenizer.hasMoreTokens()) {
481 // Get current token and add it
482 tokens[index] = tokenizer.nextToken();
485 this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
492 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
499 * Returns boolean field value from given method name by invoking it
501 * @param instance The instance to call
502 * @param targetClass Target class to look in
503 * @param methodName Method name to look for
504 * @return Boolean value from field
505 * @throws java.lang.NoSuchMethodException If the method was not found
506 * @throws java.lang.IllegalAccessException If the method cannot be accessed
507 * @throws java.lang.reflect.InvocationTargetException Some other problems?
509 protected boolean getBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
511 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
513 // Get method instance
514 Method method = this.getMethodFromName(instance, targetClass, methodName);
516 // Get value from field
517 Boolean value = (Boolean) method.invoke(instance);
520 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
529 * @param manager the manager instance to set
531 protected final void setContactManager (final Manageable manager) {
532 this.manager = manager;
536 * Returns any field value from given method name by invoking it
538 * @param instance The instance to call
539 * @param targetClass Target class to look in
540 * @param methodName Method name to look for
541 * @return Any value from field
542 * @throws java.lang.NoSuchMethodException If the method was not found
543 * @throws java.lang.IllegalAccessException If the method cannot be accessed
544 * @throws java.lang.reflect.InvocationTargetException Some other problems?
546 protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
548 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2}", instance, targetClass, methodName)); //NOI18N
550 // Get method to call
551 Method method = this.getMethodFromName(instance, targetClass, methodName);
554 this.getLogger().debug(MessageFormat.format("method={0},instance={1}", method, instance)); //NOI18N
556 // Get value from field
557 Object value = method.invoke(instance);
560 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
567 * Name of used database table, handled over to backend
569 * @param tableName the tableName to set
571 protected final void setTableName (final String tableName) {
572 this.tableName = tableName;
576 * Converts null to empty string or leaves original string.
578 * @param str Any string
579 * @return Empty string if null or original string
581 protected Object convertNullToEmpty (final Object str) {
583 this.getLogger().trace(MessageFormat.format("str={0}", str)); //NOI18N
587 // Return empty string
592 this.getLogger().trace(MessageFormat.format("str={0} - EXIT!", str)); //NOI18N
599 * Getter for Contact instance
601 * @return Contact instance
603 protected final Contact getContact () {
608 * Setter for Contact instance
610 * @param contact A Contact instance
612 protected final void setContact (final Contact contact) {
613 this.contact = contact;
617 * Initializes i18n bundles
619 protected void initBundle () {
621 this.getLogger().trace("CALLED!"); //NOI18N
623 // Is the bundle set?
624 if (isBundledInitialized()) {
626 throw new IllegalStateException("called twice"); //NOI18N
630 setBundle(ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE)); // NOI18N
633 this.getLogger().trace("EXIT!"); //NOI18N
637 * Checks if the bundle is initialized
639 * @return Whether the bundle has been initialized
641 protected static boolean isBundledInitialized () {
643 return (bundle instanceof ResourceBundle);