public void addPropertyDefinition(PropertyMappingDefinition propDef) { propertyCacheByColName.put(propDef.getColName(), propDef); propertyCacheByPropName.put(propDef.getPropDesc().getName(), propDef); }
@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 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); } }
@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; }
@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); } }
@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()); }
@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()); } }