@Override
public Object find(Class entityClass, Object key)
{
HttpResponse response = null;
EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClass);
MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel(
entityMetadata.getPersistenceUnit());
try
{
if (key instanceof JsonElement)
{
key = ((JsonElement) key).getAsString();
}
String _id = null;
if (metaModel.isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType()))
{
Field field = (Field) entityMetadata.getIdAttribute().getJavaMember();
EmbeddableType embeddableType = metaModel.embeddable(entityMetadata.getIdAttribute()
.getBindableJavaType());
_id = CouchDBObjectMapper.get_Id(field, key, embeddableType, entityMetadata.getTableName());
}
else
{
_id = entityMetadata.getTableName() + PropertyAccessorHelper.getString(key);
}
URI uri = new URI(CouchDBConstants.PROTOCOL, null, httpHost.getHostName(), httpHost.getPort(),
CouchDBConstants.URL_SAPRATOR + entityMetadata.getSchema().toLowerCase()
+ CouchDBConstants.URL_SAPRATOR + _id, null, null);
HttpGet get = new HttpGet(uri);
get.addHeader("Accept", "application/json");
response = httpClient.execute(httpHost, get, CouchDBUtils.getContext(httpHost));
InputStream content = response.getEntity().getContent();
Reader reader = new InputStreamReader(content);
JsonObject jsonObject = gson.fromJson(reader, JsonObject.class);
// Check for deleted object. if object is deleted then return null.
if (jsonObject.get(((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName()) == null)
{
return null;
}
return CouchDBObjectMapper.getEntityFromJson(entityClass, entityMetadata, jsonObject,
entityMetadata.getRelationNames(), kunderaMetadata);
}
catch (Exception e)
{
log.error("Error while finding object by key {}, Caused by {}.", key, e);
throw new KunderaException(e);