Skip to content

Commit

Permalink
Merge pull request #101 from hycomsa/90
Browse files Browse the repository at this point in the history
Improve mock list pagination on "Configuration" page. #90
  • Loading branch information
kulasekp authored Feb 18, 2020
2 parents 9cef147 + 9458a4c commit b3b7bc1
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import pl.hycom.mokka.security.UserRepository;
import pl.hycom.mokka.security.model.AuditedRevisionEntity;
import pl.hycom.mokka.security.model.User;
import pl.hycom.mokka.util.ArithmeticUtils;
import pl.hycom.mokka.util.query.MockSearch;
import pl.hycom.mokka.util.query.Q;
import pl.hycom.mokka.util.query.QManager;
Expand Down Expand Up @@ -223,7 +224,7 @@ public WrappedMockConfiguration getMockConfigurations(HttpServletRequest req) {
mockSearch.setStartingIndex(Integer.parseInt(req.getParameter("from")) * mocksPerPage);
}

// start perPage & startFromc
// start perPage & startFrom
if (StringUtils.isNumeric(req.getParameter("perPage"))) {
mockSearch.setMaxResults(Integer.parseInt(req.getParameter("perPage")) * mocksPerPage);
} else {
Expand Down Expand Up @@ -261,12 +262,9 @@ public WrappedMockConfiguration getMockConfigurations(HttpServletRequest req) {
}

wrappedMockConfiguration.mocks = mockSearch.find();

if (wrappedMockConfiguration.mocks.size() >= mocksPerPage + 1) {
wrappedMockConfiguration.hasNext = true;
} else {
wrappedMockConfiguration.hasNext = false;
}
Long allMockCount = mockSearch.countAllPossibleResults();
wrappedMockConfiguration.pageCount = ArithmeticUtils.divideAndCeil(allMockCount, mocksPerPage);
mockSearch.clearParameterMap();

return wrappedMockConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public class WrappedMockConfiguration implements Serializable {

@JsonView(View.General.class)
@TrackChanges
public boolean hasNext = false;
public Long pageCount;
}
27 changes: 27 additions & 0 deletions src/mokka/src/main/java/pl/hycom/mokka/util/ArithmeticUtils.java
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;
}
}
239 changes: 124 additions & 115 deletions src/mokka/src/main/java/pl/hycom/mokka/util/query/MockSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Loading

0 comments on commit b3b7bc1

Please sign in to comment.