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
ff400445
Commit
ff400445
authored
Oct 18, 2019
by
Jan Hrabal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
514b0131
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
121 deletions
+41
-121
AbstractHibernateRepository.java
...ain/java/com/jh/boot/jpa/AbstractHibernateRepository.java
+1
-121
SecurityViolationException.java
...ava/com/jh/boot/web/error/SecurityViolationException.java
+40
-0
No files found.
src/main/java/com/jh/boot/jpa/AbstractHibernateRepository.java
View file @
ff400445
package
com
.
jh
.
boot
.
jpa
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.Set
;
import
java.util.function.Function
;
...
...
@@ -14,21 +11,7 @@ import javax.persistence.PersistenceContext;
import
javax.persistence.Query
;
import
javax.persistence.criteria.CriteriaQuery
;
import
org.hibernate.Criteria
;
import
org.hibernate.criterion.Conjunction
;
import
org.hibernate.criterion.Disjunction
;
import
org.hibernate.criterion.MatchMode
;
import
org.hibernate.criterion.Order
;
import
org.hibernate.criterion.ProjectionList
;
import
org.hibernate.criterion.Projections
;
import
org.hibernate.criterion.Restrictions
;
import
org.hibernate.query.NativeQuery
;
import
org.hibernate.transform.Transformers
;
import
com.jh.boot.web.list.Page
;
import
com.jh.boot.web.list.PagingInfo
;
import
com.jh.boot.web.list.SortTrend
;
import
com.jh.boot.web.list.SortingInfo
;
/**
...
...
@@ -68,106 +51,8 @@ public abstract class AbstractHibernateRepository {
return
null
;
}
}
/**
* TODO comment
*
*
* @param tokens the tokens
* @param fields the fields
* @return the conjunction
*/
protected
Conjunction
fulltextCriteria
(
List
<
String
>
tokens
,
String
...
fields
)
{
Objects
.
requireNonNull
(
tokens
,
"Tokens must not be null"
);
Objects
.
requireNonNull
(
fields
,
"Fields must not be null"
);
Conjunction
and
=
Restrictions
.
conjunction
();
for
(
String
t
:
tokens
)
{
Disjunction
d
=
Restrictions
.
disjunction
();
for
(
String
f
:
fields
)
{
d
.
add
(
Restrictions
.
ilike
(
f
,
t
,
MatchMode
.
ANYWHERE
));
}
and
.
add
(
d
);
}
return
and
;
}
/**
* TODO comment.
*
* @param c the c
* @param clazz the clazz
* @param properties the properties
*/
protected
void
fields
(
Criteria
c
,
Class
<?>
clazz
,
String
...
properties
)
{
Objects
.
requireNonNull
(
c
,
"Criteria must not be null"
);
Objects
.
requireNonNull
(
clazz
,
"Class must not be null"
);
if
(
properties
==
null
||
properties
.
length
==
0
)
{
return
;
}
ProjectionList
pl
=
Projections
.
projectionList
();
for
(
String
p
:
properties
)
{
pl
.
add
(
Projections
.
alias
(
Projections
.
property
(
p
),
p
));
}
c
.
setProjection
(
pl
);
c
.
setResultTransformer
(
Transformers
.
aliasToBean
(
clazz
));
}
/**
* TODO comment.
*
* @param <T> the generic type
* @param c the c
* @param pagingInfo the paging info
* @return the page
*/
@SuppressWarnings
(
"unchecked"
)
protected
<
T
>
Page
<
T
>
pagedResult
(
Criteria
c
,
PagingInfo
pagingInfo
)
{
if
(
pagingInfo
==
null
)
{
return
new
Page
<>(
c
.
list
());
}
else
if
(
pagingInfo
.
getPageSize
()
<
1
)
{
return
new
Page
<>(
Collections
.
emptyList
());
}
int
pageSize
=
pagingInfo
.
getPageSize
();
int
count
=
((
Number
)
c
.
setProjection
(
Projections
.
rowCount
()).
uniqueResult
()).
intValue
();
//count pages count
int
pagesCount
=
count
/
pageSize
+
(
count
%
pageSize
==
0
?
0
:
1
);
//limit results
c
.
setFirstResult
(
pageSize
*
pagingInfo
.
getPage
());
c
.
setMaxResults
(
pageSize
);
//reset criteria https://stackoverflow.com/a/1472958
c
.
setProjection
(
null
);
c
.
setResultTransformer
(
Criteria
.
ROOT_ENTITY
);
sortedCriteria
(
c
,
pagingInfo
);
return
new
Page
<>(
pagingInfo
.
getPage
(),
pagesCount
,
pagingInfo
.
getPageSize
(),
count
,
c
.
list
(),
pagingInfo
.
getField
(),
pagingInfo
.
getTrend
());
}
/**
* Sorted criteria.
*
* @param c the c
* @param sortingInfo the sorting info
* @return the criteria
*/
protected
Criteria
sortedCriteria
(
Criteria
c
,
SortingInfo
sortingInfo
)
{
if
(
sortingInfo
!=
null
&&
sortingInfo
.
getField
()
!=
null
)
{
c
.
addOrder
(
sortingInfo
.
getTrend
()
==
SortTrend
.
DESCENDING
?
Order
.
desc
(
sortingInfo
.
getField
())
:
Order
.
asc
(
sortingInfo
.
getField
()));
}
return
c
;
}
public
<
ID
,
T
>
T
findById
(
Class
<
T
>
clazz
,
ID
id
)
{
return
entityManager
.
find
(
clazz
,
id
);
}
...
...
@@ -268,11 +153,6 @@ public abstract class AbstractHibernateRepository {
syncReferences
(
entity
,
referencedClass
,
referencedField
,
valueProviders
.
apply
(
entity
));
}
}
// @SuppressWarnings("unchecked")
// public <T extends AbstractIdEntity> T merge(T entity) {
// return (T) getSession().merge(entity);
// }
}
src/main/java/com/jh/boot/web/error/SecurityViolationException.java
0 → 100644
View file @
ff400445
package
com
.
jh
.
boot
.
web
.
error
;
import
java.util.Collection
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
/**
* The Class ValidationFailedException.
*/
@ResponseStatus
(
value
=
HttpStatus
.
FORBIDDEN
)
public
class
SecurityViolationException
extends
RestApiException
{
/** The Constant serialVersionUID. */
private
static
final
long
serialVersionUID
=
1L
;
/**
* Instantiates a new validation failed exception.
*
* @param errors the errors
*/
public
SecurityViolationException
(
Collection
<
ErrorMessage
>
errors
)
{
super
(
HttpStatus
.
FORBIDDEN
,
errors
);
}
/**
* Instantiates a new validation failed exception.
*
* @param errors the errors
*/
public
SecurityViolationException
(
ErrorMessage
...
errors
)
{
super
(
HttpStatus
.
FORBIDDEN
,
errors
);
}
}
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