1 2 12 package com.versant.core.server; 13 14 import com.versant.core.common.Debug; 15 import com.versant.core.common.OID; 16 import com.versant.core.common.State; 17 import com.versant.core.metadata.ModelMetaData; 18 19 import java.util.Date ; 20 import java.util.Map ; 21 import java.util.HashMap ; 22 23 30 public class PersistGraph extends OIDGraph { 31 32 protected ModelMetaData jmd; 33 protected OID[] oids; 34 protected State[] oldStates; 35 protected State[] newStates; 36 protected Map oidIndexMap; 37 38 public boolean optimistic; 40 public PersistGraph(ModelMetaData jmd, int maxSize) { 41 this.jmd = jmd; 42 oids = new OID[maxSize]; 43 oldStates = new State[maxSize]; 44 newStates = new State[maxSize]; 45 size = 0; 46 oidIndexMap = new HashMap (); 47 } 48 49 52 public void clear() { 53 for (int i = size - 1; i >= 0; i--) { 54 oids[i] = null; 55 oldStates[i] = null; 56 newStates[i] = null; 57 } 58 size = 0; 59 oidIndexMap.clear(); 60 } 61 62 65 public void add(OID oid, State oldState, State newState) { 66 oids[size] = oid; 67 oldStates[size] = oldState; 68 newStates[size] = newState; 69 oidIndexMap.put(oid, new Integer (size++)); 70 } 71 72 75 public int size() { 76 return size; 77 } 78 79 82 public OID getOID(int index) { 83 return oids[index]; 84 } 85 86 89 public State getOldState(int index) { 90 return oldStates[index]; 91 } 92 93 96 public State getNewState(int index) { 97 return newStates[index]; 98 } 99 100 103 public void dump() { 104 for (int i = 0; i < size; i++) dump(i); 105 } 106 107 110 public void dump(int index) { 111 if (Debug.DEBUG) { 112 System.out.println("[" + index + "] " + oids[index] + " " + 113 newStates[index]); 114 } 115 } 116 117 124 public int indexOf(OID oid) { 125 Object o = oidIndexMap.get(oid); 126 return o == null ? -1 : ((Integer )o).intValue(); 127 } 128 129 136 public int indexOfAfterPersist(OID oid) { 137 if (oidIndexMap.size() == 0) { 138 OID[] oids = this.oids; 139 for (int i = size - 1; i >= 0; i--) { 140 oidIndexMap.put(oids[i], new Integer (i)); 141 } 142 } 143 Object o = oidIndexMap.get(oid); 144 return o == null ? -1 : ((Integer )o).intValue(); 145 } 146 147 150 public void doAutoSets() { 151 Date now = new Date (); 152 for (int i = 0; i < size; i++) { 153 OID oid = oids[i]; 154 State ns = newStates[i]; 155 if (ns == null) break; 156 ns.setClassMetaData(oid.getClassMetaData()); 157 if (oid.isNew()) { 158 ns.updateAutoSetFieldsCreated(now); 159 } else { 160 ns.updateAutoSetFieldsModified(now, oldStates[i]); 161 } 162 } 163 } 164 165 171 protected int compare(int a, int b) { 172 final OID oidA = oids[a]; 173 final OID oidB = oids[b]; 174 175 int diff = oidB.getClassMetaData().referenceGraphIndex 176 - oidA.getClassMetaData().referenceGraphIndex; 177 if (diff != 0) return diff; 178 179 boolean newA = oidA.isNew(); 181 boolean newB = oidB.isNew(); 182 if (newA && !newB) return -1; 183 if (!newA && newB) return +1; 184 185 return newStates[a].compareToPass1(newStates[b]); 187 } 188 189 192 protected void swap(int index1, int index2) { 193 OID tempStr = oids[index1]; 194 State tempOldState = oldStates[index1]; 195 State tempNewState = newStates[index1]; 196 197 oids[index1] = oids[index2]; 198 oldStates[index1] = oldStates[index2]; 199 newStates[index1] = newStates[index2]; 200 201 oids[index2] = tempStr; 202 oldStates[index2] = tempOldState; 203 newStates[index2] = tempNewState; 204 } 205 } 206 | Popular Tags |