Home » openejb-3.1.2-src » org.apache » openejb » core » cmp » [javadoc | source]

    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   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    */
   18   package org.apache.openejb.core.cmp;
   19   
   20   import java.lang.reflect.Field;
   21   import java.util.ArrayList;
   22   import java.util.Collections;
   23   import java.util.List;
   24   import javax.ejb.EJBException;
   25   import javax.ejb.EntityBean;
   26   
   27   import org.apache.openejb.OpenEJBException;
   28   
   29   public class ComplexKeyGenerator extends AbstractKeyGenerator {
   30       protected final List<PkField> fields;
   31       private final Class pkClass;
   32   
   33       public ComplexKeyGenerator(Class entityBeanClass, Class pkClass) 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, pkObjectField.getName());
   39                   if (!isValidPkField(entityBeanField)) {
   40                       throw new OpenEJBException("Invalid primray key field: " + entityBeanField);
   41                   }
   42                   PkField pkField = new PkField(entityBeanField, pkObjectField);
   43                   fields.add(pkField);
   44               }
   45           }
   46           this.fields = Collections.unmodifiableList(fields);
   47       }
   48   
   49       public Object getPrimaryKey(EntityBean bean) {
   50           Object pkObject = null;
   51           try {
   52               pkObject = pkClass.newInstance();
   53           } catch (Exception e) {
   54               throw new EJBException("Unable to create complex primary key instance: " + pkClass.getName(), e);
   55           }
   56   
   57           for (PkField pkField : fields) {
   58               pkField.copyToPkObject(bean, pkObject);
   59           }
   60           return pkObject;
   61       }
   62   
   63       protected static class PkField {
   64           private final Field entityBeanField;
   65           private final Field pkObjectField;
   66   
   67           public PkField(Field entityBeanField, Field pkObjectField) {
   68               entityBeanField.setAccessible(true);
   69               pkObjectField.setAccessible(true);
   70               
   71               this.entityBeanField = entityBeanField;
   72               this.pkObjectField = pkObjectField;
   73           }
   74   
   75           public void copyToPkObject(EntityBean bean, Object pkObject) {
   76               Object value = getFieldValue(entityBeanField, bean);
   77               setFieldValue(pkObjectField, pkObject, value);
   78           }
   79   
   80           public Object getPkFieldValue(Object pkObject) {
   81               Object value = getFieldValue(pkObjectField, pkObject);
   82               return value;
   83           }
   84       }
   85   }

Home » openejb-3.1.2-src » org.apache » openejb » core » cmp » [javadoc | source]