// This prevents the dreaded KeyValueCoding null object exception, for say key paths: // object.entityName // Should just return null instead of throwing. public static Object contextValueForKeyNoInferenceNoException(D2WContext c, String keyPath) { Object result = null; int i = keyPath.indexOf("."); if (i == -1) { result = c.valueForKeyNoInference(keyPath); } else { String first = keyPath.substring(0, i); String second = keyPath.substring(i + 1); result = c.valueForKeyNoInference(first); if (result != null) { // Optimized for two paths deep try { if (second.indexOf(".") == -1) { result = NSKeyValueCoding.Utility.valueForKey(result, second); } else { NSArray parts = NSArray.componentsSeparatedByString(second, "."); for (int j = 0; j < parts.count(); j++) { String part = (String) parts.objectAtIndex(j); result = NSKeyValueCoding.Utility.valueForKey(result, part); if (result == null) break; } } } catch (NSKeyValueCoding.UnknownKeyException e) { if (log.isDebugEnabled()) { log.debug( "keyPath {} is not available for context with entity: {}; task: {}", keyPath, c.entity().name(), c.task()); } return null; } } } return result; }
public static void finalizeContext(D2WContext context) { if (context != null) context.pageFinalized(); }