Example usage for org.apache.commons.collections SequencedHashMap values

List of usage examples for org.apache.commons.collections SequencedHashMap values

Introduction

In this page you can find the example usage for org.apache.commons.collections SequencedHashMap values.

Prototype

public Collection values() 

Source Link

Document

Implements Map#values() .

Usage

From source file:xdoclet.modules.ojb.constraints.ModelConstraints.java

/**
 * Ensures that the foreign keys required by the given collection are present in the element class.
 * //from  w w  w.j  a  v a2 s . c o m
 * @param modelDef The model
 * @param collDef  The collection
 * @throws ConstraintException If there is a problem with the foreign keys
 */
private void ensureReferencedFKs(ModelDef modelDef, CollectionDescriptorDef collDef)
        throws ConstraintException {
    String elementClassName = collDef.getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF);
    ClassDescriptorDef elementClassDef = modelDef.getClass(elementClassName);
    String fkFieldNames = collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY);
    ArrayList missingFields = new ArrayList();
    SequencedHashMap fkFields = new SequencedHashMap();

    // first we gather all field names
    for (CommaListIterator it = new CommaListIterator(fkFieldNames); it.hasNext();) {
        String fieldName = (String) it.next();
        FieldDescriptorDef fieldDef = elementClassDef.getField(fieldName);

        if (fieldDef == null) {
            missingFields.add(fieldName);
        }
        fkFields.put(fieldName, fieldDef);
    }

    // next we traverse all sub types and gather fields as we go
    for (Iterator it = elementClassDef.getAllExtentClasses(); it.hasNext() && !missingFields.isEmpty();) {
        ClassDescriptorDef subTypeDef = (ClassDescriptorDef) it.next();

        for (int idx = 0; idx < missingFields.size();) {
            FieldDescriptorDef fieldDef = subTypeDef.getField((String) missingFields.get(idx));

            if (fieldDef != null) {
                fkFields.put(fieldDef.getName(), fieldDef);
                missingFields.remove(idx);
            } else {
                idx++;
            }
        }
    }
    if (!missingFields.isEmpty()) {
        throw new ConstraintException(
                "Cannot find field " + missingFields.get(0).toString() + " in the hierarchy with root type "
                        + elementClassDef.getName() + " which is used as foreignkey in collection "
                        + collDef.getName() + " in " + collDef.getOwner().getName());
    }

    // copy the found fields into the element class
    ensureFields(elementClassDef, fkFields.values());
}

From source file:xdoclet.modules.ojb.constraints.ModelConstraints.java

/**
 * Gathers the pk fields from the hierarchy of the given class, and copies them into the class.
 * //from w ww.j  a v  a  2 s  .  c  o m
 * @param classDef The root of the hierarchy
 * @throws ConstraintException If there is a conflict between the pk fields 
 */
private void ensurePKsFromHierarchy(ClassDescriptorDef classDef) throws ConstraintException {
    SequencedHashMap pks = new SequencedHashMap();

    for (Iterator it = classDef.getAllExtentClasses(); it.hasNext();) {
        ClassDescriptorDef subTypeDef = (ClassDescriptorDef) it.next();

        ArrayList subPKs = subTypeDef.getPrimaryKeys();

        // check against already present PKs
        for (Iterator pkIt = subPKs.iterator(); pkIt.hasNext();) {
            FieldDescriptorDef fieldDef = (FieldDescriptorDef) pkIt.next();
            FieldDescriptorDef foundPKDef = (FieldDescriptorDef) pks.get(fieldDef.getName());

            if (foundPKDef != null) {
                if (!isEqual(fieldDef, foundPKDef)) {
                    throw new ConstraintException(
                            "Cannot pull up the declaration of the required primary key " + fieldDef.getName()
                                    + " because its definitions in " + fieldDef.getOwner().getName() + " and "
                                    + foundPKDef.getOwner().getName() + " differ");
                }
            } else {
                pks.put(fieldDef.getName(), fieldDef);
            }
        }
    }

    ensureFields(classDef, pks.values());
}