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.

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.
This method is used to rollback the transaction. Similar to commitAndClose() Method, this method will rollback all the open transactions.
By default, the active filters will be enabled. Consider the line below.
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.
This method is used to check whether a particular record exists 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,
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]).

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 filterThis 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_idTo 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]).
Add a comment