Search in sources :

Example 1 with EntitySchemaFactory

use of org.onebusaway.csv_entities.schema.EntitySchemaFactory in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method setObjectPropertiesFromJsonUsingCsvFields.

private void setObjectPropertiesFromJsonUsingCsvFields(Object object, JSONObject json, String line) throws JSONException, TransformSpecificationMissingArgumentException {
    EntitySchemaFactory entitySchemaFactory = _transformer.getReader().getEntitySchemaFactory();
    EntitySchema schema = entitySchemaFactory.getSchema(object.getClass());
    BeanWrapper wrapped = BeanWrapperFactory.wrap(object);
    Map<String, Object> values = new HashMap<String, Object>();
    for (Iterator<?> it = json.keys(); it.hasNext(); ) {
        String key = (String) it.next();
        Object v = json.get(key);
        if (v instanceof JSONArray) {
            JSONArray array = (JSONArray) v;
            List<Object> asList = new ArrayList<Object>();
            for (int i = 0; i < array.length(); ++i) {
                asList.add(array.get(i));
            }
            v = asList;
        }
        values.put(key, v);
    }
    CsvEntityContext context = new CsvEntityContextImpl();
    for (FieldMapping mapping : schema.getFields()) {
        try {
            mapping.translateFromCSVToObject(context, values, wrapped);
        } catch (MissingRequiredFieldException ex) {
            String verboseMessage = "line=" + line + ", context=" + context + ", json=" + json + ", object=" + object;
            _log.error("missing required field; details:" + verboseMessage);
            throw new TransformSpecificationMissingArgumentException(verboseMessage, ex.getFieldName());
        }
    }
}
Also used : MissingRequiredFieldException(org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException) HashMap(java.util.HashMap) FieldMapping(org.onebusaway.csv_entities.schema.FieldMapping) SingleFieldMapping(org.onebusaway.csv_entities.schema.SingleFieldMapping) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) CsvEntityContextImpl(org.onebusaway.csv_entities.CsvEntityContextImpl) BeanWrapper(org.onebusaway.csv_entities.schema.BeanWrapper) EntitySchemaFactory(org.onebusaway.csv_entities.schema.EntitySchemaFactory) CsvEntityContext(org.onebusaway.csv_entities.CsvEntityContext) JSONObject(org.json.JSONObject) TransformSpecificationMissingArgumentException(org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException) EntitySchema(org.onebusaway.csv_entities.schema.EntitySchema)

Example 2 with EntitySchemaFactory

use of org.onebusaway.csv_entities.schema.EntitySchemaFactory in project onebusaway-gtfs-modules by OneBusAway.

the class EntitySchemaCache method addEntitySchemasFromGtfsReader.

public void addEntitySchemasFromGtfsReader(GtfsReader reader) {
    EntitySchemaFactory factory = reader.getEntitySchemaFactory();
    for (Class<?> entityType : reader.getEntityClasses()) {
        EntitySchema schema = factory.getSchema(entityType);
        addEntitySchema(schema);
    }
}
Also used : EntitySchemaFactory(org.onebusaway.csv_entities.schema.EntitySchemaFactory) EntitySchema(org.onebusaway.csv_entities.schema.EntitySchema)

Aggregations

EntitySchema (org.onebusaway.csv_entities.schema.EntitySchema)2 EntitySchemaFactory (org.onebusaway.csv_entities.schema.EntitySchemaFactory)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 CsvEntityContext (org.onebusaway.csv_entities.CsvEntityContext)1 CsvEntityContextImpl (org.onebusaway.csv_entities.CsvEntityContextImpl)1 MissingRequiredFieldException (org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException)1 BeanWrapper (org.onebusaway.csv_entities.schema.BeanWrapper)1 FieldMapping (org.onebusaway.csv_entities.schema.FieldMapping)1 SingleFieldMapping (org.onebusaway.csv_entities.schema.SingleFieldMapping)1 TransformSpecificationMissingArgumentException (org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException)1