use of org.alfresco.repo.search.impl.querymodel.QueryModelException in project alfresco-repository by Alfresco.
the class OpenCmisQueryTest method testOrderBy.
private <T> void testOrderBy(String query, int size, boolean shouldThrow, Order order, CMISQueryMode mode, String... orderByPropertyName) {
CMISResultSet rs = null;
try {
CMISQueryOptions options = new CMISQueryOptions(query, rootNodeRef.getStoreRef());
options.setQueryMode(mode);
rs = cmisQueryService.query(options);
Comparable<?>[] previous = null;
boolean[] wasNull = null;
boolean[] hasValue = null;
for (CMISResultSetRow row : rs) {
if (previous == null) {
previous = new Comparable[orderByPropertyName.length];
wasNull = new boolean[orderByPropertyName.length];
hasValue = new boolean[orderByPropertyName.length];
for (int i = 0; i < orderByPropertyName.length; i++) {
Serializable sValue = row.getValue(orderByPropertyName[i]);
if (sValue instanceof Comparable<?>) {
Comparable<?> comparable = (Comparable<?>) sValue;
previous[i] = comparable;
hasValue[i] = true;
} else {
previous[i] = null;
wasNull[i] = true;
}
}
} else // if (row.getIndex() == 0)
// {
// Serializable sValue = row.getValue(returnPropertyName);
// returnValue = (T) DefaultTypeConverter.INSTANCE.convert(returnType.getClass(), sValue);
//
// }
{
for (int i = 0; i < orderByPropertyName.length; i++) {
Serializable current = row.getValue(orderByPropertyName[i]);
Comparable<?> last = previous[i];
if (last != null) {
if (current == null) {
switch(order) {
case ASCENDING:
if (shouldThrow) {
throw new IllegalStateException("Incorrect Order");
} else {
fail("Null found after value ascending");
}
case DESCENDING:
// OK
break;
default:
throw new UnsupportedOperationException();
}
}
int comparison = 0;
if (last instanceof String) {
Collator myCollator = Collator.getInstance();
if (last == null) {
if (current == null) {
comparison = 0;
} else {
comparison = -1;
}
} else {
if (current == null) {
comparison = 1;
} else {
comparison = myCollator.compare(last, current);
}
}
} else {
Method ct = null;
Method[] methods = last.getClass().getMethods();
for (int m = 0; m < methods.length; m++) {
if (methods[m].getName().equals("compareTo") && (methods[m].getParameterTypes().length == 1) && (methods[m].getParameterTypes()[0].equals(methods[m].getDeclaringClass()))) {
if (ct != null) {
throw new IllegalStateException("Found 2 or more compareTo methods");
}
ct = methods[m];
}
}
comparison = (Integer) ct.invoke(last, current);
}
switch(order) {
case ASCENDING:
if (comparison <= 0) {
// as expected
break;
} else {
if (shouldThrow) {
throw new IllegalStateException("Incorrect Order");
} else {
fail("Incorrect Order");
}
}
case DESCENDING:
if (comparison >= 0) {
// as expected
break;
} else {
if (shouldThrow) {
throw new IllegalStateException("Incorrect Order");
} else {
fail("Incorrect Order");
}
}
default:
throw new UnsupportedOperationException("Column data type is not comparable " + orderByPropertyName[i]);
}
} else {
if (current != null) {
switch(order) {
case ASCENDING:
// OK
break;
case DESCENDING:
if (shouldThrow) {
throw new IllegalStateException("Incorrect Order");
} else {
fail("Null found descending");
}
default:
throw new UnsupportedOperationException();
}
}
}
}
for (int i = 0; i < orderByPropertyName.length; i++) {
Serializable sValue = row.getValue(orderByPropertyName[i]);
if (sValue instanceof Comparable<?>) {
Comparable<?> comparable = (Comparable<?>) sValue;
previous[i] = comparable;
hasValue[i] = true;
} else {
previous[i] = null;
wasNull[i] = true;
}
}
}
}
for (int i = 0; i < hasValue.length; i++) {
if (!hasValue[i]) {
throw new UnsupportedOperationException("Only nulls found for " + orderByPropertyName[i]);
}
}
if (size >= 0) {
assertEquals(size, rs.getLength());
}
if (shouldThrow) {
fail("Should have thrown an exception");
}
} catch (CmisBaseException e) {
if (shouldThrow) {
return;
} else {
throw e;
}
} catch (QueryModelException e) {
if (shouldThrow) {
return;
} else {
throw e;
}
} catch (FTSQueryException e) {
if (shouldThrow) {
return;
} else {
throw e;
}
} catch (UnsupportedOperationException e) {
if (shouldThrow) {
return;
} else {
throw e;
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
if (shouldThrow) {
return;
} else {
throw e;
}
} finally {
if (rs != null) {
try {
rs.close();
} finally {
rs = null;
}
}
}
}
Aggregations