use of de.janrufmonitor.repository.filter.YearFilter in project janrufmonitor by tbrandt77.
the class HsqldbCallDatabaseHandler method prepareStatement.
private String prepareStatement(IFilter[] filters, int count, int offset, boolean isCounter, ISearchTerm[] searchTerms) {
StringBuffer sql = new StringBuffer();
// build SQL statement
sql.append("SELECT");
if (filters.length == 1 && filters[0] == null)
filters = null;
// 2008/11/28: optimized attribute filter call
if (filters != null && filters.length > 0 && hasOnlyAttributeFilter(filters)) {
if (isCounter) {
sql.append(" COUNT(calls.uuid) ");
} else {
sql.append(" content ");
}
sql.append("FROM attributes LEFT JOIN calls ON calls.uuid=attributes.ref AND ");
IFilter f = null;
for (int i = 0; i < filters.length; i++) {
f = filters[i];
if (f.getType() == FilterType.ATTRIBUTE) {
IAttributeMap m = ((AttributeFilter) f).getAttributeMap();
if (m != null && m.size() > 0) {
sql.append("(");
sql.append("(");
Iterator iter = m.iterator();
IAttribute a = null;
while (iter.hasNext()) {
a = (IAttribute) iter.next();
sql.append("attributes.name='");
sql.append(a.getName());
sql.append("'");
sql.append(" AND ");
sql.append("attributes.value='");
sql.append(a.getValue());
sql.append("'");
if (iter.hasNext())
sql.append(" OR ");
}
sql.append("))");
}
}
}
if (searchTerms != null && searchTerms.length > 0) {
sql.append(" AND");
sql.append(createSearchTerm(searchTerms));
}
} else if (filters != null && filters.length > 0) {
int limit = -1;
for (int i = 0; i < filters.length; i++) {
if (filters[i] != null && filters[i].getType() == FilterType.ITEMCOUNT) {
limit = ((ItemCountFilter) filters[i]).getLimit();
if (!isCounter) {
// 2008/05/19: to be done since offset was introduced !!
count = limit;
offset = 0;
}
}
}
if (isCounter) {
sql.append(" COUNT(calls.uuid) ");
} else {
sql.append(" content ");
}
sql.append("FROM calls");
if (hasAttributeFilter(filters))
sql.append(", attributes");
if (filters.length == 1 && filters[0] != null && filters[0].getType() == FilterType.ITEMCOUNT)
sql.append("");
else
sql.append(" WHERE ");
IFilter f = null;
boolean isCallerFilter = false;
boolean isMsnFilter = false;
for (int i = 0; i < filters.length; i++) {
if (filters[i] != null && filters[i].getType() == FilterType.ITEMCOUNT) {
// limit=((ItemCountFilter)filters[i]).getLimit();
continue;
}
f = filters[i];
if (f == null)
continue;
if (isCallerFilter && (f.getType() == FilterType.CALLER || f.getType() == FilterType.PHONENUMBER)) {
if (i > 0)
sql.append(" OR ");
} else if (isMsnFilter && f.getType() == FilterType.MSN) {
if (i > 0)
sql.append(" OR ");
} else {
if (i > 0 && filters[i - 1].getType() != FilterType.ITEMCOUNT)
sql.append(" AND ");
}
if (f.getType() == FilterType.DATE) {
DateFilter df = (DateFilter) f;
sql.append("(calls.cdate>");
sql.append((df.getDateTo() == null ? new Date().getTime() : df.getDateTo().getTime()));
sql.append(" AND calls.cdate<");
sql.append((df.getDateFrom() == null ? new Date().getTime() : df.getDateFrom().getTime()));
sql.append(")");
}
if (f.getType() == FilterType.YEAR) {
YearFilter df = (YearFilter) f;
sql.append("(calls.cdate>");
sql.append((df.getDateFrom() == null ? new Date().getTime() : df.getDateFrom().getTime()));
sql.append(" AND calls.cdate<");
sql.append((df.getDateTo() == null ? new Date().getTime() : df.getDateTo().getTime()));
sql.append(")");
}
if (f.getType() == FilterType.MONTH_YEAR) {
MonthYearFilter df = (MonthYearFilter) f;
sql.append("(calls.cdate>");
sql.append((df.getDateFrom() == null ? new Date().getTime() : df.getDateFrom().getTime()));
sql.append(" AND calls.cdate<");
sql.append((df.getDateTo() == null ? new Date().getTime() : df.getDateTo().getTime()));
sql.append(")");
}
if (f.getType() == FilterType.CALLER) {
if (!isCallerFilter)
sql.append("(");
isCallerFilter = true;
ICaller c = (ICaller) f.getFilterObject();
IPhonenumber pn = c.getPhoneNumber();
sql.append("(calls.country='");
sql.append(pn.getIntAreaCode());
sql.append("' AND calls.areacode='");
sql.append(pn.getAreaCode());
sql.append("' AND calls.number='");
sql.append(pn.getCallNumber());
sql.append("')");
if (isCallerFilter && ((i + 1) >= filters.length || (filters[i + 1] != null && (filters[i + 1].getType() != FilterType.CALLER))))
sql.append(")");
}
if (f.getType() == FilterType.PHONENUMBER) {
if (!isCallerFilter)
sql.append("(");
isCallerFilter = true;
IPhonenumber pn = (IPhonenumber) f.getFilterObject();
sql.append("(calls.country='");
sql.append(pn.getIntAreaCode());
sql.append("' AND calls.areacode='");
sql.append(pn.getAreaCode());
sql.append("' AND calls.number='");
sql.append(pn.getCallNumber());
sql.append("')");
if (isCallerFilter && ((i + 1) >= filters.length || (filters[i + 1] != null && (filters[i + 1].getType() != FilterType.PHONENUMBER))))
sql.append(")");
}
if (f.getType() == FilterType.CIP) {
ICip cip = (ICip) f.getFilterObject();
sql.append("calls.cip='");
sql.append(cip.getCIP());
sql.append("'");
}
if (f.getType() == FilterType.MSN) {
IMsn[] msn = (IMsn[]) f.getFilterObject();
if (msn != null && msn.length > 0) {
if (!isMsnFilter)
sql.append("(");
isMsnFilter = true;
sql.append("(");
for (int j = 0; j < msn.length; j++) {
if (j > 0)
sql.append(" OR ");
sql.append("calls.msn='");
sql.append(msn[j].getMSN());
sql.append("'");
}
sql.append(")");
if (isMsnFilter && ((i + 1) >= filters.length || (filters[i + 1] != null && (filters[i + 1].getType() != FilterType.MSN))))
sql.append(")");
}
}
if (f.getType() == FilterType.UUID) {
String[] uuids = (String[]) f.getFilterObject();
if (uuids != null && uuids.length > 0) {
sql.append("(");
for (int j = 0; j < uuids.length; j++) {
if (j > 0)
sql.append(" OR ");
sql.append("calls.uuid='");
sql.append(uuids[i]);
sql.append("'");
}
sql.append(")");
}
}
if (f.getType() == FilterType.ATTRIBUTE) {
IAttributeMap m = ((AttributeFilter) f).getAttributeMap();
if (m != null && m.size() > 0) {
sql.append("(");
sql.append("calls.uuid=attributes.ref AND (");
Iterator iter = m.iterator();
IAttribute a = null;
while (iter.hasNext()) {
a = (IAttribute) iter.next();
sql.append("attributes.name='");
sql.append(a.getName());
sql.append("'");
sql.append(" AND ");
sql.append("attributes.value='");
sql.append(a.getValue());
sql.append("'");
if (iter.hasNext())
sql.append(" OR ");
}
sql.append("))");
}
}
}
if (searchTerms != null && searchTerms.length > 0) {
if (filters.length == 1 && filters[0] != null && filters[0].getType() == FilterType.ITEMCOUNT)
sql.append(" WHERE");
else
sql.append(" AND");
sql.append(createSearchTerm(searchTerms));
}
if (limit > 0 && !isCounter) {
sql.append(" ORDER BY cdate DESC");
}
} else {
if (isCounter) {
sql.append(" COUNT(calls.uuid) ");
} else {
sql.append(" content ");
}
sql.append("FROM calls");
if (count > 0 && offset >= 0) {
sql.append(" AS rtable");
}
if (searchTerms != null && searchTerms.length > 0) {
sql.append(" WHERE");
sql.append(createSearchTerm(searchTerms));
}
}
if (count > 0 && offset >= 0) {
sql.append(" LIMIT ");
sql.append(count);
sql.append(" OFFSET ");
sql.append(offset);
}
sql.append(";");
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info(sql.toString());
return sql.toString();
}
use of de.janrufmonitor.repository.filter.YearFilter in project janrufmonitor by tbrandt77.
the class AbstractFilterCallManager method filterCalls.
protected ICallList filterCalls(IFilter filter, ICallList cl) {
if (cl == null) {
cl = this.getInitialCallList(filter);
}
long start = System.currentTimeMillis();
this.m_logger.info("Start filtering with filter: " + filter);
this.m_logger.info("CallList size before filtering: " + cl.size());
ICall c = null;
if (filter != null) {
if (filter.getType().equals(FilterType.DATE) || filter.getType().equals(FilterType.YEAR) || filter.getType().equals(FilterType.MONTH_YEAR)) {
long from = 0;
long to = 0;
if (filter instanceof DateFilter) {
DateFilter df = (DateFilter) filter;
to = (df.getDateFrom() == null ? 0 : df.getDateFrom().getTime());
from = df.getDateTo().getTime();
}
if (filter instanceof YearFilter) {
YearFilter df = (YearFilter) filter;
from = (df.getDateFrom() == null ? 0 : df.getDateFrom().getTime());
to = df.getDateTo().getTime();
}
if (filter instanceof MonthYearFilter) {
MonthYearFilter df = (MonthYearFilter) filter;
from = (df.getDateFrom() == null ? 0 : df.getDateFrom().getTime());
to = df.getDateTo().getTime();
}
long cdate = 0;
for (int i = cl.size() - 1; i >= 0; i--) {
c = cl.get(i);
cdate = c.getDate().getTime();
if (to > 0) {
if (cdate < from || cdate > to)
cl.remove(c);
} else {
if (cdate < from)
cl.remove(c);
}
}
}
if (filter.getType().equals(FilterType.CALLER)) {
ICaller cfilter = (ICaller) filter.getFilterObject();
IPhonenumber pn = cfilter.getPhoneNumber();
for (int i = cl.size() - 1; i >= 0; i--) {
c = cl.get(i);
if (!c.getCaller().getPhoneNumber().equals(pn))
cl.remove(c);
}
}
if (filter.getType().equals(FilterType.PHONENUMBER)) {
IPhonenumber pn = (IPhonenumber) filter.getFilterObject();
for (int i = cl.size() - 1; i >= 0; i--) {
c = cl.get(i);
if (!c.getCaller().getPhoneNumber().equals(pn))
cl.remove(c);
}
}
if (filter.getType().equals(FilterType.CIP)) {
ICip cip = (ICip) filter.getFilterObject();
for (int i = cl.size() - 1; i >= 0; i--) {
c = cl.get(i);
if (!c.getCIP().equals(cip))
cl.remove(c);
}
}
if (filter.getType().equals(FilterType.MSN)) {
IMsn msn = (IMsn) filter.getFilterObject();
for (int i = cl.size() - 1; i >= 0; i--) {
c = cl.get(i);
if (!c.getMSN().equals(msn))
cl.remove(c);
}
}
if (filter.getType().equals(FilterType.UUID)) {
String[] uuids = (String[]) filter.getFilterObject();
for (int i = cl.size() - 1; i >= 0; i--) {
c = cl.get(i);
boolean hasUUID = false;
for (int j = 0; j < uuids.length; j++) {
if (c.getUUID().equalsIgnoreCase(uuids[j]))
hasUUID = true;
}
if (!hasUUID)
cl.remove(c);
}
}
if (filter.getType().equals(FilterType.ATTRIBUTE)) {
IAttributeMap m = ((AttributeFilter) filter).getAttributeMap();
if (m != null && m.size() > 0) {
Iterator iter = m.iterator();
IAttribute a = null;
while (iter.hasNext()) {
a = (IAttribute) iter.next();
IAttribute ua = null;
for (int i = cl.size() - 1; i >= 0; i--) {
c = cl.get(i);
ua = c.getAttribute(a.getName());
if (ua == null || !ua.getValue().equalsIgnoreCase(a.getValue())) {
cl.remove(c);
}
}
}
}
}
if (filter.getType().equals(FilterType.ITEMCOUNT)) {
int itemcount = ((ItemCountFilter) filter).getLimit();
if (itemcount > 0) {
ICallList tmpCl = this.getRuntime().getCallFactory().createCallList(itemcount);
// added: 2006/04/20: just a work-a-round
cl.sort(0, false);
for (int i = 0, j = Math.min(cl.size(), itemcount); i < j; i++) {
tmpCl.add(cl.get(i));
}
cl.clear();
cl.add(tmpCl);
}
}
}
this.m_logger.info("CallList size after filtering: " + cl.size());
this.m_logger.info("Finished filtering with filter <" + filter + "> in " + Long.toString(System.currentTimeMillis() - start) + " msec.");
return cl;
}
use of de.janrufmonitor.repository.filter.YearFilter in project janrufmonitor by tbrandt77.
the class JournalFilterManager method getRuntimeFilters.
@Override
public IFilter[][] getRuntimeFilters() {
String cfg = this.getRuntime().getConfigManagerFactory().getConfigManager().getProperty(Journal.NAMESPACE, "rt_filters_years");
int years = 1;
if (cfg != null && cfg.length() > 0) {
years = Integer.parseInt(cfg);
}
if (years < 0)
return new IFilter[][] {};
int c_y = Calendar.getInstance().get(Calendar.YEAR);
int c_m = Calendar.getInstance().get(Calendar.MONTH);
List filters = new ArrayList(years * 12 + years);
for (int y = c_y - years; y <= c_y; y++) {
filters.add(new YearFilter(y));
int max_month = 12;
if (y == c_y)
max_month = c_m + 1;
for (int m = 1; m <= max_month; m++) {
filters.add(new MonthYearFilter(y, m));
}
}
IFilter[][] filters2 = new IFilter[filters.size()][1];
for (int i = 0; i < filters2.length; i++) filters2[i][0] = (IFilter) filters.get(i);
return filters2;
}
Aggregations