FacebookID String to BigInt Java and Parse - java

I am building an app which utilizes the Parse back-end. As part of my user object I want to directly use the FacebookID as my UserID, but I want to convert it to a BigInt instead of a string.
I used the following code, but when I check the data in Parse the value is slightly off.
"user" is a GraphUser object which I have declared above.
String iDString = user.getId();
BigInteger iD = new BigInteger(iDString);
currentUser.put("faceID", iD);
When I check the data in Parse I get the following:
Original ID in string format (* are used to cover digits):
************27645
BigInt ID:
************27644
Does anyone know why the last digit is 1 less than the digit above? May it have something to with the sign of the BigInt?

I don't know which is your problem due to the lack of information in your post.
This simple case works very well for me though:
String iDString = "99999999999997645";
BigInteger iD = new BigInteger(iDString);
System.out.println(iD.toString());
The printed number is exactly the same of the iDString "number".

Related

How can we convert a string to a working variable which fetch values in JAVA?

I have a case where I need to store a location of each key value of json, so that for each key, it automatically fetches the location and gives the value for it from json.
Here, I have a location of the key 'vehicle_id' inside json 'car' assigned to a variable like:
String location="jresp.getJSONArray('cars').getJSONObject(0).getString('vehicle_id')"
How do I make it as a variable in JAVA such that this location fetches the value of vehicle_id for me from JSON? I need it like:
String value=jresp.getJSONArray('cars').getJSONObject(0).getString('vehicle_id');
so that it gives me a value. I've searched in net, but couldn't find it anywhere. Please help me!
Instead of having the complete statement as a variable like -
String location="jresp.getJSONArray('cars').getJSONObject(0).getString('vehicle_id')"
You could have 3 different variables like -
String jsonArray = "cars";//You might need to do some string processing to get these
int objSeq = 0;
String key = "vehicle_id";
Then you can definitely use it in your Java statement -
String value=jresp.getJSONArray(jsonArray).getJSONObject(objSeq).getString(key);

How to convert string to MarketDataIncrementalRefresh in QUICKFIX in Java?

I am working on the Stock and Exchange Markets. I have a situation like : I need to take a string from the log and convert it to "Message" type Object. As per this link I have tried using all the three methods of the "MessageUtils" class in JAVA. But my String is being stripped to a Message class type object with unique tags. But as my string is "MarketDataIncrementalRefresh" type I want each every tag to be present in the Message.
For example : I am providing the following string to "MessageUtils.parse()" method.
8=FIX.4.2|9=00795|35=W|49=TT_PRICE|56=SAP0094X|34=2392|52=20170623-04:41:33.375|55=CL|48=00A0HR00CLZ|10455=CLQ7|167=FUT|207=CME|15=USD|262=MDRQ-751|200=201708|18210=1|387=12292|268=24|269=0|290=1|270=4290|271=33|269=0|290=2|270=4289|271=34|269=0|290=3|270=4288|271=40|269=0|290=4|270=4287|271=38|269=0|290=5|270=4286|271=46|269=0|290=6|270=4285|271=53|269=0|290=7|270=4284|271=46|269=0|290=8|270=4283|271=66|269=0|290=9|270=4282|271=48|269=0|290=10|270=4281|271=64|269=1|290=1|270=4291|271=21|269=1|290=2|270=4292|271=40|269=1|290=3|270=4293|271=48|269=1|290=4|270=4294|271=83|269=1|290=5|270=4295|271=62|269=1|290=6|270=4296|271=46|269=1|290=7|270=4297|271=34|269=1|290=8|270=4298|271=55|269=1|290=9|270=4299|271=31|269=1|290=10|270=4300|271=128|269=2|270=4291|271=1|269=4|270=4280|269=7|270=4292|269=8|270=4277|10=044|
But what I am getting is this:
8=FIX.4.2|9=192|35=W|34=2|49=TT_PRICE|52=20170622-14:16:23.685|56=SAP0094X|15=USD|48=00A0HR00GCZ|55=GC|167=FUT|200=201708|207=CME|262=MDRQ-21|268=25|269=0|270=12510|271=24|290=1|387=121890|10455=GCQ7|18210=1|10=036|
As you can observe only unique tags are present in the String. But I want each and every tag , no matter how many times it exists in the provided string.
Please can anyone help me doing this in JAVA. It will be really appreciable.
Below is the code I am using for converting :
MessageUtils mu = new MessageUtils();
Session session = Session.lookupSession(sessionID);
Message msg = MessageUtils.parse(new DefaultMessageFactory(), null, str);
// Message msg = new Message(str, false); //This can also be used for converting
System.out.println(msg.toString());
The other thread says:
MessageUtils.parse(MessageFactory messageFactory, DataDictionary dataDictionary, java.lang.String messageString)
And your code says:
Message msg = MessageUtils.parse(new DefaultMessageFactory(), null, str);
So you need to fix your data dictionary and pass it to the parse method instead of passing 'null'
I think the problem is as follows. There's a repeating group that starts with tag 286 (NoMDEntries). The order of fields in a repeating group should be strict, i.e. the same order as the definition of the repeating group. See Market Data - Snapshot/Full Refresh or the data dictionnary supplied by QuickFIX/J (FIX42.xml).
The 268 tag should be followed by 269 and then 270. I am seeing in your message string: |268=24|269=0|290=1|270=4290| which is the incorrect order of tags. That is probably the reason why the message is truncated by MessageUtils.parse.
As a test you could try to manually correct the order in the string and try parsing that to see if that gives the correct message.

Convert Map Object from String "[a:12,b:[a:b,c:d]]"

Is there any out of box method in java (groovy) that converts the string like
String s = "[a:12,b:[a:b,c:d]]";
to a Map object with key value pairs.
Update: It is somehow similar to the question asked as Groovy: isn't there a stringToMap out of the box?(Groovy: isn't there a stringToMap out of the box?). The difference is here the keys are of string type which makes easier to parse, since i have retrieved the mentioned map by doing .toString() i am unable to parse by the answered methods because my string actually contains date strings which i need it back to date objects. So it was difficult to parse the whole string.
You can convert this string to JSON-like format, and simple parse it:
String s = "[a:12,b:[a:b,c:d]]";
def result = new JsonSlurper().setType(JsonParserType.LAX).parseText(s.replaceAll('\\[', '{').replaceAll('\\]', '}'))
I have a way that may work depending on your actual data. The problem with your sample data is that you supply unquoted strings b and d as part of the structure:
String s = "[a:12,b:[a:b,c:d]]";
If what you had was actually
String s = "[a:12,b:[a:'b',c:'d']]";
or
String s = "[a:12,b:[a:1,c:2]]";
Then you could just do this:
def map=Eval.me(s)
but as is, eval tries to resolve b and d as variables which aren't defined in the scope.

How to show formatted contact numbers?

I have a field called phone number which has values such as 0833888999 that I want to format as following 0833 888 999.
The answer that Rachana offered blew is true for few countries but not all countries.
Therefore,
I am using this Google library to format contact numbers from different countries; however, the problem is that after persisting the contact numbers in database I can not search for them on database, for example the library would format contact numbers of different countries in different format, for example add space or "-" between them that makes hibernate unable to find them.
+18182223333 >>> +1 818-222-3333
+441135558888 >>> +44 113 555 8888
Hibernate
.add(Restrictions.ilike("user.phone","+18182223333");
Try this
<s:property value="getText('{0,number,0### ### ###}',{phone})"/>
Where,phone=0833888999
Hope this will help you also see Using Struts2 Tags to Formatting Numbers
you will get clear idea about number formatting
I think you should keep the raw phone number (e.g. 0833888999) in the database and it's the View responsibility to format it accordingly.
You can have a separate table "country_phone_format" holding a country_id and a phone_format and you could fetch the phone_format as a #ManyToOne entity so that you have both the raw data and the format to properly display it into the View.
The PhoneFormat could be easily cached with the second level cache, as they should be rarely modified.
Just as Vlad mentions, you should keep the raw phone number (e.g. 0833888999) in the database (or save it with the country and area code if you prefer) and leave th responsibility to format it accordingly to the View.
You can use Type Conversion to convert to/from the format you desire - and take advantage of the google library you mention. Something like the following can get you started (abbreviated just to get the gist of it):
public class MyConverter extends StrutsTypeConverter {
public Object convertFromString(Map context, String[] values, Class toClass) {
String phoneNr = values[0]
MyPhoneObject phone = googleLibrary.doYourMagic(phoneNr);
return phone;
}
public String convertToString(Map context, Object o) {
googleLibrary.parseString( ((MyPhoneObject)o).rawPhoneNr() );
}
}
Don't forget to register the converter in xwork-conversion.properties
my.PhoneObject=path.to.MyConverter

java, Morphia how to compare strings when using the Morphia Find Methods

Im new to this so here goes.
Trying to get a user called "Bob" from the MongoDb.
I have the:
UserData ud = MonConMan.instance().getDb().find(UserData.class, "name","bob").get();
The "bob" cannot be found if it has capital "Bob".
I understand i can get a List and do equalsIgnoreCase but are
there some Operators i can use?
I have users logging on and must test to see if they are registered. A user can type his name anyway he likes so must find a way to equalsIgnoreCase. Yea this is a problem, i cannot get all names and do equalsIgnoreCase, if there are like 10,000. One could of course initially save all user names in lowercase but that would destroy the visual appearance of the name.
looking at the wiki but cannot see any..
http://code.google.com/p/morphia/wiki/Query
Use java regex, like this.
String name = "bob";
Pattern pattern = Pattern.compile("^" + bob + "$", Pattern.CASE_INSENSITIVE);//This line will create a pattern to match words starts with "b", ends with "b" and its case insensitive too.
Query<UserData> query = createQuery().field("name").equal(pattern).retrievedFields(true, "id");//Replace `id` with what ever name you use in UserData for '_id'
UserData user = query.get();
if(user!=null){
//he is already registered
}
else{
//He is a new guy
}
(I am not good at regex, so you may have read about$&^somewhere. )
You should be sure that the user names you are using to validate a new user should be unique across your system.
Ended up keeping two fields like
- lowercaseusername
- originalusername
This way i could search for a user using the lowercaseusername
You can make find a name of a UserData using this code :
Query<UserData> query = createQuery().filter("name","bob");
find(query);
In my application, this code return all UserData that haves a field name with "bob" value.
The code can be this way too :
Query<UserData> query = createQuery().field("name").equal("bob");
find(query);
These codes will be in a UserDataDao that extends BasicDao, and receives in the construtor the datastore from morphia.

Categories