-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
0def2c7
commit 39538ba
Showing
16 changed files
with
1,081 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...e/src/main/java/org/databiosphere/workspacedataservice/dataimport/PfbRecordConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.databiosphere.workspacedataservice.dataimport; | ||
|
||
import java.util.List; | ||
import org.apache.avro.Schema; | ||
import org.apache.avro.generic.GenericRecord; | ||
import org.databiosphere.workspacedataservice.shared.model.Record; | ||
import org.databiosphere.workspacedataservice.shared.model.RecordAttributes; | ||
import org.databiosphere.workspacedataservice.shared.model.RecordType; | ||
|
||
/** Logic to convert a PFB's GenericRecord to WDS's Record */ | ||
public class PfbRecordConverter { | ||
|
||
public static final String ID_FIELD = "id"; | ||
public static final String TYPE_FIELD = "name"; | ||
public static final String OBJECT_FIELD = "object"; | ||
|
||
public Record genericRecordToRecord(GenericRecord genRec) { | ||
Record converted = | ||
new Record( | ||
genRec.get(ID_FIELD).toString(), | ||
RecordType.valueOf(genRec.get(TYPE_FIELD).toString()), | ||
RecordAttributes.empty()); | ||
|
||
// contains attributes | ||
if (genRec.get(OBJECT_FIELD) instanceof GenericRecord objectAttributes) { | ||
Schema schema = objectAttributes.getSchema(); | ||
List<Schema.Field> fields = schema.getFields(); | ||
RecordAttributes attributes = RecordAttributes.empty(); | ||
for (Schema.Field field : fields) { | ||
String fieldName = field.name(); | ||
Object value = | ||
objectAttributes.get(fieldName) == null | ||
? null | ||
: convertAttributeType(objectAttributes.get(fieldName)); | ||
attributes.putAttribute(fieldName, value); | ||
} | ||
converted.setAttributes(attributes); | ||
} | ||
|
||
return converted; | ||
} | ||
|
||
// TODO AJ-1452: respect the datatypes returned by the PFB. For now, we make no guarantee that | ||
// about datatypes; many values are just toString()-ed. This allows us to commit incremental | ||
// progress and save some complicated work for later. | ||
Object convertAttributeType(Object attribute) { | ||
if (attribute == null) { | ||
return null; | ||
} | ||
if (attribute instanceof Long /*or other number*/) { | ||
return attribute; | ||
} | ||
return attribute.toString(); // easier for the datatype inferer to parse | ||
} | ||
} |
Oops, something went wrong.