Database operations that return LiveData are automatically executed on a background thread. I need to do some "post-processing" on the returned LiveData in order to add information from another table.
However, my code to perform this processing executes too early, before the LiveData is actually returned
from the thread that performs the Dao call. Eventually, the LiveData-wrapped list is successfully populated, but its Person objects do not contain the data that I'd intended to augment them with (i.e., the possession data).
What I'd like to do, unless someone can suggest a better overall approach, is to ensure that the method that performs the post-processing, aggregatePersonData, is invoked after the LiveData is returned from the query thread.
The following pared-down code snippets illustrate my approach.
// Person.java
#Entity(tableName = "People")
public class Person
{
#PrimaryKey(autoGenerate = true)
private long id;
// ...
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
// ...
}
// from PersonDao.java
#Query("SELECT * FROM People ORDER BY last_name")
LiveData<List<AggregatePerson>> getAllPeople();
// PeoplePossessions is a one-to-many table (i.e., one Person to many Possessions).
// from PersonPossessionDao
#Query("SELECT p.* FROM Possessions p INNER JOIN PeoplePossessions pp ON p.id = pp.possession_id WHERE pp.person_id=:personId")
LiveData<List<Possession>> getPossessionsForPerson(final long personId);
// (not an entity)
// AggregatePerson.java
public class AggregatePerson extends Person
{
// fields from a join between the People table and (an)other table(s) would go here...
#Ignore
private List<String> mPossessions;
// ...
public List<String> getPossessions()
{
return mPossessions;
}
public void setPossessions(List<String> possessions)
{
mPossessions = possessions;
}
}
// MyRepository.java
public class MyRepository
{
private PersonDao mPersonDao;
private PersonPossessionDao mPersonPossessionDao;
private LiveData<List<AggregatePerson>> mAllPeople;
public MyRepository(Application application)
{
PersonDatabase database = PersonDatabase.getInstance(application);
// Room automatically executes database operations that return
// LiveData objects on a background thread.
mPersonDao = database.personDao();
mAllPeople = mPersonDao.getAllPeople();
mPersonPossessionDao = database.personPossessionDao();
}
//...
public LiveData<List<AggregatePerson>> getAllPeople()
{
aggregatePersonData(mAllPeople.getValue());
return mAllPeople;
}
// This method executes too early, before the LiveData<List<AggregatePerson>>
// has been populated. Thus, the code that adds the possession data is skipped.
private void aggregatePersonData(List<AggregatePerson> people)
{
if (null != people)
{
StringBuilder sb = new StringBuilder();
List<String> items = new ArrayList<>();
// In this loop, a query is performed for each person.
for (AggregatePerson person : people)
{
long personId = person.getId();
List<Possession> possessions = mPersonPossessionDao.getPossessionsForPerson(personId);
if (null != possessions)
{
items.clear();
sb.setLength(0);
for (int i = 0; i < possessions.size(); i++)
{
Possession possession = possession.get(i);
sb.append(possession.getName());
sb.append(": ");
sb.append(possession.getValue());
sb.append(", ");
sb.append(possession.getWeight());
items.add(sb.toString());
}
person.setPossessions(items);
}
// more data aggregation for current person would go here...
}
}
}
}
I am currently developing an android application for a client who is insisting to use Odoo for API. I don't have any idea about it. I am not getting it even after referring to this link. They provide an URL, Database name, username, and password. If anyone did Odoo with Android before, can you give any suggestions?
There are a lot of ways to connect Android to Odoo. Here they are:
Json-RPC
XML-RPC (especially aXMLRPC, this is what I am using)
There is also a framework called Odoo Mobile Framework . I have tried it but found a lot of issues and I was not able to get it work properly. You can find the documentation here.
Odoo has a Web Service API which is available for Python, Ruby, PHP and Java. I strongly recommend to take a look.
For my case, I have cloned the aXMLRPC git repository, created a package in my project and adapted the original package name. But recently I have found this on Stack Overflow explaining how to add aXMLRPC to your Android project using Gradle (I didn't give it a try yet).
Odoo had made available three endpoints:
xmlrpc/2/db to get the list of available databases on your server, it does not require to be authenticated;
xmlrpc/2/common to log in to the server, it does not require to be authenticated;
xmlrpc/2/object, is used to call methods of odoo models via the execute_kw RPC function.
public class OdooConnect {
String url;
private XMLRPCClient client;
public OdooConnect(String serverAddress, String path) {
url = serverAddress + "/xmlrpc/2/" + path;
client = new XMLRPCClient(url);
}
public Object login(String db, String username, String password) {
Object object;
try {
object = client.call("login", db, username, password);
return object;
} catch (XMLRPCException e) {
e.printStackTrace();
}
return null;
}
public Object checkServer() {
Object object;
try {
object = client.call("list", new Object[]{});
return object;
} catch (XMLRPCException e) {
e.printStackTrace();
}
return null;
}
}
In this class, the constructor as arguments the server address (it can be http(s)://your_ip_address:the_port_number) and the path ('db', 'common' or 'object').
The checkServer method returns an object which is actually an array containing the list of available databases.
The login mehtod returns an Integer which is the Id of the authenticated user.
For the Odoo CRUD mehtods (search_read, search_count, search, write, create, unlink) you can take a look to the Odoo Web Service API Java code matching the method you want.
Here is an example of the search_read method. I assume that you've an XMLRPCClient named client.
public Object search_read(String db, int user_id, String password, String object, List conditions, Map<String, List> fields) {
Object result = null;
try {
result = client.call("execute_kw", db, user_id, password, object, "search_read", conditions, fields);
} catch (XMLRPCException e) {
e.printStackTrace();
}
return result;
}
Where
object is an Odoo model for example "res.partner"
conditions is the domain (filter) something like this: Collections.singletonList(Collections.singletonList(Arrays.asList("supplier", "=", true)));
fields, the fields you want to get,
fields = new HashMap() {{put("fields", Arrays.asList("id","name","is_company","street")); }};
You must cast the result of the method to Object[] which will give you an array containing a list of objects each representing a record.
Object[] objects = (Object[]) result;
if (objects.length > 0) {
for (Object object : objects) {
String name= OdooUtil.getString((Map<String, Object>) object, "name");
boolean is_company= OdooUtil.getBoolean((Map<String, Object>) object, "is_company");
String street = OdooUtil.getString((Map<String, Object>) object, "street");
int id= OdooUtil.getInteger((Map<String, Object>) object, "id");
}
}
Here the OdooUtil class
public class OdooUtil {
public static String getString(Map<String, Object> map, String fieldName) {
String res = "";
if (map.get(fieldName) instanceof String) {
res = (String) map.get(fieldName);
}
return res;
}
public static Integer getInteger(Map<String, Object> map, String fieldName) {
Integer res = 0;
if (map.get(fieldName) instanceof Integer) {
res = (Integer) map.get(fieldName);
}
return res;
}
public static Double getDouble(Map<String, Object> map, String fieldName) {
Double res = 0.0;
if (map.get(fieldName) instanceof Double) {
res = (Double) map.get(fieldName);
}
return res;
}
public static Boolean getBoolean(Map<String, Object> map, String fieldName) {
Boolean res = false;
if (map.get(fieldName) instanceof Boolean) {
res = (Boolean) map.get(fieldName);
}
return res;
}
public static Float getFloat(Map<String, Object> map, String fieldName) {
Float res = 0f;
if (map.get(fieldName) instanceof Float) {
res = (Float) map.get(fieldName);
}
return res;
}
}
If you have a many2one field you only have access to the id and the name of the related record. You can use the following class to get the id and the name of the many2one record.
public class Many2One {
private int id;
private String name;
public Many2One() {
}
public static Many2One getMany2One(Map<String, Object> stringObjectMap, String fieldName) {
Integer fieldId = 0;
String fieldValue = "";
Many2One res = new Many2One();
if (stringObjectMap.get(fieldName) instanceof Object[]) {
Object[] field = (Object[]) stringObjectMap.get(fieldName);
if (field.length > 0) {
fieldId = (Integer) field[0];
fieldValue = (String) field[1];
}
}
res.id = fieldId;
res.name = fieldValue;
return res;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
Example of usage of Many2One class
String partner_name= Many2One.getMany2One((Map<String, Object>) object, "partner_id").getName();
int partner_id= Many2One.getMany2One((Map<String, Object>) object, "partner_id").getId();
For other remaining CRUD methods, you can easily find a way how they work by reading the Odoo Web Service API documentation.
I hope this gives you some insights.
This is Just an Example did to access contacts/partners from odoo:
#!/usr/bin/env python
import csv
from xmlrpclib import ServerProxy
SERVER = 'http://localhost:8069'
DATABASE = 'testcompany'
USERNAME = 'admin'
PASSWORD = 'password'
FILE_PATH = 'ODOO_clientsMain2_test.csv'
server = ServerProxy('http://localhost:8069/xmlrpc/common')
user_id = server.login(DATABASE, USERNAME, PASSWORD)
server = ServerProxy('http://localhost:8069/xmlrpc/object')
def search(list, key):
for item in list:
return item[key]
reader = csv.reader(open(FILE_PATH,'rb'))
for row in reader:
#print row
partner_template = {
'name': row[0],
#'company_id': row[1],
}
if row[2] is not None and row[2]<>'':
partner_template.update({'email': row[2]})
if row[5] is not None and row[5]<>'':
partner_template.update({'tin': row[5]})
if row[6] is not None and row[6]<>'':
partner_template.update({'ref': row[6]})
if row[8] is not None and row[8]<>'':
partner_template.update({'phone': row[8]})
if row[9] is not None and row[9]<>'':
partner_template.update({'mobile': row[9]})
print partner_template
partner_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'res.partner', 'create', [partner_template])
#create External ID
external_ids = {
'model': 'res.partner',
'name': row[11],
'res_id': partner_id,
}
external_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'ir.model.data', 'create', [external_ids])
# update related fields
if row[7] is not None and row[7]<>'':
#look up and update payment term
payment_term_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'account.payment.term', 'search_read', [[['name','=',row[7]],['active', '=', True]]],{'fields': ['id'], 'limit': 1})
if payment_term_id is not None:
id = server.execute_kw(DATABASE, user_id, PASSWORD, 'res.partner', 'write', [[partner_id],{'property_payment_term': search(payment_term_id,'id')}])
if row[10] is not None and row[10]<>'':
#look up and update pricelist
pricelist_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'product.pricelist', 'search_read', [[['name','=',row[10]],['active', '=', True]]],{'fields': ['id'], 'limit': 1})
if pricelist_id is not None:
id = server.execute_kw(DATABASE, user_id, PASSWORD, 'res.partner', 'write', [[partner_id],{'property_product_pricelist': search(pricelist_id,'id')}])
If you are creating your application from stretch and only required Android API for Odoo, here is open-source API https://github.com/oogbox/odoo-mobile-api (Odoo android api)
To use in android, first add the following dependency to your app level build.gradle
compile 'com.oogbox.api:odoo:1.0.0'
Documentation: https://github.com/oogbox/odoo-mobile-api#getting-started
Thanks
I am creating a chatting application for android. I am using Firebase Real time database for this purpose. This is how "chats" branch of database looks like :
There are unique ID's for chat rooms generated using Users unique ID's such as "513","675" etc. Inside theese chatrooms there are message objects which also have unique ID's and inside them they store information of the date message sent, name of the sender, and the text of the message. Constructor of Message object is as follows :
public Message(String text,String senderUID, Long date){
this.text = text;
this.senderUID = senderUID;
this.date = date;
}
This is how I generate Time for the each message and send them to firebase database.
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calendar = Calendar.getInstance();
String second,hour,minute;
String time;
if(calendar.get(Calendar.SECOND)<10){
second = "0"+calendar.get(Calendar.SECOND);
}
else
{
second = ""+calendar.get(Calendar.SECOND);
}
if(calendar.get(Calendar.MINUTE)<10){
minute = "0"+calendar.get(Calendar.MINUTE);
}
else
{
minute = ""+calendar.get(Calendar.MINUTE);
}
if(calendar.get(Calendar.HOUR)<10){
hour = "0"+calendar.get(Calendar.HOUR);
}
else
{
hour = ""+calendar.get(Calendar.HOUR);
}
time = date + hour + minute + second;
Log.d("time",time);
Message message = new Message(messageEditText.getText().toString(), user.getDisplayName(), Long.valueOf(time));
chatRoomDatabaseRef.child(chatID).child(user.getUid() + generateRandomNumber()).setValue(message);
messageEditText.setText("");
}
});
Here is how I get the data from database with value event listener :
chatRoomDatabaseRef.child(chatID).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Set<Message> set = new HashSet<Message>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Message message = snapshot.getValue(Message.class);
set.add(message);
}
messageList.clear();
messageList.addAll(set);
Collections.sort(messageList, new Comparator<Message>() {
#Override
public int compare(Message o1, Message o2) {
return Long.valueOf(o1.date).compareTo(Long.valueOf(o2.date));
}
});
messageAdapter.notifyDataSetChanged();
messageListView.setSelection(messageAdapter.getCount() - 1);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
After I get the data from Firebase database I order them according to their date attribute and list them. Everything works fine but when I am filling messages' date attribute, it fills according to the local time on the phone because of that I can't sort the messages correctly. Time can differ device to device. I need to use a Time which is common and same for all the devices using my app. But I couldn't find a way.
Edit:
I still couldn't figure out but as a quick solution I created an object called sequence number in the database. I added one more attribute to the message constructor called sequence number. I read the sequence number from the database, give that number to the next message and increase the value in the database for the new messages. Then I order messages according to that number. It is not the best way to do that but it is something until I find a better way.
Try this
firebase
.database()
.ref("/.info/serverTimeOffset")
.on("value", function(offset) {
let offsetVal = offset.val() || 0;
let serverTime = Date.now() + offsetVal;
console.log("serverTime", serverTime);
});
Use as time
Message message = new Message(messageEditText.getText().toString(), user.getDisplayName(), ServerValue.TIMESTAMP);
and for retrieving it
private String getDate(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time);
String date = DateFormat.format("dd-MM-yyyy", cal).toString();
return date;
}
I've been working with Xamarin for a few weeks now and i'm trying to find the best practices for using SQLite ORM queries.I have a log in page which is launched first before users can access the app.I have the database created before this first activity comes to the screen and administrator log in details inserted into the user's table as soon as the tables are created.The point is,the admin is meant to log in and import an xml file containing all of the other user's personal information.This information is read from the file and saved to sqlite as well.
Next,the other users can log in after their details have been imported and saved successfully.My challenge is,at the point of logging in,i would like to verify the details as follows:
1.Compare the entered username with a single username from the db
**2.Check the password entered to see if it matches the username entered **
I'm currently using a select query to pull up all the passwords from the database,before comparing the two strings(from the db and from the edit text field).If it's a large db however,reading all the data will be quite expensive.How do i go about this?
How do i also look up the password for that username?
Below is my code:
namespace sample.NameSpace
{
[Activity (MainLauncher = true)]
public class MainActivity : Activity
{
Button login = FindViewById<Button> (Resource.Id.login);
try{
login.Click += (object sender, EventArgs e) => {
if (TextUtils.IsEmpty(userName.Text.ToString())) {
makeToast();
} else if (TextUtils.IsEmpty(password.Text.ToString())) {
makeToast();
} else {
returningUser = userName.Text.ToString().Trim();
returningUserPassword = password.Text.ToString().Trim();
}
//Check to see if the name is in the db already
List<string> allUsers = GetAllUserNames();
//Loop through the list of names and compare the retrieved username with the name entered in the text field
string retrievedDbName ="";
foreach(string name in allUsers )
{
retrievedDbName = name .Trim();
}
//Verify name
if(retrievedDbName .Equals(returningUser))
{
Toast.MakeText(this,
"Login Successful !", ToastLength.Short).Show();
Intent intent = new Intent (this, typeof(HomeActivity));
StartActivity (intent);
}
else
{
Toast.MakeText(this, "User Name or Password does not match", ToastLength.Short).Show();
}
};
} catch(Exception e){
logger.Exception (this, e);
}
public List<string>GetAllUserNames()
{
List<UserInfoTable> allUserNames = new List<UserInfoTable> ();
allUserNames = dataManager.GetSingleUserName ();
string name = "";
foreach(var UserName in allUserNames)
{
Console.WriteLine ("Usernames from db :" + name.ToString());
}
return allUserNames;
}
Then the DataManager class:
public List<UserInfoTable> GetSingleUserName()
{
UserInfoTable user = new UserInfoTable ();
using (var db = dbHandler.getUserDatabaseConnection ()) {
var userName = db.Query<UserInfoTable> ("select * from UserInfoTable where user_name = ?", user.USER_NAME);
return userName;
}
}
public bool CheckLogin(string user, string password)
{
bool valid = false;
using (var db = dbHandler.getUserDatabaseConnection ()) {
var user = db.Query<UserInfoTable> ("select * from UserInfoTable where user_name = ? and password = ?", user, password);
if (user != null) valid = true;
}
return valid;
}
i have a problem getting timestamp(rowversion) from my SQL Azure database.
In my tables there is a column with datatype timestamp. This timestamp isn't similar to datetime, it's more like a rowversion.
I can get all other data in this table with the query from MobileServiceTable, there is no problem.
But this special datatype is a problem.
My class for this table looks like:
public class ArbeitsgangBezeichnung {
#com.google.gson.annotations.SerializedName("id")
private int ID;
#com.google.gson.annotations.SerializedName("ABZ_ArbeitsgangBezeichnungID")
private int ABZ_ArbeitsgangBezeichnungID;
#com.google.gson.annotations.SerializedName("ABZ_Bezeichnung")
private String ABZ_Bezeichnung;
#com.google.gson.annotations.SerializedName("ABZ_RowVersion")
private StringMap<Number> ABZ_RowVersion;
//constructor, getter, setter, etc....
}
If i login in Azure and look at the table, there are my example values and the automatic generated timestamp. The timestamp value looks like "AAAAAAAAB/M=". If i login in sql database and let me show the data, then for timestamp there is only "binarydata" (in pointed brackets) and not that value as it is shown in Azure.
The variable "ABZ_RowVersion" should include this timestamp, but the data in the StringMap doesn't look like the one in Azure. I tried String and Byte as datatype for the StringMap, but it doesn't helped.
I tried byte[] for ABZ_RowVersion, but then i got an exception in the callback method.
Then i tried Object for ABZ_RowVersion, that time i found out, that it is a StringMap, but nothing more.
Does anybody know, how to get the data from timestamp, i need it for comparison.
Thanks already
When you create a timestamp column in a table, it's essentially a varbinary(8) column. In the node SQL driver, it's mapped to a Buffer type (the usual node.js type used for binary data). The object which you see ({"0":0, "1":0, ..., "length":8}) is the way that a buffer is stringified into JSON. That representation doesn't map to the default byte array representation from the Gson serializer in Android (or to the byte[] in the managed code).
To be able to use timestamp columns, the first thing you need to do is to "teach" the serializer how to understand the format of the column returned by the server. You can do that with a JsonDeserializer<byte[]> class:
public class ByteArrayFromNodeBufferGsonSerializer
implements JsonDeserializer<byte[]> {
#Override
public byte[] deserialize(JsonElement element, Type type,
JsonDeserializationContext context) throws JsonParseException {
if (element == null || element.isJsonNull()) {
return null;
} else {
JsonObject jo = element.getAsJsonObject();
int len = jo.get("length").getAsInt();
byte[] result = new byte[len];
for (int i = 0; i < len; i++) {
String key = Integer.toString(i);
result[i] = jo.get(key).getAsByte();
}
return result;
}
}
}
Now you should be able to read data. There's still another problem, though. On insert and update operations, the value of the column is sent by the client, and SQL doesn't let you set them in them. So let's take this class:
public class Test {
#SerializedName("id")
private int mId;
#SerializedName("name")
private String mName;
#SerializedName("version")
private byte[] mVersion;
public int getId() { return mId; }
public void setId(int id) { this.mId = id; }
public String getName() { return mName; }
public void setName(String name) { this.mName = name; }
public byte[] getVersion() { return mVersion; }
public void setVersion(byte[] version) { this.mVersion = version; }
}
On the insert and update operations, the first thing we need to do in the server-side script is to remove that property from the object. And there's another issue: after the insert is done, the runtime doesn't return the rowversion property (i.e., it doesn't update the item variable. So we need to perform a lookup against the DB to retrieve that column as well:
function insert(item, user, request) {
delete item.version;
request.execute({
success: function() {
tables.current.lookup(item.id, {
success: function(inserted) {
request.respond(201, inserted);
}
});
}
});
}
And the same on update:
function update(item, user, request) {
delete item.version;
request.execute({
success: function() {
tables.current.lookup(item.id, {
success: function(updated) {
request.respond(200, updated);
}
});
}
});
}
Now, this definitely is a lot of work - the support for this type of column should be better. I've created a feature request in the UserVoice page at http://mobileservices.uservoice.com/forums/182281-feature-requests/suggestions/4670504-better-support-for-timestamp-columns, so feel free to vote it up to help the team prioritize it.