final T subjectTO = attrUtil.newSubjectTO();
// 1. fill with data from connector object
for (AbstractMappingItem item : attrUtil.getUidToMappingItems(syncTask.getResource(),
MappingPurpose.SYNCHRONIZATION, attrUtil.getType())) {
Attribute attribute = obj.getAttributeByName(item.getExtAttrName());
AttributeTO attributeTO;
switch (item.getIntMappingType()) {
case UserId:
case RoleId:
break;
case Password:
if (subjectTO instanceof UserTO && attribute != null && attribute.getValue() != null
&& !attribute.getValue().isEmpty()) {
((UserTO) subjectTO).setPassword(getPassword(attribute.getValue().get(0)));
}
break;
case Username:
if (subjectTO instanceof UserTO) {
((UserTO) subjectTO).setUsername(attribute == null || attribute.getValue().isEmpty()
|| attribute.getValue().get(0) == null
? null
: attribute.getValue().get(0).toString());
}
break;
case RoleName:
if (subjectTO instanceof RoleTO) {
((RoleTO) subjectTO).setName(attribute == null || attribute.getValue().isEmpty()
|| attribute.getValue().get(0) == null
? null
: attribute.getValue().get(0).toString());
}
break;
case RoleOwnerSchema:
if (subjectTO instanceof RoleTO && attribute != null) {
// using a special attribute (with schema "", that will be ignored) for carrying the
// RoleOwnerSchema value
attributeTO = new AttributeTO();
attributeTO.setSchema(StringUtils.EMPTY);
if (attribute.getValue().isEmpty() || attribute.getValue().get(0) == null) {
attributeTO.getValues().add(StringUtils.EMPTY);
} else {
attributeTO.getValues().add(attribute.getValue().get(0).toString());
}
((RoleTO) subjectTO).getAttrs().add(attributeTO);
}
break;
case UserSchema:
case RoleSchema:
attributeTO = new AttributeTO();
attributeTO.setSchema(item.getIntAttrName());
AbstractNormalSchema schema = schemaDAO.find(item.getIntAttrName(), attrUtil.schemaClass());
for (Object value : attribute == null || attribute.getValue() == null
? Collections.emptyList()
: attribute.getValue()) {
if (value != null) {
final AbstractAttrValue attrValue = attrUtil.newAttrValue();
if (schema == null) {
attrValue.setStringValue(value.toString());
} else if (schema.getType() == AttributeSchemaType.Binary) {
attrValue.setBinaryValue((byte[]) value);
} else {
try {
attrValue.parseValue(schema, value.toString());
} catch (ParsingValidationException e) {
LOG.error("While parsing provided value {}", value, e);
attrValue.setStringValue(value.toString());
}
}
attributeTO.getValues().add(attrValue.getValueAsString(
schema == null ? AttributeSchemaType.String : schema.getType()));
}
}
subjectTO.getAttrs().add(attributeTO);
break;
case UserDerivedSchema:
case RoleDerivedSchema:
attributeTO = new AttributeTO();
attributeTO.setSchema(item.getIntAttrName());
subjectTO.getDerAttrs().add(attributeTO);
break;
case UserVirtualSchema:
case RoleVirtualSchema:
attributeTO = new AttributeTO();
attributeTO.setSchema(item.getIntAttrName());
for (Object value : attribute == null || attribute.getValue() == null
? Collections.emptyList()
: attribute.getValue()) {
if (value != null) {
attributeTO.getValues().add(value.toString());
}
}