Google App Engine Query .addFilter - java

I've been struggling with queries with Google's datastore and wanted to get some help.
I have a form that saves a double to the datastore. Here is a snippet from the servlet:
String temp = req.getParameter("temp");
message.setProperty("temp", temp);
temp is a string but containes a number with decimal places.
In my JSP code I'm trying to run the query:
query.addFilter("temp",Query.FilterOperator.GREATER_THAN, -0.9);
But it only seems to work if the value (-0.9) is an integer (-1). Also, when I try to use a variable I get an invalid constant error:
query.addFilter("temp",Query.FilterOperator.GREATER_THAN, request.getParameter('mintemp'));
Any help would be appreciated!
Thanks!

Chance are you are setting your property as a String, you should store the property as a Float instead see the documentation for setProperty.
Convert your String to a Float before setting the Entity property:
message.setProperty("temp", Float.parseFloat(temp));

Related

How to fetch JsonNode values using string variable as key?

I am trying to fetch values from a JsonNode in order to carry out some validations.
However, I get null when i try to fetch the value using a predefined variable (String).
When I fetch the same value using hardcoded key, it gives me the correct value.
I tried naming a new variable String as the key, but it does not seem to be working as expected.
boolean validateFormData(JsonNode formData) {
System.out.println("1-" + formData.get("name"));
String nameVar = "name";
System.out.println("2-" + formData.get(nameVar));
return false;
}
1-"Mohammed"
2-null
Is being printed I am not sure why.
The reason I want to use a variable is because the keys are not fixed, they vary from Form to Form.
I am new to stackoverflow so kindly ignore the formatting errors.
I shall get better with time.
Any help is appreciated, thanks in advance.
Even if the question is repeated kindly point me to the right answer.

GAE DataStore query filter in JAVA with Query.FilterOperator.EQUAL unable to compare with numeric value

I'm trying to create a query with selects only the Entities with specific numeric value in their property but the following Filter returns an empty result
Filter tmpFilter = new Query.FilterPredicate("cost", Query.FilterOperator.EQUAL, 10);
I have several entities that should be selected, I have tried to send the "10" as a string but still no luck.
When I try to select with GREATER_THAN or other filter operators then it works.
I have tried to run EQUAL on a string value and it worked but not on a numeric value.
Any ideas ?
Integer values are stored as Long in the Datastore. Try 10L instead of 10.

salesforce to java type conversion

I'm fetching data from salesforce to java using partner API .
well upto here is no problem , but my concern is something annoying .
in my salesforec end i've one field, type of currency and suppose it has value $850,000,000,000 ok
now when i'm fetching data through partner in java like
String value = (String)sobj.getField("MyFieldName");
i'll get 8.5E8.... thats wht i'm getting .
now my question is
1) if i'm getting data as String then why it cast to decimal automatically
2) and i couldn't cast it another type as well for example
BigInterger bi = (BigInteger)sobj.getField("MyFieldName");
gives classcast Exception as well as
Double d = (Double)sobj.getField("MyFieldName"); too gives same exception
moreover
Double d = new Double(sobj.getField("MyFieldName").toString());
again gives exponential value ...
please solve my issue , coz value in salesforce is too big in multi billions
Try Long bi = new BigInteger(sobj.getField("MyFieldName")).longValue()
Also, you should read up on casting, I think you are confused about what it is and how it works.

Unable to retrive Projection/Multi- Relantion field Requests.Custom_SFDCChangeReqID2 using versionone java sdk

I have been trying to retrieve information from querying a specific Asset(Story/Defect) on V1 using the VersionOne.SDK.Java.APIClient. I have been able to retrieve information like ID.Number, Status.Name but not Requests.Custom_SFDCChangeReqID2 under a Story or a Defect.
I check the metadata for:
https://.../Story?xsl=api.xsl
https://.../meta.V1/Defect?xsl=api.xsl
https://.../meta.V1/Request?xsl=api.xsl
And the naming and information looks right.
Here is my code:
IAssetType type = metaModel.getAssetType("Story");
IAttributeDefinition requestCRIDAttribute = type.getAttributeDefinition("Requests.Custom_SFDCChangeReqID2");
IAttributeDefinition idNumberAttribute = type.getAttributeDefinition("ID.Number")
Query query = new Query(type);
query.getSelection().add(requestCRIDAttribute);
query.getSelection().add(idNumberAttribute);
Asset[] results = v1Api.retrieve(query).getAssets();
String RequestCRID= result.getAttribute(requestCRIDAttribute).getValue().toString();
String IdNumber= result.getAttribute(idNumberAttribute).getValue().toString();
At this point, I can get some values for ID.Number but I am not able to retrieving any information for the value Custom_SFDCChangeReqID2.
When I run the restful query to retrieve information using a browser from a server standpoint it works and it does retrieve the information I am looking for. I used this syntax:
https://.../rest-1.v1/Data/Story?sel=Number,ID,Story.Requests.Custom_SFDCChangeReqID2,Story.
Alex: Remember that Results is an array of Asset´s, so I guess you should be accessing the information using something like
String RequestCRID= results[0].getAttribute(requestCRIDAttribute).getValue().toString();
String IdNumber= results[0].getAttribute(idNumberAttribute).getValue().toString();
or Iterate through the array.
Also notice that you have defined:
Asset[] results and not result
Hi thanks for your answer! I completely forgot about representing the loop, I was too focus on the retriving information part, yes I was actually using a loop and yes I created a temporary variable to check what I was getting from the query in the form
Because I was getting the variables one by one so I was only using the first record. My code works after all. It was just that What I was querying didn't contain any information of my use, that's why I was not finding any. Anyway thanks for your comment and observations

Max/Min value(without field name) in mongodb with java

I am using the below mentioned MongoDB query in Java to find the maximun value of field price:
DBCursor cursor = coll.find(query,fields).sort(new BasicDBObject("price",1)).limit(1);
fields argument passing to coll.find function here is having the price field only.
So I am getting the output in the form:
{"price" : value}
Is there any way to get value only in the output without the field name and braces etc, so that it can be assigned to a variable or returned to the calling function etc.
Or if there is any other query or mechanism available that I can use for the same purpose.
Pls suggest..
Thanks & Regards
You can get value of price from the DBCursor object as follows.
while (cursor.hasNext()) {
Double price = (Double) cursor.next().get("price");
}
On the mongo shell you can do it as follows :
db.priceObj.find({},{_id:0, price:1}).sort({price:-1}).limit(1)[0].price
You cannot do this due to the fact that MongoDB communicates using BSON.
A single value like you want would be invalid BSON. It is easy enough to filter it out your side.

Categories

Resources