fix filter (use different crud methods instead of in-memory filtering, which failed to support count)

This commit is contained in:
F43nd1r 2017-12-19 07:02:59 +01:00
parent 7ab07f6ca3
commit c34d93f58d
7 changed files with 38 additions and 27 deletions

View file

@ -1,7 +1,11 @@
package com.faendir.acra.sql.data; package com.faendir.acra.sql.data;
import com.faendir.acra.sql.model.App; import com.faendir.acra.sql.model.App;
import com.faendir.acra.sql.model.Permission;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import java.util.Optional; import java.util.Optional;
@ -11,9 +15,20 @@ import java.util.Optional;
* @since 11.12.2017 * @since 11.12.2017
*/ */
public interface AppRepository extends JpaRepository<App, Integer> { public interface AppRepository extends JpaRepository<App, Integer> {
Optional<App> findByReporterUsername(@NonNull String username); Optional<App> findByReporterUsername(@NonNull String username);
@Query("select app from App app where app not in (select permission.app from User user join user.permissions permission where user.username = ?1 and permission.level < ?2)")
Slice<App> findAllByPermissionWithDefaultIncluded(@NonNull String username, @NonNull Permission.Level level, @NonNull Pageable pageable);
@Query("select app from App app where app in (select permission.app from User user join user.permissions permission where user.username = ?1 and permission.level >= ?2)")
Slice<App> findAllByPermissionWithDefaultExcluded(@NonNull String username, @NonNull Permission.Level level, @NonNull Pageable pageable);
@Query("select count(app) from App app where app not in (select permission.app from User user join user.permissions permission where user.username = ?1 and permission.level < ?2)")
int countByPermissionWithDefaultIncluded(@NonNull String username, @NonNull Permission.Level level);
@Query("select count(app) from App app where app in (select permission.app from User user join user.permissions permission where user.username = ?1 and permission.level >= ?2)")
int countByPermissionWithDefaultExcluded(@NonNull String username, @NonNull Permission.Level level);
@SuppressWarnings("SpringDataMethodInconsistencyInspection") @SuppressWarnings("SpringDataMethodInconsistencyInspection")
default Optional<App> findByEncodedId(String encodedId) { default Optional<App> findByEncodedId(String encodedId) {
try { try {

View file

@ -15,8 +15,12 @@ import java.util.Optional;
*/ */
public interface BugRepository extends JpaRepository<Bug, Integer> { public interface BugRepository extends JpaRepository<Bug, Integer> {
Slice<Bug> findAllByApp(@NonNull App app, @NonNull Pageable pageable); Slice<Bug> findAllByApp(@NonNull App app, @NonNull Pageable pageable);
Slice<Bug> findAllByAppAndSolvedFalse(@NonNull App app, @NonNull Pageable pageable); Slice<Bug> findAllByAppAndSolvedFalse(@NonNull App app, @NonNull Pageable pageable);
int countAllByApp(@NonNull App app); int countAllByApp(@NonNull App app);
int countAllByAppAndSolvedFalse(@NonNull App app); int countAllByAppAndSolvedFalse(@NonNull App app);
Optional<Bug> findBugByAppAndStacktrace(@NonNull App app, @NonNull String stacktrace); Optional<Bug> findBugByAppAndStacktrace(@NonNull App app, @NonNull String stacktrace);
} }

View file

@ -26,18 +26,16 @@ public interface ReportRepository extends JpaRepository<Report, String> {
int countAllByBugApp(@NonNull App app); int countAllByBugApp(@NonNull App app);
@SuppressWarnings("SpringDataRepositoryMethodReturnTypeInspection") @SuppressWarnings("SpringDataRepositoryMethodReturnTypeInspection")
@Query("select new com.faendir.acra.sql.util.DayCount(report.date, count(report)) from Report report join Bug bug on report.bug = bug " + @Query("select new com.faendir.acra.sql.util.DayCount(report.date, count(report)) from Report report join Bug bug on report.bug = bug "
"where bug.app = ?1 and report.date > ?2 group by function('year',report.date), function('month',report.date), function('day',report.date)") + "where bug.app = ?1 and report.date > ?2 group by function('year',report.date), function('month',report.date), function('day',report.date)")
List<DayCount> countAllByDayAfter(@NonNull App app, @NonNull Date date); List<DayCount> countAllByDayAfter(@NonNull App app, @NonNull Date date);
List<Report> findAllByBug(@NonNull Bug bug);
Slice<Report> findAllByBug(@NonNull Bug bug, @NonNull Pageable pageable); Slice<Report> findAllByBug(@NonNull Bug bug, @NonNull Pageable pageable);
int countAllByBug(@NonNull Bug bug); int countAllByBug(@NonNull Bug bug);
@SuppressWarnings("SpringDataRepositoryMethodReturnTypeInspection") @SuppressWarnings("SpringDataRepositoryMethodReturnTypeInspection")
@Query("select new com.faendir.acra.sql.util.AndroidVersionCount(report.androidVersion, count(report)) from Report report join Bug bug on report.bug = bug " + @Query("select new com.faendir.acra.sql.util.AndroidVersionCount(report.androidVersion, count(report)) from Report report join Bug bug on report.bug = bug "
"where bug.app = ?1 group by report.androidVersion") + "where bug.app = ?1 group by report.androidVersion")
List<AndroidVersionCount> countAllByAndroidVersion(@NonNull App app); List<AndroidVersionCount> countAllByAndroidVersion(@NonNull App app);
} }

View file

@ -66,9 +66,7 @@ public class UserManager {
username = generator.generate(16); username = generator.generate(16);
} while (userRepository.existsById(username)); } while (userRepository.existsById(username));
String password = generator.generate(16); String password = generator.generate(16);
User user = new User(username, passwordEncoder.encode(password), Collections.singleton(ROLE_REPORTER)); return Pair.of(new User(username, passwordEncoder.encode(password), Collections.singleton(ROLE_REPORTER)), password);
userRepository.save(user);
return Pair.of(user, password);
} }
public boolean checkPassword(@Nullable User user, @NonNull String password) { public boolean checkPassword(@Nullable User user, @NonNull String password) {

View file

@ -13,7 +13,6 @@ import com.faendir.acra.ui.view.base.NamedView;
import com.faendir.acra.ui.view.base.Popup; import com.faendir.acra.ui.view.base.Popup;
import com.faendir.acra.util.BufferedDataProvider; import com.faendir.acra.util.BufferedDataProvider;
import com.faendir.acra.util.Style; import com.faendir.acra.util.Style;
import com.vaadin.data.provider.ConfigurableFilterDataProvider;
import com.vaadin.navigator.ViewChangeListener; import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.spring.annotation.SpringView; import com.vaadin.spring.annotation.SpringView;
import com.vaadin.ui.Button; import com.vaadin.ui.Button;
@ -24,8 +23,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair; import org.springframework.data.util.Pair;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import java.util.function.Predicate;
/** /**
* @author Lukas * @author Lukas
* @since 23.03.2017 * @since 23.03.2017
@ -46,10 +43,11 @@ public class Overview extends NamedView {
@Override @Override
public void enter(ViewChangeListener.ViewChangeEvent event) { public void enter(ViewChangeListener.ViewChangeEvent event) {
ConfigurableFilterDataProvider<App, Void, Predicate<App>> dataProvider = new BufferedDataProvider<>(appRepository::findAll, grid = new MyGrid<>("Apps", new BufferedDataProvider<>(SecurityUtils.hasRole(UserManager.ROLE_ADMIN), (admin, pageable) -> admin ?
() -> Math.toIntExact(appRepository.count())).withConfigurableFilter(); appRepository.findAllByPermissionWithDefaultIncluded(SecurityUtils.getUsername(), Permission.Level.VIEW, pageable) :
dataProvider.setFilter(app -> SecurityUtils.hasPermission(app, Permission.Level.VIEW)); appRepository.findAllByPermissionWithDefaultExcluded(SecurityUtils.getUsername(), Permission.Level.VIEW, pageable), admin -> admin ?
grid = new MyGrid<>("Apps", dataProvider); appRepository.countByPermissionWithDefaultIncluded(SecurityUtils.getUsername(), Permission.Level.VIEW) :
appRepository.countByPermissionWithDefaultExcluded(SecurityUtils.getUsername(), Permission.Level.VIEW)));
grid.setWidth(100, Unit.PERCENTAGE); grid.setWidth(100, Unit.PERCENTAGE);
grid.setSelectionMode(Grid.SelectionMode.NONE); grid.setSelectionMode(Grid.SelectionMode.NONE);
grid.addColumn(App::getName, "Name"); grid.addColumn(App::getName, "Name");

View file

@ -3,9 +3,11 @@ package com.faendir.acra.ui.view.tabs;
import com.faendir.acra.sql.data.AppRepository; import com.faendir.acra.sql.data.AppRepository;
import com.faendir.acra.sql.data.ReportRepository; import com.faendir.acra.sql.data.ReportRepository;
import com.faendir.acra.sql.model.App; import com.faendir.acra.sql.model.App;
import com.faendir.acra.sql.model.Permission;
import com.faendir.acra.sql.model.User; import com.faendir.acra.sql.model.User;
import com.faendir.acra.sql.user.UserManager; import com.faendir.acra.sql.user.UserManager;
import com.faendir.acra.ui.NavigationManager; import com.faendir.acra.ui.NavigationManager;
import com.faendir.acra.ui.annotation.RequiresAppPermission;
import com.faendir.acra.ui.view.base.ConfigurationLabel; import com.faendir.acra.ui.view.base.ConfigurationLabel;
import com.faendir.acra.ui.view.base.MyTabSheet; import com.faendir.acra.ui.view.base.MyTabSheet;
import com.faendir.acra.ui.view.base.Popup; import com.faendir.acra.ui.view.base.Popup;
@ -30,6 +32,7 @@ import java.util.Date;
* @author Lukas * @author Lukas
* @since 19.05.2017 * @since 19.05.2017
*/ */
@RequiresAppPermission(Permission.Level.ADMIN)
@SpringComponent @SpringComponent
@ViewScope @ViewScope
public class PropertiesTab implements MyTabSheet.Tab { public class PropertiesTab implements MyTabSheet.Tab {

View file

@ -11,7 +11,6 @@ import java.util.List;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.IntSupplier; import java.util.function.IntSupplier;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -19,7 +18,7 @@ import java.util.stream.Stream;
* @author Lukas * @author Lukas
* @since 13.12.2017 * @since 13.12.2017
*/ */
public class BufferedDataProvider<T> extends AbstractBackEndDataProvider<T, Predicate<T>> { public class BufferedDataProvider<T> extends AbstractBackEndDataProvider<T, Void> {
private static final int PAGE_SIZE = 32; private static final int PAGE_SIZE = 32;
private final Function<Pageable, Slice<T>> getter; private final Function<Pageable, Slice<T>> getter;
private final IntSupplier counter; private final IntSupplier counter;
@ -30,12 +29,11 @@ public class BufferedDataProvider<T> extends AbstractBackEndDataProvider<T, Pred
} }
public <P> BufferedDataProvider(P parameter, BiFunction<P, Pageable, Slice<T>> getter, Function<P, Integer> counter) { public <P> BufferedDataProvider(P parameter, BiFunction<P, Pageable, Slice<T>> getter, Function<P, Integer> counter) {
this.getter = pageable -> getter.apply(parameter, pageable); this(pageable -> getter.apply(parameter, pageable), () -> counter.apply(parameter));
this.counter = () -> counter.apply(parameter);
} }
@Override @Override
protected Stream<T> fetchFromBackEnd(Query<T, Predicate<T>> query) { protected Stream<T> fetchFromBackEnd(Query<T, Void> query) {
Sort sort = Sort.by(query.getSortOrders().stream().map(OrderAdapter::new).collect(Collectors.toList())); Sort sort = Sort.by(query.getSortOrders().stream().map(OrderAdapter::new).collect(Collectors.toList()));
Slice<T> slice = getter.apply(PageRequest.of(query.getOffset() / PAGE_SIZE, PAGE_SIZE, sort)); Slice<T> slice = getter.apply(PageRequest.of(query.getOffset() / PAGE_SIZE, PAGE_SIZE, sort));
if (!slice.hasContent()) return Stream.empty(); if (!slice.hasContent()) return Stream.empty();
@ -51,14 +49,11 @@ public class BufferedDataProvider<T> extends AbstractBackEndDataProvider<T, Pred
result = Stream.concat(result, content.stream()); result = Stream.concat(result, content.stream());
} }
} }
if(query.getFilter().isPresent()){
result = result.filter(query.getFilter().get());
}
return result; return result;
} }
@Override @Override
protected int sizeInBackEnd(Query<T, Predicate<T>> query) { protected int sizeInBackEnd(Query<T, Void> query) {
return counter.getAsInt(); return counter.getAsInt();
} }
} }