public Entity loadEntity(Object providerContext)
{
DirContextOperations ctx = (DirContextOperations)((SearchResult)(providerContext)).getObject();
String entityId = null;
Entity entity = null;
String dn = ctx.getNameInNamespace();
Set<Attribute> attributes = new HashSet<Attribute>();
Attributes attrs = ctx.getAttributes();
for (AttributeDef attrDef : searchConfiguration.getEntityAttributeDefinitionsMap().values())
{
List<String> values = null;
values = getStringAttributes(attrs, attrDef.getName(), attrDef.requiresDnDefaultValue());
if (values != null)
{
Attribute a = new AttributeImpl(attrDef);
if (attrDef.isMultiValue())
{
// remove the dummy value for required fields when present.
if (attrDef.isRequired())
{
String defaultValue = attrDef.requiresDnDefaultValue() ? dn : attrDef.getRequiredDefaultValue();
values.remove(defaultValue);
}
if (values.size() != 0)
{
a.setValues(values);
}
else
{
attributes.add(a);
}
}
else
{
String value = values.get(0);
if (attrDef.isEntityIdAttribute())
{
entityId = value;
}
a.setValue(value);
}
attributes.add(a);
}
}
if (entityId == null)
{
DistinguishedName name = new DistinguishedName(dn);
LdapRdn rdn = name.getLdapRdn(name.size()-1);
if (rdn.getKey().equals(searchConfiguration.getLdapIdAttribute()))
{
entityId = rdn.getValue();
}
else
{
// TODO: throw exception???
return null;
}
}
entity = internalCreateEntity(entityId, dn, attributes);
entity.setLive(true);
return entity;
}