[Major] Refactored ReportFilterPolicy for easier extension
This commit is contained in:
parent
20632279f9
commit
c052911d71
|
@ -1,40 +1,20 @@
|
||||||
package li.strolch.report.policy;
|
package li.strolch.report.policy;
|
||||||
|
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
import li.strolch.model.parameter.Parameter;
|
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
|
import li.strolch.utils.ObjectHelper;
|
||||||
|
|
||||||
public class EqualsReportFilter extends ReportFilterPolicy {
|
public class EqualsReportFilter extends ReportFilterPolicy {
|
||||||
|
|
||||||
private boolean negate;
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
public EqualsReportFilter(ComponentContainer container, StrolchTransaction tx) {
|
public EqualsReportFilter(ComponentContainer container, StrolchTransaction tx) {
|
||||||
super(container, tx);
|
super(container, tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(String value) {
|
protected boolean filter(Object left, Object right, boolean negate) {
|
||||||
if (value.startsWith("!")) {
|
if (negate)
|
||||||
this.negate = true;
|
return !ObjectHelper.equals(left, right, false);
|
||||||
this.value = value.substring(1);
|
|
||||||
} else {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean filter(Parameter<?> parameter) {
|
|
||||||
String value = parameter.getValueAsString();
|
|
||||||
return filter(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean filter(String value) {
|
|
||||||
if (this.negate)
|
|
||||||
return !this.value.equals(value);
|
|
||||||
else
|
else
|
||||||
return this.value.equals(value);
|
return ObjectHelper.equals(left, right, false);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -267,16 +267,7 @@ public class GenericReport extends ReportPolicy {
|
||||||
|
|
||||||
if (fieldRefP.getValue().startsWith("$")) {
|
if (fieldRefP.getValue().startsWith("$")) {
|
||||||
Object value = evaluateColumnValue(fieldRefP, row);
|
Object value = evaluateColumnValue(fieldRefP, row);
|
||||||
|
if (!filterPolicy.filter(value))
|
||||||
String columnValue;
|
|
||||||
if (value instanceof Date)
|
|
||||||
columnValue = ISO8601FormatFactory.getInstance().formatDate((Date) value);
|
|
||||||
else if (value instanceof Parameter)
|
|
||||||
columnValue = ((Parameter) value).getValueAsString();
|
|
||||||
else
|
|
||||||
columnValue = value.toString();
|
|
||||||
|
|
||||||
if (!filterPolicy.filter(columnValue))
|
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
Optional<Parameter<?>> param = lookupParameter(fieldRefP, column);
|
Optional<Parameter<?>> param = lookupParameter(fieldRefP, column);
|
||||||
|
|
|
@ -1,41 +1,20 @@
|
||||||
package li.strolch.report.policy;
|
package li.strolch.report.policy;
|
||||||
|
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
import li.strolch.model.parameter.FloatParameter;
|
|
||||||
import li.strolch.model.parameter.Parameter;
|
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
|
import li.strolch.utils.ObjectHelper;
|
||||||
|
|
||||||
public class GreaterThanReportFilter extends ReportFilterPolicy {
|
public class GreaterThanReportFilter extends ReportFilterPolicy {
|
||||||
|
|
||||||
private boolean negate;
|
|
||||||
private double value;
|
|
||||||
|
|
||||||
public GreaterThanReportFilter(ComponentContainer container, StrolchTransaction tx) {
|
public GreaterThanReportFilter(ComponentContainer container, StrolchTransaction tx) {
|
||||||
super(container, tx);
|
super(container, tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(String value) {
|
protected boolean filter(Object left, Object right, boolean negate) {
|
||||||
if (value.startsWith("!")) {
|
if (negate)
|
||||||
this.negate = true;
|
return ObjectHelper.compare(left, right, false) > 0;
|
||||||
this.value = Double.parseDouble(value.substring(1));
|
|
||||||
} else {
|
|
||||||
this.value = Double.parseDouble(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean filter(Parameter<?> parameter) {
|
|
||||||
|
|
||||||
FloatParameter floatP = (FloatParameter) parameter;
|
|
||||||
if (this.negate)
|
|
||||||
return floatP.getValue().doubleValue() <= this.value;
|
|
||||||
else
|
else
|
||||||
return floatP.getValue().doubleValue() > this.value;
|
return ObjectHelper.compare(right, left, false) > 0;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean filter(String value) {
|
|
||||||
throw new UnsupportedOperationException("Greater Than not supported on string!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package li.strolch.report.policy;
|
package li.strolch.report.policy;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
import li.strolch.model.parameter.Parameter;
|
import li.strolch.model.parameter.Parameter;
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
import li.strolch.policy.StrolchPolicy;
|
import li.strolch.policy.StrolchPolicy;
|
||||||
|
import li.strolch.utils.iso8601.ISO8601FormatFactory;
|
||||||
|
|
||||||
public abstract class ReportFilterPolicy extends StrolchPolicy {
|
public abstract class ReportFilterPolicy extends StrolchPolicy {
|
||||||
|
|
||||||
|
@ -11,11 +14,65 @@ public abstract class ReportFilterPolicy extends StrolchPolicy {
|
||||||
super(container, tx);
|
super(container, tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void init(String value);
|
private boolean negate;
|
||||||
|
private String filterValue;
|
||||||
|
private Object left;
|
||||||
|
|
||||||
public abstract boolean filter(String value);
|
public boolean isNegate() {
|
||||||
|
return this.negate;
|
||||||
|
}
|
||||||
|
|
||||||
public abstract boolean filter(Parameter<?> param);
|
public void setNegate(boolean negate) {
|
||||||
|
this.negate = negate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFilterValue(String filterValue) {
|
||||||
|
this.filterValue = filterValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getLeft() {
|
||||||
|
return this.left;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLeft(Object left) {
|
||||||
|
this.left = left;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init(String value) {
|
||||||
|
if (value.startsWith("!")) {
|
||||||
|
setNegate(true);
|
||||||
|
setFilterValue(value.substring(1));
|
||||||
|
} else {
|
||||||
|
setFilterValue(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean filter(Object value) {
|
||||||
|
|
||||||
|
Object right;
|
||||||
|
if (value instanceof Date) {
|
||||||
|
|
||||||
|
if (this.left == null)
|
||||||
|
this.left = ISO8601FormatFactory.getInstance().parseDate(this.filterValue);
|
||||||
|
right = value;
|
||||||
|
|
||||||
|
} else if (value instanceof Parameter) {
|
||||||
|
|
||||||
|
Parameter parameter = (Parameter) value;
|
||||||
|
if (this.left == null)
|
||||||
|
this.left = parameter.getValueType().parseValue(this.filterValue);
|
||||||
|
right = parameter.getValue();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if (this.left == null)
|
||||||
|
this.left = this.filterValue;
|
||||||
|
right = value.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return filter(this.left, right, this.negate);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract boolean filter(Object left, Object right, boolean negate);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo() {
|
public void undo() {
|
||||||
|
|
Loading…
Reference in New Issue