Skip to content
Snippets Groups Projects
Commit 8165e0b7 authored by Adam Laskowski's avatar Adam Laskowski
Browse files

SSDM-55: Improved property converter for multi-value properties

parent 90490517
No related branches found
No related tags found
1 merge request!40SSDM-13578 : 2PT : Database and V3 Implementation - include the new AFS "free"...
...@@ -55,66 +55,53 @@ public abstract class PropertyTranslator extends ...@@ -55,66 +55,53 @@ public abstract class PropertyTranslator extends
records.stream().filter(r -> r.sample_id != null).map(r -> r.sample_id) records.stream().filter(r -> r.sample_id != null).map(r -> r.sample_id)
.collect(Collectors.toSet())); .collect(Collectors.toSet()));
Map<Long, Map<String, Serializable>> properties = new HashMap<Long, Map<String, Serializable>>(); Map<Long, Map<String, Serializable>> properties =
new HashMap<Long, Map<String, Serializable>>();
for (PropertyRecord record : records) for (PropertyRecord record : records)
{ {
Map<String, Serializable> objectProperties = properties.get(record.objectId); Map<String, Serializable> objectProperties = properties.get(record.objectId);
if (objectProperties == null) if (objectProperties == null)
{ {
objectProperties = new HashMap<String, Serializable>(); objectProperties = new HashMap<>();
properties.put(record.objectId, objectProperties); properties.put(record.objectId, objectProperties);
} }
if (record.propertyValue != null) if (record.propertyValue != null)
{ {
objectProperties.put(record.propertyCode, record.propertyValue); updateObjectProperty(objectProperties, record.propertyCode, record.propertyValue);
} else if (record.materialPropertyValueCode != null) } else if (record.materialPropertyValueCode != null)
{ {
objectProperties.put(record.propertyCode, updateObjectProperty(objectProperties, record.propertyCode,
record.materialPropertyValueCode + " (" + record.materialPropertyValueTypeCode record.materialPropertyValueCode + " (" + record.materialPropertyValueTypeCode
+ ")"); + ")");
} else if (record.vocabularyPropertyValue != null) } else if (record.vocabularyPropertyValue != null)
{ {
if(objectProperties.containsKey(record.propertyCode)) { updateObjectProperty(objectProperties, record.propertyCode,
Serializable current = objectProperties.get(record.propertyCode); record.vocabularyPropertyValue);
Serializable newValue = composeMultiValueProperty(current, record.vocabularyPropertyValue);
objectProperties.put(record.propertyCode, newValue);
} else {
objectProperties.put(record.propertyCode, record.vocabularyPropertyValue);
}
} else if (record.sample_perm_id != null) } else if (record.sample_perm_id != null)
{ {
if (visibaleSamples.contains(record.sample_id)) updateObjectProperty(objectProperties, record.propertyCode, record.sample_perm_id);
{
if(objectProperties.containsKey(record.propertyCode)) {
Serializable current = objectProperties.get(record.propertyCode);
Serializable newValue = composeMultiValueProperty(current, record.sample_perm_id);
objectProperties.put(record.propertyCode, newValue);
} else
{
objectProperties.put(record.propertyCode, record.sample_perm_id);
}
}
} else if (record.integerArrayPropertyValue != null) } else if (record.integerArrayPropertyValue != null)
{ {
objectProperties.put(record.propertyCode, updateArrayObjectProperty(objectProperties, record.propertyCode,
convertArrayToString(record.integerArrayPropertyValue)); record.integerArrayPropertyValue);
} else if (record.realArrayPropertyValue != null) } else if (record.realArrayPropertyValue != null)
{ {
objectProperties.put(record.propertyCode, updateArrayObjectProperty(objectProperties, record.propertyCode,
convertArrayToString(record.realArrayPropertyValue)); record.realArrayPropertyValue);
} else if (record.stringArrayPropertyValue != null) } else if (record.stringArrayPropertyValue != null)
{ {
objectProperties.put(record.propertyCode, updateArrayObjectProperty(objectProperties, record.propertyCode,
convertArrayToString(record.stringArrayPropertyValue)); record.stringArrayPropertyValue);
} else if (record.timestampArrayPropertyValue != null) } else if (record.timestampArrayPropertyValue != null)
{ {
objectProperties.put(record.propertyCode, updateArrayObjectProperty(objectProperties, record.propertyCode,
convertArrayToString(record.timestampArrayPropertyValue)); record.timestampArrayPropertyValue);
} else if (record.jsonPropertyValue != null) } else if (record.jsonPropertyValue != null)
{ {
objectProperties.put(record.propertyCode, record.jsonPropertyValue); updateObjectProperty(objectProperties, record.propertyCode,
record.jsonPropertyValue);
} else } else
{ {
// SAMPLE property with deleted sample. Thus, nothing is put to objectProperties // SAMPLE property with deleted sample. Thus, nothing is put to objectProperties
...@@ -124,15 +111,57 @@ public abstract class PropertyTranslator extends ...@@ -124,15 +111,57 @@ public abstract class PropertyTranslator extends
return properties; return properties;
} }
private Serializable composeMultiValueProperty(Serializable current, Serializable newValue) { private void updateObjectProperty(Map<String, Serializable> objectProperties,
String propertyCode, Serializable propertyValue)
{
if (objectProperties.containsKey(propertyCode))
{
Serializable current = objectProperties.get(propertyCode);
Serializable newValue = composeMultiValueProperty(current, propertyValue);
objectProperties.put(propertyCode, newValue);
} else
{
objectProperties.put(propertyCode, propertyValue);
}
}
private void updateArrayObjectProperty(Map<String, Serializable> objectProperties,
String propertyCode, Serializable[] propertyValue)
{
if (objectProperties.containsKey(propertyCode))
{
Serializable[] current = (Serializable[]) objectProperties.get(propertyCode);
Serializable[] result;
if(current.length > 0) {
if(current[0].getClass().isArray()) {
result = new Serializable[current.length + 1];
System.arraycopy(current, 0, result, 0, current.length);
result[current.length] = propertyValue;
} else {
result = new Serializable[] {current, propertyValue};
}
} else {
result = propertyValue;
}
objectProperties.put(propertyCode, result);
} else
{
objectProperties.put(propertyCode, propertyValue);
}
}
private Serializable composeMultiValueProperty(Serializable current, Serializable newValue)
{
Serializable[] result; Serializable[] result;
if(current.getClass().isArray()) { if (current.getClass().isArray())
{
Serializable[] values = (Serializable[]) current; Serializable[] values = (Serializable[]) current;
result = new Serializable[values.length + 1]; result = new Serializable[values.length + 1];
System.arraycopy(values, 0, result, 0, values.length); System.arraycopy(values, 0, result, 0, values.length);
result[values.length] = newValue; result[values.length] = newValue;
} else { } else
result = new Serializable[] {current, newValue}; {
result = new Serializable[] { current, newValue };
} }
return result; return result;
} }
...@@ -150,7 +179,8 @@ public abstract class PropertyTranslator extends ...@@ -150,7 +179,8 @@ public abstract class PropertyTranslator extends
ObjectHolder<Map<String, Serializable>> result, Object relations, ObjectHolder<Map<String, Serializable>> result, Object relations,
PropertyFetchOptions fetchOptions) PropertyFetchOptions fetchOptions)
{ {
Map<Long, Map<String, Serializable>> properties = (Map<Long, Map<String, Serializable>>) relations; Map<Long, Map<String, Serializable>> properties =
(Map<Long, Map<String, Serializable>>) relations;
Map<String, Serializable> objectProperties = properties.get(objectId); Map<String, Serializable> objectProperties = properties.get(objectId);
if (objectProperties == null) if (objectProperties == null)
......
...@@ -268,7 +268,7 @@ public final class EntityPropertiesConverter implements IEntityPropertiesConvert ...@@ -268,7 +268,7 @@ public final class EntityPropertiesConverter implements IEntityPropertiesConvert
{ {
final String propertyCode = property.getPropertyType().getCode(); final String propertyCode = property.getPropertyType().getCode();
final PropertyTypePE propertyType = getPropertyType(propertyCode); final PropertyTypePE propertyType = getPropertyType(propertyCode);
final String valueOrNull = property.tryGetAsString(); final Serializable valueOrNull = property.getValue();
ExtendedEntityTypePropertyType extendedETPT = ExtendedEntityTypePropertyType extendedETPT =
getEntityTypePropertyType(entityTypePE, propertyType); getEntityTypePropertyType(entityTypePE, propertyType);
final EntityTypePropertyTypePE entityTypePropertyTypePE = final EntityTypePropertyTypePE entityTypePropertyTypePE =
...@@ -281,12 +281,26 @@ public final class EntityPropertiesConverter implements IEntityPropertiesConvert ...@@ -281,12 +281,26 @@ public final class EntityPropertiesConverter implements IEntityPropertiesConvert
if (isNullOrBlank(valueOrNull) == false) if (isNullOrBlank(valueOrNull) == false)
{ {
List<T> results = new ArrayList<>(); List<T> results = new ArrayList<>();
String translatedValue = extendedETPT.translate(registrator, valueOrNull); if(propertyType.isMultiValue() && valueOrNull.getClass().isArray()) {
for (Serializable value : (Serializable[]) valueOrNull)
{
String translatedValue =
extendedETPT.translate(registrator, (String) value);
final String validatedValue =
propertyValueValidator.validatePropertyValue(propertyType,
translatedValue);
results.addAll(createEntityProperty(registrator, propertyType,
entityTypePropertyTypePE,
validatedValue));
}
} else {
String translatedValue = extendedETPT.translate(registrator, (String) valueOrNull);
final String validatedValue = final String validatedValue =
propertyValueValidator.validatePropertyValue(propertyType, translatedValue); propertyValueValidator.validatePropertyValue(propertyType, translatedValue);
results.addAll(createEntityProperty(registrator, propertyType, entityTypePropertyTypePE, results.addAll(createEntityProperty(registrator, propertyType, entityTypePropertyTypePE,
validatedValue)); validatedValue));
}
return results; return results;
} }
return null; return null;
...@@ -299,7 +313,9 @@ public final class EntityPropertiesConverter implements IEntityPropertiesConvert ...@@ -299,7 +313,9 @@ public final class EntityPropertiesConverter implements IEntityPropertiesConvert
{ {
List<T> entityProperties = new ArrayList<>(); List<T> entityProperties = new ArrayList<>();
String val = value; String val = value;
if (propertyType.isMultiValue()) List<DataTypeCode> arrayTypes = List.of(DataTypeCode.ARRAY_STRING,
DataTypeCode.ARRAY_INTEGER, DataTypeCode.ARRAY_REAL, DataTypeCode.ARRAY_TIMESTAMP);
if (propertyType.isMultiValue() && !arrayTypes.contains(propertyType.getType().getCode()))
{ {
if (val.startsWith("[")) if (val.startsWith("["))
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment