/** * Marshal collection. * * @param cassandraTypeClazz * the cassandra type clazz * @param result * the result * @param clazz * the clazz * @return the collection */ public static Collection marshalCollection(Class cassandraTypeClazz, Collection result, Class clazz, Class resultTypeClass) { Collection mappedCollection = result; if (cassandraTypeClazz.isAssignableFrom(BytesType.class)) { mappedCollection = (Collection) PropertyAccessorHelper.getObject(resultTypeClass); for (Object value : result) { byte[] valueAsBytes = new byte[((ByteBuffer) value).remaining()]; ((ByteBuffer) value).get(valueAsBytes); mappedCollection.add(PropertyAccessorHelper.getObject(clazz, valueAsBytes)); } } return mappedCollection; }
/** * Appends column value with parametrised builder object. Returns true if * value is present. * * @param builder * the builder * @param valueObj * the value obj * @param column * the column * @return true if value is not null,else false. */ private boolean appendColumnValue(StringBuilder builder, Object valueObj, Field column) { Object value = PropertyAccessorHelper.getObject(valueObj, column); boolean isPresent = false; isPresent = appendValue(builder, column.getType(), value, isPresent, false); return isPresent; }
/** * Build where clause with @ EQ_CLAUSE} clause. * * @param builder * the builder * @param field * the field * @param member * the member * @param entity * the entity */ public void buildWhereClause(StringBuilder builder, String field, Field member, Object entity) { // builder = ensureCase(builder, field, false); // builder.append(EQ_CLAUSE); // appendColumnValue(builder, entity, member); // builder.append(AND_CLAUSE); Object value = PropertyAccessorHelper.getObject(entity, member); buildWhereClause(builder, member.getType(), field, value, EQ_CLAUSE, false); }
/** * @param result * @param entityMetadata * @param em * @return */ private List onNativeCassResults(List result, EntityMetadata entityMetadata, EntityManager em) { Map<String, Client<Query>> clients = (Map<String, Client<Query>>) em.getDelegate(); Client client = clients.get(entityMetadata.getPersistenceUnit()); if((client.getClass().getSimpleName().equals("ThriftClient") || client.getClass().getSimpleName().equals("PelopsClient") || client.getClass().getSimpleName().equals("DSClient")) && Column.class.equals(result.get(0).getClass())) { int count = 0; for(Object column : result) { Map<Object, Object> valueMap = new HashMap<Object, Object>(); valueMap.put(PropertyAccessorHelper.getObject(String.class, ((Column) column).getName()), PropertyAccessorHelper.getObject(Long.class, ((Column) column).getValue())); result.set(count, valueMap); count ++; } } return result; }
/** * Gets field value as byte-array. * * @param from * the from * @param field * the field * * @return the byte[] * * @throws PropertyAccessException * the property access exception */ public static byte[] get(Object from, Field field) { PropertyAccessor<?> accessor = PropertyAccessorFactory.getPropertyAccessor(field); return accessor.toBytes(getObject(from, field)); }
/** * Gets the compound key. * * @param attribute * the attribute * @param entity * the entity * @return the compound key * @throws InstantiationException * the instantiation exception * @throws IllegalAccessException * the illegal access exception */ private Object getCompoundKey(Attribute attribute, Object entity) throws InstantiationException, IllegalAccessException { Object compoundKeyObject = null; if (entity != null) { compoundKeyObject = PropertyAccessorHelper.getObject(entity, (Field) attribute.getJavaMember()); if (compoundKeyObject == null) { compoundKeyObject = ((AbstractAttribute) attribute).getBindableJavaType().newInstance(); } } return compoundKeyObject; }
/** * Gets the string. * * @param from * the from * @param field * the field * * @return the string * * @throws PropertyAccessException * the property access exception */ public static String getString(Object from, Field field) { PropertyAccessor<?> accessor = PropertyAccessorFactory.getPropertyAccessor(field); Object object = getObject(from, field); return object != null ? accessor.toString(object) : null; }
/** * Test method for {@link com.impetus.kundera.property.PropertyAccessorHelper#getObject(java.lang.Class)}. */ @Test public void testGetObjectClassOfQ() { Object o = PropertyAccessorHelper.getObject(PhotographerUni_1_M_1_M.class); Assert.assertNotNull(o); Assert.assertTrue(o instanceof PhotographerUni_1_M_1_M); }
/** * Test method for {@link com.impetus.kundera.property.PropertyAccessorHelper#getObject(java.lang.Object, java.lang.String)}. */ @Test public void testGetObjectObjectString() { PersonnelDTO person = new PersonnelDTO("1", "Amresh", "Singh"); try { Object id = PropertyAccessorHelper.getObject(person, "personId"); Object fn = PropertyAccessorHelper.getObject(person, "firstName"); Object ln = PropertyAccessorHelper.getObject(person, "lastName"); Assert.assertEquals("1", id); Assert.assertEquals("Amresh", fn); Assert.assertEquals("Singh", ln); } catch (SecurityException e) { Assert.fail(e.getMessage()); } }
/** * Adds the source. * * @param entity * the entity * @param values * the values * @param entityType * the entity type */ private void addSource(Object entity, Map<String, Object> values, EntityType entityType) { Set<Attribute> attributes = entityType.getAttributes(); for (Attribute attrib : attributes) { if (!attrib.isAssociation()) { Object value = PropertyAccessorHelper.getObject(entity, (Field) attrib.getJavaMember()); values.put(((AbstractAttribute) attrib).getJPAColumnName(), value); } } }
/** * On embeddable. * * @param entityType * the entity type * @param column * the column * @param entity * the entity * @param embeddableType * the embeddable type * @param jsonObject * the json object */ private static void onEmbeddable(EntityType entityType, Attribute column, Object entity, EmbeddableType embeddableType, JsonObject jsonObject) { Object embeddedObject = PropertyAccessorHelper.getObject(entity, (Field) column.getJavaMember()); String embeddedColumnName = ((AbstractAttribute) column).getJPAColumnName(); Set<Attribute> embeddableAttributes = embeddableType.getAttributes(); jsonObject.add(embeddedColumnName, getJsonObject(embeddableAttributes, embeddedObject)); }
/** * Gets the json object. * * @param columns * the columns * @param object * the object * @return the json object */ private static JsonObject getJsonObject(Set<Attribute> columns, Object object) { JsonObject jsonObject = new JsonObject(); for (Attribute column : columns) { if (!column.isAssociation()) { Object valueObject = PropertyAccessorHelper.getObject(object, (Field) column.getJavaMember()); jsonObject.add(((AbstractAttribute) column).getJPAColumnName(), getJsonPrimitive(valueObject, column.getJavaType())); } } return jsonObject; }
/** * Test method for {@link com.impetus.kundera.property.PropertyAccessorHelper#getObject(java.lang.Object, java.lang.reflect.Field)}. */ @Test public void testGetObjectObjectField() { PersonnelDTO person = new PersonnelDTO("1", "Amresh", "Singh"); try { Object id = PropertyAccessorHelper.getObject(person, person.getClass().getDeclaredField("personId")); Object fn = PropertyAccessorHelper.getObject(person, person.getClass().getDeclaredField("firstName")); Object ln = PropertyAccessorHelper.getObject(person, person.getClass().getDeclaredField("lastName")); Assert.assertEquals("1", id); Assert.assertEquals("Amresh", fn); Assert.assertEquals("Singh", ln); } catch (SecurityException e) { Assert.fail(e.getMessage()); } catch (NoSuchFieldException e) { Assert.fail(e.getMessage()); } }
/** * Iterate and populate json object. * * @param entity * the entity * @param iterator * the iterator * @return the json object */ private JsonObject iterateAndPopulateJsonObject(Object entity, Iterator<Attribute> iterator, String tableName) { JsonObject obj = JsonObject.create(); while (iterator.hasNext()) { Attribute attribute = iterator.next(); Field field = (Field) attribute.getJavaMember(); Object value = PropertyAccessorHelper.getObject(entity, field); obj.put(((AbstractAttribute) attribute).getJPAColumnName(), value); } obj.put(CouchbaseConstants.KUNDERA_ENTITY, tableName); return obj; }
/** * Get identifier of an entity object by invoking getXXX() method. * * * @param entity * the entity * @param metadata * the metadata * * @return the id * * @throws PropertyAccessException * the property access exception */ public static Object getId(Object entity, EntityMetadata metadata) { // If an Entity has been wrapped in a Proxy, we can call the Proxy // classes' getId() method if (entity instanceof EnhanceEntity) { return ((EnhanceEntity) entity).getEntityId(); } // Otherwise, as Kundera currently supports only field access, access // the underlying Entity's id field return getObject(entity, (Field) metadata.getIdAttribute().getJavaMember()); }
private Object getObjectFromByteArray(EntityType entityType, byte[] value, String jpaColumnName, EntityMetadata m) { if (jpaColumnName != null) { String fieldName = m.getFieldName(jpaColumnName); if (fieldName != null) { Attribute attribute = fieldName != null ? entityType.getAttribute(fieldName) : null; EntityMetadata relationMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, attribute.getJavaType()); Object colValue = PropertyAccessorHelper.getObject(relationMetadata.getIdAttribute().getJavaType(), (byte[]) value); return colValue; } } log.warn("No value found for column {}, returning null.", jpaColumnName); return null; }
/** * setter field. * * @param row * the row * @param schemaTable * the schema table * @param embeddedObject * the embedded object * @param embeddedAttrib * the embedded attrib */ private void setField(Row row, Table schemaTable, Object embeddedObject, Attribute embeddedAttrib) { Field field = (Field) embeddedAttrib.getJavaMember(); FieldDef fieldDef = schemaTable.getField(((AbstractAttribute) embeddedAttrib).getJPAColumnName()); Object valueObj = PropertyAccessorHelper.getObject(embeddedObject, field); if (valueObj != null) NoSqlDBUtils.add(fieldDef, row, valueObj, ((AbstractAttribute) embeddedAttrib).getJPAColumnName()); }
/** * Gets (if available) or creates a relationship for the given entity */ private Relationship getOrCreateRelationshipWithUniqueFactory(Object entity, EntityMetadata m, GraphDatabaseService graphDb) { Object id = PropertyAccessorHelper.getObject(entity, (Field) m.getIdAttribute().getJavaMember()); final String idFieldName = m.getIdAttribute().getName(); UniqueFactory<Relationship> factory = new UniqueFactory.UniqueRelationshipFactory(graphDb, m.getIndexName()) { @Override protected Relationship create(Map<String, Object> paramMap) { return null; } @Override protected void initialize(Relationship relationship, Map<String, Object> properties) { relationship.setProperty(idFieldName, properties.get(idFieldName)); } }; return factory.getOrCreate(idFieldName, id); }
private void assertBytes(UUID randomId, byte[] recInBytes) { try { String str = (String) PropertyAccessorHelper.getObject(String.class, recInBytes); if(!str.equals("Vivek")) // string record is already compared { UUIDAccessor uuidAccessor = new UUIDAccessor(); UUID recAsUUID = uuidAccessor.fromBytes(UUID.class, recInBytes); Assert.assertEquals(randomId, recAsUUID); } } catch (Exception e) { Assert.fail(); } }
private void assertBytes(UUID randomId, byte[] recInBytes) { try { String str = (String) PropertyAccessorHelper.getObject(String.class, recInBytes); if(!str.equals("Vivek")) // string record is already compared { UUIDAccessor uuidAccessor = new UUIDAccessor(); UUID recAsUUID = uuidAccessor.fromBytes(UUID.class, recInBytes); Assert.assertEquals(randomId, recAsUUID); } } catch (Exception e) { Assert.fail(); } }