type = session.getTypeDefinition(typeId.toString());
}
// some preparation
BindingsObjectFactory bof = getBindingsObjectFactory();
List<PropertyData<?>> propertyList = new ArrayList<PropertyData<?>>();
// the big loop
for (Map.Entry<String, ?> property : properties.entrySet()) {
if ((property == null) || (property.getKey() == null)) {
continue;
}
String id = property.getKey();
Object value = property.getValue();
if (value instanceof Property<?>) {
Property<?> p = (Property<?>) value;
if (!id.equals(p.getId())) {
throw new IllegalArgumentException("Property id mismatch: '" + id + "' != '" + p.getId() + "'!");
}
value = (p.getDefinition().getCardinality() == Cardinality.SINGLE ? p.getFirstValue() : p.getValues());
}
// get the property definition
PropertyDefinition<?> definition = type.getPropertyDefinitions().get(id);
if (definition == null) {
throw new IllegalArgumentException("Property '" + id + "' is not valid for this type!");
}
// check updatability
if (updatabilityFilter != null) {
if (!updatabilityFilter.contains(definition.getUpdatability())) {
continue;
}
}
// single and multi value check
List<?> values;
if (value == null) {
values = null;
} else if (value instanceof List<?>) {
if (definition.getCardinality() != Cardinality.MULTI) {
throw new IllegalArgumentException("Property '" + id + "' is not a multi value property!");
}
values = (List<?>) value;
// check if the list is homogeneous and does not contain null
// values
Class<?> valueClazz = null;
for (Object o : values) {
if (o == null) {
throw new IllegalArgumentException("Property '" + id + "' contains null values!");
}
if (valueClazz == null) {
valueClazz = o.getClass();
} else {
if (!valueClazz.isInstance(o)) {
throw new IllegalArgumentException("Property '" + id + "' is inhomogeneous!");
}
}
}
} else {
if (definition.getCardinality() != Cardinality.SINGLE) {
throw new IllegalArgumentException("Property '" + id + "' is not a single value property!");
}
values = Collections.singletonList(value);
}
// assemble property
PropertyData<?> propertyData = null;
Object firstValue = (values == null || values.isEmpty() ? null : values.get(0));
if (definition instanceof PropertyStringDefinition) {
if (firstValue == null) {
propertyData = bof.createPropertyStringData(id, (List<String>) null);
} else if (firstValue instanceof String) {
propertyData = bof.createPropertyStringData(id, (List<String>) values);
} else {
throw new IllegalArgumentException("Property '" + id + "' is a String property!");
}
} else if (definition instanceof PropertyIdDefinition) {
if (firstValue == null) {
propertyData = bof.createPropertyIdData(id, (List<String>) null);
} else if (firstValue instanceof String) {
propertyData = bof.createPropertyIdData(id, (List<String>) values);
} else {
throw new IllegalArgumentException("Property '" + id + "' is an Id property!");
}
} else if (definition instanceof PropertyHtmlDefinition) {
if (firstValue == null) {
propertyData = bof.createPropertyHtmlData(id, (List<String>) values);
} else if (firstValue instanceof String) {
propertyData = bof.createPropertyHtmlData(id, (List<String>) values);
} else {
throw new IllegalArgumentException("Property '" + id + "' is a HTML property!");
}
} else if (definition instanceof PropertyUriDefinition) {
if (firstValue == null) {
propertyData = bof.createPropertyUriData(id, (List<String>) null);
} else if (firstValue instanceof String) {
propertyData = bof.createPropertyUriData(id, (List<String>) values);
} else {
throw new IllegalArgumentException("Property '" + id + "' is an URI property!");
}
} else if (definition instanceof PropertyIntegerDefinition) {
if (firstValue == null) {
propertyData = bof.createPropertyIntegerData(id, (List<BigInteger>) null);
} else if (firstValue instanceof BigInteger) {
propertyData = bof.createPropertyIntegerData(id, (List<BigInteger>) values);
} else if ((firstValue instanceof Byte) || (firstValue instanceof Short)
|| (firstValue instanceof Integer) || (firstValue instanceof Long)) {
// we accept all kinds of integers
List<BigInteger> list = new ArrayList<BigInteger>(values.size());
for (Object v : values) {
list.add(BigInteger.valueOf(((Number) v).longValue()));
}
propertyData = bof.createPropertyIntegerData(id, list);
} else {
throw new IllegalArgumentException("Property '" + id + "' is an Integer property!");
}
} else if (definition instanceof PropertyBooleanDefinition) {
if (firstValue == null) {
propertyData = bof.createPropertyBooleanData(id, (List<Boolean>) null);
} else if (firstValue instanceof Boolean) {
propertyData = bof.createPropertyBooleanData(id, (List<Boolean>) values);
} else {
throw new IllegalArgumentException("Property '" + id + "' is a Boolean property!");
}
} else if (definition instanceof PropertyDecimalDefinition) {
if (firstValue == null) {
propertyData = bof.createPropertyDecimalData(id, (List<BigDecimal>) null);
} else if (firstValue instanceof BigDecimal) {
propertyData = bof.createPropertyDecimalData(id, (List<BigDecimal>) values);
} else if ((firstValue instanceof Float) || (firstValue instanceof Double)
|| (firstValue instanceof Byte) || (firstValue instanceof Short)
|| (firstValue instanceof Integer) || (firstValue instanceof Long)) {
// we accept all kinds of integers
// as well as floats and doubles
List<BigDecimal> list = new ArrayList<BigDecimal>(values.size());
for (Object v : values) {
list.add(new BigDecimal(v.toString()));
}
propertyData = bof.createPropertyDecimalData(id, list);
} else {
throw new IllegalArgumentException("Property '" + id + "' is a Decimal property!");
}
} else if (definition instanceof PropertyDateTimeDefinition) {
if (firstValue == null) {
propertyData = bof.createPropertyDateTimeData(id, (List<GregorianCalendar>) null);
} else if (firstValue instanceof GregorianCalendar) {
propertyData = bof.createPropertyDateTimeData(id, (List<GregorianCalendar>) values);
} else {
throw new IllegalArgumentException("Property '" + id + "' is a DateTime property!");
}
}
// do we have something?
if (propertyData == null) {
throw new IllegalArgumentException("Property '" + id + "' doesn't match the property defintion!");
}
propertyList.add(propertyData);
}
return bof.createPropertiesData(propertyList);
}