[New] Implemented param null selector and ParameterBagSelector

This commit is contained in:
Robert von Burg 2014-09-04 20:30:28 +02:00
parent dc45e3ec8c
commit 00ea65ece2
4 changed files with 136 additions and 2 deletions

View File

@ -26,18 +26,22 @@ import li.strolch.model.query.IdSelection;
import li.strolch.model.query.NameSelection;
import li.strolch.model.query.NotSelection;
import li.strolch.model.query.OrSelection;
import li.strolch.model.query.ParameterBagSelection;
import li.strolch.model.query.ParameterBagSelection.NullParameterBagSelection;
import li.strolch.model.query.ParameterSelection.BooleanParameterSelection;
import li.strolch.model.query.ParameterSelection.DateParameterSelection;
import li.strolch.model.query.ParameterSelection.DateRangeParameterSelection;
import li.strolch.model.query.ParameterSelection.FloatParameterSelection;
import li.strolch.model.query.ParameterSelection.IntegerParameterSelection;
import li.strolch.model.query.ParameterSelection.LongParameterSelection;
import li.strolch.model.query.ParameterSelection.NullParameterSelection;
import li.strolch.model.query.ParameterSelection.StringListParameterSelection;
import li.strolch.model.query.ParameterSelection.StringParameterSelection;
import li.strolch.model.query.ParameterSelectionVisitor;
import li.strolch.model.query.Selection;
import li.strolch.model.query.StrolchElementSelectionVisitor;
import li.strolch.model.query.StrolchRootElementSelectionVisitor;
import li.strolch.persistence.api.StrolchDao;
import li.strolch.runtime.query.inmemory.ParameterBagSelector.NullParameterBagSelector;
import li.strolch.runtime.query.inmemory.ParameterSelector.StringParameterSelector;
import ch.eitchnet.utils.dbc.DBC;
@ -45,7 +49,7 @@ import ch.eitchnet.utils.dbc.DBC;
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public abstract class InMemoryQueryVisitor<T extends GroupedParameterizedElement, S extends StrolchDao<?>> implements
StrolchElementSelectionVisitor, ParameterSelectionVisitor {
StrolchRootElementSelectionVisitor, ParameterSelectionVisitor {
private Navigator<T> navigator;
private List<Selector<T>> selectors;
@ -195,4 +199,19 @@ public abstract class InMemoryQueryVisitor<T extends GroupedParameterizedElement
addSelector(ParameterSelector.<T> stringListSelector(selection.getBagKey(), selection.getParamKey(),
selection.getValue()));
}
@Override
public void visit(NullParameterSelection selection) {
addSelector(ParameterSelector.<T> nullSelector(selection.getBagKey(), selection.getParamKey()));
}
@Override
public void visit(NullParameterBagSelection selection) {
addSelector(new NullParameterBagSelector<T>(selection.getBagKey()));
}
@Override
public void visit(ParameterBagSelection selection) {
addSelector(new ParameterBagSelector<T>(selection.getBagKey()));
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.runtime.query.inmemory;
import li.strolch.model.GroupedParameterizedElement;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ParameterBagSelector<T extends GroupedParameterizedElement> implements Selector<T> {
protected String bagKey;
public ParameterBagSelector(String bagKey) {
this.bagKey = bagKey;
}
@Override
public boolean select(T element) {
return element.hasParameterBag(this.bagKey);
}
public static class NullParameterBagSelector<T extends GroupedParameterizedElement> extends ParameterBagSelector<T> {
public NullParameterBagSelector(String bagKey) {
super(bagKey);
}
@Override
public boolean select(T element) {
return !element.hasParameterBag(this.bagKey);
}
}
}

View File

@ -85,6 +85,29 @@ public abstract class ParameterSelector<T extends GroupedParameterizedElement> i
return new StringListParameterSelector<>(bagKey, paramKey, value);
}
public static <T extends GroupedParameterizedElement> NullParameterSelector<T> nullSelector(String bagKey,
String paramKey) {
return new NullParameterSelector<>(bagKey, paramKey);
}
public static class NullParameterSelector<T extends GroupedParameterizedElement> extends ParameterSelector<T> {
public NullParameterSelector(String bagKey, String key) {
super(bagKey, key);
}
@Override
public boolean select(GroupedParameterizedElement element) {
ParameterBag bag = element.getParameterBag(this.bagKey);
if (bag == null) {
return false;
}
return !bag.hasParameter(this.paramKey);
}
}
public static class StringParameterSelector<T extends GroupedParameterizedElement> extends ParameterSelector<T> {
private StringMatchMode matchMode;

View File

@ -145,6 +145,51 @@ public class InMemoryQueryTest {
assertEquals(1, result.size());
}
@Test
public void shouldQueryByNullParameter1() {
List<Resource> resources = getResources();
resources.add(getBallResource());
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery.and().with( //
ParameterSelection.nullSelection("parameters", "color"));
List<Resource> result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
assertEquals(0, result.size());
}
@Test
public void shouldQueryByNullParameter2() {
List<Resource> resources = getResources();
resources.add(getBallResource());
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery.and().with( //
ParameterSelection.nullSelection("parameters", "weight"));
List<Resource> result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
assertEquals(1, result.size());
}
@Test
public void shouldQueryByNullParameter3() {
List<Resource> resources = getResources();
resources.add(getBallResource());
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery.and().with( //
ParameterSelection.nullSelection("parameters", "weight"));
List<Resource> result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
assertEquals(1, result.size());
}
private Resource getBallResource() {
Resource res1 = new Resource("childrensBall", "Ball 1", "Ball");
ParameterBag bag = new ParameterBag("parameters", "Ball Details", "Parameters");