Permalink
Browse files

Changes made:

1) Client API refactoring for join table related method.
  • Loading branch information...
1 parent 127bc59 commit 0f744f5f09647563748e78216c6c03cd9e4efec5 Vivs committed Apr 13, 2012
@@ -638,62 +638,49 @@ private void createIndexesOnColumns(String tableName, String poolName, List<Colu
return foreignKeys;
}
- /*
- * (non-Javadoc)
- *
- * @see
- * com.impetus.kundera.client.Client#findParentEntityFromJoinTable(com.impetus
- * .kundera.metadata.model.EntityMetadata, java.lang.String,
- * java.lang.String, java.lang.String, java.lang.Object)
+
+
+ /* (non-Javadoc)
+ * @see com.impetus.kundera.client.Client#findIdsByColumn(java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Class)
*/
@Override
- public List<Object> findIdsByColumn(EntityMetadata parentMetadata, String joinTableName,
- String joinColumnName, String inverseJoinColumnName, Object childId)
+ public Object[] findIdsByColumn(String tableName, String pKeyName, String columnName, Object columnValue,Class entityClazz)
{
-
Selector selector = Pelops.createSelector(PelopsUtils.generatePoolName(getPersistenceUnit()));
SlicePredicate slicePredicate = Selector.newColumnsPredicateAll(false, 10000);
+ EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(entityClazz);
+ String childIdStr = (String) columnValue;
- String childIdStr = (String) childId;
-
- List<Object> entities = null;
IndexClause ix = Selector.newIndexClause(
Bytes.EMPTY,
10000,
- Selector.newIndexExpression(inverseJoinColumnName + "_" + childIdStr, IndexOperator.EQ,
+ Selector.newIndexExpression(columnName + "_" + childIdStr, IndexOperator.EQ,
Bytes.fromByteArray(childIdStr.getBytes())));
- Map<Bytes, List<Column>> qResults = selector.getIndexedColumns(joinTableName, ix, slicePredicate,
+ Map<Bytes, List<Column>> qResults = selector.getIndexedColumns(tableName, ix, slicePredicate,
ConsistencyLevel.ONE);
List<Object> rowKeys = new ArrayList<Object>();
- entities = new ArrayList<Object>(qResults.size());
- try
- {
// iterate through complete map and
Iterator<Bytes> rowIter = qResults.keySet().iterator();
while (rowIter.hasNext())
{
Bytes rowKey = rowIter.next();
- PropertyAccessor<?> accessor = PropertyAccessorFactory.getPropertyAccessor(parentMetadata.getIdColumn()
+ PropertyAccessor<?> accessor = PropertyAccessorFactory.getPropertyAccessor(metadata.getIdColumn()
.getField());
- Object value = accessor.fromBytes(parentMetadata.getIdColumn().getField().getClass(),
+ Object value = accessor.fromBytes(metadata.getIdColumn().getField().getClass(),
rowKey.toByteArray());
rowKeys.add(value);
}
- entities.addAll(findAll(parentMetadata.getEntityClazz(), rowKeys.toArray(new Object[0])));
- }
- catch (Exception e)
- {
- throw new KunderaException(e);
- }
-
- return entities;
-
+ if (rowKeys != null && !rowKeys.isEmpty())
+ {
+ return rowKeys.toArray(new Object[0]);
+ }
+ return null;
}
/*
@@ -22,7 +22,6 @@
import com.impetus.kundera.graph.Node;
import com.impetus.kundera.index.IndexManager;
-import com.impetus.kundera.metadata.model.EntityMetadata;
import com.impetus.kundera.persistence.EntityReader;
import com.impetus.kundera.persistence.context.jointable.JoinTableData;
@@ -133,16 +132,16 @@
<E> List<E> getColumnsById(String tableName, String pKeyColumnName, String columnName, String pKeyColumnValue);
/**
- * Returns list of primary keys based fetched by parameterized column name and
- * @param <E> type case
- * @param parentMetadata
- * @param tableName
- * @param joinColumnName
- * @param inverseJoinColumnName
- * @param childId
- * @return
+ * Returns array of primary key for given column name and it's value.
+ * @param tableName table/column family name.
+ * @param pKeyName primary key column name.
+ * @param columnName column name to be used for search.
+ * @param columnValue value for parameterised <columnName>.
+ * @param entity class entity class
+ * @return array containing fetched primary keys.
*/
- <E> List<E> findIdsByColumn(EntityMetadata parentMetadata, String tableName, String joinColumnName, String inverseJoinColumnName, Object childId);
+
+ Object[] findIdsByColumn(String tableName, String pKeyName, String columnName, Object columnValue, Class entityClazz);
/**
* Delete rows from given table for given column name and corresponding value..
@@ -790,8 +790,10 @@ private void onBiDirection(EnhanceEntity e, Client client, Field relationalField
String joinColumnName = (String) joinColumns.toArray()[0];
String inverseJoinColumnName = (String) inverseJoinColumns.toArray()[0];
- List<Object> parentEntities = client.findIdsByColumn(origMetadata,
- joinTableName, joinColumnName, inverseJoinColumnName, id);
+ Object[] pKeys = client.findIdsByColumn(joinTableName, joinColumnName, inverseJoinColumnName, id, origMetadata.getEntityClazz());
+ List<Object> parentEntities = pKeys != null? client.findAll(origMetadata.getEntityClazz(), pKeys):null;
+ /*client.findIdsByColumn(origMetadata,
+ joinTableName, joinColumnName, inverseJoinColumnName, id);*/
if (results == null)
{
@@ -364,23 +364,6 @@ public void persistJoinTable(JoinTableData joinTableData)
/*
* (non-Javadoc)
*
- * @see
- * com.impetus.kundera.client.Client#findParentEntityFromJoinTable(com.impetus
- * .kundera.metadata.model.EntityMetadata, java.lang.String,
- * java.lang.String, java.lang.String, java.lang.Object)
- */
- @Override
- public <E> List<E> findIdsByColumn(EntityMetadata parentMetadata, String joinTableName,
- String joinColumnName, String inverseJoinColumnName, Object childId)
- {
- return handler.findParentEntityFromJoinTable(parentMetadata, joinTableName, joinColumnName,
- inverseJoinColumnName, childId);
-
- }
-
- /*
- * (non-Javadoc)
- *
* @see com.impetus.kundera.client.Client#deleteByColumn(java.lang.String,
* java.lang.String, java.lang.Object)
*/
@@ -443,4 +426,13 @@ public EntityReader getReader()
return LuceneQuery.class;
}
+ /* (non-Javadoc)
+ * @see com.impetus.kundera.client.Client#findIdsByColumn(java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Class)
+ */
+ @Override
+ public Object[] findIdsByColumn(String tableName, String pKeyName, String columnName, Object columnValue, Class entityClazz)
+ {
+ throw new UnsupportedOperationException("Method not supported.");
+ }
+
}
@@ -162,48 +162,41 @@ public void persistJoinTable(JoinTableData joinTableData)
return foreignKeys;
}
+
+ /* (non-Javadoc)
+ * @see com.impetus.kundera.client.Client#findIdsByColumn(java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Class)
+ */
@Override
- public List<Object> findIdsByColumn(EntityMetadata parentMetadata, String joinTableName,
- String joinColumnName, String inverseJoinColumnName, Object childId)
+ public Object[] findIdsByColumn(String tableName, String pKeyName, String columnName, Object columnValue,Class entityClazz)
{
-
- String childIdStr = (String) childId;
+ String childIdStr = (String) columnValue;
List<Object> primaryKeys = new ArrayList<Object>();
- DBCollection dbCollection = mongoDb.getCollection(joinTableName);
+ DBCollection dbCollection = mongoDb.getCollection(tableName);
BasicDBObject query = new BasicDBObject();
- query.put(inverseJoinColumnName, childIdStr);
+ query.put(columnName, childIdStr);
DBCursor cursor = dbCollection.find(query);
DBObject fetchedDocument = null;
while (cursor.hasNext())
{
fetchedDocument = cursor.next();
- String primaryKey = (String) fetchedDocument.get(joinColumnName);
+ String primaryKey = (String) fetchedDocument.get(pKeyName);
primaryKeys.add(primaryKey);
}
- List<Object> entities = null;
- try
- {
- if (!primaryKeys.isEmpty())
- {
- entities = new ArrayList<Object>();
- entities.addAll(findAll(parentMetadata.getEntityClazz(), primaryKeys.toArray(new Object[0])));
- }
- }
- catch (Exception e)
+ if (primaryKeys != null && !primaryKeys.isEmpty())
{
- throw new KunderaException(e);
+ return primaryKeys.toArray(new Object[0]);
}
- return entities;
+ return null;
+
}
-
/*
* (non-Javadoc)
*
@@ -468,19 +468,18 @@ public void persistJoinTable(JoinTableData joinTableData)
return foreignKeys;
}
+
/* (non-Javadoc)
- * @see com.impetus.kundera.client.Client#findParentEntityFromJoinTable(com.impetus.kundera.metadata.model.EntityMetadata, java.lang.String, java.lang.String, java.lang.String, java.lang.Object)
+ * @see com.impetus.kundera.client.Client#findIdsByColumn(java.lang.String, java.lang.String, java.lang.String, java.lang.Object, java.lang.Class)
*/
@Override
- public List<Object> findIdsByColumn(EntityMetadata parentMetadata, String joinTableName,
- String joinColumnName, String inverseJoinColumnName, Object childId)
+ public Object[] findIdsByColumn(String tableName, String pKeyName, String columnName, Object columnValue,Class entityClazz)
{
-
- String childIdStr = (String) childId;
+ String childIdStr = (String) columnValue;
StringBuffer sqlQuery = new StringBuffer();
- sqlQuery.append("SELECT ").append(joinColumnName).append(" FROM ").append(joinTableName).append(" WHERE ")
- .append(inverseJoinColumnName).append("='").append(childIdStr).append("'");
+ sqlQuery.append("SELECT ").append(pKeyName).append(" FROM ").append(tableName).append(" WHERE ")
+ .append(columnName).append("='").append(childIdStr).append("'");
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
@@ -491,23 +490,13 @@ public void persistJoinTable(JoinTableData joinTableData)
primaryKeys = query.list();
+
s.close();
-
- List<Object> entities = null;
- try
+ if (primaryKeys != null && !primaryKeys.isEmpty())
{
- if (primaryKeys != null && !primaryKeys.isEmpty())
- {
- entities = new ArrayList<Object>();
- entities.addAll(findAll(parentMetadata.getEntityClazz(), primaryKeys.toArray(new Object[0])));
- }
+ return primaryKeys.toArray(new Object[0]);
}
- catch (Exception e)
- {
- e.printStackTrace();
- }
-
- return entities;
+ return null;
}

0 comments on commit 0f744f5

Please sign in to comment.