private Map<String, Definition> createMapDefinition(ColumnDefinitions columnDefinitions) { Map<String, Definition> map = new HashMap<String, ColumnDefinitions.Definition>(); for (Definition column : columnDefinitions) { map.put(column.getName(), column); } return map; }
/** * Populates and iterates through a {@link Row} using {@link ColumnDefinitions} provided by the * driver. * * @param row * @return */ public T get(Row row) { // Entity and its associated id object T entity = null; Object idObj = null; try { entity = entityClass.newInstance(); idObj = null; ColumnMapping idcolmap = null; // Note that this early invocation means that // you can't populate on #setId in your java bean method if (embedded != null) { idObj = embedded.field.getType().newInstance(); embedded.set(entity, idObj); } else { idObj = idMapping.field.getType().newInstance(); idMapping.set(entity, idObj); } } catch (InstantiationException | IllegalAccessException e) { throw new IllegalAccessError( "A configuration exception has occurred in creating entity: " + entityClass); } ColumnDefinitions metaData = row.getColumnDefinitions(); List<Definition> defList = metaData.asList(); for (Definition def : defList) { ColumnMapping mapping = colsToFields.get(def.getName()); if (mapping == null) { // it could be an id column if (idMapping != null && def.getName().equals(idMapping.name)) { idMapping.set(entity, getValue(row, idMapping, def)); continue; } // else: need to find it, possible point of refactor ColumnMapping nembed = null; if (embedded != null) nembed = embedded.get(def.getName()); if (nembed != null) nembed.set(idObj, getValue(row, nembed, def)); continue; } Object value = getValue(row, mapping, def); // set it if (value != null) mapping.set(entity, value); } return entity; }
@Override public Object getObject(Map<String, Definition> mapDefinition, Field field, Row row) { Definition column = mapDefinition.get(ColumnUtil.INTANCE.getColumnName(field).toLowerCase()); ByteBuffer buffer = (ByteBuffer) RelationShipJavaCassandra.INSTANCE.getObject( row, column.getType().getName(), column.getName()); CustomData customData = field.getAnnotation(CustomData.class); Customizable customizable = Customizable.class.cast(ReflectionUtil.INSTANCE.newInstance(customData.classCustmo())); return customizable.write(buffer); }
@Override public Object getObject(Map<String, Definition> mapDefinition, Field field, Row row) { Definition column = mapDefinition.get(ColumnUtil.INTANCE.getColumnName(field).toLowerCase()); return RelationShipJavaCassandra.INSTANCE.getObject( row, column.getType().getName(), column.getName()); }
private Object getValue(Row row, ColumnMapping mapping, Definition def) { Object value = null; if (DataType.text().equals(mapping.type)) { value = row.getString(def.getName()); } else if (DataType.blob().equals(mapping.type)) { value = row.getBytes(def.getName()); } else if (DataType.map(DataType.text(), DataType.text()).equals(mapping.type)) { value = row.getMap(def.getName(), String.class, String.class); } else if (DataType.cint().equals(mapping.type)) { value = row.getInt(def.getName()); } else if (DataType.bigint().equals(mapping.type)) { value = row.getLong(def.getName()); } else if (DataType.timestamp().equals(mapping.type)) { value = row.getDate(def.getName()); } else if (DataType.blob().equals(mapping.type)) { value = row.getBytes(def.getName()); } else if (DataType.cdouble().equals(mapping.type)) { value = row.getBytes(def.getName()); } else if (DataType.cfloat().equals(mapping.type)) { value = row.getFloat(def.getName()); } else if (DataType.inet().equals(mapping.type)) { value = row.getInet(def.getName()); } else if (DataType.cboolean().equals(mapping.type)) { value = row.getBool(def.getName()); } else if (DataType.uuid().equals(mapping.type)) { value = row.getUUID(def.getName()); } // what if value is null? primitives won't like this return value; }