SettingsRepository implements MongoRepository<Setting, String>, Queryable<Setting> { @Override public QueryResult<Setting> query(final net.explorviz.shared.querying.Query<Setting> query) { final String originField = "origin"; final String typeField = "type"; String originFilter = null; List<Class<? extends Setting>> classes = new ArrayList<>(Setting.TYPES); if (query.getFilters().get(typeField) != null) { if (query.getFilters().get(typeField).size() == 1) { final String typeFilter = query.getFilters().get(typeField).get(0); classes = classes.stream() .filter(c -> c.getSimpleName().toLowerCase().contentEquals(typeFilter)) .collect(Collectors.toList()); } else { return new QueryResult<>(query, new ArrayList<Setting>(), 0); } } if (query.getFilters().get(originField) != null) { if (query.getFilters().get(originField).size() == 1) { originFilter = query.getFilters().get(originField).get(0); } else { return new QueryResult<>(query, new ArrayList<Setting>(), 0); } } List<Setting> data = new ArrayList<>(); for (final Class<?> cls : classes) { final xyz.morphia.query.Query<Setting> q = (Query<Setting>) this.datastore.createQuery(cls); if (originFilter != null) { q.field(originField).contains(originFilter); } data.addAll(q.asList()); } final int total = data.size(); if (query.doPaginate()) { final int from = Math.min(query.getPageNumber() * query.getPageSize(), data.size()); final int to = Math.min(query.getPageNumber() * query.getPageSize() + query.getPageSize(), data.size()); data = data.subList(from, to); } return new QueryResult<>(query, data, total); } @Inject  SettingsRepository(final Datastore datastore, final IdGenerator idgen); @Override List<Setting> findAll(); @Override Optional<Setting> find(final String id); @Override void delete(final String id); @Override Setting createOrUpdate(final Setting setting); void createOrOverride(final Setting setting); @Override QueryResult<Setting> query(final net.explorviz.shared.querying.Query<Setting> query);  }
@Test public void filterByType() { final MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>(); params.putSingle("filter[type]", "flagsetting"); final Query<FlagSetting> q = mock(Query.class); when(this.ds.createQuery(FlagSetting.class)).thenReturn(q); when(q.asList()).thenReturn(this.flagSettings); final QueryResult<Setting> res = this.sps.query(net.explorviz.shared.querying.Query.fromParameterMap(params)); assertEquals(this.flagSettings, res.getData()); }
@Test public void testPagination() { this.flagSettings.add(new FlagSetting("Name", "Desc", "Some", false)); final MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>(); params.putSingle("page[number]", "1"); params.putSingle("page[size]", "1"); final net.explorviz.shared.querying.Query<Setting> q = net.explorviz.shared.querying.Query.fromParameterMap(params); final Query<FlagSetting> qFlag = mock(Query.class); final Query<RangeSetting> qRange = mock(Query.class); when(this.ds.createQuery(FlagSetting.class)).thenReturn(qFlag); when(this.ds.createQuery(RangeSetting.class)).thenReturn(qRange); when(qFlag.asList()).thenReturn(this.flagSettings); when(qRange.asList()).thenReturn(this.rangeSettings); final QueryResult<Setting> res = this.sps.query(q); assertEquals(1, res.getData().size()); assertEquals(0, res.getPreviousPage()); assertEquals(2, res.getNextPage()); }