1 2 12 package com.versant.core.jdo.sco; 13 14 import java.beans.DefaultPersistenceDelegate ; 15 import java.beans.Encoder ; 16 import java.beans.Expression ; 17 import java.beans.Statement ; 18 import java.util.*; 19 20 29 public class PersistenceDelegateManager { 30 31 34 public static void register(Encoder encoder) { 35 encoder.setPersistenceDelegate(com.versant.core.jdo.sco.Date.class, 36 new SCOPersistenceDelegate(java.util.Date .class)); 37 38 encoder.setPersistenceDelegate(SCOList.class, new SCOListPD(ArrayList.class)); 39 encoder.setPersistenceDelegate(SCOArrayList.class, new SCOListPD(ArrayList.class)); 40 encoder.setPersistenceDelegate(SCOVector.class, new SCOListPD(Vector.class)); 41 42 encoder.setPersistenceDelegate(SCOHashMap.class, new SCOMapPD(HashMap.class)); 43 encoder.setPersistenceDelegate(SCOHashtable.class, new SCOMapPD(Hashtable.class)); 44 encoder.setPersistenceDelegate(SCOTreeMap.class, new SCOMapPD(TreeMap.class)); 45 encoder.setPersistenceDelegate(SCOTreeSet.class, new SCOMapPD(TreeSet.class)); 46 47 encoder.setPersistenceDelegate(SCOHashSet.class, new SCOCollectionPD(HashSet.class)); 48 encoder.setPersistenceDelegate(SCOLinkedList.class, new SCOCollectionPD(LinkedList.class)); 49 } 50 51 private static void invokeStatement(Object instance, String methodName, 52 Object [] args, Encoder out) { 53 out.writeStatement(new Statement (instance, methodName, args)); 54 } 55 56 private static boolean equals(Object o1, Object o2) { 57 return (o1 == null) ? (o2 == null) : o1.equals(o2); 58 } 59 60 public static class SCOPersistenceDelegate extends DefaultPersistenceDelegate { 61 62 Class javaType; 63 64 public SCOPersistenceDelegate(Class javaType) { 65 this.javaType = javaType; 66 } 67 68 protected Expression instantiate(Object oldInstance, Encoder out) { 69 70 return new Expression (oldInstance, 71 javaType, 72 "new", 73 new Object []{}); 74 } 75 76 protected boolean mutatesTo(Object oldInstance, Object newInstance) { 77 if (oldInstance != null && newInstance != null) { 78 return true; 79 } 80 return super.mutatesTo(oldInstance, newInstance); 81 } 82 } 83 84 public static class SCOCollectionPD extends SCOPersistenceDelegate { 85 86 public SCOCollectionPD(Class javaType) { 87 super(javaType); 88 } 89 90 protected void initialize(Class type, Object oldInstance, 91 Object newInstance, Encoder out) { 92 java.util.Collection oldO = (java.util.Collection ) oldInstance; 93 java.util.Collection newO = (java.util.Collection ) newInstance; 94 95 if (newO.size() != 0) { 96 PersistenceDelegateManager.invokeStatement(oldInstance, "clear", new Object []{}, out); 97 } 98 for (Iterator i = oldO.iterator(); i.hasNext();) { 99 PersistenceDelegateManager.invokeStatement(oldInstance, "add", new Object []{ 100 i.next()}, out); 101 } 102 } 103 } 104 105 public static class SCOListPD extends SCOPersistenceDelegate { 106 107 public SCOListPD(Class javaType) { 108 super(javaType); 109 } 110 111 protected void initialize(Class type, Object oldInstance, 112 Object newInstance, Encoder out) { 113 114 java.util.List oldO = (java.util.List ) oldInstance; 115 java.util.List newO = (java.util.List ) newInstance; 116 int oldSize = oldO.size(); 117 int newSize = (newO == null) ? 0 : newO.size(); 118 if (oldSize < newSize) { 119 PersistenceDelegateManager.invokeStatement(oldInstance, "clear", new Object []{}, out); 120 newSize = 0; 121 } 122 for (int i = 0; i < newSize; i++) { 123 Object index = new Integer (i); 124 125 Expression oldGetExp = new Expression (oldInstance, "get", new Object []{ 126 index}); 127 Expression newGetExp = new Expression (newInstance, "get", new Object []{ 128 index}); 129 try { 130 Object oldValue = oldGetExp.getValue(); 131 Object newValue = newGetExp.getValue(); 132 out.writeExpression(oldGetExp); 133 if (!PersistenceDelegateManager.equals(newValue, out.get(oldValue))) { 134 PersistenceDelegateManager.invokeStatement(oldInstance, "set", new Object []{ 135 index, oldValue}, out); 136 } 137 } catch (Exception e) { 138 out.getExceptionListener().exceptionThrown(e); 139 } 140 } 141 for (int i = newSize; i < oldSize; i++) { 142 PersistenceDelegateManager.invokeStatement(oldInstance, "add", new Object []{ 143 oldO.get(i)}, out); 144 } 145 } 146 147 } 148 149 public static class SCOMapPD extends SCOPersistenceDelegate { 150 151 public SCOMapPD(Class javaType) { 152 super(javaType); 153 } 154 155 protected void initialize(Class type, Object oldInstance, 156 Object newInstance, Encoder out) { 157 java.util.Map oldMap = (java.util.Map ) oldInstance; 158 java.util.Map newMap = (java.util.Map ) newInstance; 159 if (newMap != null) { 162 java.util.Iterator newKeys = newMap.keySet().iterator(); 163 while (newKeys.hasNext()) { 164 Object newKey = newKeys.next(); 165 if (!oldMap.containsKey(newKey)) { 167 PersistenceDelegateManager.invokeStatement(oldInstance, "remove", new Object []{ 168 newKey}, out); 169 } 170 } 171 } 172 java.util.Iterator oldKeys = oldMap.keySet().iterator(); 174 while (oldKeys.hasNext()) { 175 Object oldKey = oldKeys.next(); 176 177 Expression oldGetExp = new Expression (oldInstance, "get", new Object []{ 178 oldKey}); 179 Expression newGetExp = new Expression (newInstance, "get", new Object []{ 181 oldKey}); 182 try { 183 Object oldValue = oldGetExp.getValue(); 184 Object newValue = newGetExp.getValue(); 185 out.writeExpression(oldGetExp); 186 if (!PersistenceDelegateManager.equals(newValue, out.get(oldValue))) { 187 PersistenceDelegateManager.invokeStatement(oldInstance, "put", new Object []{ 188 oldKey, oldValue}, out); 189 } 190 } catch (Exception e) { 191 out.getExceptionListener().exceptionThrown(e); 192 } 193 } 194 } 195 } 196 } 197 | Popular Tags |