|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package org.kuali.rice.krad.service.impl;  | 
  |  17 |     | 
     | 
  |  18 |     | 
   import java.beans.PropertyDescriptor;  | 
  |  19 |     | 
   import java.util.ArrayList;  | 
  |  20 |     | 
   import java.util.Collection;  | 
  |  21 |     | 
   import java.util.HashMap;  | 
  |  22 |     | 
   import java.util.Iterator;  | 
  |  23 |     | 
   import java.util.List;  | 
  |  24 |     | 
   import java.util.Map;  | 
  |  25 |     | 
   import java.util.Set;  | 
  |  26 |     | 
     | 
  |  27 |     | 
   import org.apache.commons.beanutils.PropertyUtils;  | 
  |  28 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  29 |     | 
   import org.kuali.rice.core.framework.persistence.jpa.metadata.EntityDescriptor;  | 
  |  30 |     | 
   import org.kuali.rice.core.framework.persistence.jpa.metadata.JoinColumnDescriptor;  | 
  |  31 |     | 
   import org.kuali.rice.core.framework.persistence.jpa.metadata.MetadataManager;  | 
  |  32 |     | 
   import org.kuali.rice.core.framework.persistence.jpa.metadata.ObjectDescriptor;  | 
  |  33 |     | 
   import org.kuali.rice.krad.bo.DataObjectRelationship;  | 
  |  34 |     | 
   import org.kuali.rice.krad.bo.PersistableBusinessObject;  | 
  |  35 |     | 
   import org.kuali.rice.krad.exception.ObjectNotABusinessObjectRuntimeException;  | 
  |  36 |     | 
   import org.kuali.rice.krad.exception.ReferenceAttributeDoesntExistException;  | 
  |  37 |     | 
   import org.kuali.rice.krad.exception.ReferenceAttributeNotAnOjbReferenceException;  | 
  |  38 |     | 
   import org.kuali.rice.krad.service.PersistenceStructureService;  | 
  |  39 |     | 
   import org.kuali.rice.krad.util.ForeignKeyFieldsPopulationState;  | 
  |  40 |     | 
   import org.kuali.rice.krad.util.spring.Cached;  | 
  |  41 |     | 
     | 
  |  42 |    0 |    public class PersistenceStructureServiceJpaImpl extends PersistenceServiceImplBase implements PersistenceStructureService { | 
  |  43 |     | 
     | 
  |  44 |     | 
             | 
  |  45 |     | 
     | 
  |  46 |     | 
     | 
  |  47 |     | 
     | 
  |  48 |     | 
     | 
  |  49 |     | 
     | 
  |  50 |     | 
     | 
  |  51 |     | 
     | 
  |  52 |     | 
     | 
  |  53 |     | 
     | 
  |  54 |     | 
     | 
  |  55 |     | 
     | 
  |  56 |     | 
     | 
  |  57 |     | 
     | 
  |  58 |     | 
     | 
  |  59 |    0 |            public static Map<Class, Class> referenceConversionMap = new HashMap<Class, Class>();  | 
  |  60 |     | 
     | 
  |  61 |     | 
             | 
  |  62 |     | 
     | 
  |  63 |     | 
     | 
  |  64 |     | 
           @Cached  | 
  |  65 |     | 
           public boolean isPersistable(Class clazz) { | 
  |  66 |    0 |                    boolean isPersistable = false;  | 
  |  67 |     | 
     | 
  |  68 |    0 |                    if (MetadataManager.getEntityDescriptor(clazz) != null) { | 
  |  69 |    0 |                            isPersistable = true;  | 
  |  70 |     | 
                   }  | 
  |  71 |     | 
     | 
  |  72 |    0 |                    return isPersistable;  | 
  |  73 |     | 
           }  | 
  |  74 |     | 
     | 
  |  75 |     | 
             | 
  |  76 |     | 
     | 
  |  77 |     | 
     | 
  |  78 |     | 
           @Cached  | 
  |  79 |     | 
           public List getPrimaryKeys(Class clazz) { | 
  |  80 |    0 |                    List pkList = new ArrayList();  | 
  |  81 |     | 
     | 
  |  82 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(clazz);  | 
  |  83 |    0 |                    for (org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor field : descriptor.getPrimaryKeys()) { | 
  |  84 |    0 |                            pkList.add(field.getName());  | 
  |  85 |     | 
                   }  | 
  |  86 |     | 
     | 
  |  87 |    0 |                    return pkList;  | 
  |  88 |     | 
           }  | 
  |  89 |     | 
     | 
  |  90 |     | 
             | 
  |  91 |     | 
     | 
  |  92 |     | 
     | 
  |  93 |     | 
           @Cached  | 
  |  94 |     | 
           public List listFieldNames(Class clazz) { | 
  |  95 |    0 |                    List fieldNames = new ArrayList();  | 
  |  96 |     | 
     | 
  |  97 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(clazz);  | 
  |  98 |    0 |                    for (org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor field : descriptor.getFields()) { | 
  |  99 |    0 |                            fieldNames.add(field.getName());  | 
  |  100 |     | 
                   }  | 
  |  101 |     | 
     | 
  |  102 |    0 |                    return fieldNames;  | 
  |  103 |     | 
           }  | 
  |  104 |     | 
     | 
  |  105 |     | 
             | 
  |  106 |     | 
     | 
  |  107 |     | 
     | 
  |  108 |     | 
             | 
  |  109 |     | 
             | 
  |  110 |     | 
           public Object clearPrimaryKeyFields(Object persistableObject) { | 
  |  111 |     | 
                     | 
  |  112 |     | 
     | 
  |  113 |     | 
     | 
  |  114 |     | 
     | 
  |  115 |     | 
     | 
  |  116 |     | 
     | 
  |  117 |     | 
     | 
  |  118 |     | 
     | 
  |  119 |     | 
     | 
  |  120 |     | 
     | 
  |  121 |     | 
     | 
  |  122 |     | 
     | 
  |  123 |     | 
     | 
  |  124 |     | 
     | 
  |  125 |     | 
     | 
  |  126 |     | 
     | 
  |  127 |     | 
     | 
  |  128 |     | 
     | 
  |  129 |     | 
     | 
  |  130 |     | 
     | 
  |  131 |     | 
     | 
  |  132 |    0 |                    return persistableObject;  | 
  |  133 |     | 
           }  | 
  |  134 |     | 
     | 
  |  135 |     | 
             | 
  |  136 |     | 
     | 
  |  137 |     | 
     | 
  |  138 |     | 
           @Cached  | 
  |  139 |     | 
             | 
  |  140 |     | 
             | 
  |  141 |     | 
           public List listPersistableSubclasses(Class superclazz) { | 
  |  142 |    0 |                    List persistableSubclasses = new ArrayList();  | 
  |  143 |     | 
                     | 
  |  144 |     | 
     | 
  |  145 |     | 
     | 
  |  146 |     | 
     | 
  |  147 |     | 
     | 
  |  148 |     | 
     | 
  |  149 |     | 
     | 
  |  150 |     | 
     | 
  |  151 |     | 
     | 
  |  152 |     | 
     | 
  |  153 |     | 
     | 
  |  154 |     | 
     | 
  |  155 |     | 
     | 
  |  156 |     | 
     | 
  |  157 |    0 |                    return persistableSubclasses;  | 
  |  158 |     | 
           }  | 
  |  159 |     | 
     | 
  |  160 |     | 
             | 
  |  161 |     | 
     | 
  |  162 |     | 
     | 
  |  163 |     | 
     | 
  |  164 |     | 
           @Cached  | 
  |  165 |     | 
           public Map<String, DataObjectRelationship> getRelationshipMetadata(Class persistableClass, String attributeName, String attributePrefix) { | 
  |  166 |    0 |                    if (persistableClass == null) { | 
  |  167 |    0 |                            throw new IllegalArgumentException("invalid (null) persistableClass"); | 
  |  168 |     | 
                   }  | 
  |  169 |    0 |                    if (StringUtils.isBlank(attributeName)) { | 
  |  170 |    0 |                            throw new IllegalArgumentException("invalid (blank) attributeName"); | 
  |  171 |     | 
                   }  | 
  |  172 |     | 
     | 
  |  173 |    0 |                    Map<String, DataObjectRelationship> relationships = new HashMap<String, DataObjectRelationship>();  | 
  |  174 |     | 
     | 
  |  175 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableClass);  | 
  |  176 |    0 |                    for (ObjectDescriptor objectDescriptor : descriptor.getObjectRelationships()) { | 
  |  177 |    0 |                            List<String> fks = objectDescriptor.getForeignKeyFields();  | 
  |  178 |    0 |                            if (fks.contains(attributeName) || objectDescriptor.getAttributeName().equals(attributeName)) { | 
  |  179 |    0 |                                    Map<String, String> fkToPkRefs = getForeignKeysForReference(persistableClass, objectDescriptor.getAttributeName());  | 
  |  180 |     | 
                                   DataObjectRelationship  | 
  |  181 |    0 |                            rel = new DataObjectRelationship(persistableClass, objectDescriptor.getAttributeName(), objectDescriptor.getTargetEntity());  | 
  |  182 |    0 |                                    for (Map.Entry<String, String> ref : fkToPkRefs.entrySet()) { | 
  |  183 |    0 |                                            if (StringUtils.isBlank(attributePrefix)) { | 
  |  184 |    0 |                                                    rel.getParentToChildReferences().put(ref.getKey(), ref.getValue());  | 
  |  185 |     | 
                                           } else { | 
  |  186 |    0 |                                                    rel.getParentToChildReferences().put(attributePrefix + "." + ref.getKey(), ref.getValue());  | 
  |  187 |     | 
                                           }  | 
  |  188 |     | 
                                   }  | 
  |  189 |    0 |                                    relationships.put(objectDescriptor.getAttributeName(), rel);  | 
  |  190 |     | 
                           }  | 
  |  191 |    0 |                    }  | 
  |  192 |     | 
     | 
  |  193 |    0 |                    return relationships;  | 
  |  194 |     | 
           }  | 
  |  195 |     | 
     | 
  |  196 |     | 
           @Cached  | 
  |  197 |     | 
             | 
  |  198 |     | 
           public Map<String, DataObjectRelationship> getRelationshipMetadata(Class persistableClass, String attributeName) { | 
  |  199 |    0 |                    return getRelationshipMetadata(persistableClass, attributeName, null);  | 
  |  200 |     | 
           }  | 
  |  201 |     | 
     | 
  |  202 |     | 
             | 
  |  203 |     | 
     | 
  |  204 |     | 
     | 
  |  205 |     | 
     | 
  |  206 |     | 
           @Cached  | 
  |  207 |     | 
           public String getForeignKeyFieldName(Class persistableObjectClass, String attributeName, String pkName) { | 
  |  208 |    0 |                    String fkName = null;  | 
  |  209 |     | 
     | 
  |  210 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableObjectClass);  | 
  |  211 |    0 |                    ObjectDescriptor objectDescriptor = descriptor.getObjectDescriptorByName(attributeName);  | 
  |  212 |    0 |                    if (objectDescriptor == null) { | 
  |  213 |    0 |                            throw new RuntimeException("Attribute name " + attributeName + " is not a valid reference to class " + persistableObjectClass.getName()); | 
  |  214 |     | 
                   }  | 
  |  215 |    0 |                    List<org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor> matches = new ArrayList<org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor>();  | 
  |  216 |    0 |                    for (org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor field : descriptor.getFields()) { | 
  |  217 |    0 |                            String column = field.getColumn();  | 
  |  218 |    0 |                            for (JoinColumnDescriptor join : objectDescriptor.getJoinColumnDescriptors()) { | 
  |  219 |    0 |                                    if (column != null && column.equals(join.getName())) { | 
  |  220 |    0 |                                            matches.add(field);  | 
  |  221 |     | 
                                   }  | 
  |  222 |     | 
                           }  | 
  |  223 |    0 |                    }  | 
  |  224 |     | 
     | 
  |  225 |    0 |                    if (matches.size() == 1) { | 
  |  226 |    0 |                            fkName = matches.get(0).getName();  | 
  |  227 |     | 
                   } else { | 
  |  228 |    0 |                            throw new RuntimeException("Implement me!"); | 
  |  229 |     | 
                   }  | 
  |  230 |     | 
     | 
  |  231 |    0 |                    return fkName;  | 
  |  232 |     | 
           }  | 
  |  233 |     | 
     | 
  |  234 |     | 
             | 
  |  235 |     | 
     | 
  |  236 |     | 
     | 
  |  237 |     | 
     | 
  |  238 |     | 
           @Cached  | 
  |  239 |     | 
           public Map getReferencesForForeignKey(Class persistableObjectClass, String attributeName) { | 
  |  240 |    0 |                    Map referenceClasses = new HashMap();  | 
  |  241 |     | 
     | 
  |  242 |    0 |                    if (PersistableBusinessObject.class.isAssignableFrom(persistableObjectClass)) { | 
  |  243 |    0 |                            EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableObjectClass);  | 
  |  244 |    0 |                            for (ObjectDescriptor objectDescriptor : descriptor.getObjectRelationships()) { | 
  |  245 |    0 |                                    List<String> refFkNames = objectDescriptor.getForeignKeyFields();  | 
  |  246 |    0 |                                    for (String fk : refFkNames) { | 
  |  247 |    0 |                                            if (fk.equals(attributeName)) { | 
  |  248 |    0 |                                                    referenceClasses.put(objectDescriptor.getAttributeName(), objectDescriptor.getTargetEntity());  | 
  |  249 |     | 
                                           }  | 
  |  250 |     | 
                                   }  | 
  |  251 |    0 |                            }  | 
  |  252 |     | 
                   }  | 
  |  253 |     | 
     | 
  |  254 |    0 |                    return referenceClasses;  | 
  |  255 |     | 
           }  | 
  |  256 |     | 
     | 
  |  257 |     | 
             | 
  |  258 |     | 
     | 
  |  259 |     | 
     | 
  |  260 |     | 
     | 
  |  261 |     | 
     | 
  |  262 |     | 
     | 
  |  263 |     | 
     | 
  |  264 |     | 
     | 
  |  265 |     | 
     | 
  |  266 |     | 
     | 
  |  267 |     | 
     | 
  |  268 |     | 
     | 
  |  269 |     | 
     | 
  |  270 |     | 
     | 
  |  271 |     | 
     | 
  |  272 |     | 
     | 
  |  273 |     | 
           @Cached  | 
  |  274 |     | 
           public Map getForeignKeysForReference(Class clazz, String attributeName) { | 
  |  275 |     | 
     | 
  |  276 |     | 
                     | 
  |  277 |    0 |                    if (clazz == null) { | 
  |  278 |    0 |                            throw new IllegalArgumentException("The Class passed in for the clazz argument was null."); | 
  |  279 |     | 
                   }  | 
  |  280 |    0 |                    if (attributeName == null) { | 
  |  281 |    0 |                            throw new IllegalArgumentException("The String passed in for the attributeName argument was null."); | 
  |  282 |     | 
                   }  | 
  |  283 |     | 
     | 
  |  284 |     | 
                     | 
  |  285 |    0 |                    Class attributeClass = getBusinessObjectAttributeClass(clazz, attributeName);  | 
  |  286 |    0 |                    if (attributeClass == null) { | 
  |  287 |    0 |                            throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + attributeName + "' does not exist on class: '" + clazz.getName() + "'."); | 
  |  288 |     | 
                   }  | 
  |  289 |     | 
     | 
  |  290 |     | 
                     | 
  |  291 |     | 
                     | 
  |  292 |    0 |                    if (!PersistableBusinessObject.class.isAssignableFrom(attributeClass)) { | 
  |  293 |    0 |                            throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + attributeName + ") is of class: '" + attributeClass.getName() + "' and is not a descendent of BusinessObject.  Only descendents of BusinessObject can be used."); | 
  |  294 |     | 
                   }  | 
  |  295 |     | 
     | 
  |  296 |    0 |                    return determineFkMap(clazz, attributeName, attributeClass);  | 
  |  297 |     | 
           }  | 
  |  298 |     | 
     | 
  |  299 |     | 
           private Map determineFkMap(Class clazz, String attributeName, Class attributeClass) { | 
  |  300 |    0 |                    Map fkMap = new HashMap();  | 
  |  301 |    0 |                    EntityDescriptor entityDescriptor = MetadataManager.getEntityDescriptor(clazz);  | 
  |  302 |    0 |                    ObjectDescriptor objectDescriptor = entityDescriptor.getObjectDescriptorByName(attributeName);  | 
  |  303 |    0 |                    if (objectDescriptor == null) { | 
  |  304 |    0 |                            throw new ReferenceAttributeNotAnOjbReferenceException("Attribute requested (" + attributeName + ") is not defined in JPA annotations for class: '" + clazz.getName() + "'"); | 
  |  305 |     | 
                   }  | 
  |  306 |     | 
     | 
  |  307 |     | 
                     | 
  |  308 |     | 
                     | 
  |  309 |     | 
                     | 
  |  310 |     | 
                     | 
  |  311 |     | 
                     | 
  |  312 |    0 |                    if (!attributeClass.equals(objectDescriptor.getTargetEntity())) { | 
  |  313 |    0 |                            if (referenceConversionMap.containsKey(attributeClass)) { | 
  |  314 |    0 |                                    attributeClass = referenceConversionMap.get(attributeClass);  | 
  |  315 |     | 
                           } else { | 
  |  316 |    0 |                                    throw new RuntimeException("The Class of the Java member [" + attributeClass.getName() + "] '" + attributeName + "' does not match the class of the reference [" + objectDescriptor.getTargetEntity().getName() + "]. " + "This is an unhandled special case for which special code needs to be written in this class."); | 
  |  317 |     | 
                           }  | 
  |  318 |     | 
                   }  | 
  |  319 |     | 
     | 
  |  320 |     | 
                     | 
  |  321 |     | 
                     | 
  |  322 |    0 |                    List<String> fkFields = objectDescriptor.getForeignKeyFields();  | 
  |  323 |    0 |                    Iterator<String> fkIterator = fkFields.iterator();  | 
  |  324 |     | 
     | 
  |  325 |     | 
                     | 
  |  326 |     | 
                     | 
  |  327 |    0 |                    List pkFields = getPrimaryKeys(attributeClass);  | 
  |  328 |    0 |                    Iterator pkIterator = pkFields.iterator();  | 
  |  329 |     | 
     | 
  |  330 |     | 
                     | 
  |  331 |     | 
                     | 
  |  332 |    0 |                    if (pkFields.size() != fkFields.size()) { | 
  |  333 |    0 |                            throw new RuntimeException("KualiPersistenceStructureService Error: The number of " + "foreign keys doesnt match the number of primary keys."); | 
  |  334 |     | 
                   }  | 
  |  335 |     | 
     | 
  |  336 |     | 
                     | 
  |  337 |    0 |                    while (fkIterator.hasNext()) { | 
  |  338 |     | 
                             | 
  |  339 |     | 
                             | 
  |  340 |     | 
                             | 
  |  341 |    0 |                            if (!pkIterator.hasNext()) { | 
  |  342 |    0 |                                    throw new RuntimeException("The number of foriegn keys dont match the number of primary keys for the reference '" + attributeName + "', on BO of type '" + clazz.getName() + "'.  " + "This should never happen under normal circumstances."); | 
  |  343 |     | 
                           }  | 
  |  344 |     | 
     | 
  |  345 |     | 
                             | 
  |  346 |    0 |                            String fkFieldName = (String) fkIterator.next();  | 
  |  347 |    0 |                            String pkFieldName = (String) pkIterator.next();  | 
  |  348 |     | 
     | 
  |  349 |     | 
                             | 
  |  350 |    0 |                            fkMap.put(fkFieldName, pkFieldName);  | 
  |  351 |    0 |                    }  | 
  |  352 |    0 |                    return fkMap;  | 
  |  353 |     | 
           }  | 
  |  354 |     | 
     | 
  |  355 |     | 
           @Cached  | 
  |  356 |     | 
           public Map<String, String> getInverseForeignKeysForCollection(Class boClass, String collectionName) { | 
  |  357 |     | 
                     | 
  |  358 |    0 |                    if (boClass == null) { | 
  |  359 |    0 |                            throw new IllegalArgumentException("The Class passed in for the boClass argument was null."); | 
  |  360 |     | 
                   }  | 
  |  361 |    0 |                    if (collectionName == null) { | 
  |  362 |    0 |                            throw new IllegalArgumentException("The String passed in for the attributeName argument was null."); | 
  |  363 |     | 
                   }  | 
  |  364 |     | 
     | 
  |  365 |    0 |                    PropertyDescriptor propertyDescriptor = null;  | 
  |  366 |     | 
     | 
  |  367 |     | 
                     | 
  |  368 |     | 
                   Object classInstance;  | 
  |  369 |     | 
                   try { | 
  |  370 |    0 |                            classInstance = boClass.newInstance();  | 
  |  371 |    0 |                    } catch (Exception e) { | 
  |  372 |    0 |                            throw new RuntimeException(e);  | 
  |  373 |    0 |                    }  | 
  |  374 |     | 
     | 
  |  375 |     | 
                     | 
  |  376 |     | 
                   try { | 
  |  377 |    0 |                            propertyDescriptor = PropertyUtils.getPropertyDescriptor(classInstance, collectionName);  | 
  |  378 |    0 |                    } catch (Exception e) { | 
  |  379 |    0 |                            throw new RuntimeException(e);  | 
  |  380 |    0 |                    }  | 
  |  381 |    0 |                    if (propertyDescriptor == null) { | 
  |  382 |    0 |                            throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + collectionName + "' does not exist " + "on class: '" + boClass.getName() + "'. GFK"); | 
  |  383 |     | 
                   }  | 
  |  384 |     | 
     | 
  |  385 |     | 
                     | 
  |  386 |    0 |                    Class attributeClass = propertyDescriptor.getPropertyType();  | 
  |  387 |     | 
     | 
  |  388 |     | 
                     | 
  |  389 |     | 
                     | 
  |  390 |    0 |                    if (!Collection.class.isAssignableFrom(attributeClass)) { | 
  |  391 |    0 |                            throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + collectionName + ") is of class: " + "'" + attributeClass.getName() + "' and is not a " + "descendent of Collection"); | 
  |  392 |     | 
                   }  | 
  |  393 |     | 
     | 
  |  394 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass);  | 
  |  395 |    0 |                    org.kuali.rice.core.framework.persistence.jpa.metadata.CollectionDescriptor cd = descriptor.getCollectionDescriptorByName(collectionName);  | 
  |  396 |    0 |                    List<String> childPrimaryKeys = cd.getForeignKeyFields();  | 
  |  397 |     | 
     | 
  |  398 |    0 |                    List parentForeignKeys = getPrimaryKeys(boClass);  | 
  |  399 |     | 
     | 
  |  400 |    0 |                    if (parentForeignKeys.size() != childPrimaryKeys.size()) { | 
  |  401 |    0 |                            throw new RuntimeException("The number of keys in the class descriptor and the inverse foreign key mapping for the collection descriptors do not match."); | 
  |  402 |     | 
                   }  | 
  |  403 |     | 
     | 
  |  404 |    0 |                    Map<String, String> fkToPkMap = new HashMap<String, String>();  | 
  |  405 |    0 |                    Iterator pFKIter = parentForeignKeys.iterator();  | 
  |  406 |    0 |                    Iterator cPKIterator = childPrimaryKeys.iterator();  | 
  |  407 |     | 
     | 
  |  408 |    0 |                    while (pFKIter.hasNext()) { | 
  |  409 |    0 |                            String parentForeignKey = (String) pFKIter.next();  | 
  |  410 |    0 |                            String childPrimaryKey = (String) cPKIterator.next();  | 
  |  411 |     | 
     | 
  |  412 |    0 |                            fkToPkMap.put(parentForeignKey, childPrimaryKey);  | 
  |  413 |    0 |                    }  | 
  |  414 |    0 |                    return fkToPkMap;  | 
  |  415 |     | 
           }  | 
  |  416 |     | 
     | 
  |  417 |     | 
             | 
  |  418 |     | 
     | 
  |  419 |     | 
     | 
  |  420 |     | 
           @Cached  | 
  |  421 |     | 
           public Map getNestedForeignKeyMap(Class persistableObjectClass) { | 
  |  422 |    0 |                    Map fkMap = new HashMap();  | 
  |  423 |     | 
     | 
  |  424 |     | 
                     | 
  |  425 |     | 
                     | 
  |  426 |     | 
                     | 
  |  427 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableObjectClass);  | 
  |  428 |    0 |                    for (ObjectDescriptor objectReferenceDescriptor : descriptor.getObjectRelationships()) { | 
  |  429 |    0 |                            EntityDescriptor referenceDescriptor = MetadataManager.getEntityDescriptor(objectReferenceDescriptor.getTargetEntity());  | 
  |  430 |    0 |                            List<String> fkFields = objectReferenceDescriptor.getForeignKeyFields();  | 
  |  431 |    0 |                            Set<org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor> pkFields = referenceDescriptor.getPrimaryKeys();  | 
  |  432 |    0 |                            int i = 0;  | 
  |  433 |    0 |                            for (org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor fd : pkFields) { | 
  |  434 |    0 |                                    fkMap.put(objectReferenceDescriptor.getAttributeName() + "." + fd.getName(), fkFields.get(i));  | 
  |  435 |    0 |                                    i++;  | 
  |  436 |     | 
                           }  | 
  |  437 |    0 |                    }  | 
  |  438 |     | 
     | 
  |  439 |    0 |                    return fkMap;  | 
  |  440 |     | 
           }  | 
  |  441 |     | 
     | 
  |  442 |     | 
             | 
  |  443 |     | 
     | 
  |  444 |     | 
     | 
  |  445 |     | 
           public boolean hasPrimaryKeyFieldValues(Object persistableObject) { | 
  |  446 |    0 |                    Map keyFields = getPrimaryKeyFieldValues(persistableObject);  | 
  |  447 |     | 
     | 
  |  448 |    0 |                    boolean emptyField = false;  | 
  |  449 |    0 |                    for (Iterator i = keyFields.entrySet().iterator(); !emptyField && i.hasNext();) { | 
  |  450 |    0 |                            Map.Entry e = (Map.Entry) i.next();  | 
  |  451 |     | 
     | 
  |  452 |    0 |                            Object fieldValue = e.getValue();  | 
  |  453 |    0 |                            if (fieldValue == null) { | 
  |  454 |    0 |                                    emptyField = true;  | 
  |  455 |    0 |                            } else if (fieldValue instanceof String) { | 
  |  456 |    0 |                                    if (StringUtils.isEmpty((String) fieldValue)) { | 
  |  457 |    0 |                                            emptyField = true;  | 
  |  458 |     | 
                                   } else { | 
  |  459 |    0 |                                            emptyField = false;  | 
  |  460 |     | 
                                   }  | 
  |  461 |     | 
                           }  | 
  |  462 |    0 |                    }  | 
  |  463 |     | 
     | 
  |  464 |    0 |                    return !emptyField;  | 
  |  465 |     | 
           }  | 
  |  466 |     | 
     | 
  |  467 |     | 
             | 
  |  468 |     | 
     | 
  |  469 |     | 
     | 
  |  470 |     | 
     | 
  |  471 |     | 
           public ForeignKeyFieldsPopulationState getForeignKeyFieldsPopulationState(PersistableBusinessObject bo, String referenceName) { | 
  |  472 |     | 
     | 
  |  473 |    0 |                    boolean allFieldsPopulated = true;  | 
  |  474 |    0 |                    boolean anyFieldsPopulated = false;  | 
  |  475 |    0 |                    List<String> unpopulatedFields = new ArrayList<String>();  | 
  |  476 |     | 
     | 
  |  477 |     | 
                     | 
  |  478 |    0 |                    if (bo == null) { | 
  |  479 |    0 |                            throw new IllegalArgumentException("The Class passed in for the BusinessObject argument was null."); | 
  |  480 |     | 
                   }  | 
  |  481 |    0 |                    if (StringUtils.isBlank(referenceName)) { | 
  |  482 |    0 |                            throw new IllegalArgumentException("The String passed in for the referenceName argument was null or empty."); | 
  |  483 |     | 
                   }  | 
  |  484 |     | 
     | 
  |  485 |    0 |                    PropertyDescriptor propertyDescriptor = null;  | 
  |  486 |     | 
     | 
  |  487 |     | 
                     | 
  |  488 |     | 
                   try { | 
  |  489 |    0 |                            propertyDescriptor = PropertyUtils.getPropertyDescriptor(bo, referenceName);  | 
  |  490 |    0 |                    } catch (Exception e) { | 
  |  491 |    0 |                            throw new RuntimeException(e);  | 
  |  492 |    0 |                    }  | 
  |  493 |    0 |                    if (propertyDescriptor == null) { | 
  |  494 |    0 |                            throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + referenceName + "' does not exist " + "on class: '" + bo.getClass().getName() + "'."); | 
  |  495 |     | 
                   }  | 
  |  496 |     | 
     | 
  |  497 |     | 
                     | 
  |  498 |    0 |                    Class referenceClass = propertyDescriptor.getPropertyType();  | 
  |  499 |     | 
     | 
  |  500 |     | 
                     | 
  |  501 |     | 
                     | 
  |  502 |    0 |                    if (!PersistableBusinessObject.class.isAssignableFrom(referenceClass)) { | 
  |  503 |    0 |                            throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + referenceName + ") is of class: " + "'" + referenceClass.getName() + "' and is not a " + "descendent of BusinessObject.  Only descendents of BusinessObject " + "can be used."); | 
  |  504 |     | 
                   }  | 
  |  505 |     | 
     | 
  |  506 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(bo.getClass());  | 
  |  507 |    0 |                    ObjectDescriptor objectDescriptor = descriptor.getObjectDescriptorByName(referenceName);  | 
  |  508 |     | 
     | 
  |  509 |    0 |                    if (objectDescriptor == null) { | 
  |  510 |    0 |                            throw new ReferenceAttributeNotAnOjbReferenceException("Attribute requested (" + referenceName + ") is not listed " + "in OJB as a reference-descriptor for class: '" + bo.getClass().getName() + "'"); | 
  |  511 |     | 
                   }  | 
  |  512 |     | 
     | 
  |  513 |    0 |                    List<String> fkFields = objectDescriptor.getForeignKeyFields();  | 
  |  514 |    0 |                    Iterator fkIterator = fkFields.iterator();  | 
  |  515 |     | 
     | 
  |  516 |     | 
                     | 
  |  517 |    0 |                    while (fkIterator.hasNext()) { | 
  |  518 |     | 
     | 
  |  519 |     | 
                             | 
  |  520 |    0 |                            String fkFieldName = (String) fkIterator.next();  | 
  |  521 |     | 
     | 
  |  522 |     | 
                             | 
  |  523 |    0 |                            Object fkFieldValue = null;  | 
  |  524 |     | 
                           try { | 
  |  525 |    0 |                                    fkFieldValue = PropertyUtils.getSimpleProperty(bo, fkFieldName);  | 
  |  526 |     | 
                           }  | 
  |  527 |     | 
     | 
  |  528 |     | 
                             | 
  |  529 |    0 |                            catch (Exception e) { | 
  |  530 |    0 |                                    throw new RuntimeException(e);  | 
  |  531 |    0 |                            }  | 
  |  532 |     | 
     | 
  |  533 |     | 
                             | 
  |  534 |    0 |                            if (fkFieldValue == null) { | 
  |  535 |    0 |                                    allFieldsPopulated = false;  | 
  |  536 |    0 |                                    unpopulatedFields.add(fkFieldName);  | 
  |  537 |    0 |                            } else if (fkFieldValue instanceof String) { | 
  |  538 |    0 |                                    if (StringUtils.isBlank((String) fkFieldValue)) { | 
  |  539 |    0 |                                            allFieldsPopulated = false;  | 
  |  540 |    0 |                                            unpopulatedFields.add(fkFieldName);  | 
  |  541 |     | 
                                   } else { | 
  |  542 |    0 |                                            anyFieldsPopulated = true;  | 
  |  543 |     | 
                                   }  | 
  |  544 |     | 
                           } else { | 
  |  545 |    0 |                                    anyFieldsPopulated = true;  | 
  |  546 |     | 
                           }  | 
  |  547 |    0 |                    }  | 
  |  548 |     | 
     | 
  |  549 |     | 
                     | 
  |  550 |     | 
                     | 
  |  551 |    0 |                    if (allFieldsPopulated) { | 
  |  552 |    0 |                            if (!unpopulatedFields.isEmpty()) { | 
  |  553 |    0 |                                    throw new RuntimeException("The flag is set that indicates all fields are populated, but there " + "are fields present in the unpopulatedFields list.  This should never happen, and indicates " + "that the logic in this method is broken."); | 
  |  554 |     | 
                           }  | 
  |  555 |     | 
                   }  | 
  |  556 |     | 
     | 
  |  557 |    0 |                    return new ForeignKeyFieldsPopulationState(allFieldsPopulated, anyFieldsPopulated, unpopulatedFields);  | 
  |  558 |     | 
           }  | 
  |  559 |     | 
     | 
  |  560 |     | 
             | 
  |  561 |     | 
     | 
  |  562 |     | 
     | 
  |  563 |     | 
           @Cached  | 
  |  564 |     | 
           public Map<String, Class> listReferenceObjectFields(Class boClass) { | 
  |  565 |     | 
                     | 
  |  566 |    0 |                    if (boClass == null) { | 
  |  567 |    0 |                            throw new IllegalArgumentException("Class specified in the parameter was null."); | 
  |  568 |     | 
                   }  | 
  |  569 |    0 |                    if (!PersistableBusinessObject.class.isAssignableFrom(boClass)) { | 
  |  570 |    0 |                            throw new IllegalArgumentException("Class specified [" + boClass.getName() + "] must be a class that " + "inherits from BusinessObject."); | 
  |  571 |     | 
                   }  | 
  |  572 |     | 
     | 
  |  573 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass);  | 
  |  574 |    0 |                    Map<String, Class> references = new HashMap();  | 
  |  575 |    0 |                    for (org.kuali.rice.core.framework.persistence.jpa.metadata.ObjectDescriptor od : descriptor.getObjectRelationships()) { | 
  |  576 |    0 |                            references.put(od.getAttributeName(), od.getTargetEntity());  | 
  |  577 |     | 
                   }  | 
  |  578 |     | 
     | 
  |  579 |    0 |                    return references;  | 
  |  580 |     | 
           }  | 
  |  581 |     | 
     | 
  |  582 |     | 
           @Cached  | 
  |  583 |     | 
           public Map<String, Class> listCollectionObjectTypes(Class boClass) { | 
  |  584 |    0 |                    if (boClass == null) { | 
  |  585 |    0 |                            throw new IllegalArgumentException("Class specified in the parameter was null."); | 
  |  586 |     | 
                   }  | 
  |  587 |     | 
     | 
  |  588 |    0 |                    Map<String, Class> references = new HashMap();  | 
  |  589 |     | 
     | 
  |  590 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass);  | 
  |  591 |    0 |                    if (descriptor == null) { | 
  |  592 |    0 |                            return references;  | 
  |  593 |     | 
                   }  | 
  |  594 |     | 
     | 
  |  595 |    0 |                    for (org.kuali.rice.core.framework.persistence.jpa.metadata.CollectionDescriptor cd : descriptor.getCollectionRelationships()) { | 
  |  596 |    0 |                            references.put(cd.getAttributeName(), cd.getTargetEntity());  | 
  |  597 |     | 
                   }  | 
  |  598 |     | 
     | 
  |  599 |    0 |                    return references;  | 
  |  600 |     | 
           }  | 
  |  601 |     | 
     | 
  |  602 |     | 
           public Map<String, Class> listCollectionObjectTypes(PersistableBusinessObject bo) { | 
  |  603 |     | 
                     | 
  |  604 |    0 |                    if (bo == null) { | 
  |  605 |    0 |                            throw new IllegalArgumentException("BO specified in the parameter was null."); | 
  |  606 |     | 
                   }  | 
  |  607 |    0 |                    if (!(bo instanceof PersistableBusinessObject)) { | 
  |  608 |    0 |                            throw new IllegalArgumentException("BO specified [" + bo.getClass().getName() + "] must be a class that " + "inherits from BusinessObject."); | 
  |  609 |     | 
                   }  | 
  |  610 |     | 
     | 
  |  611 |    0 |                    return listCollectionObjectTypes(bo.getClass());  | 
  |  612 |     | 
           }  | 
  |  613 |     | 
     | 
  |  614 |     | 
             | 
  |  615 |     | 
     | 
  |  616 |     | 
     | 
  |  617 |     | 
           public Map<String, Class> listReferenceObjectFields(PersistableBusinessObject bo) { | 
  |  618 |     | 
                     | 
  |  619 |    0 |                    if (bo == null) { | 
  |  620 |    0 |                            throw new IllegalArgumentException("BO specified in the parameter was null."); | 
  |  621 |     | 
                   }  | 
  |  622 |    0 |                    if (!(bo instanceof PersistableBusinessObject)) { | 
  |  623 |    0 |                            throw new IllegalArgumentException("BO specified [" + bo.getClass().getName() + "] must be a class that " + "inherits from BusinessObject."); | 
  |  624 |     | 
                   }  | 
  |  625 |     | 
     | 
  |  626 |    0 |                    return listReferenceObjectFields(bo.getClass());  | 
  |  627 |     | 
           }  | 
  |  628 |     | 
     | 
  |  629 |     | 
           @Cached  | 
  |  630 |     | 
           public boolean isReferenceUpdatable(Class boClass, String referenceName) { | 
  |  631 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass);  | 
  |  632 |    0 |                    return descriptor.getObjectDescriptorByName(referenceName).isUpdateable();  | 
  |  633 |     | 
     | 
  |  634 |     | 
     | 
  |  635 |     | 
     | 
  |  636 |     | 
     | 
  |  637 |     | 
     | 
  |  638 |     | 
     | 
  |  639 |     | 
           }  | 
  |  640 |     | 
     | 
  |  641 |     | 
           @Cached  | 
  |  642 |     | 
           public boolean isCollectionUpdatable(Class boClass, String collectionName) { | 
  |  643 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass);  | 
  |  644 |    0 |                    return descriptor.getCollectionDescriptorByName(collectionName).isUpdateable();  | 
  |  645 |     | 
     | 
  |  646 |     | 
     | 
  |  647 |     | 
     | 
  |  648 |     | 
     | 
  |  649 |     | 
     | 
  |  650 |     | 
     | 
  |  651 |     | 
           }  | 
  |  652 |     | 
     | 
  |  653 |     | 
           @Cached  | 
  |  654 |     | 
           public boolean hasCollection(Class boClass, String collectionName) { | 
  |  655 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass);  | 
  |  656 |    0 |                    return descriptor.getCollectionDescriptorByName(collectionName) != null;  | 
  |  657 |     | 
           }  | 
  |  658 |     | 
     | 
  |  659 |     | 
           @Cached  | 
  |  660 |     | 
           public boolean hasReference(Class boClass, String referenceName) { | 
  |  661 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass);  | 
  |  662 |    0 |                    return descriptor.getObjectDescriptorByName(referenceName) != null;  | 
  |  663 |     | 
           }  | 
  |  664 |     | 
     | 
  |  665 |     | 
             | 
  |  666 |     | 
     | 
  |  667 |     | 
     | 
  |  668 |     | 
           @Cached  | 
  |  669 |     | 
           public String getTableName(Class<? extends PersistableBusinessObject> boClass) { | 
  |  670 |    0 |                    EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass);  | 
  |  671 |    0 |                    return descriptor.getTable();  | 
  |  672 |     | 
           }  | 
  |  673 |     | 
   }  | 
  |  674 |     | 
     |