01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package org.apache.openejb.core.cmp;
18:
19: import java.lang.reflect.Field;
20: import java.util.ArrayList;
21: import java.util.Collections;
22: import java.util.List;
23: import javax.ejb.EJBException;
24: import javax.ejb.EntityBean;
25:
26: import org.apache.openejb.OpenEJBException;
27:
28: public class ComplexKeyGenerator extends AbstractKeyGenerator {
29: protected final List<PkField> fields;
30: private final Class pkClass;
31:
32: public ComplexKeyGenerator(Class entityBeanClass, Class pkClass)
33: throws OpenEJBException {
34: this .pkClass = pkClass;
35: List<org.apache.openejb.core.cmp.ComplexKeyGenerator.PkField> fields = new ArrayList<PkField>();
36: for (Field pkObjectField : pkClass.getFields()) {
37: if (isValidPkField(pkObjectField)) {
38: Field entityBeanField = getField(entityBeanClass,
39: pkObjectField.getName());
40: if (!isValidPkField(entityBeanField)) {
41: throw new OpenEJBException(
42: "Invalid primray key field: "
43: + entityBeanField);
44: }
45: PkField pkField = new PkField(entityBeanField,
46: pkObjectField);
47: fields.add(pkField);
48: }
49: }
50: this .fields = Collections.unmodifiableList(fields);
51: }
52:
53: public Object getPrimaryKey(EntityBean bean) {
54: Object pkObject = null;
55: try {
56: pkObject = pkClass.newInstance();
57: } catch (Exception e) {
58: throw new EJBException(
59: "Unable to create complex primary key instance: "
60: + pkClass.getName(), e);
61: }
62:
63: for (PkField pkField : fields) {
64: pkField.copyToPkObject(bean, pkObject);
65: }
66: return pkObject;
67: }
68:
69: protected static class PkField {
70: private final Field entityBeanField;
71: private final Field pkObjectField;
72:
73: public PkField(Field entityBeanField, Field pkObjectField) {
74: entityBeanField.setAccessible(true);
75: pkObjectField.setAccessible(true);
76:
77: this .entityBeanField = entityBeanField;
78: this .pkObjectField = pkObjectField;
79: }
80:
81: public void copyToPkObject(EntityBean bean, Object pkObject) {
82: Object value = getFieldValue(entityBeanField, bean);
83: setFieldValue(pkObjectField, pkObject, value);
84: }
85:
86: public Object getPkFieldValue(Object pkObject) {
87: Object value = getFieldValue(pkObjectField, pkObject);
88: return value;
89: }
90: }
91: }
|