[New] modified the Selection to have a builder pattern
Further modified the StringParameterSelection to allow for contains and case insensitive querying
This commit is contained in:
parent
743e1c039b
commit
368f8f14a4
|
@ -20,15 +20,30 @@ import java.util.List;
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*/
|
*/
|
||||||
public class AndSelection<T extends Selection> extends BooleanSelection<T> {
|
public class AndSelection extends BooleanSelection {
|
||||||
|
|
||||||
|
public AndSelection() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public AndSelection(Selection... selections) {
|
||||||
|
super(selections);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param selections
|
* @param selections
|
||||||
*/
|
*/
|
||||||
public AndSelection(List<T> selections) {
|
public AndSelection(List<Selection> selections) {
|
||||||
super(selections);
|
super(selections);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AndSelection with(Selection selection) {
|
||||||
|
super.with(selection);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public void accept(QueryVisitor visitor) {
|
public void accept(QueryVisitor visitor) {
|
||||||
visitor.visitAnd(this);
|
visitor.visitAnd(this);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,23 +15,37 @@
|
||||||
*/
|
*/
|
||||||
package li.strolch.model.query;
|
package li.strolch.model.query;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public abstract class BooleanSelection<T extends Selection> implements Selection {
|
public abstract class BooleanSelection implements Selection {
|
||||||
|
|
||||||
protected List<T> selections;
|
protected List<Selection> selections;
|
||||||
|
|
||||||
public BooleanSelection(List<T> selections) {
|
public BooleanSelection() {
|
||||||
|
this.selections = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanSelection(List<Selection> selections) {
|
||||||
this.selections = selections;
|
this.selections = selections;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<T> getSelections() {
|
public BooleanSelection(Selection... selections) {
|
||||||
|
this.selections = Arrays.asList(selections);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Selection> getSelections() {
|
||||||
return this.selections;
|
return this.selections;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BooleanSelection with(Selection selection) {
|
||||||
|
this.selections.add(selection);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public abstract void accept(QueryVisitor visitor);
|
public abstract void accept(QueryVisitor visitor);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,14 +19,28 @@ import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class OrSelection<T extends Selection> extends BooleanSelection<T> {
|
public class OrSelection extends BooleanSelection {
|
||||||
|
|
||||||
public OrSelection(List<T> selections) {
|
public OrSelection() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrSelection(List<Selection> selections) {
|
||||||
super(selections);
|
super(selections);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public OrSelection(Selection... selections) {
|
||||||
|
super(selections);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OrSelection with(Selection selection) {
|
||||||
|
super.with(selection);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(QueryVisitor visitor) {
|
public void accept(QueryVisitor visitor) {
|
||||||
visitor.visitOr(this);
|
visitor.visitOr(this);
|
||||||
|
|
|
@ -52,37 +52,39 @@ public abstract class ParameterSelection implements Selection {
|
||||||
|
|
||||||
public abstract void accept(ParameterSelectionVisitor visitor);
|
public abstract void accept(ParameterSelectionVisitor visitor);
|
||||||
|
|
||||||
public static ParameterSelection stringSelection(String bagKey, String paramKey, String value) {
|
public static StringParameterSelection stringSelection(String bagKey, String paramKey, String value) {
|
||||||
return new StringParameterSelection(bagKey, paramKey, value);
|
return new StringParameterSelection(bagKey, paramKey, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ParameterSelection integerSelection(String bagKey, String paramKey, int value) {
|
public static IntegerParameterSelection integerSelection(String bagKey, String paramKey, int value) {
|
||||||
return new IntegerParameterSelection(bagKey, paramKey, value);
|
return new IntegerParameterSelection(bagKey, paramKey, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ParameterSelection booleanSelection(String bagKey, String paramKey, boolean value) {
|
public static BooleanParameterSelection booleanSelection(String bagKey, String paramKey, boolean value) {
|
||||||
return new BooleanParameterSelection(bagKey, paramKey, value);
|
return new BooleanParameterSelection(bagKey, paramKey, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ParameterSelection floatSelection(String bagKey, String paramKey, double value) {
|
public static FloatParameterSelection floatSelection(String bagKey, String paramKey, double value) {
|
||||||
return new FloatParameterSelection(bagKey, paramKey, value);
|
return new FloatParameterSelection(bagKey, paramKey, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ParameterSelection longSelection(String bagKey, String paramKey, long value) {
|
public static LongParameterSelection longSelection(String bagKey, String paramKey, long value) {
|
||||||
return new LongParameterSelection(bagKey, paramKey, value);
|
return new LongParameterSelection(bagKey, paramKey, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ParameterSelection dateSelection(String bagKey, String paramKey, Date value) {
|
public static DateParameterSelection dateSelection(String bagKey, String paramKey, Date value) {
|
||||||
return new DateParameterSelection(bagKey, paramKey, value);
|
return new DateParameterSelection(bagKey, paramKey, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ParameterSelection stringListSelection(String bagKey, String paramKey, List<String> value) {
|
public static StringListParameterSelection stringListSelection(String bagKey, String paramKey, List<String> value) {
|
||||||
return new StringListParameterSelection(bagKey, paramKey, value);
|
return new StringListParameterSelection(bagKey, paramKey, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class StringParameterSelection extends ParameterSelection {
|
public static class StringParameterSelection extends ParameterSelection {
|
||||||
|
|
||||||
private String value;
|
private String value;
|
||||||
|
private boolean contains;
|
||||||
|
private boolean caseInsensitive;
|
||||||
|
|
||||||
public StringParameterSelection(String bagKey, String paramKey, String value) {
|
public StringParameterSelection(String bagKey, String paramKey, String value) {
|
||||||
super(bagKey, paramKey);
|
super(bagKey, paramKey);
|
||||||
|
@ -93,6 +95,24 @@ public abstract class ParameterSelection implements Selection {
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isContains() {
|
||||||
|
return this.contains;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCaseInsensitive() {
|
||||||
|
return this.caseInsensitive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringParameterSelection contains(boolean contains) {
|
||||||
|
this.contains = contains;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringParameterSelection caseInsensitive(boolean caseInsensitive) {
|
||||||
|
this.caseInsensitive = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(ParameterSelectionVisitor visitor) {
|
public void accept(ParameterSelectionVisitor visitor) {
|
||||||
visitor.visit(this);
|
visitor.visit(this);
|
||||||
|
|
|
@ -17,11 +17,10 @@ package li.strolch.model.query;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface QueryVisitor {
|
public interface QueryVisitor {
|
||||||
|
|
||||||
public <T extends Selection> void visitAnd(AndSelection<T> andSelection);
|
public void visitAnd(AndSelection andSelection);
|
||||||
|
|
||||||
public <T extends Selection> void visitOr(OrSelection<T> orSelection);
|
public void visitOr(OrSelection orSelection);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ package li.strolch.model.query;
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*/
|
*/
|
||||||
public interface StrolchElementSelectionVisitor extends QueryVisitor{
|
public interface StrolchElementSelectionVisitor extends QueryVisitor {
|
||||||
|
|
||||||
public void visit(StrolchTypeNavigation navigation);
|
public void visit(StrolchTypeNavigation navigation);
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ public abstract class StrolchQuery<T extends QueryVisitor> {
|
||||||
this.selections = new ArrayList<>();
|
this.selections = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSelection(BooleanSelection<Selection> selection) {
|
public void addSelection(BooleanSelection selection) {
|
||||||
this.selections.add(selection);
|
this.selections.add(selection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,21 +44,21 @@ public abstract class AbstractToSaxWriterVisitor {
|
||||||
writeStartStrolchElement(tag, isEmpty, element);
|
writeStartStrolchElement(tag, isEmpty, element);
|
||||||
if (!isEmpty) {
|
if (!isEmpty) {
|
||||||
writeParameterBags(element);
|
writeParameterBags(element);
|
||||||
writer.writeEndElement();
|
this.writer.writeEndElement();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void writeStartStrolchElement(String tag, boolean empty, StrolchElement element)
|
protected void writeStartStrolchElement(String tag, boolean empty, StrolchElement element)
|
||||||
throws XMLStreamException {
|
throws XMLStreamException {
|
||||||
if (empty)
|
if (empty)
|
||||||
writer.writeEmptyElement(tag);
|
this.writer.writeEmptyElement(tag);
|
||||||
else
|
else
|
||||||
writer.writeStartElement(tag);
|
this.writer.writeStartElement(tag);
|
||||||
|
|
||||||
writer.writeAttribute(Tags.ID, element.getId());
|
this.writer.writeAttribute(Tags.ID, element.getId());
|
||||||
if (!StringHelper.isEmpty(element.getName()))
|
if (!StringHelper.isEmpty(element.getName()))
|
||||||
writer.writeAttribute(Tags.NAME, element.getName());
|
this.writer.writeAttribute(Tags.NAME, element.getName());
|
||||||
writer.writeAttribute(Tags.TYPE, element.getType());
|
this.writer.writeAttribute(Tags.TYPE, element.getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void writeParameters(ParameterizedElement element) throws XMLStreamException {
|
protected void writeParameters(ParameterizedElement element) throws XMLStreamException {
|
||||||
|
@ -70,13 +70,13 @@ public abstract class AbstractToSaxWriterVisitor {
|
||||||
writeStartStrolchElement(Tags.PARAMETER, true, parameter);
|
writeStartStrolchElement(Tags.PARAMETER, true, parameter);
|
||||||
|
|
||||||
if (!Parameter.INTERPRETATION_NONE.equals(parameter.getInterpretation()))
|
if (!Parameter.INTERPRETATION_NONE.equals(parameter.getInterpretation()))
|
||||||
writer.writeAttribute(Tags.INTERPRETATION, parameter.getInterpretation());
|
this.writer.writeAttribute(Tags.INTERPRETATION, parameter.getInterpretation());
|
||||||
if (!Parameter.UOM_NONE.equals(parameter.getUom()))
|
if (!Parameter.UOM_NONE.equals(parameter.getUom()))
|
||||||
writer.writeAttribute(Tags.UOM, parameter.getUom());
|
this.writer.writeAttribute(Tags.UOM, parameter.getUom());
|
||||||
if (parameter.isHidden())
|
if (parameter.isHidden())
|
||||||
writer.writeAttribute(Tags.HIDDEN, Boolean.toString(parameter.isHidden()));
|
this.writer.writeAttribute(Tags.HIDDEN, Boolean.toString(parameter.isHidden()));
|
||||||
|
|
||||||
writer.writeAttribute(Tags.VALUE, parameter.getValueAsString());
|
this.writer.writeAttribute(Tags.VALUE, parameter.getValueAsString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +89,7 @@ public abstract class AbstractToSaxWriterVisitor {
|
||||||
writeStartStrolchElement(Tags.PARAMETER_BAG, isEmpty, parameterBag);
|
writeStartStrolchElement(Tags.PARAMETER_BAG, isEmpty, parameterBag);
|
||||||
if (!isEmpty) {
|
if (!isEmpty) {
|
||||||
writeParameters(parameterBag);
|
writeParameters(parameterBag);
|
||||||
writer.writeEndElement();
|
this.writer.writeEndElement();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ public class OrderToSaxWriterVisitor extends AbstractToSaxWriterVisitor implemen
|
||||||
try {
|
try {
|
||||||
writeElement(Tags.ORDER, order);
|
writeElement(Tags.ORDER, order);
|
||||||
} catch (XMLStreamException e) {
|
} catch (XMLStreamException e) {
|
||||||
String msg = "Failed to write Order {0} due to {1}";
|
String msg = "Failed to write Order {0} due to {1}"; //$NON-NLS-1$
|
||||||
msg = MessageFormat.format(msg, order.getLocator(), e.getMessage());
|
msg = MessageFormat.format(msg, order.getLocator(), e.getMessage());
|
||||||
throw new StrolchException(msg, e);
|
throw new StrolchException(msg, e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ public class ResourceToSaxWriterVisitor extends AbstractToSaxWriterVisitor imple
|
||||||
try {
|
try {
|
||||||
writeElement(Tags.RESOURCE, resource);
|
writeElement(Tags.RESOURCE, resource);
|
||||||
} catch (XMLStreamException e) {
|
} catch (XMLStreamException e) {
|
||||||
String msg = "Failed to write Resource {0} due to {1}";
|
String msg = "Failed to write Resource {0} due to {1}"; //$NON-NLS-1$
|
||||||
msg = MessageFormat.format(msg, resource.getLocator(), e.getMessage());
|
msg = MessageFormat.format(msg, resource.getLocator(), e.getMessage());
|
||||||
throw new StrolchException(msg, e);
|
throw new StrolchException(msg, e);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue