KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > containers > castor_cmp11 > KeyGeneratorFactory


1 package org.openejb.alt.containers.castor_cmp11;
2
3 import java.util.HashMap JavaDoc;
4
5 import org.openejb.core.DeploymentInfo;
6 import org.openejb.util.Logger;
7
8 public abstract class KeyGeneratorFactory {
9     protected final static Logger logger = Logger.getInstance("CastorCMP", "org.openejb.alt.util.resources");
10
11     private static class PrimitiveKey implements org.openejb.alt.containers.castor_cmp11.KeyGenerator{
12         private final java.lang.reflect.Field JavaDoc field;
13         
14         PrimitiveKey(DeploymentInfo di) throws org.openejb.OpenEJBException{
15             field = di.getPrimaryKeyField();
16         }
17
18         public java.lang.Object JavaDoc getPrimaryKey(javax.ejb.EntityBean JavaDoc bean){
19             try{
20                 return field.get(bean);
21             }catch(Exception JavaDoc e) {
22                 e.printStackTrace();
23                 throw new IllegalStateException JavaDoc("Don't have access to field "+field+": received "+e);
24             }
25         }
26
27         public org.exolab.castor.persist.spi.Complex getJdoComplex(java.lang.Object JavaDoc primaryKey){
28             throw new IllegalStateException JavaDoc("This method must not be called on primitive primary keys");
29         }
30
31         public boolean isKeyComplex( ){
32             return false;
33         }
34     }
35
36     private static class ComplexKey implements org.openejb.alt.containers.castor_cmp11.KeyGenerator{
37         private final java.lang.Class JavaDoc pkClass;
38         private final Object JavaDoc[] cmFields;
39         private final HashMap JavaDoc pkFieldMap;
40         private final HashMap JavaDoc beanFieldMap;
41         
42         ComplexKey(DeploymentInfo di) throws org.openejb.OpenEJBException{
43             pkClass = di.getPrimaryKeyClass();
44             Class JavaDoc beanClass= di.getBeanClass();
45             java.util.List JavaDoc v= new java.util.ArrayList JavaDoc();
46             java.lang.reflect.Field JavaDoc[] fields=pkClass.getFields();
47             pkFieldMap = new HashMap JavaDoc();
48             beanFieldMap = new HashMap JavaDoc();
49             for(int i=0; i<fields.length; ++i) {
50                 String JavaDoc fieldName=fields[i].getName();
51                 try{
52                     beanFieldMap.put(fieldName, beanClass.getField(fieldName));
53                     pkFieldMap.put(fieldName, fields[i]);
54                     v.add(fieldName);
55                 }catch(java.lang.NoSuchFieldException JavaDoc e) {
56                     // we skip this field. It's not in the intersection of both classes
57
}
58             }
59             cmFields=new Object JavaDoc[v.size()];
60             v.toArray(cmFields);
61         }
62
63         
64         public java.lang.Object JavaDoc getPrimaryKey(javax.ejb.EntityBean JavaDoc bean){
65             try{
66                 Object JavaDoc pk = pkClass.newInstance();
67                 for(int i=0; i<cmFields.length; ++i) {
68                     Object JavaDoc fieldName=cmFields[i];
69                     Object JavaDoc value = ((java.lang.reflect.Field JavaDoc) beanFieldMap.get(fieldName)).get(bean);
70                     ((java.lang.reflect.Field JavaDoc) pkFieldMap.get(fieldName)).set(pk, value);
71                 }
72                 return pk;
73             }catch(Exception JavaDoc e) {
74                 logger.error("getPrimaryKey() received exception "+e, e);
75                 throw new IllegalStateException JavaDoc();
76             }
77         }
78
79         public org.exolab.castor.persist.spi.Complex getJdoComplex(java.lang.Object JavaDoc primaryKey){
80             int len = cmFields.length;
81             java.lang.Object JavaDoc [] args = new java.lang.Object JavaDoc[len];
82             try{
83                 for(int i=0; i<len; ++i) {
84                     args[i] = ((java.lang.reflect.Field JavaDoc) pkFieldMap.get(cmFields[i])).get(primaryKey);
85                 }
86                 return new org.exolab.castor.persist.spi.Complex(len, args);
87             }catch(Exception JavaDoc e) {
88                 logger.error("getJdoComplex() received exception "+e, e);
89                 throw new IllegalStateException JavaDoc();
90             }
91         }
92
93         public boolean isKeyComplex( ){
94             return true;
95         }
96     }
97     
98     public static KeyGenerator createKeyGenerator(DeploymentInfo di)
99     throws org.openejb.OpenEJBException{
100
101         if(di.getPrimaryKeyField()!=null) {
102             return new PrimitiveKey(di);
103         } else {
104             return new ComplexKey(di);
105         }
106     }
107 }
108
109
Popular Tags