public void addIdPropertyMap(PropertyMappingDefinition idProperty) { idPropertyMap.put(idProperty.getPropDesc().getName(), idProperty); }
public void addPropertyDefinition(PropertyMappingDefinition propDef) { propertyCacheByColName.put(propDef.getColName(), propDef); propertyCacheByPropName.put(propDef.getPropDesc().getName(), propDef); }
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); } }
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 <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 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); }
private <T> void setSimpleId(CFMappingDef<T> cfMapDef, T obj, Object pkObj) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { PropertyMappingDefinition md = cfMapDef.getKeyDef().getIdPropertyMap().values().iterator() .next(); if (null == md) { throw new HectorObjectMapperException( "Trying to build new object but haven't annotated a field with @" + Id.class.getSimpleName()); } Method meth = md.getPropDesc().getWriteMethod(); if (null == meth) { logger.debug("@Id annotation found - but can't find setter for property, " + md.getPropDesc().getName()); throw new HectorObjectMapperException( "Trying to build new object but can't find setter for property, " + md.getPropDesc().getName()); } meth.invoke(obj, pkObj); }
@Override public Object convertCassTypeToObjType(PropertyMappingDefinition md, byte[] value) { Serializer<?> s = HectorObjectMapper.determineSerializer(md.getPropDesc().getPropertyType()); return s.fromBytes(value); }
@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)"); } }
private <T> void setComplexId(CFMappingDef<T> cfMapDef, T obj, Object pkObj) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { KeyDefinition keyDef = cfMapDef.getKeyDef(); if (!pkObj.getClass().equals(keyDef.getPkClazz())) { throw new HectorObjectMapperException("primary key object, " + pkObj.getClass().getName() + ", must be of type " + keyDef.getPkClazz().getName()); } for (PropertyDescriptor pd : keyDef.getPropertyDescriptorMap().values()) { PropertyMappingDefinition md = keyDef.getIdPropertyMap().get(pd.getName()); if (null == md) { throw new HectorObjectMapperException("Trying to set complex key type, but field, " + pd.getName() + ", is not annotated with @" + Id.class.getSimpleName() + " in POJO, " + cfMapDef.getRealClass().getName()); } Method meth = md.getPropDesc().getWriteMethod(); if (null == meth) { logger.debug("@Id annotation found - but can't find setter for property, " + md.getPropDesc().getName()); } meth.invoke(obj, pd.getReadMethod().invoke(pkObj, (Object[]) null)); } }
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); }
@SuppressWarnings("rawtypes") public void instantiateCollection(Object obj, HColumn<String, byte[]> col, PropertyMappingDefinition md) { CollectionInfoColValue colValue = parseCollectionInfoColValue(col.getValue()); try { // type name is Class that needs instantiating. examine for special class // features, like ArrayList taking a size hint in constructor Collection<?> collObj; Class clazz = Class.forName(colValue.getCollTypeName()); if (ArrayList.class.isAssignableFrom(clazz)) { Constructor<ArrayList> cons = ArrayList.class.getConstructor(int.class); collObj = cons.newInstance(colValue.getSize()); } else { collObj = (Collection<?>) clazz.newInstance(); } // now save collection to object PropertyDescriptor pd = md.getPropDesc(); if (null == pd.getWriteMethod()) { throw new RuntimeException("property, " + pd.getName() + ", does not have a setter and therefore cannot be set"); } pd.getWriteMethod().invoke(obj, collObj); } catch (Throwable e) { throw new HectorObjectMapperException("exception while instantiating Collection type, " + colValue.getCollTypeName(), e); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private <T> Collection<HColumn<String, byte[]>> createColumnsFromCollectionProperty(T obj, PropertyMappingDefinition md) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { // get collection Object tmpColl = reflectionHelper.invokeGetter(obj, md); if (!(tmpColl instanceof Collection)) { throw new HectorObjectMapperException("property, " + md.getColName() + ", is marked as a collection type, but not actually a Collection. Property is type, " + md.getPropDesc().getPropertyType()); } LinkedList<HColumn<String, byte[]>> colList = new LinkedList<HColumn<String, byte[]>>(); // save collection info Collection coll = (Collection) tmpColl; colList.add(createHColumn(md.getColName(), collMapperHelper.createCollectionInfoColValue(coll))); // iterate over collection applying converter to its elements int count = 0; for (Object elem : coll) { byte[] bytes = collMapperHelper.serializeCollectionValue(elem); if (null == bytes) { return null; } colList.add(createHColumn( collMapperHelper.createCollectionItemColName(md.getColName(), count), bytes)); count++; } return colList; }
String colName = col.getName(); PropertyMappingDefinition md = cfMapDefInstance.getPropMapByColumnName(colName); if (null != md && null != md.getPropDesc()) { if (!md.isCollectionType()) { setPropertyUsingColumn(obj, col, md);
@Test public void testGetColFamMapDefByClass() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); cacheMgr.initializeCacheForClass(MyTestBean.class); CFMappingDef<?> cfMapDef = cacheMgr.getCfMapDef(MyTestBean.class, false); assertNotNull(cfMapDef); assertEquals( "TestBeanColumnFamily", cfMapDef.getColFamName()); assertNotNull( "Column family not registered properly", cacheMgr.getCfMapDef("TestBeanColumnFamily", false)); assertEquals(MyTestBean.class, cfMapDef.getEffectiveClass()); assertEquals("did not find @Id properly", "baseId", cfMapDef.getKeyDef().getIdPropertyMap() .values().iterator().next() .getPropDesc().getName()); assertEquals("did not setup properties properly", ColorConverter.class, cfMapDef.getPropMapByColumnName("color").getConverter().getClass()); }
@Test public void testInheritanceOfEntity() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); CFMappingDef<MyRedTestBean> cfMapDef = cacheMgr.initializeCacheForClass(MyRedTestBean.class); // 13 is valid when custom conversion of enumerations works again // don't like hard coding numbers into JUnits, but took easy way for now assertEquals(14, cfMapDef.getAllProperties().size()); assertNotNull(cfMapDef.getCfBaseMapDef()); assertEquals(MyRedTestBean.class, cfMapDef.getEffectiveClass()); assertEquals("TestBeanColumnFamily", cfMapDef.getEffectiveColFamName()); assertEquals("myType", cfMapDef.getDiscColumn()); assertEquals(DiscriminatorType.STRING, cfMapDef.getDiscType()); assertEquals("baseId", cfMapDef.getKeyDef().getIdPropertyMap().values().iterator().next() .getPropDesc().getName()); // check super class settings assertEquals( MyRedTestBean.class.getSuperclass(), cfMapDef.getCfSuperMapDef().getRealClass()); assertFalse( cfMapDef.getCfSuperMapDef().isColumnSliceRequired()); }
@Test public void testInheritanceWithMultiLevels() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); CFMappingDef<Desk> cfMapDef = cacheMgr.initializeCacheForClass(Desk.class); CFMappingDef<Furniture> cfBaseMapDef = cacheMgr.getCfMapDef(Furniture.class, true); assertEquals(7, cfMapDef.getAllProperties().size()); assertNotNull(cfMapDef.getCfSuperMapDef()); assertNotNull(cfMapDef.getCfBaseMapDef()); assertEquals(Desk.class.getSuperclass(), cfMapDef.getCfSuperMapDef().getEffectiveClass()); assertEquals(Desk.class.getSuperclass().getSuperclass(), cfMapDef.getCfSuperMapDef() .getCfSuperMapDef() .getEffectiveClass()); assertEquals(cfBaseMapDef.getEffectiveColFamName(), cfMapDef.getEffectiveColFamName()); assertEquals("type", cfMapDef.getDiscColumn()); assertEquals("table_desk", cfMapDef.getDiscValue()); assertEquals(DiscriminatorType.STRING, cfMapDef.getDiscType()); assertEquals("id", cfMapDef.getKeyDef().getIdPropertyMap().values().iterator().next() .getPropDesc().getName()); }