KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > VersantOid


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

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 JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectInput JavaDoc;
23 import java.io.ObjectOutput JavaDoc;
24
25 import com.versant.core.common.BindingSupportImpl;
26
27 /**
28  * This is the JDO Genie specific class that represents datastore id
29  * instances for usage by users. Requests for the id instance of an
30  * datastore instance will return this and the same for getObjectIdClass.
31  */

32
33
34  
35 public final class VersantOid implements Externalizable JavaDoc {
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             //don't set the sm if the actual oid is available.
60
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 JavaDoc 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 JavaDoc e) {
95             if( BindingSupportImpl.getInstance().isOwnInvalidObjectIdException(e) )
96             {
97                 throw (RuntimeException JavaDoc)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 JavaDoc 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 JavaDoc 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 JavaDoc out) throws IOException JavaDoc {
122         convertToRealOID();
123         out.writeInt(classId);
124         out.writeLong(pk);
125     }
126
127     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc {
128         classId = in.readInt();
129         pk = in.readLong();
130     }
131
132     public String JavaDoc 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     /**
148      * Attempt to obtain the real oid for this newOid.
149      */

150     private void getActualOID(NewObjectOID newOid) {
151         if (newOid.realOID != null) {
152             actualOID = ((NewObjectOID)actualOID).realOID;
153         } else {
154             //try from sm
155
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 JavaDoc 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