[New] Added a new parameter: DurationParameter of type "Duration"
The parameter is implemented in all visitors and also persistence layer postgre.
This commit is contained in:
parent
956d61f32e
commit
d7c6025c59
|
@ -31,6 +31,7 @@ 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.DurationParameterSelection;
|
||||
import li.strolch.model.query.ParameterSelection.FloatParameterSelection;
|
||||
import li.strolch.model.query.ParameterSelection.IntegerParameterSelection;
|
||||
import li.strolch.model.query.ParameterSelection.LongParameterSelection;
|
||||
|
@ -188,6 +189,12 @@ public abstract class InMemoryQueryVisitor<T extends GroupedParameterizedElement
|
|||
selection.getValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(DurationParameterSelection selection) {
|
||||
addSelector(ParameterSelector.<T> durationSelector(selection.getBagKey(), selection.getParamKey(),
|
||||
selection.getValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(DateRangeParameterSelection selection) {
|
||||
addSelector(ParameterSelector.<T> dateRangeSelector(selection.getBagKey(), selection.getParamKey(),
|
||||
|
|
|
@ -22,6 +22,7 @@ import li.strolch.model.GroupedParameterizedElement;
|
|||
import li.strolch.model.ParameterBag;
|
||||
import li.strolch.model.parameter.BooleanParameter;
|
||||
import li.strolch.model.parameter.DateParameter;
|
||||
import li.strolch.model.parameter.DurationParameter;
|
||||
import li.strolch.model.parameter.FloatParameter;
|
||||
import li.strolch.model.parameter.IntegerParameter;
|
||||
import li.strolch.model.parameter.LongParameter;
|
||||
|
@ -75,6 +76,11 @@ public abstract class ParameterSelector<T extends GroupedParameterizedElement> i
|
|||
return new DateParameterSelector<>(bagKey, paramKey, value);
|
||||
}
|
||||
|
||||
public static <T extends GroupedParameterizedElement> DurationParameterSelector<T> durationSelector(String bagKey,
|
||||
String paramKey, Long value) {
|
||||
return new DurationParameterSelector<>(bagKey, paramKey, value);
|
||||
}
|
||||
|
||||
public static <T extends GroupedParameterizedElement> DateRangeParameterSelector<T> dateRangeSelector(
|
||||
String bagKey, String paramKey, Date from, Date to) {
|
||||
return new DateRangeParameterSelector<>(bagKey, paramKey, from, to);
|
||||
|
@ -304,4 +310,27 @@ public abstract class ParameterSelector<T extends GroupedParameterizedElement> i
|
|||
return param.getValue().equals(this.value);
|
||||
}
|
||||
}
|
||||
|
||||
public static class DurationParameterSelector<T extends GroupedParameterizedElement> extends ParameterSelector<T> {
|
||||
|
||||
private Long value;
|
||||
|
||||
public DurationParameterSelector(String bagKey, String paramKey, Long value) {
|
||||
super(bagKey, paramKey);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean select(GroupedParameterizedElement element) {
|
||||
if (!element.hasParameterBag(this.bagKey))
|
||||
return false;
|
||||
|
||||
ParameterBag bag = element.getParameterBag(this.bagKey);
|
||||
if (!bag.hasParameter(this.paramKey))
|
||||
return false;
|
||||
|
||||
DurationParameter param = bag.getParameter(this.paramKey);
|
||||
return param.getValue().equals(this.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ import li.strolch.model.audit.AccessType;
|
|||
import li.strolch.model.audit.Audit;
|
||||
import li.strolch.model.parameter.BooleanParameter;
|
||||
import li.strolch.model.parameter.DateParameter;
|
||||
import li.strolch.model.parameter.DurationParameter;
|
||||
import li.strolch.model.parameter.FloatParameter;
|
||||
import li.strolch.model.parameter.IntegerParameter;
|
||||
import li.strolch.model.parameter.LongParameter;
|
||||
|
@ -45,6 +46,7 @@ import li.strolch.model.timevalue.impl.IntegerValue;
|
|||
import li.strolch.model.timevalue.impl.StringSetValue;
|
||||
import li.strolch.model.timevalue.impl.ValueChange;
|
||||
import ch.eitchnet.utils.helper.StringHelper;
|
||||
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
|
||||
|
||||
/**
|
||||
* Class which can be used to generate objects which implement {@link StrolchElement}. These generated classes can then
|
||||
|
@ -76,6 +78,9 @@ public class ModelGenerator {
|
|||
public static final String PARAM_LIST_STRING_ID = "@param7";
|
||||
public static final String PARAM_LIST_STRING_NAME = "StringList Param";
|
||||
|
||||
public static final String PARAM_DURATION_ID = "@param8";
|
||||
public static final String PARAM_DURATION_NAME = "Duration Param";
|
||||
|
||||
public static final String STATE_FLOAT_ID = "@state1";
|
||||
public static final String STATE_FLOAT_NAME = "Float State";
|
||||
|
||||
|
@ -371,6 +376,11 @@ public class ModelGenerator {
|
|||
stringList);
|
||||
stringListP.setIndex(7);
|
||||
bag.addParameter(stringListP);
|
||||
|
||||
DurationParameter durationParam = new DurationParameter(PARAM_DURATION_ID, PARAM_DURATION_NAME,
|
||||
ISO8601FormatFactory.getInstance().getDurationFormat().parse("P1D"));
|
||||
durationParam.setIndex(8);
|
||||
bag.addParameter(durationParam);
|
||||
}
|
||||
|
||||
private static String randomValue(Random rand, String[] values) {
|
||||
|
|
|
@ -28,6 +28,7 @@ import li.strolch.exception.StrolchException;
|
|||
import li.strolch.model.Locator.LocatorBuilder;
|
||||
import li.strolch.model.parameter.BooleanParameter;
|
||||
import li.strolch.model.parameter.DateParameter;
|
||||
import li.strolch.model.parameter.DurationParameter;
|
||||
import li.strolch.model.parameter.FloatParameter;
|
||||
import li.strolch.model.parameter.IntegerParameter;
|
||||
import li.strolch.model.parameter.LongParameter;
|
||||
|
@ -232,6 +233,9 @@ public abstract class ParameterizedElement extends AbstractStrolchElement {
|
|||
} else if (paramtype.equals(DateParameter.TYPE)) {
|
||||
DateParameter param = new DateParameter(paramElement);
|
||||
addParameter(param);
|
||||
} else if (paramtype.equals(DurationParameter.TYPE)) {
|
||||
DurationParameter param = new DurationParameter(paramElement);
|
||||
addParameter(param);
|
||||
} else if (paramtype.equals(BooleanParameter.TYPE)) {
|
||||
BooleanParameter param = new BooleanParameter(paramElement);
|
||||
addParameter(param);
|
||||
|
|
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* 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.model.parameter;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
|
||||
import li.strolch.exception.StrolchException;
|
||||
import li.strolch.model.Tags;
|
||||
import li.strolch.model.visitor.ParameterVisitor;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import ch.eitchnet.utils.helper.StringHelper;
|
||||
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class DurationParameter extends AbstractParameter<Long> {
|
||||
|
||||
public static final String TYPE = "Duration"; //$NON-NLS-1$
|
||||
private static final long serialVersionUID = 0L;
|
||||
|
||||
private Long value;
|
||||
|
||||
/**
|
||||
* Empty constructor
|
||||
*/
|
||||
public DurationParameter() {
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Default Constructor
|
||||
*
|
||||
* @param id
|
||||
* @param name
|
||||
* @param value
|
||||
*/
|
||||
public DurationParameter(String id, String name, Long value) {
|
||||
super(id, name);
|
||||
setValue(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* DOM Constructor
|
||||
*
|
||||
* @param element
|
||||
*/
|
||||
public DurationParameter(Element element) {
|
||||
super.fromDom(element);
|
||||
|
||||
String valueS = element.getAttribute(Tags.VALUE);
|
||||
if (StringHelper.isEmpty(valueS)) {
|
||||
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
|
||||
throw new StrolchException(msg);
|
||||
}
|
||||
|
||||
setValue(parseFromString(valueS));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValueAsString() {
|
||||
return ISO8601FormatFactory.getInstance().formatDuration(this.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Long value) {
|
||||
validateValue(value);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return DurationParameter.TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DurationParameter getClone() {
|
||||
DurationParameter clone = new DurationParameter();
|
||||
|
||||
super.fillClone(clone);
|
||||
|
||||
clone.setValue(this.value);
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <U> U accept(ParameterVisitor visitor) {
|
||||
return visitor.visitDurationParam(this);
|
||||
}
|
||||
|
||||
public static Long parseFromString(String valueS) {
|
||||
return ISO8601FormatFactory.getInstance().getDurationFormat().parse(valueS);
|
||||
}
|
||||
}
|
|
@ -20,6 +20,7 @@ import java.util.List;
|
|||
|
||||
import ch.eitchnet.utils.StringMatchMode;
|
||||
import ch.eitchnet.utils.dbc.DBC;
|
||||
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
|
@ -97,6 +98,15 @@ public abstract class ParameterSelection implements Selection {
|
|||
return new DateParameterSelection(bagKey, paramKey, value);
|
||||
}
|
||||
|
||||
public static DurationParameterSelection durationSelection(String bagKey, String paramKey, String valueAsString) {
|
||||
return durationSelection(bagKey, paramKey,
|
||||
ISO8601FormatFactory.getInstance().getDurationFormat().parse(valueAsString));
|
||||
}
|
||||
|
||||
public static DurationParameterSelection durationSelection(String bagKey, String paramKey, long value) {
|
||||
return new DurationParameterSelection(bagKey, paramKey, value);
|
||||
}
|
||||
|
||||
public static DateRangeParameterSelection dateRangeSelection(String bagKey, String paramKey, Date from, Date to) {
|
||||
return new DateRangeParameterSelection(bagKey, paramKey, from, to);
|
||||
}
|
||||
|
@ -267,6 +277,25 @@ public abstract class ParameterSelection implements Selection {
|
|||
}
|
||||
}
|
||||
|
||||
public static class DurationParameterSelection extends ParameterSelection {
|
||||
|
||||
private Long value;
|
||||
|
||||
public DurationParameterSelection(String bagKey, String paramKey, Long value) {
|
||||
super(bagKey, paramKey);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Long getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(ParameterSelectionVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
public static class StringListParameterSelection extends ParameterSelection {
|
||||
|
||||
private List<String> value;
|
||||
|
|
|
@ -18,6 +18,7 @@ package li.strolch.model.query;
|
|||
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.DurationParameterSelection;
|
||||
import li.strolch.model.query.ParameterSelection.FloatParameterSelection;
|
||||
import li.strolch.model.query.ParameterSelection.IntegerParameterSelection;
|
||||
import li.strolch.model.query.ParameterSelection.LongParameterSelection;
|
||||
|
@ -42,6 +43,8 @@ public interface ParameterSelectionVisitor extends QueryVisitor {
|
|||
|
||||
public void visit(DateParameterSelection selection);
|
||||
|
||||
public void visit(DurationParameterSelection selection);
|
||||
|
||||
public void visit(DateRangeParameterSelection selection);
|
||||
|
||||
public void visit(StringListParameterSelection selection);
|
||||
|
|
|
@ -15,10 +15,12 @@
|
|||
*/
|
||||
package li.strolch.model.query;
|
||||
|
||||
import li.strolch.model.visitor.StrolchVisitor;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
*/
|
||||
public interface QueryVisitor {
|
||||
public interface QueryVisitor extends StrolchVisitor {
|
||||
|
||||
public void visitAny();
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ package li.strolch.model.visitor;
|
|||
|
||||
import li.strolch.model.parameter.BooleanParameter;
|
||||
import li.strolch.model.parameter.DateParameter;
|
||||
import li.strolch.model.parameter.DurationParameter;
|
||||
import li.strolch.model.parameter.FloatParameter;
|
||||
import li.strolch.model.parameter.IntegerParameter;
|
||||
import li.strolch.model.parameter.ListParameter;
|
||||
|
@ -36,6 +37,8 @@ public interface ParameterVisitor {
|
|||
|
||||
public <T> T visitDateParam(DateParameter param);
|
||||
|
||||
public <T> T visitDurationParam(DurationParameter param);
|
||||
|
||||
public <T> T visitFloatParam(FloatParameter param);
|
||||
|
||||
public <T> T visitIntegerParam(IntegerParameter param);
|
||||
|
|
|
@ -17,6 +17,7 @@ package li.strolch.model.visitor;
|
|||
|
||||
import li.strolch.model.parameter.BooleanParameter;
|
||||
import li.strolch.model.parameter.DateParameter;
|
||||
import li.strolch.model.parameter.DurationParameter;
|
||||
import li.strolch.model.parameter.FloatParameter;
|
||||
import li.strolch.model.parameter.IntegerParameter;
|
||||
import li.strolch.model.parameter.ListParameter;
|
||||
|
@ -63,6 +64,12 @@ public class SetParameterValueVisitor implements ParameterVisitor {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T visitDurationParam(DurationParameter param) {
|
||||
param.setValue(DurationParameter.parseFromString(this.value));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T visitFloatParam(FloatParameter param) {
|
||||
param.setValue(FloatParameter.parseFromString(this.value));
|
||||
|
@ -92,5 +99,4 @@ public class SetParameterValueVisitor implements ParameterVisitor {
|
|||
param.setValue(StringListParameter.parseFromString(this.value));
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ import li.strolch.model.State;
|
|||
import li.strolch.model.Tags;
|
||||
import li.strolch.model.parameter.BooleanParameter;
|
||||
import li.strolch.model.parameter.DateParameter;
|
||||
import li.strolch.model.parameter.DurationParameter;
|
||||
import li.strolch.model.parameter.FloatParameter;
|
||||
import li.strolch.model.parameter.IntegerParameter;
|
||||
import li.strolch.model.parameter.LongParameter;
|
||||
|
@ -130,6 +131,9 @@ public class XmlModelSaxReader extends DefaultHandler {
|
|||
break;
|
||||
|
||||
case Tags.PARAMETER:
|
||||
|
||||
// TODO refactor this code into using visitors
|
||||
|
||||
String paramId = attributes.getValue(Tags.ID);
|
||||
String paramName = attributes.getValue(Tags.NAME);
|
||||
String paramType = attributes.getValue(Tags.TYPE);
|
||||
|
@ -159,6 +163,9 @@ public class XmlModelSaxReader extends DefaultHandler {
|
|||
case DateParameter.TYPE:
|
||||
param = new DateParameter(paramId, paramName, DateParameter.parseFromString(paramValue));
|
||||
break;
|
||||
case DurationParameter.TYPE:
|
||||
param = new DurationParameter(paramId, paramName, DurationParameter.parseFromString(paramValue));
|
||||
break;
|
||||
case StringListParameter.TYPE:
|
||||
param = new StringListParameter(paramId, paramName, StringListParameter.parseFromString(paramValue));
|
||||
break;
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
<Order Id="MyTestOrder" Name="Test Name" Type="TestType" Date="2013-11-20T07:42:57.699+01:00" State="CREATED">
|
||||
<ParameterBag Id="@bag01" Name="Test Bag" Type="TestBag">
|
||||
<Parameter Id="@param8" Name="Duration Param" Type="Duration" Value="P1D" />
|
||||
<Parameter Id="@param7" Name="StringList Param" Type="StringList" Value="Hello;World" />
|
||||
<Parameter Id="@param6" Name="Date Param" Type="Date" Value="2012-11-30T18:12:05.628+01:00" />
|
||||
<Parameter Id="@param5" Name="String Param" Type="String" Value="Strolch" />
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
<Resource Id="MyTestResource" Name="Test Name" Type="TestType">
|
||||
<ParameterBag Id="@bag01" Name="Test Bag" Type="TestBag">
|
||||
<Parameter Id="@param8" Name="Duration Param" Type="Duration" Value="P1D" />
|
||||
<Parameter Id="@param7" Name="StringList Param" Type="StringList" Value="Hello;World" />
|
||||
<Parameter Id="@param6" Name="Date Param" Type="Date" Value="2012-11-30T18:12:05.628+01:00" />
|
||||
<Parameter Id="@param5" Name="String Param" Type="String" Value="Strolch" />
|
||||
|
|
|
@ -33,6 +33,7 @@ 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.DurationParameterSelection;
|
||||
import li.strolch.model.query.ParameterSelection.FloatParameterSelection;
|
||||
import li.strolch.model.query.ParameterSelection.IntegerParameterSelection;
|
||||
import li.strolch.model.query.ParameterSelection.LongParameterSelection;
|
||||
|
@ -301,6 +302,12 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
|
|||
ISO8601FormatFactory.getInstance().formatDate(selection.getValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(DurationParameterSelection selection) {
|
||||
xpath(selection.getBagKey(), selection.getParamKey(),
|
||||
ISO8601FormatFactory.getInstance().formatDuration(selection.getValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(NullParameterSelection selection) {
|
||||
String xpath = "cast(xpath('//Resource/ParameterBag[@Id=\"${bagKey}\"]/Parameter[@Id=\"${paramKey}\"]', asxml) as text[]) = '{}'\n";
|
||||
|
|
|
@ -328,6 +328,13 @@ public class QueryTest {
|
|||
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryResourceByDurationParam() throws SQLException {
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(ParameterSelection.durationSelection("@bag01", "@param8", "P1D"));
|
||||
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryResourceByNullParam1() throws SQLException {
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
|
||||
|
|
Loading…
Reference in New Issue