use of org.sagebionetworks.repo.model.query.FieldType in project Synapse-Repository-Services by Sage-Bionetworks.
the class JDONodeQueryDAOImplTest method populateNodesForTest.
private void populateNodesForTest() throws Exception {
Iterator<String> it = fieldTypeMap.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
FieldType type = fieldTypeMap.get(key);
}
// Create a few datasets
nodeIds = new ArrayList<String>();
for (int i = 0; i < totalNumberOfDatasets; i++) {
Node parent = NodeTestUtils.createNew("dsName" + i, createdBy);
Date now = new Date(System.currentTimeMillis());
parent.setDescription("description" + i);
parent.setCreatedByPrincipalId(createdBy);
parent.setNodeType(EntityType.dataset.name());
// Create this dataset
String parentId = nodeDao.createNew(parent);
idToNameMap.put(parentId, parent.getName());
nodeIds.add(parentId);
NamedAnnotations named = nodeDao.getAnnotations(parentId);
Annotations parentAnnos = named.getAdditionalAnnotations();
parentAnnos.addAnnotation(attOnall, "someNumber" + i);
// Add some attributes to others.
if ((i % 2) == 0) {
parentAnnos.addAnnotation(attOnEven, new Long(i));
} else {
parentAnnos.addAnnotation(attOnOdd, now);
}
// Make sure we add one of each type
parentAnnos.addAnnotation(attString, "someString" + i);
parentAnnos.addAnnotation(attDate, new Date(System.currentTimeMillis() + i));
parentAnnos.addAnnotation(attLong, new Long(123456));
parentAnnos.addAnnotation(attDouble, new Double(123456.3));
nodeDao.updateAnnotations(parentId, named);
// Add a child to the parent
Node child = createChild(now, i, createdBy);
child.setParentId(parentId);
// Add a layer attribute
String childId = nodeDao.createNew(child);
idToNameMap.put(childId, child.getName());
NamedAnnotations childNamed = nodeDao.getAnnotations(childId);
Annotations childAnnos = childNamed.getPrimaryAnnotations();
childAnnos.addAnnotation("layerAnnotation", "layerAnnotValue" + i);
if ((i % 2) == 0) {
childAnnos.addAnnotation(attLayerType, LayerTypeNames.C.name());
} else if ((i % 3) == 0) {
childAnnos.addAnnotation(attLayerType, LayerTypeNames.E.name());
} else {
childAnnos.addAnnotation(attLayerType, LayerTypeNames.G.name());
}
// Update the child annoations.
nodeDao.updateAnnotations(childId, childNamed);
// Thread.sleep(1000);
}
}
use of org.sagebionetworks.repo.model.query.FieldType in project Synapse-Repository-Services by Sage-Bionetworks.
the class JDONodeQueryDaoImpl method buildWhere.
/**
* Build up the from
*
* @param builder
* @param from
* @throws AttributeDoesNotExist
* @throws DatastoreException
*/
protected void buildWhere(StringBuilder whereBuilder, Map<String, FieldType> aliasMap, Map parameters, BasicQuery in) throws AttributeDoesNotExist, DatastoreException {
// We need a where clause if there is more than one table in this query or if there are any filters.
int conditionCount = 0;
// Add the join from node to node to node revision
whereBuilder.append("where");
whereBuilder.append(" ");
whereBuilder.append(SqlConstants.NODE_ALIAS);
whereBuilder.append(".");
whereBuilder.append(SqlConstants.COL_NODE_ID);
whereBuilder.append(" = ");
whereBuilder.append(SqlConstants.REVISION_ALIAS);
whereBuilder.append(".");
whereBuilder.append(SqlConstants.COL_REVISION_OWNER_NODE);
conditionCount++;
whereBuilder.append(" and ");
whereBuilder.append(SqlConstants.NODE_ALIAS);
whereBuilder.append(".");
whereBuilder.append(SqlConstants.COL_CURRENT_REV);
whereBuilder.append(" = ");
whereBuilder.append(SqlConstants.REVISION_ALIAS);
whereBuilder.append(".");
whereBuilder.append(SqlConstants.COL_REVISION_NUMBER);
conditionCount++;
// First add all of the join conditions to the where.
if (aliasMap.size() > 1) {
Iterator<String> keyIt = aliasMap.keySet().iterator();
while (keyIt.hasNext()) {
String alias = keyIt.next();
FieldType type = aliasMap.get(alias);
if (FieldType.PRIMARY_FIELD == type)
continue;
prepareWhereBuilder(whereBuilder, conditionCount);
// Join this alias to the node table.
whereBuilder.append(" ");
whereBuilder.append(SqlConstants.NODE_ALIAS);
whereBuilder.append(".");
whereBuilder.append(SqlConstants.COL_NODE_ID);
whereBuilder.append(" = ");
whereBuilder.append(alias);
whereBuilder.append(".");
whereBuilder.append(SqlConstants.ANNOTATION_OWNER_ID_COLUMN);
conditionCount++;
}
}
// We need a condition for sorting when we are not sorting on the primary field
if (in.getSort() != null) {
FieldType sortType = getFieldType(in.getSort());
if (FieldType.PRIMARY_FIELD != sortType) {
prepareWhereBuilder(whereBuilder, conditionCount);
whereBuilder.append(" ");
whereBuilder.append(SqlConstants.SORT_ALIAS);
whereBuilder.append(".");
whereBuilder.append(SqlConstants.ANNOTATION_ATTRIBUTE_COLUMN);
whereBuilder.append(" = :sortAttName ");
parameters.put("sortAttName", in.getSort());
conditionCount++;
}
}
// Add each filter
if (in.getFilters() != null) {
for (int i = 0; i < in.getFilters().size(); i++) {
Expression exp = in.getFilters().get(i);
// First look up the column name
CompoundId id = exp.getId();
if (id == null)
throw new IllegalArgumentException("Compound id cannot be null");
FieldType type = getFieldType(id.getFieldName());
// Add where or and
prepareWhereBuilder(whereBuilder, conditionCount);
// Throw an exception if the field does not exist
if (FieldType.DOES_NOT_EXIST == type)
throw new AttributeDoesNotExist("No attribute found for: " + id.getFieldName());
if (FieldType.PRIMARY_FIELD == type) {
// This is a simple primary field filter
NodeField nodeField = NodeField.getFieldForName(exp.getId().getFieldName());
whereBuilder.append(" ");
whereBuilder.append(nodeField.getTableAlias());
whereBuilder.append(".");
whereBuilder.append(nodeField.getColumnName());
whereBuilder.append(" ");
if (exp.getValue() == null) {
whereBuilder.append(" IS NULL ");
} else {
whereBuilder.append(SqlConstants.getSqlForComparator(exp.getCompare()));
if (Comparator.IN == exp.getCompare()) {
whereBuilder.append("(");
}
whereBuilder.append(" :");
// Add a bind variable
String bindKey = "expKey" + i;
whereBuilder.append(bindKey);
// Bind the value to the parameters
if ((NodeField.PARENT_ID == nodeField) || (NodeField.ID == nodeField) || (NodeField.BENEFACTOR_ID == nodeField)) {
parameters.put(bindKey, KeyFactory.stringToKey(exp.getValue().toString()));
} else {
parameters.put(bindKey, exp.getValue());
}
if (Comparator.IN == exp.getCompare()) {
whereBuilder.append(")");
}
}
} else {
// This is not a primary field
String attTableName = QueryUtils.getTableNameForFieldType(type);
whereBuilder.append(" ");
String alais = SqlConstants.EXPRESSION_ALIAS_PREFIX + i;
whereBuilder.append(alais);
whereBuilder.append(".");
whereBuilder.append(SqlConstants.ANNOTATION_ATTRIBUTE_COLUMN);
whereBuilder.append(" = :");
String attNameKey = "attName" + i;
whereBuilder.append(attNameKey);
// Bind the key
parameters.put(attNameKey, exp.getId().getFieldName());
whereBuilder.append(" and ");
whereBuilder.append(alais);
whereBuilder.append(".");
whereBuilder.append(SqlConstants.ANNOTATION_VALUE_COLUMN);
whereBuilder.append(" ");
whereBuilder.append(SqlConstants.getSqlForComparator(exp.getCompare()));
whereBuilder.append(" :");
String valueKey = "valeKey" + i;
whereBuilder.append(valueKey);
// Bind the value
parameters.put(valueKey, exp.getValue());
}
conditionCount++;
}
}
}
use of org.sagebionetworks.repo.model.query.FieldType in project Synapse-Repository-Services by Sage-Bionetworks.
the class JDONodeQueryDaoImpl method buildAllSorting.
/**
* Build all parts involved in sorting
*
* @param outerJoinAttributeSort
* @param orderByClause
* @param sort
* @param ascending
* @param parameters
* @throws DatastoreException
*/
private void buildAllSorting(StringBuilder orderByClause, String sort, boolean ascending) throws DatastoreException, AttributeDoesNotExist {
// The first thing we need to do is determine if we are sorting on a
// primary field or an attribute.
String ascString = null;
if (ascending) {
ascString = "asc";
} else {
ascString = "desc";
}
String sortColumnName = null;
FieldType type = getFieldType(sort);
String alias = null;
if (FieldType.DOES_NOT_EXIST == type)
throw new AttributeDoesNotExist("No attribute found for: " + sort);
if (FieldType.PRIMARY_FIELD == type) {
NodeField nodeField = NodeField.getFieldForName(sort);
sortColumnName = nodeField.getColumnName();
alias = nodeField.getTableAlias();
} else {
// We are sorting on an attribute which means we need a left outer
// join.
sortColumnName = SqlConstants.ANNOTATION_VALUE_COLUMN;
alias = SqlConstants.SORT_ALIAS;
}
// Add the order by
orderByClause.append(" order by ");
orderByClause.append(alias);
orderByClause.append(".");
orderByClause.append(sortColumnName);
orderByClause.append(" ");
orderByClause.append(ascString);
}
use of org.sagebionetworks.repo.model.query.FieldType in project Synapse-Repository-Services by Sage-Bionetworks.
the class FieldTypeCacheTest method testAllExampleEntity.
@Test
public void testAllExampleEntity() throws Exception {
// This is the expected mapping:
Map<String, FieldType> expected = new HashMap<String, FieldType>();
expected.put("concept", FieldType.STRING_ATTRIBUTE);
expected.put("versionLabel", FieldType.PRIMARY_FIELD);
expected.put("etag", FieldType.STRING_ATTRIBUTE);
expected.put("singleDouble", FieldType.DOUBLE_ATTRIBUTE);
expected.put("modifiedBy", FieldType.PRIMARY_FIELD);
expected.put("singleString", FieldType.STRING_ATTRIBUTE);
expected.put("stringList", FieldType.STRING_ATTRIBUTE);
ObjectSchema exampleSchema = EntityFactory.createEntityFromJSONString(ExampleEntity.EFFECTIVE_SCHEMA, ObjectSchema.class);
Iterator<String> it = exampleSchema.getProperties().keySet().iterator();
while (it.hasNext()) {
String key = it.next();
FieldType type = FieldTypeCache.getInstance().getTypeForName(key);
assertNotNull(type);
FieldType expectedType = expected.get(key);
if (expectedType != null) {
assertEquals(expectedType, type);
}
System.out.println(key + ": " + type.name());
}
}
use of org.sagebionetworks.repo.model.query.FieldType in project Synapse-Repository-Services by Sage-Bionetworks.
the class FieldTypeCache method addEntityTypeNamesToCache.
/**
* Walk over all of the entity types and map the field type to column types.
*
* @param localCache
* @throws JSONObjectAdapterException
*/
protected static void addEntityTypeNamesToCache(AutoGenFactory factory, Map<String, FieldType> localCache) throws JSONObjectAdapterException {
Iterator<String> it = factory.getKeySetIterator();
while (it.hasNext()) {
JSONEntity jsonEntity = factory.newInstance(it.next());
if (jsonEntity instanceof Entity) {
// Load the schema
ObjectSchema schema = EntityFactory.createEntityFromJSONString(jsonEntity.getJSONSchema(), ObjectSchema.class);
// Iterate over the properties.
Map<String, ObjectSchema> propMap = schema.getProperties();
for (String key : schema.getProperties().keySet()) {
ObjectSchema propSchema = propMap.get(key);
// Lookup the field type for the property type.
FieldType fieldType = getFieldTypeForProperty(key, schema.getId(), propSchema);
validateAndAddToCache(key, fieldType, localCache);
}
}
}
}
Aggregations