Source code: com/webobjects/directtoweb/ERD2WUtilities.java
1 /*
2 * Copyright (C) NetStruxr, Inc. All rights reserved.
3 *
4 * This software is published under the terms of the NetStruxr
5 * Public Software License version 0.5, a copy of which has been
6 * included with this distribution in the LICENSE.NPL file. */
7 package com.webobjects.directtoweb;
8
9 import com.webobjects.foundation.*;
10
11 // This is needed because pageFinalized is a protected method.
12 public class ERD2WUtilities {
13
14 public static void finalizeContext(D2WContext context) {
15 if (context != null)
16 context.pageFinalized();
17 }
18
19 /*
20 public static void resetContextCache(D2WContext context) {
21 if (context != null)
22 context._localValues.clear();
23 }
24 */
25 public static boolean assignmentsAreEqual(Assignment a1, Assignment a2) {
26 boolean areEqual = false;
27 if (a1.getClass().equals(a2.getClass()) && a1.keyPath() != null && a2.keyPath() != null && a1.value() != null && a2.value() != null) {
28 areEqual = a1.keyPath().equals(a2.keyPath()) && a1.value().equals(a2.value());
29 }
30 return areEqual;
31 }
32
33 // This prevents the dreaded KeyValueCoding null object exception, for say key paths: object.entityName
34 // Should just return null instead of throwing.
35 public static Object contextValueForKeyNoInferenceNoException(D2WContext c, String keyPath) {
36 Object result = null;
37 int i = keyPath.indexOf(".");
38 if (i == -1) {
39 result = c.valueForKeyNoInference(keyPath);
40 } else {
41 String first = keyPath.substring(0, i);
42 String second = keyPath.substring(i + 1);
43 result = c.valueForKeyNoInference(first);
44 if (result != null) {
45 // Optimized for two paths deep
46 if (second.indexOf(".") == -1) {
47 result = NSKeyValueCoding.Utility.valueForKey(result, second);
48 } else {
49 NSArray parts = NSArray.componentsSeparatedByString(second, ".");
50 for (int j = 0; j < parts.count(); j++) {
51 String part = (String)parts.objectAtIndex(j);
52 result = NSKeyValueCoding.Utility.valueForKey(result, part);
53 if (result == null)
54 break;
55 }
56 }
57 }
58 }
59 return result;
60 }
61 }