1 2 12 package com.versant.core.jdo; 13 14 import com.versant.core.common.OID; 15 import com.versant.core.common.NewObjectOID; 16 import com.versant.core.metadata.ModelMetaData; 17 import com.versant.core.metadata.MDStatics; 18 import com.versant.core.metadata.ClassMetaData; 19 20 import java.io.Externalizable ; 21 import java.io.IOException ; 22 import java.io.ObjectInput ; 23 import java.io.ObjectOutput ; 24 25 import com.versant.core.common.BindingSupportImpl; 26 27 32 33 34 35 public final class VersantOid implements Externalizable { 36 37 private transient PMCacheEntry ce; 38 public transient OID actualOID; 39 40 public int classId; 41 public long pk; 42 43 public VersantOid() { 44 } 45 46 public VersantOid(OID actualOID, ModelMetaData jmd, boolean resolved) { 47 this.actualOID = actualOID; 48 if (resolved) { 49 classId = actualOID.getClassMetaData().classId; 50 } else { 51 classId = actualOID.getAvailableClassId(); 52 } 53 pk = actualOID.getLongPrimaryKey(); 54 } 55 56 public VersantOid(PCStateMan sm, ModelMetaData jmd, boolean resolved) { 57 actualOID = sm.oid; 58 if (sm.oid.isNew() && ((NewObjectOID)sm.oid).realOID != null) { 59 actualOID = ((NewObjectOID)sm.oid).realOID; 61 } else { 62 this.ce = sm.cacheEntry; 63 } 64 65 classId = actualOID.getAvailableClassId(); 66 pk = actualOID.getLongPrimaryKey(); 67 } 68 69 public VersantOid(String s) { 70 try { 71 char c = s.charAt(0); 72 int classId = c - '0'; 73 checkClassIDDigit(classId, c, s); 74 int i = 1; 75 for (; ;) { 76 c = s.charAt(i++); 77 if (c == MDStatics.OID_CHAR_SEPERATOR) break; 78 int digit = c - '0'; 79 checkClassIDDigit(digit, c, s); 80 classId = classId * 10 + digit; 81 } 82 this.classId = classId; 83 c = s.charAt(i++); 84 int n = s.length(); 85 long pk = c - '0'; 86 checkPkDigit(pk, c, s); 87 for (; i < n;) { 88 c = s.charAt(i++); 89 int digit = c - '0'; 90 checkPkDigit(digit, c, s); 91 pk = pk * 10 + digit; 92 } 93 this.pk = pk; 94 } catch (Exception e) { 95 if( BindingSupportImpl.getInstance().isOwnInvalidObjectIdException(e) ) 96 { 97 throw (RuntimeException )e; 98 } 99 else 100 { 101 throw BindingSupportImpl.getInstance().invalidObjectId("Invalid OID String: '" + s + "': " + e, 102 e); 103 } 104 } 105 } 106 107 private void checkClassIDDigit(int digit, char c, String s) { 108 if (digit < 0 || digit > 9) { 109 throw BindingSupportImpl.getInstance().invalidObjectId("Invalid digit '" + c + 110 "' in classID for OID String: '" + s + "'", null); 111 } 112 } 113 114 private void checkPkDigit(long digit, char c, String s) { 115 if (digit < 0 || digit > 9) { 116 throw BindingSupportImpl.getInstance().invalidObjectId("Invalid digit '" + c + 117 "' in primary key for OID String: '" + s + "'", null); 118 } 119 } 120 121 public void writeExternal(ObjectOutput out) throws IOException { 122 convertToRealOID(); 123 out.writeInt(classId); 124 out.writeLong(pk); 125 } 126 127 public void readExternal(ObjectInput in) throws IOException { 128 classId = in.readInt(); 129 pk = in.readLong(); 130 } 131 132 public String toString() { 133 convertToRealOID(); 134 return classId + "-" + pk; 135 } 136 137 private void convertToRealOID() { 138 if (actualOID == null) return; 139 if (!actualOID.isNew()) return; 140 NewObjectOID newOid = (NewObjectOID) actualOID; 141 getActualOID(newOid); 142 143 classId = newOid.getCmd().classId; 144 pk = actualOID.getLongPrimaryKey(); 145 } 146 147 150 private void getActualOID(NewObjectOID newOid) { 151 if (newOid.realOID != null) { 152 actualOID = ((NewObjectOID)actualOID).realOID; 153 } else { 154 if (ce != null) { 156 PCStateMan sm = (PCStateMan) ce.get(); 157 if (sm != null && !sm.getPmProxy().isClosed()) { 158 actualOID = sm.getRealOID(); 159 } else { 160 throw BindingSupportImpl.getInstance().exception("The transaction is which this " + 161 "JDO Object Id was used was never finished"); 162 } 163 } else { 164 throw BindingSupportImpl.getInstance().exception("The transaction is which this " + 165 "JDO Object Id was used was never finished"); 166 } 167 } 168 } 169 170 public boolean equals(Object object) { 171 convertToRealOID(); 172 if (object instanceof VersantOid) { 173 VersantOid o = (VersantOid)object; 174 o.convertToRealOID(); 175 return classId == o.classId && pk == o.pk; 176 } 177 return false; 178 } 179 180 public int hashCode() { 181 convertToRealOID(); 182 return classId + (int)pk * 29; 183 } 184 185 186 } 187 | Popular Tags |