Skip to main content

Understanding OBDal in Openbravo


OBDal is an extensively used class in Openbravo. If offers lots of useful functions to enable external access to the Data Access Layer. OBDal provides the basic functions like insert,save,remove, etc. OBDal forms the base for the Data Access Layer(DAL) in Openbravo.
DAL Architecture
Though there are lots of useful functions in OBDal, we often use only few basic functions like save, remove, etc. Lets see the use of  the methods in OBDal and its usage.
1. void commitAndClose():
This function commits the transaction and closes the transaction. It will not only commit the current transaction but all the pending transactions. However, having more than one open transactions at a time is not advisable. There are few reasons why we should not have more than one open transactions. One of the reason is, if one of the open transactions failed to commit, all the pending transactions will be rolled back. This can be used in the code as follows.
OBDal.getInstance().commitAndClose();
There may be situations where we still need the transaction but just commit it. But when we use this method, it will not only commit but also closes the transaction. In this scenario, we can make use of the class SessionHandler( This class is exclusively for maintaining hibernate session for transactions). This class has a method commitAndStart(). This method will commit the transaction and starts a new session. This can be used in the code as follows.
SessionHandler.getInstance().commitAndStart();
2. void rollbackAndClose()
This method is used to rollback the transaction. Similar to commitAndClose() Method, this method will rollback all the open transactions.
OBDal.getInstance().rollbackAndClose();
The SessionHandler has a method rollback() which can be used when we don't want to close the session but rollback the transaction.
SessionHandler.getInstance().rollback();
The SessionHandler class has two other methods that enables us to mark the transactions that needs to be rolled back. This can be done using the method, void setDoRollback(boolean). If the argument is true, then the transaction will be marked for rollback and if it is false, the transaction will not be marked for rollback. The other method is boolean getDoRollback() which will return whether the transaction is marked for rollback or not.
SessionHandler.getInstance().setDoRollback(true);
 SessionHandler.getInstance().getDoRollback();
3. void disableActiveFilter() and void enableActiveFilter()
By default, the active filters will be enabled. Consider the line below.
Client clientList = OBDal.getInstance().get(Client.class,null); //Client is the class which we are referring and null denotes there is no filter
This will return all the records in ad_client  table that are active. We can disable this by using the method disableActiveFilter(). After disabling, the above line will return all the records irrespective of either the record is active or not. enableActiveFilter() is used to enable the filter. This methods can be used as follows.
OBDal.getInstance().disableActiveFilter();
 Client clientList = OBDal.getInstance().get(Client.class,null);
 for(Client client : clientList.list()) {
 //Processing
 }
 OBDal.getIstance().enableActiveFilter();
4. boolean isActiveFilterEnabled()
This method is used to verify whether the active filter is enabled or not. At any point of time, the active filter can be enabled and disabled and this method is useful to verify the status.
if(OBDal.getInstance().isActiveFilterEnabled()) {
 OBDal.getInstance().disableActiveFilter();
 }
5. boolean exists(String entityName, Object id)
This method is used to check whether a particular record exists in the database.

OBDal.getInstance().exists(ADClient,"0");//ADClient is the entity name for Client Class and "0" refers to ad_client_id
To understand the use of this method, consider the code below.
Client clientList = OBDal.getInstance().get(Client.class,"45");
 try {
 String name = clientList.getName();
 } catch(Exception e) {
 log.info(e.getMessage());
 }
In the above code, if the record with ad_client_id = "45" doesn't exists, it will throw java.lang.NullPointerException since we are trying to access the value(clientList.getName()). To avoid this, we can use exists() method as follows.
Client clientList = OBDal.getInstance().get(Client.class,"45");
 if(clientList.getInstance().exists(ADClient,"45"))  {
 try {
 String name = clientList.getName();
 } catch(Exception e) {
 log.info(e.getMessage());
 }
The above code will be executed only if the record is present in the database.
6. String getReadableClientsInClause() & String getReadableOrganizationsInClause()
This method returns an in-clause string of the clients that are readable by the current user. The In-Clause String looks like ("0","1000000"). This method is useful in many scenarios. Below is one such scenario.
While retrieving the records using OBDal, it will automatically take care of the security. However, when we use HQL Queries, we need to take care of the security. In order to retrieve the client and organization readable by the user, we can use this method.
For example,
String hql = "SELECT name  "
 +"FROM ADRole r"
 +"WHERE r.organization IN "+OBDal.getInstance().getReadableOrganizationsInClause()
 +" AND r.client IN "+OBDal.getInstance().getReadableClientsInClause()
 +" AND r.active=true ";
 Query query = OBDal.getInstance().getSession().createQuery(hql);
This query will return the name of the roles that are readable for the current user.
These are few methods that I felt important in OBDal that would ease development using DAL. For details on how to write DAL code, refer ([1] and [2]).

Comments

Popular posts from this blog

My 10 years in Application Development

It's been around 10 years since I started programming. May be a bit more, if I have to include my first attempts at C programming in school. But it would be a grave insult to call remembering ten lines of code and just replicating it to put a star on the screen. I also did a course on Visual basic, but it was more gimmicks that Application development. But it was truly in the year 2007 when I started my Master in Computer Applications that I found a new flare for developing applications. 
I was very late to computers in general and even when I joined my Masters, computers were not really accessible to me. But when I joined my Masters where one is actually expected to hone the skills which they already have, but in case it was were I was learning the skills, initially it was very difficult. I was not sure what computers had for me nor I had a natural flair for programming. But the thing about computers that attracted me was the visual medium it gave. Initially it was just a door t…

Changing Timezone in Postgresql, Ubuntu

Timezone and locale are integral concepts that one should be aware of when implementing a System. For example. if you create a database in PostgreSql, it will use the default System settings for the database. Lets say you have a server in UK and your client is US. If the client tries to retrieve the current date or time, it is going to show the UK time and not the US one.
Today I encountered a different issue in our client place where the Ubuntu timezone and PostgreSql timezone were showing a incorrect values. Instead of Indian Standard Time (IST) it was showing Pacific Daylight Time(PDT). These are the steps that I followed to change the timezone to IST in PostgreSql and Ubuntu.
Changing timezone in Ubuntu:
sudo dpkg-reconfigure tzdataYou will be prompted to choose the continent and then the area. Provided screen shots where we configure the location.
Use TAB key to navigate to the OK button and then press Enter.

The changes will get reflected immediately. To check it you can open a n…

கிறுக்கனின் கிறுக்கல்கள் - அழுகையின் சிறப்பு

அழுகை என்பதை வெறுத்தவன் நான்... என்னை ஒரு நாள் பிரிந்திருக்க முடியாமல் , கைப்பேசியில் நீ சிந்திய உன் கண்ணீர் ஓசை என் காதில் கேட்கும் வரை...

பின்குறிப்பு: கிறுக்கனின் கிறுக்கல்கள் எங்கிருந்தோ தழுவப்பட்டது போல் தோன்றினால் கிறுக்கன் ( நான் உண்மையை ஏற்றுக்கொள்பவன்..:) ) பொறுப்பல்ல. கிறுக்கனின் கிறுக்கல்களில் வரும் கிறுக்கல்கள் யாவும் என் சிறய மூளையில் உதித்ததே ஆகும்.  Inspirations could have been drawn from somewhere else, but its the execution that counts....Isn't it ?