Java Code Examples for org.springframework.ldap.odm.annotations.Id

The following examples show how to use org.springframework.ldap.odm.annotations.Id. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-ldap   Source File: AttributeMetaData.java    License: Apache License 2.0 5 votes vote down vote up
private boolean processIdAnnotation(Field field, Class<?> fieldType) {
    // Are we dealing with the Id field?
    isId=field.getAnnotation(Id.class)!=null;

    if (isId) {  
        // It must be of type Name or a subclass of that of
        if (!Name.class.isAssignableFrom(fieldType)) {
            throw new MetaDataException(
                    String.format("The id field must be of type javax.naming.Name or a subclass that of in Entry class %1$s",
                            field.getDeclaringClass()));
        }
    }
    
    return isId;
}
 
Example 2
Source Project: spring-ldap   Source File: AttributeMetaData.java    License: Apache License 2.0 5 votes vote down vote up
public AttributeMetaData(Field field) {
    this.field=field;

    this.dnAttribute = field.getAnnotation(DnAttribute.class);
    if(this.dnAttribute != null && !field.getType().equals(String.class)) {
        throw new MetaDataException(String.format("%s is of type %s, but only String attributes can be declared as @DnAttributes",
                field.toString(),
                field.getType().toString()));
    }

    Transient transientAnnotation = field.getAnnotation(Transient.class);
    if(transientAnnotation != null) {
        this.isTransient = true;
        return;
    }

    // Reflection data
    determineFieldType(field);


    // Data from the @Attribute annotation
    boolean foundAttributeAnnotation=processAttributeAnnotation(field);

    // Data from the @Id annotation
    boolean foundIdAnnoation=processIdAnnotation(field, valueClass);

    // Check that the field has not been annotated with both @Attribute and with @Id
    if (foundAttributeAnnotation && foundIdAnnoation) {
        throw new MetaDataException(
                String.format("You may not specifiy an %1$s annoation and an %2$s annotation on the same field, error in field %3$s in Entry class %4$s",
                        Id.class, Attribute.class, field.getName(), field.getDeclaringClass()));
    }
    
    // If this is the objectclass attribute then it must be of type List<String>
    if (isObjectClass() && (!isCollection() || valueClass!=String.class)) {
        throw new MetaDataException(String.format("The type of the objectclass attribute must be List<String> in classs %1$s",
                field.getDeclaringClass()));
    }
}
 
Example 3
Source Project: spring-ldap   Source File: ObjectMetaData.java    License: Apache License 2.0 4 votes vote down vote up
public ObjectMetaData(Class<?> clazz) {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Extracting metadata from %1$s", clazz));
    }
    
    // Get object class metadata - the @Entity annotation
    Entry entity = clazz.getAnnotation(Entry.class);
    if (entity != null) {
        // Default objectclass name to the class name unless it's specified
        // in @Entity(name={objectclass1, objectclass2});
        String[] localObjectClasses = entity.objectClasses();
        if (localObjectClasses != null && localObjectClasses.length > 0 && localObjectClasses[0].length() > 0) {
            for (String localObjectClass:localObjectClasses) {
                objectClasses.add(new CaseIgnoreString(localObjectClass));
            }
        } else {
            objectClasses.add(new CaseIgnoreString(clazz.getSimpleName()));
        }

        String base = entity.base();
        if(StringUtils.hasText(base)) {
            this.base = LdapUtils.newLdapName(base);
        }
    } else {
        throw new MetaDataException(String.format("Class %1$s must have a class level %2$s annotation", clazz,
                Entry.class));
    }

    // Check the class is final
    if (!Modifier.isFinal(clazz.getModifiers())) {
        LOG.warn(String.format("The Entry class %1$s should be declared final", clazz.getSimpleName()));
    }

    // Get field meta-data - the @Attribute annotation
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        // So we can write to private fields
        field.setAccessible(true);

        // Skip synthetic or static fields
        if (Modifier.isStatic(field.getModifiers()) || field.isSynthetic()) {
            continue;
        }

        AttributeMetaData currentAttributeMetaData=new AttributeMetaData(field);
        if (currentAttributeMetaData.isId()) {
            if (idAttribute!=null) {
                // There can be only one id field
                throw new MetaDataException(
                      String.format("You man have only one field with the %1$s annotation in class %2$s", Id.class, clazz));
            }
            idAttribute=currentAttributeMetaData;
        }
        fieldToAttribute.put(field, currentAttributeMetaData);

        if(currentAttributeMetaData.isDnAttribute()) {
            dnAttributes.add(currentAttributeMetaData);
        }
    }

    if (idAttribute == null) {
        throw new MetaDataException(
                String.format("All Entry classes must define a field with the %1$s annotation, error in class %2$s", Id.class,
                              clazz));
    }

    postProcessDnAttributes(clazz);

    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Extracted metadata from %1$s as %2$s", clazz, this));
    }
}