package ch.openech.mj.resources;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import ch.openech.mj.model.PropertyInterface;
import ch.openech.mj.util.MultiResourceBundle;
import ch.openech.mj.util.StringUtils;

public class Resources {

	private static final ResourceBundle defaultResourcebundle = ResourceBundle.getBundle(Resources.class.getPackage().getName() + ".MinimalJ");
	private static ResourceBundle resourceBundle = defaultResourcebundle;
	
	public static ResourceBundle getDefaultResourcebundle() {
		return defaultResourcebundle;
	}
	
	public static ResourceBundle getResourceBundle() {
		return resourceBundle;
	}
	
	public static void setResourceBundle(ResourceBundle resourceBundle) {
		Resources.resourceBundle = resourceBundle;
	}
	
	public static void addResourceBundle(ResourceBundle resourceBundle) {
		Resources.resourceBundle = new MultiResourceBundle(resourceBundle, defaultResourcebundle);
	}

	public static String getString(String resourceName) {
		String text;
		try {
			text = getResourceBundle().getString(resourceName);
		} catch (MissingResourceException x) {
			System.out.println(resourceName + "=");
			text = "!" + resourceName + "!";
		} catch (NullPointerException x) {
			System.out.println("AbstractApplication.resourceBundle() not set");
			text = resourceName;
		}
		return text;
	}

	public static String getObjectFieldName(ResourceBundle resourceBundle, PropertyInterface property) {
		return getObjectFieldName(resourceBundle, property, null);
	}
	
	public static String getObjectFieldName(ResourceBundle resourceBundle, PropertyInterface property, String postfix) {
		// completeQualifiedKey example: "ch.openech.dm.Person.nationality"
		String fieldName = property.getFieldName();
		if (!StringUtils.isEmpty(postfix)) {
			fieldName += postfix;
		}
		
		String completeQualifiedKey = property.getDeclaringClass().getName() + "." + fieldName;
		if (resourceBundle.containsKey(completeQualifiedKey)) {
			return resourceBundle.getString(completeQualifiedKey);
		}
		
		// qualifiedKey example: "Person.nationality"
		String qualifiedKey = property.getDeclaringClass().getSimpleName() + "." + fieldName;
		if (resourceBundle.containsKey(qualifiedKey)) {
			return resourceBundle.getString(qualifiedKey);
		}

		// unqualifiedKey example: "nationality"
		if (resourceBundle.containsKey(fieldName)) {
			return resourceBundle.getString(fieldName);
		}
		
		// unqualifiedKey example: "Nationality"
		if (resourceBundle.containsKey(StringUtils.upperFirstChar(fieldName))) {
			return resourceBundle.getString(StringUtils.upperFirstChar(fieldName));
		}

		return "!!" + fieldName;
	}
	
}

@@ -107,16 +107,93 @@
 undle;%0D%0A
+import java.util.logging.Logger;%0D%0A%0D%0Aimport ch.openech.mj.application.DevMode;
 %0D%0Aimport
@@ -351,16 +351,100 @@
 es %7B%0D%0A%0D%0A
+%09private static final Logger logger = Logger.getLogger(Resources.class.getName());%0D%0A
 %09private
@@ -1306,32 +1306,105 @@
 Exception x) %7B%0D%0A
+%09%09%09if (DevMode.isActive() && !resourceName.endsWith(%22.description%22)) %7B%0D%0A%09
 %09%09%09System.out.pr
@@ -1427,24 +1427,30 @@
 me + %22=%22);%0D%0A
+%09%09%09%7D%0D%0A
 %09%09%09text = %22!
@@ -1515,34 +1515,29 @@
 ) %7B%0D%0A%09%09%09
-System.out.println
+logger.severe
 (%22Abstra

Print out missing properites only in DevMode