/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.report.selectors.baseSelectors;
import beans.report.selectors.filters.FilterByItemSelect;
import beans.report.selectors.filters.FilterPathSource;
import beans.report.selectors.filters.FilterSelect;
import beans.report.selectors.filters.FilterSelect.FiterSQL;
import framework.utils.Pair;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Query;
/**
* @param <DATA>
* @author finder
*/
public abstract class AbstractSQLSelector<DATA>
extends AbstractDataSelector<DATA>
implements FilterPathSource {
private ArrayList<FilterSelect> filterList;
private int filterID = 1;
private int filteSelectDataID = -1;
private ArrayList<Pair<Integer, FilterByItemSelect>>itemFilters;
public AbstractSQLSelector(EntityManager manager) {
super(manager);
}
/*protected int calcDayDif(Date from, Date to){
if (from == null || to == null) {
return 0;
}
long milisDif = to.getTime() - from.getTime();
return (int)Math.round(((double)milisDif / 1000d) / (60d * 60d * 24d));
}*/
public ArrayList<FilterSelect> getFilterList() {
return filterList;
}
public void setFilterList(ArrayList<FilterSelect> filterList) {
this.filterList = filterList;
}
private String chomaAddString(String target, String[] add){
if (add == null){
return target;
}
StringBuilder builder = new StringBuilder(target == null? "": target.trim());
for (int i = 0; i < add.length; i++) {
if (builder.length() > 0){
builder.append(", ");
}
builder.append(add[i]);
}
return builder.toString();
}
public List<Object[]> execQuery(String what, String from, String where, Map<String, Object> param){
if (this.filterList != null){
for (FilterSelect filterSelect : filterList) {
FiterSQL res = filterSelect.getFilterSQL(this, this);
if (filterSelect instanceof FilterByItemSelect){
String[] whatList = ((FilterByItemSelect.FiterDataSQL)res).whatStr;
what = chomaAddString(what, whatList);
if (whatList != null && whatList.length > 0){
int dataId = getNextSelectDataID(whatList.length);
itemFilters.add(new Pair<Integer, FilterByItemSelect>(dataId,
(FilterByItemSelect)filterSelect));
}
}
from = chomaAddString(from, res.fromStrs);
if (res.whereStr != null){
if (where == null){
where = res.whereStr;
}
else{
where = "(" + where + ")" + res.whereStr;
}
}
if (res.paramiterMap != null) {
param.putAll(res.paramiterMap);
}
}
}
String sql = "select " + what + " from " + from;
if (where != null){
sql += " where " + where;
}
Query qery = manager.createQuery(sql);
for (Map.Entry<String, Object> entry : param.entrySet()) {
qery.setParameter(entry.getKey(), entry.getValue());
}
@SuppressWarnings("unchecked")
List<Object[]> target = qery.getResultList();
return target;
}
protected abstract int getSelectDataCount();
public int getNextSelectDataID(int count) {
if (filteSelectDataID < 0){
filteSelectDataID = 0;
}
int res = filteSelectDataID;
filteSelectDataID += count;
return res;
}
protected boolean filtersPresent(){
return itemFilters != null;
}
protected boolean isAcceptedByFilters(Object[] extradata){
if (itemFilters == null){
return true;
}
for (int i = 0; i < itemFilters.size(); i++) {
Pair<Integer, FilterByItemSelect> pair = itemFilters.get(i);
if (!pair.second.filterItem(extradata, pair.first)){
return false;
}
}
return true;
}
@Override
public int nextID() {
return filterID++;
}
@Override
public String getDiseasePath() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public String getSerrenPath() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public String getClientPath() {
throw new UnsupportedOperationException("Not supported yet.");
}
}