private <T> void processColumnAnnotation(Field f, Column anno, PropertyDescriptor pd, CFMappingDef<T> cfMapDef) throws InstantiationException, IllegalAccessException { PropertyMappingDefinition md = new PropertyMappingDefinition(pd, anno.name(), DefaultConverter.class); cfMapDef.addPropertyDefinition(md); }
public void addPropertyDefinition(PropertyMappingDefinition propDef) { propertyCacheByColName.put(propDef.getColName(), propDef); propertyCacheByPropName.put(propDef.getPropDesc().getName(), propDef); }
private <T> void setPropertyUsingColumn(T obj, HColumn<String, byte[]> col, PropertyMappingDefinition md) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { PropertyDescriptor pd = md.getPropDesc(); if (null == pd.getWriteMethod()) { throw new RuntimeException("property, " + pd.getName() + ", on class, " + obj.getClass().getName() + ", does not have a setter and therefore cannot be set"); } Object value = md.getConverter().convertCassTypeToObjType(md, col.getValue()); pd.getWriteMethod().invoke(obj, value); }
private <T> void processIdAnnotation(Field f, Id anno, CFMappingDef<T> cfMapDef, Map<String, PropertyDescriptor> pdMap) throws InstantiationException, IllegalAccessException { // TODO lookup JPA 2 spec for class-level ids PropertyMappingDefinition md = new PropertyMappingDefinition(pdMap.get(f.getName()), null, DefaultConverter.class); if (null == md.getPropDesc() || null == md.getPropDesc().getReadMethod() || null == md.getPropDesc().getWriteMethod()) { throw new HectorObjectMapperException("@" + Id.class.getSimpleName() + " is defined on property, " + f.getName() + ", but its missing proper setter/getter"); } cfMapDef.getKeyDef().addIdPropertyMap(md); }
private void processColumnCustomAnnotation(Field f, me.prettyprint.hom.annotations.Column anno, PropertyDescriptor pd, CFMappingDef<?> cfMapDef) throws InstantiationException, IllegalAccessException { PropertyMappingDefinition md = new PropertyMappingDefinition(pd, anno.name(), anno.converter()); // if collection type and default converter then make note of collection type for later use Class<?> type = pd.getPropertyType(); if (Collection.class.isAssignableFrom(type) && md.isDefaultConverter()) { md.setCollectionType(type); } cfMapDef.addPropertyDefinition(md); } }
public void addIdPropertyMap(PropertyMappingDefinition idProperty) { idPropertyMap.put(idProperty.getPropDesc().getName(), idProperty); }
@Test public void testCollectionPropertyHandling() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); CFMappingDef<CollectionBean> cfMapDef = cacheMgr.initializeCacheForClass(CollectionBean.class); PropertyMappingDefinition md = cfMapDef.getPropMapByPropName("mySet"); assertEquals( Set.class, md.getCollectionType() ); assertEquals( "mySet", md.getColName()); assertNull( "should not be using slice query with List collection", cfMapDef.getSliceColumnNameArr()); }
@SuppressWarnings({ "unchecked" }) private <T> Collection<HColumn<String, byte[]>> createColumnsFromProperty(T obj, PropertyMappingDefinition md) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { if (!md.isCollectionType()) { byte[] colValue = createBytesFromPropertyValue(obj, md); if (null == colValue) { return null; } return Arrays.asList(createHColumn(md.getColName(), colValue)); } else { return createColumnsFromCollectionProperty(obj, md); } }
String colName = col.getName(); PropertyMappingDefinition md = cfMapDefInstance.getPropMapByColumnName(colName); if (null != md && null != md.getPropDesc()) { if (!md.isCollectionType()) { setPropertyUsingColumn(obj, col, md); } else {
private byte[] createBytesFromPropertyValue(Object obj, PropertyMappingDefinition md) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Object retVal = reflectionHelper.invokeGetter(obj, md); // if no value, then signal with null bytes if (null == retVal) { return null; } @SuppressWarnings("unchecked") byte[] bytes = md.getConverter().convertObjTypeToCassType(retVal); return bytes; }
private void generateColumnSliceIfNeeded(CFMappingDef<?> cfMapDef) { if (cfMapDef.isColumnSliceRequired()) { Collection<PropertyMappingDefinition> propColl = cfMapDef.getAllProperties(); String[] columnNames = new String[cfMapDef.isPersistableEntity() ? propColl.size() : propColl.size() + 1]; Iterator<PropertyMappingDefinition> iter = propColl.iterator(); int pos = 0; while (iter.hasNext()) { columnNames[pos++] = iter.next().getColName(); } // if an inheritance hierarchy exists we need to add in the discriminator // column if (!cfMapDef.isPersistableEntity()) { columnNames[pos] = cfMapDef.getDiscColumn(); } cfMapDef.setSliceColumnNameArr(columnNames); } }
public Collection<PropertyMappingDefinition> getCollectionProperties() { Set<PropertyMappingDefinition> collSet = new HashSet<PropertyMappingDefinition>(); for (PropertyMappingDefinition md : getAllProperties()) { if (md.isCollectionType()) { collSet.add(md); } } return collSet; }
private <T> void processIdCustomAnnotation(Field f, me.prettyprint.hom.annotations.Id anno, CFMappingDef<T> cfMapDef, Map<String, PropertyDescriptor> pdMap) throws InstantiationException, IllegalAccessException { // TODO lookup JPA 2 spec for class-level ids PropertyMappingDefinition md = new PropertyMappingDefinition(pdMap.get(f.getName()), null, anno.converter()); if (null == md.getPropDesc() || null == md.getPropDesc().getReadMethod() || null == md.getPropDesc().getWriteMethod()) { throw new HectorObjectMapperException("@" + Id.class.getSimpleName() + " is defined on property, " + f.getName() + ", but its missing proper setter/getter"); } cfMapDef.getKeyDef().addIdPropertyMap(md); }
public Object invokeGetter(Object obj, PropertyMappingDefinition md) { PropertyDescriptor pd = md.getPropDesc(); Method getter = pd.getReadMethod(); if (null == getter) { throw new RuntimeException("missing getter method for property, " + pd.getName()); } try { return getter.invoke(obj, (Object[]) null); } catch (Throwable e) { throw new HectorObjectMapperException("exception while invoking getter on object of type, " + obj.getClass().getName(), e); } }
@Test public void testCollectionWithCustomConverterPropertyHandling() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); CFMappingDef<CustomConvertedCollectionBean> cfMapDef = cacheMgr.initializeCacheForClass(CustomConvertedCollectionBean.class); PropertyMappingDefinition md = cfMapDef.getPropMapByPropName("mySet"); assertEquals( null, md.getCollectionType() ); assertEquals( "mySet", md.getColName()); assertNotNull( "should be using slice query with custom converted collection", cfMapDef.getSliceColumnNameArr()); } }
@SuppressWarnings("unchecked") private byte[] generateColumnFamilyKeyFromPkObj(CFMappingDef<?> cfMapDef, Object pkObj) { List<byte[]> segmentList = new ArrayList<byte[]>(cfMapDef.getKeyDef().getIdPropertyMap().size()); if (cfMapDef.getKeyDef().isComplexKey()) { Map<String, PropertyDescriptor> propertyDescriptorMap = cfMapDef.getKeyDef().getPropertyDescriptorMap(); for (String key : cfMapDef.getKeyDef().getIdPropertyMap().keySet()) { PropertyDescriptor pd = propertyDescriptorMap.get(key); segmentList.add(callMethodAndConvertToCassandraType(pkObj, pd.getReadMethod(), new DefaultConverter())); } } else { PropertyMappingDefinition md = cfMapDef.getKeyDef().getIdPropertyMap().values().iterator() .next(); segmentList.add(md.getConverter().convertObjTypeToCassType(pkObj)); } return keyConcatStrategy.concat(segmentList); }
public boolean isAnyCollections() { if (null == getAllProperties()) { return false; } for (PropertyMappingDefinition md : getAllProperties()) { if (md.isCollectionType()) { return true; } } return false; }
private byte[] generateColumnFamilyKeyFromPojo(Object obj, CFMappingDef<?> cfMapDef) { List<byte[]> segmentList = new ArrayList<byte[]>(cfMapDef.getKeyDef().getIdPropertyMap().size()); for (PropertyMappingDefinition md : cfMapDef.getKeyDef().getIdPropertyMap().values()) { Method meth = md.getPropDesc().getReadMethod(); segmentList.add(callMethodAndConvertToCassandraType(obj, meth, md.getConverter())); } return keyConcatStrategy.concat(segmentList); }
@Override public Object convertCassTypeToObjType(PropertyMappingDefinition md, byte[] value) { BigInteger bigInt = new BigInteger(value); // determine our target integer type and then go from there on the // conversion method Class<?> targetClass = md.getPropDesc().getPropertyType(); if (targetClass.equals(Integer.class) || targetClass.equals(int.class)) { return Integer.valueOf(bigInt.intValue()); } else if (targetClass.equals(Long.class) || targetClass.equals(long.class)) { return Long.valueOf(bigInt.longValue()); } else if (targetClass.equals(Short.class) || targetClass.equals(short.class)) { return Short.valueOf(bigInt.shortValue()); } else if (targetClass.equals(Byte.class) || targetClass.equals(byte.class)) { return Byte.valueOf(bigInt.byteValue()); } else if (targetClass.equals(BigInteger.class)) { return bigInt; } else { throw new HectorObjectMapperException("Column, " + md.getColName() + ", cannot be converted using " + getClass().getSimpleName() + " because POJO property, " + md.getPropDesc().getName() + ", of type " + md.getPropDesc().getPropertyType().getName() + " is not an integer type (in a mathematical context)"); } }
@Test(expected=HectorObjectMapperException.class) public void testStringFromCassNotWork() throws Exception { conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("str1", TestClass.class), "foo", VariableIntegerConverter.class), new String("123").getBytes()); }