Search in sources :

Example 1 with PaginatedList

use of org.summerb.easycrud.api.dto.PaginatedList in project summerb by skarpushin.

the class EasyCrudDaoMySqlImpl method query.

@Override
public PaginatedList<TDto> query(PagerParams pagerParams, Query optionalQuery, OrderBy... orderBy) {
    MapSqlParameterSource params = new MapSqlParameterSource();
    String whereClause = optionalQuery == null ? "" : "WHERE " + queryToNativeSqlCompiler.buildWhereClauseAndPopulateParams(optionalQuery, params);
    params.addValue("offset", pagerParams.getOffset());
    params.addValue("max", pagerParams.getMax());
    String query = sqlFindByCustomQuery + whereClause + buildOrderBySubclause(orderBy);
    if (!PagerParams.ALL.equals(pagerParams)) {
        query = query + sqlPartPaginator;
    }
    List<TDto> list = jdbc.query(query, params, rowMapper);
    int totalResultsCount;
    if (Top.is(pagerParams) || (PagerParams.ALL.equals(pagerParams) || (pagerParams.getOffset() == 0 && list.size() < pagerParams.getMax()))) {
        totalResultsCount = list.size();
    } else {
        // TODO: For MySQL we can use combination of SQL_CALC_FOUND_ROWS and
        // FOUND_ROWS() to improve performance -- ubt this is MySQL specific
        // functionality
        totalResultsCount = jdbc.queryForInt(sqlFindByCustomQueryForCount + whereClause, params);
    }
    return new PaginatedList<TDto>(pagerParams, list, totalResultsCount);
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList)

Example 2 with PaginatedList

use of org.summerb.easycrud.api.dto.PaginatedList in project summerb by skarpushin.

the class DataSetLoaderImplTest method testLoadObjectsByIds_ExpectManyLoadByStrings.

@Test
public void testLoadObjectsByIds_ExpectManyLoadByStrings() throws Exception {
    DataSetLoaderImpl fixture = buildMockedInstance();
    EasyCrudService service = Mockito.mock(EasyCrudService.class);
    when(fixture.getEasyCrudServiceResolver().resolveByEntityType("dto1")).thenReturn(service);
    Matcher<Query> matcher = IsEqual.equalTo(Query.n().in(HasId.FN_ID, new String[] { "s1", "s2" }));
    PaginatedList mockret = new PaginatedList<>(new PagerParams(), Arrays.asList(new TestDto1(), new TestDto1()), 2);
    when(service.query(any(PagerParams.class), argThat(matcher))).thenReturn(mockret);
    List<HasId> ret = fixture.loadObjectsByIds(ids("s1", "s2"), "dto1");
    assertNotNull(ret);
    assertEquals(2, ret.size());
}
Also used : HasId(org.summerb.easycrud.api.dto.HasId) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) Query(org.summerb.easycrud.api.query.Query) PagerParams(org.summerb.easycrud.api.dto.PagerParams) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) TestDto1(integr.org.summerb.easycrud.TestDto1) Test(org.junit.Test)

Example 3 with PaginatedList

use of org.summerb.easycrud.api.dto.PaginatedList in project summerb by skarpushin.

the class DataSetLoaderImplTest method testLoadObjectsByIds_ExpectManyLoadByLongsOk.

@Test
public void testLoadObjectsByIds_ExpectManyLoadByLongsOk() throws Exception {
    DataSetLoaderImpl fixture = buildMockedInstance();
    EasyCrudService service = Mockito.mock(EasyCrudService.class);
    when(fixture.getEasyCrudServiceResolver().resolveByEntityType("dto1")).thenReturn(service);
    Matcher<Query> matcher = IsEqual.equalTo(Query.n().in(HasId.FN_ID, new Long[] { 1L, 2L }));
    PaginatedList mockret = new PaginatedList<>(new PagerParams(), Arrays.asList(new TestDto1(), new TestDto1()), 2);
    when(service.query(any(PagerParams.class), argThat(matcher))).thenReturn(mockret);
    List<HasId> ret = fixture.loadObjectsByIds(ids(1L, 2L), "dto1");
    assertNotNull(ret);
    assertEquals(2, ret.size());
}
Also used : HasId(org.summerb.easycrud.api.dto.HasId) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) Query(org.summerb.easycrud.api.query.Query) PagerParams(org.summerb.easycrud.api.dto.PagerParams) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) TestDto1(integr.org.summerb.easycrud.TestDto1) Test(org.junit.Test)

Example 4 with PaginatedList

use of org.summerb.easycrud.api.dto.PaginatedList in project summerb by skarpushin.

the class DataSetLoaderImplTest method testLoadObjectsByIds_ExpectManyLoadByLongsNfe.

@Test(expected = GenericEntityNotFoundException.class)
public void testLoadObjectsByIds_ExpectManyLoadByLongsNfe() throws Exception {
    DataSetLoaderImpl fixture = buildMockedInstance();
    EasyCrudService service = Mockito.mock(EasyCrudService.class);
    when(fixture.getEasyCrudServiceResolver().resolveByEntityType("dto1")).thenReturn(service);
    PaginatedList mockret = new PaginatedList<>(new PagerParams(), Collections.emptyList(), 0);
    when(service.query(any(PagerParams.class), any(Query.class))).thenReturn(mockret);
    fixture.loadObjectsByIds(ids(1L, 2L), "dto1");
}
Also used : EasyCrudService(org.summerb.easycrud.api.EasyCrudService) Query(org.summerb.easycrud.api.query.Query) PagerParams(org.summerb.easycrud.api.dto.PagerParams) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) Test(org.junit.Test)

Example 5 with PaginatedList

use of org.summerb.easycrud.api.dto.PaginatedList in project summerb by skarpushin.

the class StringIdAliasServiceEagerImplFactory method createDaoMock.

private static StringIdAliasDao createDaoMock() {
    StringIdAliasDao ret = Mockito.mock(StringIdAliasDao.class);
    when(ret.createAliasFor(NAME)).thenReturn(NAME_ALIAS);
    when(ret.findAliasFor(NAME)).thenReturn(NAME_ALIAS);
    when(ret.loadAllAliases(any(PagerParams.class))).thenAnswer(new Answer<PaginatedList<Entry<String, Long>>>() {

        @Override
        public PaginatedList<Entry<String, Long>> answer(InvocationOnMock invocation) throws Throwable {
            PagerParams pagerParams = (PagerParams) invocation.getArguments()[0];
            // Synthetic pause, simulate
            Thread.sleep(250);
            PaginatedList<Entry<String, Long>> result = new PaginatedList<Entry<String, Long>>();
            result.setPagerParams(pagerParams);
            result.setTotalResults(150);
            ArrayList<Entry<String, Long>> items = new ArrayList<Entry<String, Long>>();
            result.setItems(items);
            long offset = pagerParams.getOffset();
            long max = -1;
            if (offset == 0) {
                max = 100;
            } else if (offset == 100) {
                max = 50;
            } else {
                fail();
            }
            for (long i = offset; i < max; i++) {
                items.add(new AliasEntry("str" + i, i));
            }
            return result;
        }
    });
    return ret;
}
Also used : PagerParams(org.summerb.easycrud.api.dto.PagerParams) ArrayList(java.util.ArrayList) StringIdAliasDao(org.summerb.properties.impl.dao.StringIdAliasDao) AliasEntry(org.summerb.properties.impl.dao.AliasEntry) Entry(java.util.Map.Entry) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) AliasEntry(org.summerb.properties.impl.dao.AliasEntry)

Aggregations

PaginatedList (org.summerb.easycrud.api.dto.PaginatedList)7 PagerParams (org.summerb.easycrud.api.dto.PagerParams)6 Test (org.junit.Test)4 EasyCrudService (org.summerb.easycrud.api.EasyCrudService)3 Query (org.summerb.easycrud.api.query.Query)3 TestDto1 (integr.org.summerb.easycrud.TestDto1)2 HasId (org.summerb.easycrud.api.dto.HasId)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Entry (java.util.Map.Entry)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1 AliasEntry (org.summerb.properties.impl.dao.AliasEntry)1 StringIdAliasDao (org.summerb.properties.impl.dao.StringIdAliasDao)1 AuthToken (org.summerb.users.api.dto.AuthToken)1 User (org.summerb.users.api.dto.User)1 AuthTokenDao (org.summerb.users.impl.dao.AuthTokenDao)1