Search in sources :

Example 1 with SortDef

use of org.alfresco.rest.api.search.model.SortDef in project alfresco-remote-api by Alfresco.

the class SearchMapper method fromSort.

/**
 * SearchParameters from List<SortDef>
 * @param sp SearchParameters
 * @param sort List<SortDef>
 */
public void fromSort(SearchParameters sp, List<SortDef> sort) {
    if (sort != null && !sort.isEmpty()) {
        if (LANGUAGE_CMIS_ALFRESCO.equals(sp.getLanguage())) {
            throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID, new Object[] { ": sort {} not allowed with cmis language" });
        }
        for (SortDef sortDef : sort) {
            try {
                SortType sortType = SortType.valueOf(sortDef.getType());
                String field = sortDef.getField();
                sp.addSort(new SortDefinition(sortType, field, sortDef.isAscending()));
            } catch (IllegalArgumentException e) {
                throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID, new Object[] { sortDef.getType() });
            }
        }
    }
}
Also used : SortType(org.alfresco.service.cmr.search.SearchParameters.SortDefinition.SortType) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) SortDef(org.alfresco.rest.api.search.model.SortDef) SortDefinition(org.alfresco.service.cmr.search.SearchParameters.SortDefinition)

Example 2 with SortDef

use of org.alfresco.rest.api.search.model.SortDef in project alfresco-remote-api by Alfresco.

the class SearchMapperTests method fromSort.

@Test
public void fromSort() throws Exception {
    SearchParameters searchParameters = new SearchParameters();
    // Doesn't error
    searchMapper.fromSort(searchParameters, null);
    try {
        searchMapper.fromSort(searchParameters, Arrays.asList(new SortDef("wrongenum", null, false)));
        fail();
    } catch (InvalidArgumentException iae) {
        // wrongenum is illegal
        assertNotNull(iae);
    }
    searchMapper.fromSort(searchParameters, Arrays.asList(new SortDef("FIELD", "my", true), new SortDef("SCORE", null, false), new SortDef("DOCUMENT", null, true)));
    assertEquals(3, searchParameters.getSortDefinitions().size());
    searchParameters.getSortDefinitions().forEach(sortDefinition -> {
        switch(sortDefinition.getSortType()) {
            case FIELD:
                assertEquals("my", sortDefinition.getField());
                assertEquals(true, sortDefinition.isAscending());
                break;
            case SCORE:
                assertNull(sortDefinition.getField());
                assertEquals(false, sortDefinition.isAscending());
                break;
            case DOCUMENT:
                assertNull(sortDefinition.getField());
                assertEquals(true, sortDefinition.isAscending());
                break;
            default:
                fail("Invalid sortDefinition");
        }
    });
    searchParameters = new SearchParameters();
    searchParameters.setLanguage(SearchService.LANGUAGE_CMIS_ALFRESCO);
    try {
        searchMapper.fromSort(searchParameters, Arrays.asList(new SortDef("FIELD", null, false)));
        fail();
    } catch (InvalidArgumentException iae) {
        // You can't specify SORT when using the CMIS language
        assertNotNull(iae);
    }
}
Also used : SearchParameters(org.alfresco.service.cmr.search.SearchParameters) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) SortDef(org.alfresco.rest.api.search.model.SortDef) Test(org.junit.Test)

Aggregations

SortDef (org.alfresco.rest.api.search.model.SortDef)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)1 SortDefinition (org.alfresco.service.cmr.search.SearchParameters.SortDefinition)1 SortType (org.alfresco.service.cmr.search.SearchParameters.SortDefinition.SortType)1 Test (org.junit.Test)1