-
Notifications
You must be signed in to change notification settings - Fork 2
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 #101 from hycomsa/90
Improve mock list pagination on "Configuration" page. #90
- Loading branch information
Showing
7 changed files
with
233 additions
and
131 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
27 changes: 27 additions & 0 deletions
27
src/mokka/src/main/java/pl/hycom/mokka/util/ArithmeticUtils.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,27 @@ | ||
package pl.hycom.mokka.util; | ||
|
||
/** | ||
* Helper class for arithmetic related operations. | ||
* | ||
* @author Kamil Adamiec ([email protected]) | ||
*/ | ||
public class ArithmeticUtils { | ||
|
||
private ArithmeticUtils() { | ||
// NOP | ||
} | ||
|
||
/** | ||
* Method which is workaround for sonar issue with boxing Math.ceil result, | ||
* to non-primitive type, for purpose of converting floating point result to integer type. | ||
* Method uses modulo operations to make sure, that result of integer division will be always rounded upwards. | ||
* | ||
* @param dividend Dividend | ||
* @param divisor Divisor | ||
* @return Rounded result | ||
*/ | ||
public static Long divideAndCeil(long dividend, long divisor) { | ||
long remainder = dividend % divisor; | ||
return (dividend + (remainder == 0 || remainder > (divisor / 2) ? remainder : (divisor - remainder))) / divisor; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -11,148 +11,157 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Tomasz Wozniak ([email protected]) | ||
*/ | ||
@Service | ||
@Transactional | ||
public class MockSearch { | ||
|
||
public static final String ID = "id"; | ||
public static final String NAME = "name"; | ||
public static final String PATH = "path"; | ||
public static final String DESCRIPTION = "description"; | ||
public static final String PATTERN = "pattern"; | ||
public static final String ENABLED = "enabled"; | ||
private static final String PERCENT = "%"; | ||
private static final String WHERE = "where "; | ||
|
||
@PersistenceContext | ||
private EntityManager em; | ||
|
||
private Integer startingIndex; | ||
private Integer maxResults; | ||
private Map<String, String> parameterMap = new HashMap<>(); | ||
|
||
public List<MockConfiguration> find(){ | ||
Query query; | ||
StringBuilder base = new StringBuilder(); | ||
|
||
if(parameterMap.size() == 0){ | ||
base.append("select m from MockConfig m "); | ||
} | ||
else{ | ||
base.append("select m from MockConfig m where "); | ||
} | ||
/** | ||
* @author Tomasz Wozniak ([email protected]) | ||
*/ | ||
@Service | ||
@Transactional | ||
public class MockSearch { | ||
|
||
public static final String ID = "id"; | ||
public static final String NAME = "name"; | ||
public static final String PATH = "path"; | ||
public static final String DESCRIPTION = "description"; | ||
public static final String PATTERN = "pattern"; | ||
public static final String ENABLED = "enabled"; | ||
private static final String PERCENT = "%"; | ||
private static final String WHERE = "where "; | ||
|
||
@PersistenceContext | ||
private EntityManager em; | ||
|
||
private Integer startingIndex; | ||
private Integer maxResults; | ||
private Map<String, String> parameterMap = new HashMap<>(); | ||
|
||
public List<MockConfiguration> find(){ | ||
Query query; | ||
StringBuilder base = new StringBuilder(); | ||
|
||
if(parameterMap.size() == 0){ | ||
base.append("select m from MockConfig m "); | ||
} | ||
else{ | ||
base.append("select m from MockConfig m where "); | ||
appendParametersToBaseQuery(base); | ||
} | ||
|
||
if(parameterMap.containsKey(ID)) { | ||
base.append("m.id = :id "); | ||
} | ||
base.append("order by m ASC"); | ||
|
||
if(parameterMap.containsKey(NAME)) { | ||
if(base.toString().endsWith(WHERE)) { | ||
base.append("lower(m.name) like lower(:name) "); | ||
} else { | ||
base.append("and lower(m.name) like lower(:name) "); | ||
} | ||
} | ||
query = em.createQuery(base.toString(), MockConfiguration.class); | ||
setQueryParameters(query); | ||
|
||
if(parameterMap.containsKey(PATH)) { | ||
if(base.toString().endsWith(WHERE)) { | ||
base.append("lower(m.path) like lower(:path) "); | ||
}else { | ||
base.append("and lower(m.path) like lower(:path) "); | ||
} | ||
} | ||
if (getStartingIndex() != null) { | ||
query.setFirstResult(getStartingIndex()); | ||
} | ||
|
||
if(parameterMap.containsKey(PATTERN)){ | ||
if(base.toString().endsWith(WHERE)) { | ||
base.append("lower(m.pattern) like lower(:pattern) "); | ||
} | ||
else { | ||
base.append("and lower(m.pattern) like lower(:pattern) "); | ||
} | ||
} | ||
if (getMaxResults() != null) { | ||
query.setMaxResults(getMaxResults()+1); | ||
} | ||
return query.getResultList(); | ||
} | ||
|
||
if(parameterMap.containsKey(DESCRIPTION)){ | ||
if(base.toString().endsWith(WHERE)){ | ||
base.append("lower(m.description) like lower(:description) "); | ||
} | ||
else { | ||
base.append("and lower(m.description) like lower(:description) "); | ||
} | ||
} | ||
public Long countAllPossibleResults() { | ||
Query query; | ||
StringBuilder base = new StringBuilder(); | ||
|
||
if(parameterMap.containsKey(ENABLED)){ | ||
if(base.toString().endsWith(WHERE)){ | ||
base.append("m.enabled ="); | ||
} | ||
else { | ||
base.append("and m.enabled ="); | ||
} | ||
|
||
if("true".equalsIgnoreCase(parameterMap.get(ENABLED))){ | ||
base.append("1 "); | ||
} | ||
else { | ||
base.append("0 "); | ||
} | ||
} | ||
if(parameterMap.size() == 0) { | ||
base.append("select count(m) from MockConfig m"); | ||
} else { | ||
base.append("select count(m) from MockConfig m where "); | ||
appendParametersToBaseQuery(base); | ||
} | ||
|
||
base.append("order by m ASC"); | ||
query = em.createQuery(base.toString(), Long.class); | ||
setQueryParameters(query); | ||
return (Long) query.getSingleResult(); | ||
} | ||
|
||
query = em.createQuery(base.toString(), MockConfiguration.class); | ||
private void appendParametersToBaseQuery(StringBuilder base) { | ||
|
||
if(parameterMap.containsKey(ID)) { | ||
query.setParameter("id", Long.valueOf(parameterMap.get(ID))); | ||
} | ||
if(parameterMap.containsKey(ID)) { | ||
base.append("m.id = :id "); | ||
} | ||
|
||
if(parameterMap.containsKey(NAME)) { | ||
query.setParameter("name", PERCENT + parameterMap.get(NAME) + PERCENT); | ||
} | ||
if(parameterMap.containsKey(NAME)) { | ||
appendCondition(base, "lower(m.name) like lower(:name) "); | ||
} | ||
|
||
if(parameterMap.containsKey(PATH)) { | ||
query.setParameter("path", PERCENT + parameterMap.get(PATH) + PERCENT); | ||
} | ||
if(parameterMap.containsKey(PATH)) { | ||
appendCondition(base, "lower(m.path) like lower(:path) "); | ||
} | ||
|
||
if(parameterMap.containsKey(DESCRIPTION)){ | ||
query.setParameter("description", PERCENT + parameterMap.get(DESCRIPTION) + PERCENT); | ||
} | ||
if(parameterMap.containsKey(PATTERN)){ | ||
appendCondition(base, "lower(m.pattern) like lower(:pattern) "); | ||
} | ||
|
||
if(parameterMap.containsKey(PATTERN)){ | ||
query.setParameter("pattern", PERCENT + parameterMap.get(PATTERN) + PERCENT); | ||
} | ||
if(parameterMap.containsKey(DESCRIPTION)){ | ||
appendCondition(base, "lower(m.description) like lower(:description) "); | ||
} | ||
|
||
if (getStartingIndex() != null) { | ||
query.setFirstResult(getStartingIndex()); | ||
} | ||
if(parameterMap.containsKey(ENABLED)){ | ||
appendCondition(base, "m.enabled ="); | ||
|
||
if (getMaxResults() != null) { | ||
query.setMaxResults(getMaxResults()+1); | ||
if("true".equalsIgnoreCase(parameterMap.get(ENABLED))){ | ||
base.append("1 "); | ||
} else { | ||
base.append("0 "); | ||
} | ||
} | ||
} | ||
|
||
parameterMap.clear(); | ||
return query.getResultList(); | ||
private void appendCondition(StringBuilder base, String conditionString) { | ||
if(base.toString().endsWith(WHERE)) { | ||
base.append("and "); | ||
} | ||
base.append(conditionString); | ||
} | ||
|
||
private void setQueryParameters(Query query) { | ||
|
||
public void add(String parameter, String value){ | ||
parameterMap.put(parameter,value); | ||
if(parameterMap.containsKey(ID)) { | ||
query.setParameter("id", Long.valueOf(parameterMap.get(ID))); | ||
} | ||
|
||
public Integer getStartingIndex() { | ||
return startingIndex; | ||
if(parameterMap.containsKey(NAME)) { | ||
query.setParameter("name", PERCENT + parameterMap.get(NAME) + PERCENT); | ||
} | ||
|
||
public void setStartingIndex(Integer startingIndex) { | ||
this.startingIndex = startingIndex; | ||
if(parameterMap.containsKey(PATH)) { | ||
query.setParameter("path", PERCENT + parameterMap.get(PATH) + PERCENT); | ||
} | ||
|
||
public Integer getMaxResults() { | ||
return maxResults; | ||
if(parameterMap.containsKey(DESCRIPTION)){ | ||
query.setParameter("description", PERCENT + parameterMap.get(DESCRIPTION) + PERCENT); | ||
} | ||
|
||
public void setMaxResults(Integer maxResults) { | ||
this.maxResults = maxResults; | ||
if(parameterMap.containsKey(PATTERN)){ | ||
query.setParameter("pattern", PERCENT + parameterMap.get(PATTERN) + PERCENT); | ||
} | ||
} | ||
|
||
public void clearParameterMap() { | ||
parameterMap.clear(); | ||
} | ||
|
||
public void add(String parameter, String value){ | ||
parameterMap.put(parameter,value); | ||
} | ||
|
||
public Integer getStartingIndex() { | ||
return startingIndex; | ||
} | ||
|
||
public void setStartingIndex(Integer startingIndex) { | ||
this.startingIndex = startingIndex; | ||
} | ||
|
||
public Integer getMaxResults() { | ||
return maxResults; | ||
} | ||
|
||
public void setMaxResults(Integer maxResults) { | ||
this.maxResults = maxResults; | ||
} | ||
} | ||
|
Oops, something went wrong.