use of com.dexels.navajo.mapping.compiler.meta.JavaDependency in project navajo by Dexels.
the class TslCompiler method fieldNode.
@SuppressWarnings("unchecked")
public String fieldNode(int ident, Element n, String className, String objectName, List<Dependency> dependencies, String tenant) throws UserException, MappingException, ClassNotFoundException, KeywordException, IOException, MetaCompileException, ParseException {
StringBuilder result = new StringBuilder();
String attributeOriginal = n.getAttribute("name");
String condition = n.getAttribute("condition");
String attribute = null;
String mapPath = null;
if (attributeOriginal.indexOf('/') != -1) {
attribute = attributeOriginal.substring(attributeOriginal.lastIndexOf('/') + 1, attributeOriginal.length());
mapPath = attributeOriginal.substring(0, attributeOriginal.lastIndexOf('/'));
} else {
attribute = attributeOriginal;
}
if (attribute == null || attribute.equals(""))
throw new UserException("Name attribute is required for field tags");
condition = (condition == null) ? "" : condition;
String totalMethodName = "set" + (attribute.charAt(0) + "").toUpperCase() + attribute.substring(1, attribute.length());
String methodName = null;
if (totalMethodName.indexOf('/') != -1) {
methodName = totalMethodName.substring(totalMethodName.lastIndexOf('/') + 1, totalMethodName.length());
} else {
methodName = totalMethodName;
}
NodeList children = n.getChildNodes();
if (!condition.equals("")) {
result.append(printIdent(ident) + "if (Condition.evaluate(" + replaceQuotes(condition) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) { \n");
} else {
result.append(printIdent(ident) + "if (true) {\n");
}
// Expression nodes.
boolean isMapped = false;
Element mapNode = null;
int exprCount = countNodes(children, "expression");
List<String> exprValues = new ArrayList<>();
for (int i = 0; i < children.getLength(); i++) {
// Has condition
if (children.item(i).getNodeName().equals("expression")) {
result.append(expressionNode(ident + 2, (Element) children.item(i), --exprCount, className, objectName));
Boolean b = false;
exprValues.add((String) getExpressionValue(((Element) children.item(i)), b)[0]);
} else if (children.item(i).getNodeName().equals("map")) {
isMapped = true;
mapNode = (Element) children.item(i);
}
}
if (!isMapped) {
String castedValue = "";
boolean isDomainObjectMapper = false;
try {
Class localContextClass = null;
try {
if (mapPath != null) {
localContextClass = locateContextClass(mapPath, 0);
} else {
if (Version.osgiActive()) {
localContextClass = resolveClassUsingService(className);
} else {
localContextClass = Class.forName(className, false, loader);
}
}
} catch (Exception e) {
throw new UserException("Could not find adapter: " + className, e);
}
addDependency("dependentObjects.add( new JavaDependency( -1, \"" + className + "\"));\n", "JAVA" + className);
dependencies.add(new JavaDependency(-1, className));
String type = null;
try {
type = MappingUtils.getFieldType(localContextClass, attribute);
checkDependentFieldResource(localContextClass, attribute, exprValues, dependencies);
} catch (Exception e) {
isDomainObjectMapper = localContextClass.isAssignableFrom(DomainObjectMapper.class);
if (isDomainObjectMapper) {
type = "java.lang.Object";
} else {
throw new UserException("Could not find field: " + attribute + " in adapter " + localContextClass.getName(), e);
}
}
if (type.equals("java.lang.String")) {
castedValue = "(String) sValue";
} else if (type.equals("com.dexels.navajo.document.types.ClockTime")) {
castedValue = "(com.dexels.navajo.document.types.ClockTime) sValue";
} else if (type.equals("int")) {
castedValue = "((Integer) sValue).intValue()";
} else if (type.equals("double")) {
castedValue = "((Double) sValue).doubleValue()";
} else if (type.equals("java.util.Date")) {
castedValue = "((java.util.Date) sValue)";
} else if (type.equals("boolean")) {
castedValue = "((Boolean) sValue).booleanValue()";
} else if (type.equals("float")) {
// sValue is never float,
// internally always Double!
castedValue = "(new Float(sValue+\"\")).floatValue()";
} else if (type.equals("com.dexels.navajo.document.types.Binary")) {
castedValue = "((com.dexels.navajo.document.types.Binary) sValue)";
} else if (type.equals("com.dexels.navajo.document.types.Money")) {
castedValue = "((com.dexels.navajo.document.types.Money) sValue)";
} else if (type.equals("com.dexels.navajo.document.types.Percentage")) {
castedValue = "((com.dexels.navajo.document.types.Percentage) sValue)";
} else if (type.equals("java.lang.Integer")) {
castedValue = "((Integer) sValue)";
} else if (type.equals("java.lang.Long")) {
castedValue = "((Long) sValue)";
} else if (type.equals("java.lang.Float")) {
// sValue is never
// float,
// internally
// always
// Double!
castedValue = "new Float(sValue+\"\")";
} else if (type.equals("java.lang.Double")) {
castedValue = "((Double) sValue)";
} else if (type.equals("java.lang.Boolean")) {
castedValue = "((Boolean) sValue)";
} else if (type.equals("java.util.List")) {
castedValue = "((List<Object>) sValue)";
} else {
castedValue = "sValue";
}
} catch (UserException e) {
throw new UserException(-1, "Error in script: could not find mappable object: " + className, e);
}
if (mapPath != null) {
if (!isDomainObjectMapper) {
result.append(printIdent(ident + 2) + "((" + locateContextClass(mapPath, 0).getName() + ")findMapByPath(\"" + mapPath + "\"))." + methodName + "(" + castedValue + ");\n");
} else {
result.append(printIdent(ident + 2) + "((" + locateContextClass(mapPath, 0).getName() + ")findMapByPath(\"" + mapPath + "\")).setDomainObjectAttribute(\"" + attribute + "\"," + castedValue + ");\n");
}
} else {
if (!isDomainObjectMapper) {
result.append(printIdent(ident + 2) + objectName + "." + methodName + "(" + castedValue + ");\n");
} else {
// set attribute in excluded fields.
// USE INTROSPECTION METHOD TO CALL METHOD ON PROXIED
// DOMAIN OBJECT...
result.append(printIdent(ident + 2) + objectName + ".setDomainObjectAttribute(\"" + attribute + "\"," + castedValue + ");\n");
}
}
} else {
// Mappable)
if (mapNode == null) {
throw new IllegalStateException("Unexpected null mapNode");
}
String ref = mapNode.getAttribute("ref");
boolean isParam = false;
if (ref.indexOf("[") != -1) {
// remove square brackets...
ref = ref.replace('[', ' ');
ref = ref.replace(']', ' ');
ref = ref.trim();
}
if (ref.startsWith("/@")) {
// replace @ with __parms__ 'parameter'
// message indication.
ref = ref.replaceAll("@", "__parms__/");
isParam = true;
}
String filter = mapNode.getAttribute("filter");
filter = (filter == null) ? "" : filter;
result.append(printIdent(ident + 2) + "// And by the way, my filter is " + filter + "\n");
result.append(printIdent(ident + 2) + "// Map message(s) to field\n");
String messageListName = "messages" + ident;
result.append(printIdent(ident + 2) + "List " + messageListName + " = null;\n");
result.append(printIdent(ident + 2) + "inSelectionRef = MappingUtils.isSelection(currentInMsg, access.getInDoc(), \"" + ref + "\");\n");
result.append(printIdent(ident + 2) + "if (!inSelectionRef)\n");
result.append(printIdent(ident + 4) + messageListName + " = MappingUtils.getMessageList(currentInMsg, access.getInDoc(), \"" + ref + "\", \"" + "" + "\", currentMap, currentParamMsg,access);\n");
result.append(printIdent(ident + 2) + "else\n");
result.append(printIdent(ident + 4) + messageListName + " = MappingUtils.getSelectedItems(currentInMsg, access.getInDoc(), \"" + ref + "\");\n");
contextClassStack.push(contextClass);
Class localContextClass = null;
try {
if (mapPath != null) {
localContextClass = locateContextClass(mapPath, 1);
} else {
localContextClass = contextClass;
}
} catch (Exception e) {
throw new UserException("Could not find adapter: " + className, e);
}
String type = null;
try {
type = MappingUtils.getFieldType(localContextClass, attribute);
} catch (Exception e) {
throw new UserException("Could not find field: " + attribute + " in adapter " + localContextClass.getName() + ", mappath = " + mapPath);
}
/**
* END.
*/
boolean isArray = MappingUtils.isArrayAttribute(localContextClass, attribute);
try {
contextClass = Class.forName(type, false, loader);
} catch (Exception e) {
throw new UserException("Could not find adapter: " + type);
}
addDependency("dependentObjects.add( new JavaDependency( -1, \"" + type + "\"));\n", "JAVA" + type);
if (isArray) {
String subObjectsName = "subObject" + subObjectCounter;
String loopCounterName = "j" + subObjectCounter;
subObjectCounter++;
String objectDefinition = type + " [] " + subObjectsName + " = null;\n";
variableClipboard.add(objectDefinition);
variableClipboard.add("int " + loopCounterName + ";\n");
result.append(printIdent(ident + 2) + subObjectsName + " = new " + type + "[" + messageListName + ".size()];\n");
result.append(printIdent(ident + 2) + "for (" + loopCounterName + " = 0; " + loopCounterName + " < " + messageListName + ".size(); " + loopCounterName + "++) {\n if (!kill){\n");
// currentInMsg, inMsgStack
ident += 4;
result.append(printIdent(ident) + "inMsgStack.push(currentInMsg);\n");
if (isParam) {
result.append(printIdent(ident) + "paramMsgStack.push(currentParamMsg);\n");
}
result.append(printIdent(ident) + "inSelectionRefStack.push(new Boolean(inSelectionRef));\n");
if (isParam) {
result.append(printIdent(ident) + "if (!inSelectionRef)\n");
result.append(printIdent(ident + 2) + "currentParamMsg = (Message) " + messageListName + ".get(" + loopCounterName + ");\n");
}
result.append(printIdent(ident) + "if (!inSelectionRef)\n");
result.append(printIdent(ident + 2) + "currentInMsg = (Message) " + messageListName + ".get(" + loopCounterName + ");\n");
result.append(printIdent(ident) + "else\n");
// currentSelection.
result.append(printIdent(ident + 2) + "currentSelection = (Selection) " + messageListName + ".get(" + loopCounterName + ");\n");
if (!filter.equals("")) {
result.append(printIdent(ident + 4) + "if (inSelectionRef || Condition.evaluate(" + replaceQuotes(filter) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) {\n");
ident += 2;
}
result.append(printIdent(ident) + "treeNodeStack.push(currentMap);\n");
result.append(printIdent(ident) + "currentMap = new MappableTreeNode(access, currentMap, classLoader.getClass(\"" + type + "\").newInstance(), false);\n");
result.append(printIdent(ident + 4) + "currentMap.setNavajoLineNr(" + n.getAttribute("linenr") + ");\n");
result.append(printIdent(ident) + "if ( currentMap.myObject instanceof Mappable ) { ((Mappable) currentMap.myObject).load(access);}\n");
result.append(printIdent(ident) + subObjectsName + "[" + loopCounterName + "] = (" + type + ") currentMap.myObject;\n");
result.append(printIdent(ident) + "try {\n");
ident = ident + 2;
children = mapNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
result.append(compile(ident + 2, children.item(i), type, subObjectsName + "[" + loopCounterName + "]", dependencies, tenant));
}
ident = ident - 2;
result.append(printIdent(ident) + "} catch (Exception e" + ident + ") {\n");
result.append(printIdent(ident + 2) + "MappingUtils.callKillOrStoreMethod( " + subObjectsName + "[" + loopCounterName + "], e" + ident + ");\n");
result.append(printIdent(ident + 2) + "throw e" + ident + ";\n");
result.append(printIdent(ident) + "}\n");
result.append(printIdent(ident) + "MappingUtils.callStoreMethod(" + subObjectsName + "[" + loopCounterName + "]);\n");
result.append(printIdent(ident) + "currentMap.setEndtime();\ncurrentMap = (MappableTreeNode) treeNodeStack.pop();\n");
if (!filter.equals("")) {
ident -= 2;
result.append(printIdent(ident + 4) + "}\n");
}
result.append(printIdent(ident) + "currentInMsg = (Message) inMsgStack.pop();\n");
if (isParam) {
result.append(printIdent(ident) + "currentParamMsg = (Message) paramMsgStack.pop();\n");
}
result.append(printIdent(ident) + "inSelectionRef = ((Boolean) inSelectionRefStack.pop()).booleanValue();\n");
result.append(printIdent(ident) + "currentSelection = null;\n");
ident -= 4;
result.append(printIdent(ident + 2) + "}\n} // FOR loop for " + loopCounterName + "\n");
if (mapPath == null) {
result.append(printIdent(ident + 2) + objectName + "." + methodName + "(" + subObjectsName + ");\n");
} else {
result.append(printIdent(ident + 2) + "((" + locateContextClass(mapPath, 1).getName() + ") findMapByPath(\"" + mapPath + "\"))." + methodName + "(" + subObjectsName + ");\n");
}
} else {
// Not an array type field, but single Mappable object.
// Push current mappable object on stack.
result.append(printIdent(ident) + "treeNodeStack.push(currentMap);\n");
// Create instance of object.
result.append(printIdent(ident) + "currentMap = new MappableTreeNode(access, currentMap, classLoader.getClass(\"" + type + "\").newInstance(), false);\n");
result.append(printIdent(ident + 4) + "currentMap.setNavajoLineNr(" + n.getAttribute("linenr") + ");\n");
// Create local variable to address new object.
String subObjectsName = "subObject" + subObjectCounter;
subObjectCounter++;
// push currentInMsg, currentParamMsg and inSelectionRef
ident += 4;
result.append(printIdent(ident) + "inMsgStack.push(currentInMsg);\n");
if (isParam) {
result.append(printIdent(ident) + "paramMsgStack.push(currentParamMsg);\n");
}
result.append(printIdent(ident) + "inSelectionRefStack.push(new Boolean(inSelectionRef));\n");
if (isParam) {
result.append(printIdent(ident) + "if (!inSelectionRef)\n");
result.append(printIdent(ident + 2) + "currentParamMsg = (Message) " + messageListName + ".get(0);\n");
}
result.append(printIdent(ident) + "if (!inSelectionRef)\n");
result.append(printIdent(ident + 2) + "currentInMsg = (Message) " + messageListName + ".get(0);\n");
result.append(printIdent(ident) + "else\n");
// currentSelection.
result.append(printIdent(ident + 2) + "currentSelection = (Selection) " + messageListName + ".get(0);\n");
// Call load on object.
result.append(printIdent(ident) + "if ( currentMap.myObject instanceof Mappable) { ((Mappable) currentMap.myObject).load(access);}\n");
// Assign local variable reference.
result.append(printIdent(ident) + type + " " + subObjectsName + " = (" + type + ") currentMap.myObject;\n");
result.append(printIdent(ident) + "try {\n");
ident = ident + 2;
// Recursively dive into children.
children = mapNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
result.append(compile(ident + 2, children.item(i), type, subObjectsName, dependencies, tenant));
}
ident = ident - 2;
result.append(printIdent(ident) + "} catch (Exception e" + ident + ") {\n");
result.append(printIdent(ident + 2) + "MappingUtils.callKillOrStoreMethod( " + subObjectsName + ", e" + ident + ");\n");
result.append(printIdent(ident + 2) + "throw e" + ident + ";\n");
result.append(printIdent(ident) + "}\n");
result.append(printIdent(ident) + "MappingUtils.callStoreMethod(" + subObjectsName + ");\n");
result.append(printIdent(ident) + "currentInMsg = (Message) inMsgStack.pop();\n");
if (isParam) {
result.append(printIdent(ident) + "currentParamMsg = (Message) paramMsgStack.pop();\n");
}
result.append(printIdent(ident) + "inSelectionRef = ((Boolean) inSelectionRefStack.pop()).booleanValue();\n");
result.append(printIdent(ident) + "currentSelection = null;\n");
result.append(printIdent(ident) + "currentMap.setEndtime();\ncurrentMap = (MappableTreeNode) treeNodeStack.pop();\n");
if (mapPath == null) {
result.append(printIdent(ident + 2) + objectName + "." + methodName + "(" + subObjectsName + ");\n");
} else {
result.append(printIdent(ident + 2) + "((" + locateContextClass(mapPath, 1).getName() + ") findMapByPath(\"" + mapPath + "\"))." + methodName + "(" + subObjectsName + ");\n");
}
}
contextClass = contextClassStack.pop();
}
result.append(printIdent(ident) + "}\n");
return result.toString();
}
Aggregations