1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.util.HashMap; import java.util.List; import java.util.Map;
public class BaseController { private static final Logger log = LoggerFactory.getLogger(BaseController.class); private static final int PAGE_SIZE = 10;
public BaseController() { }
public int getPageNo(Map<String, Object> parameterMap) { int pageNo = 1; String curr = MapUtils.getString(parameterMap, "pageNo", ""); if (StringUtils.isBlank(curr)) { curr = MapUtils.getString(parameterMap, "pager.pageNo", ""); } if (StringUtils.isNotBlank(curr)) { try { pageNo = Integer.parseInt(curr); } catch (Exception var5) { pageNo = 1; } } pageNo = Math.max(pageNo, 1); return pageNo; }
public int getPageSize(Map<String, Object> parameterMap) { return getPageSize(parameterMap, PAGE_SIZE); }
public int getPageSize(Map<String, Object> parameterMap, int defPageSize) { int pageSize = defPageSize; String curr = MapUtils.getString(parameterMap, "pageSize", ""); if (StringUtils.isBlank(curr)) { curr = MapUtils.getString(parameterMap, "pager.pageSize", ""); } if (StringUtils.isNotEmpty(curr)) { try { pageSize = Integer.parseInt(curr); } catch (Exception var5) { pageSize = defPageSize; } } return pageSize; }
public String getOrderBy(Map<String, Object> parameterMap) { String sort = MapUtils.getString(parameterMap, "sort", ""); String direction = MapUtils.getString(parameterMap, "direction", ""); return StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(direction) ? sort + " " + direction : ""; }
public Result renderSuccess() { Result result = new Result(); result.setCode(0); result.setMessage("success"); return result; }
public <T> Result<T> renderSuccess(String message) { Result result = new Result(); result.setCode(ErrorCode.OK.getCode()); result.setMessage(message); return result; }
public <T> Result<T> renderSuccess(T data) { Result<T> result = new Result(); result.setCode(0); result.setMessage("success"); result.setData(data); return result; }
public <T> Result<T> renderRowsSuccess(T rows) { Result<T> result = new Result(); result.setCode(0); result.setMessage("success"); result.setRows(rows); return result; }
public <T> Result<T> renderSuccess(T data, String message) { Result<T> result = new Result(); result.setCode(0); result.setMessage(message); result.setData(data); return result; }
public <T> Result<T> renderError(String message) { Result result = new Result(); result.setCode(1000); result.setMessage(message); return result; }
public Result<Object> exception(Exception ex) { log.error(ex.getMessage(), ex.getCause()); return this.renderError(ex.getMessage()); }
public <T> Result<List<T>> renderSuccess(List list) { Result<List<T>> result = new Result(); result.setCode(ErrorCode.OK.getCode()); result.setMessage(ErrorCode.OK.getMessage()); result.setData(list); return result; }
public Result<Object> renderSuccess(String message, Object... data) { Result<Object> result = new Result(); result.setCode(ErrorCode.OK.getCode()); result.setMessage(message); result.setData(data); return result; }
public Map<String, Object> renderWebPageInfo(Page<?> page) { Map<String, Object> jsonMap = new HashMap(); jsonMap.put("pager.totalRows", page.getTotal()); jsonMap.put("pager.pageNo", page.getCurrent()); jsonMap.put("rows", page.getRecords()); jsonMap.put("status", "0"); jsonMap.put("message", "success"); return jsonMap; } }
|