if ((mdMap == null || !mdMap.containsKey(attribute.getName())) &&
!isAllowNotDefinedAttributes)
{
throw new IdentityException("Cannot add not defined attribute. Use '" + ALLOW_NOT_DEFINED_ATTRIBUTES +
"' option if needed. Attribute name: " + attribute.getName());
}
IdentityObjectAttributeMetaData amd = mdMap.get(attribute.getName());
if (amd != null)
{
if (!amd.isMultivalued() && attribute.getSize() > 1)
{
throw new IdentityException("Cannot add multiply values to single valued attribute: " + attribute.getName());
}
if (amd.isReadonly())
{
throw new IdentityException("Cannot add readonly attribute: " + attribute.getName());
}
String type = amd.getType();
// check if all values have proper type
for (Object value : attribute.getValues())
{
if (type.equals(IdentityObjectAttributeMetaData.TEXT_TYPE) && !(value instanceof String))
{
throw new IdentityException("Cannot add text type attribute with not String type value: "
+ attribute.getName() + " / " + value);
}
if (type.equals(IdentityObjectAttributeMetaData.BINARY_TYPE) && !(value instanceof byte[]))
{
throw new IdentityException("Cannot add binary type attribute with not byte[] type value: "
+ attribute.getName() + " / " + value);
}
}
}
}
HibernateIdentityObject hibernateObject = safeGet(ctx, identity);
for (String name : mappedAttributes.keySet())
{
IdentityObjectAttribute attribute = mappedAttributes.get(name);
IdentityObjectAttributeMetaData amd = mdMap.get(attribute.getName());
// Default to text
String type = amd != null ? amd.getType() : IdentityObjectAttributeMetaData.TEXT_TYPE;
HibernateIdentityObjectAttribute hibernateAttribute = null;
for (HibernateIdentityObjectAttribute storeAttribute : hibernateObject.getAttributes())
{
if (storeAttribute.getName().equals(name))
{
hibernateAttribute = storeAttribute;
break;
}
}
if (hibernateAttribute != null)
{
if (hibernateAttribute instanceof HibernateIdentityObjectTextAttribute)
{
if (!type.equals(IdentityObjectAttributeMetaData.TEXT_TYPE))
{
throw new IdentityException("Wrong attribute mapping. Attribute persisted as text is mapped with: "
+ type + ". Attribute name: " + name);
}
Set<String> mergedValues = new HashSet<String>(hibernateAttribute.getValues());
for (Object value : attribute.getValues())
{
mergedValues.add(value.toString());
}
((HibernateIdentityObjectTextAttribute)hibernateAttribute).setValues(mergedValues);
}
else if (hibernateAttribute instanceof HibernateIdentityObjectBinaryAttribute)
{
if (!type.equals(IdentityObjectAttributeMetaData.BINARY_TYPE))
{
throw new IdentityException("Wrong attribute mapping. Attribute persisted as binary is mapped with: "
+ type + ". Attribute name: " + name);
}
Set<byte[]> mergedValues = new HashSet<byte[]>(hibernateAttribute.getValues());
for (Object value : attribute.getValues())
{
mergedValues.add((byte[])value);
}
((HibernateIdentityObjectBinaryAttribute)hibernateAttribute).setValues(mergedValues);
}
else
{
throw new IdentityException("Internal identity store error");
}
break;
}
else