Query parameters without value for boolean - servicestack

Is there any way to make a query parameter without a value TRUE for a boolean in ServiceStack?
Example:
DTO has a field: public bool IncludeOld { get; set; }
Query parameter to set TRUE:
...?includeOld=true or ...?includeOld=1
Would also like
...?includeOld to represent TRUE, which looks quite nice.

This isn't supported. A query param without a value is considered to not have a value, it needs to be assigned a truthy value for it to be deserialized as true. Which for ServiceStack.Text includes:
true or True
1
t or T
y or Y
on

Related

how to compare attributes in hazelcast with predicates

i have an object "ResponseSerializablePlus"
the object has a few attributes
private int id;
private String cod_nrbe_en;
private int num_sec_ac
this field can bring null values so i need to capture all the registers about this field, you can have id or another field duplicated more than once.
what i was thinking was building a query
Predicate sqlQuery = Predicates.sql("i dont know what to put here");
Predicate criteriaQuery = Predicates.and(
Predicates.equal("id", id),
Predicates.equal("code", cod_nrbe_en)
Predicates.equal("num", num_sec_ac)
);
you can have just 1 value and other fields are null or another case the thing is i dont know how to query or compare those 3 attributes
¿any hint?

How to use BQL In Operator with Select2 query / PXProjection and list of values

I am trying to replicate the following type of SQL query that you can perform in SQL Server...the important part here is the WHERE clause:
Select InventoryCD from InventoryItem WHERE InventoryCD IN ('123123', '154677', '445899', '998766')
It works perfectly using the IN3<> operator and a series of string constants:
i.e. And<InventoryItem.inventoryCD, In3<constantA,constantB,constantC>,
However, I need to be able to do this with an arbitrarily long list of values in an array, and I need to be able to set the values dynamically at runtime.
I'm not sure what type I need to pass in to the IN<> statement in my PXProjection query. I have been playing around with the following approach, but this throws a compiler error.
public class SOSiteStatusFilterExt : PXCacheExtension<SOSiteStatusFilter>
{
public static bool IsActive()
{
return true;
}
public abstract class searchitemsarray : PX.Data.IBqlField
{
}
[PXUnboundDefault()]
public virtual string[] Searchitemsarray { get; set; }
}
I think maybe I need an array of PXString objects? I'm really not sure, and there isn't any documentation that is helpful. Can anyone help?
This shows how to do it with a regular PXSelect: https://asiablog.acumatica.com/2017/11/sql-in-operator-in-bql.html
But I need to be able to pass in the correct type using Select2...
For reference I will post here the example mentioned by Hugues in the comments.
If you need to generate a query with an arbitrary list of values generated at runtime like this:
Select * from InventoryItem InventoryItem
Where InventoryItem.InventoryCD IN ('123123', '154677', '445899', '998766')
Order by InventoryItem.InventoryCD
You would write something like this:
Object[] values = new String[] { "123123", "154677", "445899", "998766" };
InventoryItem item = PXSelect<InventoryItem,
Where<InventoryItem.inventoryCD,
In<Required<InventoryItem.inventoryCD>>>>.Select(Base, values);
Please note that In<> operator is available only with Required<> parameter and you need to pass array of possible values manually to Select(…) method parameters. So you need to fill this array with your list before calling the Select method.
Also, the Required<> parameter should be used only in the BQL statements that are directly executed in the application code. The data views that are queried from the UI will not work if they contain Required<> parameters.
I ended up creating 100 variables and using the BQL OR operator, i.e.
And2<Where<InventoryItem.inventoryCD, Equal<CurrentValue<SOSiteStatusFilterExt.Pagefilter1>>,
Or<InventoryItem.inventoryCD, Equal<CurrentValue<SOSiteStatusFilterExt.Pagefilter2>>,
Or<InventoryItem.inventoryCD, Equal<CurrentValue<SOSiteStatusFilterExt.Pagefilter3>>,
Or<InventoryItem.inventoryCD, Equal<CurrentValue<SOSiteStatusFilterExt.Pagefilter4>>,
etc...etc...
You can then set the value of Pagefilter1, 2, etc inside of the FieldSelecting event for SOSiteStatusFilter.inventory, as an example. The key insight here, which isn't that obvious to the uninitiated in Acumatica, is that all variables parameterized in SQL Server via BQL are nullable. If the variable is null when the query is run, SQL Server automatically disables that variable using a "bit flipping" approach to disable that variable in the SQL procedure call. In normal T-SQL, this would throw an error. But Acumatica's framework handles the case of a NULL field equality by disabling that variable inside the SQL procedure before the equality is evaluated.
Performance with this approach was very good, especially because we are querying on an indexed key field (InventoryCD isn't technically the primary key but it is basically a close second).

Integer getting default value 0 where it required to be NULL in Java [duplicate]

This question already has an answer here:
h:inputText which is bound to Integer property is submitting value 0 instead of null
(1 answer)
Closed 7 years ago.
My requirement looks simple, but I feel I'm doing something wrong in code.
All I need is to insert "null" in Oracle Database when nothing is passed from InputText field of UI.
PaymantSpecFormPage.xhtml
<h:inputText id="startWeek" value="#{flowScope.paymentSpecVO.paymStartWk}" styleClass="inputTypeForm" maxlength="4"/>
PaymentSpecVO.java
public class PaymentSpecVO extends BaseVO<PaymentSpec> {
private Integer paymStartWk;
public Integer getPaymStartWk() {
return paymStartWk;
}
public void setPaymStartWk(Integer paymStartWk) {
this.paymStartWk = paymStartWk;
}
}
And in my DB Table
COLUMN_NAME DATA_TYPE NULLABLE DEFAULT_VALUE
------------ --------- -------- -------------
PAYM_START_WK NUMBER(4,0) Yes null
And when I enter nothing in inputText, then instead of null, 0 is getting entered in Table, which has different meaning in my business logic, please help me to do necessary code changes.
And when I enter nothing in inputText, then instead of null, 0 is getting entered in table
This problem is recognizable as Tomcat's EL parser not taking into account that you're using Integer instead of int. You need to add the following VM argument to Tomcat startup options:
-Dorg.apache.el.parser.COERCE_TO_ZERO=false
See also:
h:inputText which is bound to Integer property is submitting value 0 instead of null
Work around for faulty INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in Mojarra JSF 2.1
It solved my half of problem, and now its storing null values, but while on retrieve it shows 0 again
This problem is recognizable as JDBC under the covers using ResultSet#getInt() instead of ResultSet#getObject(). This is however essentially a different problem than the first one. You didn't tell anything about how your database access layer is setup, so it's hard to point out a concrete solution.
I don't know how you are reading that xhtml.
At java layer, you can override either the constructor of PaymentSpecVO or the setter for paymStartWk
Try this
public class PaymentSpecVO extends BaseVO<PaymentSpec> {
private String paymStartWk;
public String getPaymStartWk() {
return paymStartWk;
}
public void setPaymStartWk(Integer paymStartWk) {
this.paymStartWk = paymStartWk+"";
}
}
But then you might be needing a lot of parsing there.
Edit*
If you enter nothing to your "input Text" the passed value is null but since you're catching it with an Integer, it interprets null to 0.
So when you save it, even after manipulating methods and variables to null , it will store "0" , because basically thats the default value.
Another solution I think is making a custom class that stores Integers
Example :
public class IntegerVal extends Number {
private Max_val = 10000000;
private Min_val = -10000000;
private String default = null;
}
you can do whatever implementations you want ,, like returning null,
You can use that as a substitute to the Integer Class you are using , if you dont wanna use String

Linq query to return boolean

I am inserting data into my entity table using .AddObject(). The object is of the entity table's type. The object is eventStudent, it has string eventStudent.ID, bool eventStudent.StudentPresent, bool eventStudent.ParentPresent.
The students are a list of strings containing student ids. Their presence at the event is in another object called attendees, consisting of String studentID, bool studentPresent and bool parentPresent. Only student id's that have true for StudentPresent and/or ParentPresent are in the attendees list.
As I load up my eventStudent object, I need to set StudentPresent and ParentPresent. This is what I came up with:
foreach (StudentMinimum student in students)
{
eventStudent.StudentPresent = (from a in attendees
where a.StudentID.Contains(student.StudentID)
&& a.StudentPresent
select a.StudentPresent);
}
I receive the error cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool'
How can I improve my query so eventStudent.StudentPresent is set to either True or False?
The compiler doesn't know what type will be returned from your query as you haven't explicitly casted it to a type. As a result, it gets a generic IEnumerable type (there could be many records returned right? Hence the IEnumerable. And those records could each be of any type, hence the generic type).
So if the data in your DB were bad and you got multiple records back, converting:
StudentPresent
true
false
false
to a bool is not going to happen. There are a few ways you could get around this. Personally, I'd do something like
var studentPresent = (from a in attendees
where a.StudentID.Contains(student.StudentID)
&& a.StudentPresent
select a.StudentPresent).FirstOrDefault();
eventStudent.StudentPresent = (bool)studentPresent;
Well, actually, I'd use a lambda query instead but that's just personal preference.

How to override string serialization in ServiceStack.Text?

How come the following works to override Guid formatting:
ServiceStack.Text.JsConfig<Guid>.SerializeFn = guid => guid.ToString();
But doing this to force null strings to empty strings doesn't?
ServiceStack.Text.JsConfig<string>.SerializeFn = str => str ?? string.Empty;
I have this enabled:
ServiceStack.Text.JsConfig.IncludeNullValues = true;
I have also tried the String class rather than the string primitive. And the raw version named .RawSerializeFn
Is there a different work around?
String's are specially handled in ServiceStack.Text so you can't override their behavior with configuration.
Given you can't override it, the only solution I can see (other than submitting a pull-request) is to reflect over the model and populate null properties with empty strings.

Resources