package org.openntf.xrest.xsp.model;

import java.util.Map;

public class MappingField {

	private final String notesFieldName;
	private final String jsonName;
	private final MapJsonType type;
	private final boolean isFormula;
	private final boolean isReadonly;
	private final boolean isWriteonly;
	private final String formula;

	public MappingField(String name) {
		notesFieldName = name;
		jsonName = name;
		type = MapJsonType.DEFAULT;
		isFormula = false;
		formula = "";
		isReadonly = false;
		isWriteonly = false;
	}

	public MappingField(String name, Map<String, Object> options) {
		notesFieldName = name;
		jsonName = getFromMap("json", options, name);
		type = getFromMapAsMTP("type", options, MapJsonType.DEFAULT);
		isFormula = getFromMapAsBoolean("isformula", options, false);
		isReadonly = getFromMapAsBoolean("readonly", options, false);
		isWriteonly = getFromMapAsBoolean("writeonly", options, false);
		formula = getFromMap("formula", options, "");
	}

	private MapJsonType getFromMapAsMTP(String key, Map<String, Object> options, MapJsonType defaultValue) {
		if (options.containsKey(key)) {
			String type = (String) options.get(key);
			return MapJsonType.valueOf(type);
		}
		return defaultValue;
	}

	private String getFromMap(String key, Map<String, Object> options, String name) {
		if (options.containsKey(key)) {
			return (String) options.get(key);
		}
		return name;
	}

	private boolean getFromMapAsBoolean(String key, Map<String, Object> options, Boolean defaultValue) {
		if (options.containsKey(key)) {
			return (Boolean) options.get(key);
		}
		return defaultValue;
	}

	public String getNotesFieldName() {
		return notesFieldName;
	}

	public String getJsonName() {
		return jsonName;
	}

	public MapJsonType getType() {
		return type;
	}

	public boolean isFormula() {
		return isFormula;
	}

	public String getFormula() {
		return formula;
	}
	
	public boolean isReadOnly() {
		return isReadonly;
	}
	public boolean isWriteOnly() {
		return isWriteonly;
	}


}

@@ -336,32 +336,38 @@
 ic MappingField(
+final 
 String name) %7B%0A%09
@@ -546,16 +546,22 @@
 ngField(
+final 
 String n
@@ -564,16 +564,22 @@
 ng name,
+ final
  Map%3CStr
@@ -1022,16 +1022,22 @@
 apAsMTP(
+final 
 String k
@@ -1031,32 +1031,38 @@
 inal String key,
+ final
  Map%3CString, Obj
@@ -1066,32 +1066,38 @@
 Object%3E options,
+ final
  MapJsonType def
@@ -1282,16 +1282,22 @@
 FromMap(
+final 
 String k
@@ -1291,32 +1291,38 @@
 inal String key,
+ final
  Map%3CString, Obj
@@ -1334,16 +1334,22 @@
 options,
+ final
  String 
@@ -1487,16 +1487,22 @@
 Boolean(
+final 
 String k
@@ -1504,16 +1504,22 @@
 ing key,
+ final
  Map%3CStr
@@ -1539,16 +1539,22 @@
 options,
+ final
  Boolean
@@ -1949,17 +1949,16 @@
 ula;%0A%09%7D%0A
-%09
 %0A%09public
@@ -2005,16 +2005,17 @@
 nly;%0A%09%7D%0A
+%0A
 %09public 
@@ -2064,11 +2064,424 @@
 ly;%0A%09%7D%0A%0A
+%09@Override%0A%09public String toString() %7B%0A%09%09return %22MappingField %5B%22 + (notesFieldName != null ? %22notesFieldName=%22 + notesFieldName + %22, %22 : %22%22) + (jsonName != null%0A%09%09%09%09? %22jsonName=%22 + jsonName + %22, %22 : %22%22) + (type != null ? %22type=%22 + type + %22, %22 : %22%22) + %22isFormula=%22 + isFormula%0A%09%09%09%09+ %22, isReadonly=%22 + isReadonly + %22, isWriteonly=%22 + isWriteonly + %22, %22 + (formula != null ? %22formula=%22 + formula : %22%22)%0A%09%09%09%09+ %22%5D%22;%0A%09%7D%0A
 %0A%7D%0A

add toString() for debug prints