generated from FINTLabs/fint-spring-boot-template-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from FINTLabs/using_spesification
Using spesification
- Loading branch information
Showing
6 changed files
with
120 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/no/fintlabs/user/UserSpesificationBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package no.fintlabs.user; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
public class UserSpesificationBuilder { | ||
private final String search; | ||
private final List<String> orgUnits; //lovlige orgunits | ||
private final String userType; | ||
|
||
public UserSpesificationBuilder(String search, List<String> orgUnits, String userType) { | ||
this.search = search; | ||
this.orgUnits = orgUnits; | ||
this.userType = userType; | ||
} | ||
|
||
public Specification<User> build() { | ||
Specification<User> userSpec = allAutorizedOrgUnits(orgUnits) ; | ||
|
||
if (!search.isEmpty()){ | ||
userSpec = userSpec.and(usersNameLike(search)); | ||
} | ||
if (!userType.equals("ALLTYPES")) { | ||
userSpec = userSpec.and(userTypeEquals(userType.toLowerCase())); | ||
} | ||
|
||
return userSpec; | ||
} | ||
|
||
private Specification<User> allAutorizedOrgUnits(List<String> orgUnits) { | ||
|
||
return (root, query, criteriaBuilder) -> criteriaBuilder | ||
.in(root.get("mainOrganisationUnitId")).value(orgUnits); | ||
} | ||
|
||
private Specification<User> userTypeEquals(String userType) { | ||
return (root, query, criteriaBuilder) -> criteriaBuilder | ||
.equal(criteriaBuilder.lower(root.get("userType")),userType); | ||
} | ||
|
||
private Specification<User> usersNameLike(String search) { | ||
return (root, query, criteriaBuilder) -> | ||
criteriaBuilder.or( | ||
criteriaBuilder.like(criteriaBuilder.lower(root.get("firstName")), "%" + search + "%"), | ||
criteriaBuilder.like(criteriaBuilder.lower(root.get("lastName")), "%" + search + "%") | ||
); | ||
|
||
} | ||
|
||
|
||
} |