DataIterator Class
package com.collager.trillo.model;
import java.util.List;
import java.util.Map;
import com.collager.trillo.pojo.Result;
import com.collager.trillo.util.BaseApi;
import com.collager.trillo.util.CoreDSUtil;
import com.collager.trillo.util.DSApi;
import com.collager.trillo.util.LogApi;
import com.collager.trillo.util.Util;
public class DataIterator {
private DataRequest dataRequest = null;
private DataResult dataResult = null;
private Object result = null;
private int index = 0;
private boolean iteratorEnded = false;
private boolean noMorePage = false;
private boolean orderById = false;
private String originalQuery = null;
private Object lastId;
private String idAttrName = "id";
private String dsType = CoreDSUtil.MYSQL;
private boolean firstPage = true;
private int nextStart = 1;
private String countQuery = null;
private int totalItems = -1;
private boolean errored = false;
private String message = null;
public DataIterator(String className, String where, String orderBy, int start, int pageSize) {
dataRequest = new DataRequest();
dataRequest.setClassName(className);
dataRequest.setWhere(where);
dataRequest.setOrderBy(orderBy);
dataRequest.setStart(start);
dataRequest.setSize(pageSize);
dataRequest.setUsingRowLimits(true);
dataRequest.setAppName(BaseApi.app(className));
dataRequest.setDsName(BaseApi.ds(className));
dataRequest.setClassName(BaseApi.cls(className));
}
… more constructors …
public DataIterator(String className, Exp filter, String orderBy, int start, int pageSize) {
… … …
}
public DataIterator(int start, int pageSize, String sqlQuery) {
// using sql query
dataRequest = new DataRequest();
dataRequest.setSql(sqlQuery);
dataRequest.setStart(start);
dataRequest.setSize(pageSize);
dataRequest.setUsingRowLimits(true);
}
public DataIterator(String appName, String dsName, int start, int pageSize, String sqlQuery) {
… … …
}
… … …
public DataRequest getDataRequest() {
return dataRequest;
}
public DataResult getDataResult() {
return dataResult;
}
public Object getResult() {
return result;
}
public Object hasError() {
return errored;
}
public Object getMessage() {
return message;
}
public int getIndex() {
return index;
}
public Result initialize() {
if (iteratorEnded) {
return Result.getFailedResult("DataIterator has ended");
}
if (dataResult == null) {
fetch();
}
if (errored) {
return Result.getFailedResult(message);
}
return Result.getSuccessResult();
}
@SuppressWarnings("unchecked")
public Object getNext() {
// return next record
}
public boolean hasNext() {
// checks if has next record
}
public List<Map<String, Object>> getPage() {
// return a page
}
public boolean hasPage() {
// returns if more pages pending to iterate
}
private void fetch() {
// internal implementation
}
@SuppressWarnings("unchecked")
private DataResult retrievePage() {
// internal implementation
}
public int getTotalItems() {
if (totalItems != -1) {
return totalItems;
}
if (iteratorEnded) {
return totalItems;
}
if (dataResult == null) {
fetch();
}
return totalItems;
}
public String getCountQuery() {
return countQuery;
}
// to get the totalCount
public void setCountQuery(String countQuery) {
this.countQuery = countQuery;
}
}
Last updated