Search in sources :

Example 16 with QueryModelException

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;
            }
        }
    }
}
Also used : Serializable(java.io.Serializable) QueryModelException(org.alfresco.repo.search.impl.querymodel.QueryModelException) Method(java.lang.reflect.Method) FTSQueryException(org.alfresco.repo.search.impl.parsers.FTSQueryException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Collator(java.text.Collator) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException)

Aggregations

QueryModelException (org.alfresco.repo.search.impl.querymodel.QueryModelException)16 QName (org.alfresco.service.namespace.QName)9 ArrayList (java.util.ArrayList)4 Argument (org.alfresco.repo.search.impl.querymodel.Argument)4 PropertyArgument (org.alfresco.repo.search.impl.querymodel.PropertyArgument)4 AnalysisMode (org.alfresco.repo.search.adaptor.lucene.AnalysisMode)3 ParentSupport (org.alfresco.repo.search.impl.querymodel.impl.db.ParentSupport)3 PropertySupport (org.alfresco.repo.search.impl.querymodel.impl.db.PropertySupport)3 ResultSet (org.alfresco.service.cmr.search.ResultSet)3 Serializable (java.io.Serializable)2 HashSet (java.util.HashSet)2 Node (org.alfresco.repo.domain.node.Node)2 SolrJSONResultSet (org.alfresco.repo.search.impl.lucene.SolrJSONResultSet)2 FTSQueryException (org.alfresco.repo.search.impl.parsers.FTSQueryException)2 AspectSupport (org.alfresco.repo.search.impl.querymodel.impl.db.AspectSupport)2 TypeSupport (org.alfresco.repo.search.impl.querymodel.impl.db.TypeSupport)2 ChildAssocRefResultSet (org.alfresco.repo.search.results.ChildAssocRefResultSet)2 AspectDefinition (org.alfresco.service.cmr.dictionary.AspectDefinition)2 DataTypeDefinition (org.alfresco.service.cmr.dictionary.DataTypeDefinition)2 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)2