Search in sources :

Example 6 with WalkerCallbackAdapter

use of org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter in project alfresco-remote-api by Alfresco.

the class FavouritesImpl method getFavourites.

@Override
public CollectionWithPagingInfo<Favourite> getFavourites(String personId, final Parameters parameters) {
    personId = people.validatePerson(personId, true);
    Paging paging = parameters.getPaging();
    final Set<Type> filteredByClientQuery = new HashSet<Type>();
    // Default all
    Set<Type> filterTypes = FavouritesService.Type.ALL_FILTER_TYPES;
    // filterType is of the form 'target.<site|file|folder>'
    QueryHelper.walk(parameters.getQuery(), new WalkerCallbackAdapter() {

        @Override
        public void or() {
        // OR is supported but exists() will be called for each EXISTS so we don't
        // need to do anything here.  If we don't override it then it will be assumed
        // that OR in the grammar is not supported.
        }

        @Override
        public void exists(String filteredByClient, boolean negated) {
            if (filteredByClient != null) {
                int idx = filteredByClient.lastIndexOf("/");
                if (idx == -1 || idx == filteredByClient.length()) {
                    throw new InvalidArgumentException();
                } else {
                    String filtertype = filteredByClient.substring(idx + 1).toUpperCase();
                    filteredByClientQuery.add(Type.valueOf(filtertype));
                }
            }
        }
    });
    if (filteredByClientQuery.size() > 0) {
        filterTypes = filteredByClientQuery;
    }
    final PagingResults<PersonFavourite> favourites = favouritesService.getPagedFavourites(personId, filterTypes, FavouritesService.DEFAULT_SORT_PROPS, Util.getPagingRequest(paging));
    return wrap(paging, favourites, parameters);
}
Also used : Type(org.alfresco.service.cmr.favourites.FavouritesService.Type) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) PersonFavourite(org.alfresco.repo.favourites.PersonFavourite) Paging(org.alfresco.rest.framework.resource.parameters.Paging) WalkerCallbackAdapter(org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter) HashSet(java.util.HashSet)

Example 7 with WalkerCallbackAdapter

use of org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter in project alfresco-remote-api by Alfresco.

the class WhereTests method assertExistsPropertyEquals.

/**
 * Helper class for walking the query.
 */
private void assertExistsPropertyEquals(final String property, Query theQuery, final boolean isNegated) {
    assertNotNull(theQuery);
    CommonTree tree = theQuery.getTree();
    assertNotNull(tree);
    QueryHelper.walk(theQuery, new WalkerCallbackAdapter() {

        @Override
        public void exists(String propertyName, boolean negated) {
            assertTrue(property.equals(propertyName));
            assertTrue(isNegated == negated);
        }
    });
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) WalkerCallbackAdapter(org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter)

Example 8 with WalkerCallbackAdapter

use of org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter in project alfresco-remote-api by Alfresco.

the class WhereTests method existClauseTest.

@Test
public void existClauseTest() {
    Query theQuery = getWhereClause(null);
    assertNotNull(theQuery);
    assertTrue("Null passed in so nothing to theQuery.", theQuery.getTree() == null);
    try {
        theQuery = getWhereClause("fred");
        fail("Should throw an InvalidQueryException");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    try {
        theQuery = getWhereClause("(noClosingBracket");
        fail("Should throw an InvalidQueryException");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    try {
        theQuery = getWhereClause("noOpeningBracket)");
        fail("Should throw an InvalidQueryException");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    try {
        theQuery = getWhereClause("(EXISTS(target.file))");
        fail("Should throw an InvalidQueryException");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    theQuery = getWhereClause("(exists(/target/file))");
    assertExistsPropertyEquals("/target/file", theQuery, false);
    theQuery = getWhereClause("(EXISTS(b))");
    assertExistsPropertyEquals("b", theQuery, false);
    theQuery = getWhereClause("  ( EXISTS ( whitespace ) )  ");
    assertExistsPropertyEquals("whitespace", theQuery, false);
    theQuery = getWhereClause("(exists ( folder ))");
    assertExistsPropertyEquals("folder", theQuery, false);
    theQuery = getWhereClause("(NOT EXISTS(b))");
    assertExistsPropertyEquals("b", theQuery, true);
    theQuery = getWhereClause(" (NOT EXISTS(b))");
    assertExistsPropertyEquals("b", theQuery, true);
    theQuery = getWhereClause("(  NOT EXISTS(b))");
    assertExistsPropertyEquals("b", theQuery, true);
    theQuery = getWhereClause("  (  NOT EXISTS(b))");
    assertExistsPropertyEquals("b", theQuery, true);
    try {
        theQuery = getWhereClause("(exists  folder)");
        fail("Should throw an InvalidQueryException, 'folder' should have a bracket around it");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    theQuery = getWhereClause("(EXISTS(/target/folder) AND NOT EXISTS(/target/site))");
    assertNotNull(theQuery);
    CommonTree tree = theQuery.getTree();
    assertNotNull(tree);
    QueryHelper.walk(theQuery, new WalkerCallbackAdapter() {

        int i = 0;

        @Override
        public void exists(String propertyName, boolean negated) {
            if (i == 0) {
                assertTrue("/target/folder".equals(propertyName));
            } else {
                assertTrue("/target/site".equals(propertyName));
            }
            i++;
        }

        @Override
        public void and() {
        // We don't need to do anything in this method.  However, overriding the method indicates that AND is
        // supported.  OR is not supported at the same time.
        }
    });
    try {
        theQuery = getWhereClause("(EXISTS(/target/folder)OR EXISTS(/target/site))");
        fail("Should throw an InvalidQueryException, the OR should have a space before it.");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    theQuery = getWhereClause("(NOT EXISTS(/target/folder) OR EXISTS(/target/site))");
    QueryHelper.walk(theQuery, new WalkerCallbackAdapter() {

        @Override
        public void exists(String propertyName, boolean negated) {
            if (negated) {
                assertTrue("/target/folder".equals(propertyName));
            } else {
                assertTrue("/target/site".equals(propertyName));
            }
        }

        @Override
        public void or() {
        // We don't need to do anything in this method.  However, overriding the method indicates that OR is
        // supported.  AND is not supported at the same time.
        }
    });
    theQuery = getWhereClause("(EXISTS   (  /target/folder  )   OR   EXISTS(  /target/site  )  )");
    QueryHelper.walk(theQuery, new WalkerCallbackAdapter() {

        int i = 0;

        @Override
        public void exists(String propertyName, boolean negated) {
            if (i == 0) {
                assertTrue("/target/folder".equals(propertyName));
            } else {
                assertTrue("/target/site".equals(propertyName));
            }
            i++;
        }

        @Override
        public void or() {
        // We don't need to do anything in this method.  However, overriding the method indicates that OR is
        // supported.  AND is not supported at the same time.
        }
    });
    theQuery = getWhereClause("(EXISTS(target/file) AND EXISTS(target/folder) AND EXISTS(target/site))");
    QueryHelper.walk(theQuery, new WalkerCallbackAdapter() {

        int i = 0;

        @Override
        public void exists(String propertyName, boolean negated) {
            switch(i) {
                case 0:
                    assertTrue("target/file".equals(propertyName));
                    break;
                case 1:
                    assertTrue("target/folder".equals(propertyName));
                    break;
                case 2:
                    assertTrue("target/site".equals(propertyName));
                    break;
                default:
                    break;
            }
            i++;
        }

        @Override
        public void and() {
        // We don't need to do anything in this method.  However, overriding the method indicates that AND is
        // supported.  OR is not supported at the same time.
        }
    });
}
Also used : Query(org.alfresco.rest.framework.resource.parameters.where.Query) CommonTree(org.antlr.runtime.tree.CommonTree) InvalidQueryException(org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException) WalkerCallbackAdapter(org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter) Test(org.junit.Test)

Example 9 with WalkerCallbackAdapter

use of org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter in project alfresco-remote-api by Alfresco.

the class WhereTests method betweenChecks.

/**
 * Used by BetweenClauseTest, validates the clause
 * @param theQuery Query
 * @param propName String
 * @param firstValue String
 * @param secondValue String
 */
private void betweenChecks(Query theQuery, final String propName, final String firstValue, final String secondValue) {
    assertNotNull(theQuery);
    CommonTree tree = theQuery.getTree();
    assertNotNull(tree);
    assertEquals(WhereClauseParser.BETWEEN, tree.getType());
    assertEquals(WhereClauseParser.PROPERTYNAME, tree.getChild(0).getType());
    assertTrue(propName.equals(tree.getChild(0).getText()));
    assertEquals(WhereClauseParser.PROPERTYVALUE, tree.getChild(1).getType());
    assertEquals(WhereClauseParser.PROPERTYVALUE, tree.getChild(2).getType());
    QueryHelper.walk(theQuery, new WalkerCallbackAdapter() {

        @Override
        public void between(String property, String firstVal, String secondVal, boolean negated) {
            assertTrue("Property name should be " + propName, propName.equals(property));
            assertTrue("First value should be " + firstValue, firstValue.equals(firstVal));
            assertTrue("Second value should be " + secondValue, secondValue.equals(secondVal));
        }
    });
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) WalkerCallbackAdapter(org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter)

Aggregations

WalkerCallbackAdapter (org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter)9 CommonTree (org.antlr.runtime.tree.CommonTree)7 Query (org.alfresco.rest.framework.resource.parameters.where.Query)3 Test (org.junit.Test)3 InvalidQueryException (org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException)2 HashSet (java.util.HashSet)1 PersonFavourite (org.alfresco.repo.favourites.PersonFavourite)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 Paging (org.alfresco.rest.framework.resource.parameters.Paging)1 Type (org.alfresco.service.cmr.favourites.FavouritesService.Type)1