JPA 2 criteria provide runtime type for gt operator - jpa

I am building a highly generic query mechanism on top of the JPA Criteria. I get as input an XML describing the query, something like this:
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<Criteria xmlns='criteria' maxResults='2'>
<Expression>
<CompareRestriction propertyType='Date' operator='GREATER_THAN_OR_EQUALS' propertyName='deliveryDate'>2010-07-02</CompareRestriction>
<CompareRestriction propertyType='Float' operator='GREATER_THAN_OR_EQUALS' propertyName='weight'>10f</CompareRestriction>
<Restriction operator='NOT_NULL' propertyName='maxDiameter'/>
<LogicalExpression operator='OR'>
<LeftHandSideCompare propertyType="Integer" operator="EQUALS" propertyName="weight">31</LeftHandSideCompare>
<RightHandSide operator='NOT_NULL' propertyName='lastChangedDate'/>
</LogicalExpression>
<LogicalExpression operator='OR'>
<LeftHandSideCompare propertyType="Integer" operator="EQUALS" propertyName="weight">31</LeftHandSideCompare>
<RightHandSide operator='NOT_NULL' propertyName='lastChangedDate'/>
</LogicalExpression>
</Expression>
<Order propertyName='deliveryDate' type='DESC'/>
</Criteria>
and I parse this thing and build the corresponding criteria. Currently I am facing a problem with it comes to comparison operators (<,>,<=,=>) as I deal with different numerical types: I have fields with Float, Integer or Long value. So when I am mapping I do something like this:
switch (leftHandSideCompareRestriction.getOperator().value()) {
...
case "LESS_THAN" : innerPredicates.add(criteriaBuilder.gt(rootQuery.<Number>get(propName), NumberUtils.createNumber((value))));
case "LESS_THAN_OR_EQUALS" : innerPredicates.add(criteriaBuilder.gt(rootQuery.<Number>get(propName), NumberUtils.createNumber(value)));
...
}
The NumberUtils is the apache commons NumberUtils utility class
that returns a numerical type based on the input provided (Float, Integer, Long or Double). Now I need a mechanism to provide the type also for the
rootQuery<T>.get(propName)
at runtime, otherwise the JPA is complaining that I provided a Float instead of a Integer or the other way around. I tried several things and now I kind of ran out of ideas. I would highly appreciate and thoughts, ideas, suggestions about how to accomplish this in a robust fashion.

I seems that initially I missed something - I am not sure how. There some kind of issue in a different part. So, doing the query like this: it will work for sure. I tried with the following types:
Integer
Float
And it worked as expected for the following operations: >,<, <=, =>. In conclusion using Number and the NumberUtils fixes this issue in quite an elegant manner, as the appropriate type is created by the NumberUtils and JPA takes the top of the hierarchy for Number.

Related

In systemverilog how to provide commandline overrides for complex fields like associative array fields

Lets say i have added a associative array string,string field to the factory through the macro `ovm_field_aa_string_string macro. Is there a way to configure it from command line like we do with simple int fields like follows:
./simv ... +ovm_set_config_int=scope,name,value
is there something like
./simv ... +ovm_set_config_aa_string_string=scope,name,key=val,key2=val2
No, you can only set int's and strings from the command line. I strongly discourge the use of any `uvm_field macros because of their inability to deal with complex times, the the poor simulation performance they impose.
This seems to be already answered by someone else
Here is the link to the post
how to get array of values as plusargs in systemverilog?

Xtext grammar of negative numbers: terminal vs datatype rules

What I would like to achive is an Xtext grammar which is able to distinguish between negative numerics of type int and float.
As I faced the same term problems as eclipse community, I followed their recommendation, to write both as datatype rules:
SignedInteger returns ecore::EIntegerObject:
'-'? INT;
SignedFloat returns ecore::EFloatObject:
'-'? INT* '.' INT+;
But the above will give me the following error (and finally i have the same problem by the leading minus sign):
Decision can match input such as "RULE_INT" using multiple alternatives: 1, 2
To solve this I could write both as terminal rules, but then the grammar will conflict in:
The following token definitions can never be matched because prior tokens
match the same input: RULE_INT
because both rules are hidden behinded the Xtext common terminals rule INT.
It seem like the solution for one of the problems would force a conflic with the other one. Any recommendations how to solve this?
Besides another question refering ecore datatypes: What return type would you recommend, whats the difference between EInt and EIntegerObject? (Is the second the wrapper class of the primitive type?)
I solved the problem by removing the with common terminals statement. And copied the rest (without the INT rule) i need into my own grammar. So there is no conflict any more.
But i guess that is not realy the root of the problem....
If anyone can explain what's going on here I would be very thankful.
(I hope that way does not bring later problems with it)

How to Correctly Build a MongoDB Nested property Name using C# Driver

I must be missing something blindingly obvious. Somebody please shame me;
I'm building 2.2 Aggregation queries, which aren't natively supported by the C# Linq Driver, so I'm having to build up stringified names for nested properties using dotted notation. Say I have a structure like this;
db.so.insert({
a:1,
b:2,
n : {
z:4,
x:5,
y: {
v:"value",
}
}
});
So to reference the "value" I would need to use the name n.y.v or n[y][v]. Now, since I'm receiving the choice of field property names for the query from the web client (http://www.demo.org/exampleQuery?field1=n&field2=y&field3=v) I need to construct the property names thus;
var fieldNameForQuery = field1+"."+field2+"."+field3;
I'm obviously nervous about this, so of course I'm defending against NOSQL Injection by sanitising my input parameters, but I'd much rather be using the C# driver for this instead.
I guess I'd like something like;
MongoDB.Driver.BuildNestedFieldName(field1, field2, field3));
which is basically what I've had to write myself, but it feels like a kludge, and I'd rather not maintain the responsibility for building DB safe field names this way.
There currently isn't a function to do what you are wanting. However, if all the function does is stick "."'s in the middle, then we aren't solving an injection problem because the stuff we are inserting can't have been injected... The injection problem would be solved by ensuring that "field1", "field2", and "field3" are valid values for field names. Of course, there isn't much that is invalid according to http://bsonspec.org/#/specification. The only thing we'd be checking for is that there aren't 2 null terminators in the string. So... that doesn't leave us with much we can do.
Does this make sense?

Does it make sense to define a class for Complex numbers, where real/imaginary parts use Numeric[T] instead of a concrete type?

Would something like
class Complex[T: Numeric](real: T, imag: T)
make sense, instead of writing a Complex class using Doubles, one using Longs, one using BigInts, so that everyone can choose the number type he needs?
How would performance compare to the non-generic approach?
For the moment, Numeric is not #specialized. So the generic version using it will suffer from boxing and unboxing and the performances will be greatly reduced. Here is a nice blog post with performance measurments:
http://www.azavea.com/blogs/labs/2011/06/scalas-numeric-type-class-pt-2/
However, you could directly write a #specialized version of your Complex number class without using Numeric and get all the benefits.
On a strictly pragmatic point of view, I am not sure to understand what's the usage of a complex number with integer parts...

Optional attribute values in MappedField

I'm new to Scala and Lift, coming from a slightly odd background in PLT Scheme. I've done a quick search on this topic and found lots of questions but no answers. I'm probably looking in the wrong place.
I've been working my way through tutorials on using Mapper to create database-backed objects, and I've hit a stumbling block: what types should be used to stored optional attribute values.
For example, a simple ToDo object might comprise a title and an optional deadline (e.g. http://rememberthemilk.com). The former would be a MappedString, but the latter could not be a MappedDateTime since the type constraints on the field require, say, defaultValue to return a Date (rather than a Date or null/false/???).
Is an underlying NULL handled by the MappedField subclasses? Or are there optional equivalents to things like MappedInt, MappedString, MappedDateTime that allow the value to be NULL in the database? Or am I approaching this in the wrong way?
The best place to have Lift questions answered is the Lift group. They aren't into Stack Overflow, but if you do go to their mailing list, they are very receptive and helpful.
David Pollak replied with:
Mapper handles nulls for non-JVM
primitives (e.g., String, Date, but
not Int, Long, Boolean). You'll get a
"null" from the MappedDateTime.is
method.
... which is spot on.