https://github.com/JonnyD/greetapp-api-java
Small example of the Specification pattern:
/**
* GET /greets-by-user/:groupId : get all the greets by group.
*
* @return the ResponseEntity with status 200 (OK) and the list of greets in body
*/
@GetMapping("/greets-by-group/{groupId}")
@Timed
public Object getAllGreetsByGroup(@PathVariable Long groupId) {
log.debug("REST request to get all Greets by user");
List<Greet> greets = greetService.getByGang(groupId);
Optional<Gang> optionalGang = this.gangService.getById(groupId);
Gang gang = optionalGang.get();
CanViewGang canViewGang = new CanViewGang(this.userService);
if (!canViewGang.isSatisfiedBy(gang)) {
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
CanViewGreetsByGang canViewGreetsByGang = new CanViewGreetsByGang(this.userRepository);
List<Greet> greetsToShow = new ArrayList<Greet>();
for (Greet greet : greets) {
if (canViewGreetsByGang.isSatisfiedBy(greet)) {
greetsToShow.add(greet);
}
}
return greetsToShow;
}
public class CanViewGreetsByGang extends AbstractSpecification<Greet> {
private User loggedInUser;
public CanViewGreetsByGang(UserRepository userRepository) {
Optional<String> login = SecurityUtils.getCurrentUserLogin();
this.loggedInUser = userRepository.findOneByLogin(login.get()).get();
}
public boolean isSatisfiedBy(Greet greet) {
IsGangPublic isGangPublic = new IsGangPublic();
IsHostOfGreet isHostOfGreet = new IsHostOfGreet(this.loggedInUser);
IsMemberOfGang isMemberOfGang = new IsMemberOfGang(this.loggedInUser);
boolean isGangPublicOrIsMemberOfGangBoolean = isGangPublic
.or(isMemberOfGang)
.isSatisfiedBy(greet.getGang());
boolean isHostOfGreetBoolean = isHostOfGreet
.isSatisfiedBy(greet);
return isGangPublicOrIsMemberOfGangBoolean || isHostOfGreetBoolean;
}
}