Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jh-boot
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jan Hrabal
jh-boot
Commits
ccaa3e2f
Commit
ccaa3e2f
authored
Dec 15, 2019
by
Jan Hrabal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
2.0
parent
6b52ff0f
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
78 additions
and
1 deletion
+78
-1
pom.xml
pom.xml
+1
-1
AbstractHibernateRepository.java
...ain/java/com/jh/boot/jpa/AbstractHibernateRepository.java
+25
-0
SecurityHelper.java
src/main/java/com/jh/boot/security/SecurityHelper.java
+9
-0
ConverterUtils.java
src/main/java/com/jh/boot/web/ConverterUtils.java
+31
-0
Page.java
src/main/java/com/jh/boot/web/list/Page.java
+12
-0
No files found.
pom.xml
View file @
ccaa3e2f
...
...
@@ -5,7 +5,7 @@
<groupId>
com.jh
</groupId>
<artifactId>
jh-boot
</artifactId>
<version>
0.0.
1
</version>
<version>
0.0.
2
</version>
<packaging>
jar
</packaging>
<parent>
...
...
src/main/java/com/jh/boot/jpa/AbstractHibernateRepository.java
View file @
ccaa3e2f
...
...
@@ -14,10 +14,16 @@ import javax.persistence.EntityManager;
import
javax.persistence.NoResultException
;
import
javax.persistence.PersistenceContext
;
import
javax.persistence.Query
;
import
javax.persistence.criteria.CriteriaBuilder
;
import
javax.persistence.criteria.CriteriaQuery
;
import
javax.persistence.criteria.Predicate
;
import
javax.persistence.criteria.Root
;
import
org.hibernate.query.NativeQuery
;
import
com.jh.boot.web.list.Page
;
import
com.jh.boot.web.list.PagingInfo
;
/**
* TODO comment.
...
...
@@ -130,4 +136,23 @@ public abstract class AbstractHibernateRepository {
}
@SuppressWarnings
(
"unchecked"
)
protected
<
T
>
Page
<
T
>
pagedCriteria
(
CriteriaBuilder
cb
,
CriteriaQuery
<
T
>
cq
,
Root
<
T
>
root
,
PagingInfo
pagingInfo
)
{
Query
query
=
entityManager
.
createQuery
(
cq
);
query
.
setFirstResult
(
pagingInfo
.
getPage
()
*
pagingInfo
.
getPageSize
());
query
.
setMaxResults
(
pagingInfo
.
getPageSize
());
List
<
T
>
resultList
=
query
.
getResultList
();
CriteriaQuery
<
Long
>
count
=
cb
.
createQuery
(
Long
.
class
);
Root
<
T
>
countRoot
=
count
.
from
(
root
.
getModel
());
count
.
select
(
cb
.
count
(
countRoot
));
if
(
cq
.
getRestriction
()
!=
null
)
{
count
.
where
(
cq
.
getRestriction
());
}
Long
rowsCount
=
entityManager
.
createQuery
(
count
).
getSingleResult
();
return
new
Page
<
T
>(
pagingInfo
.
getPage
(),
pagingInfo
.
getPageSize
(),
rowsCount
,
resultList
,
pagingInfo
.
getField
(),
pagingInfo
.
getTrend
());
}
}
src/main/java/com/jh/boot/security/SecurityHelper.java
View file @
ccaa3e2f
...
...
@@ -25,6 +25,15 @@ public class SecurityHelper {
}
public
static
<
T
extends
AppUser
>
T
getUser
(
Authentication
auth
,
Class
<
T
>
clazz
)
{
if
(
AppUser
.
class
.
isAssignableFrom
(
clazz
))
{
return
(
T
)
auth
.
getPrincipal
();
}
return
null
;
}
public
static
boolean
hasAnyRole
(
Authentication
auth
,
String
...
roles
)
{
AppUser
user
=
auth
instanceof
AppUserAuthentication
?
((
AppUserAuthentication
)
auth
).
getUser
()
:
null
;
return
hasAnyRole
(
user
,
roles
);
...
...
src/main/java/com/jh/boot/web/ConverterUtils.java
0 → 100644
View file @
ccaa3e2f
package
com
.
jh
.
boot
.
web
;
import
java.util.StringJoiner
;
import
org.springframework.util.StringUtils
;
import
com.jh.boot.jpa.AbstractIdEntity
;
import
com.jh.boot.security.model.AppUser
;
public
class
ConverterUtils
{
public
static
Long
id
(
AbstractIdEntity
user
)
{
return
user
==
null
?
null
:
user
.
getId
();
}
public
static
String
name
(
AppUser
user
)
{
if
(
user
==
null
)
{
return
null
;
}
StringJoiner
sj
=
new
StringJoiner
(
" "
);
if
(
StringUtils
.
hasText
(
user
.
getFirstName
()))
{
sj
.
add
(
user
.
getFirstName
());
}
if
(
StringUtils
.
hasText
(
user
.
getLastName
()))
{
sj
.
add
(
user
.
getLastName
());
}
return
sj
.
toString
();
}
}
src/main/java/com/jh/boot/web/list/Page.java
View file @
ccaa3e2f
...
...
@@ -87,6 +87,18 @@ public class Page<T> {
this
.
pageSize
=
pageSize
;
}
public
Page
(
int
page
,
int
pageSize
,
long
totalElements
,
List
<
T
>
content
,
String
sortBy
,
SortTrend
trend
)
{
super
();
this
.
content
=
content
;
this
.
sortBy
=
sortBy
;
this
.
trend
=
trend
;
this
.
totalElements
=
(
int
)
totalElements
;
this
.
pageSize
=
pageSize
;
this
.
page
=
this
.
totalElements
>
0
?
Math
.
min
(
page
,
this
.
totalElements
/
Math
.
max
(
pageSize
,
1
))
:
0
;
this
.
pagesCount
=
this
.
totalElements
>
0
?
(
this
.
totalElements
+
pageSize
/
2
)
/
this
.
pageSize
:
1
;
}
/**
* Gets the content.
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment