We have seen how various entities in the JIRA database are defined and how we can introduce new entities. In this recipe, we will see how we can read and write data from the database using these entity definitions.
JIRA exposes the OfBizDelegator
(https://docs.atlassian.com/jira/latest/com/atlassian/jira/ofbiz/OfBizDelegator.html) component, which is a wrapper around org.ofbiz.core.entity.DelegatorInterface
, to communicate with its database using the Ofbiz layer.
You can get hold of an instance of OfBizDelegator
by injecting it in the constructor or from ComponentAccessor
, as follows:
OfBizDelegator delegator = ComponentAccessor.getOfBizDelegator();
We can read from the database using the various methods exposed via the previous delegator class. For example, all the records in the Employee table we defined in the previous recipe can be read as:
List<GenericValue> employees = delegator.findAll("Employee");
Here, the findAll
method takes the entity name (not the table name) and returns a list of the GenericValue
objects, each representing a row in the table. The individual fields can be read from the object using the name of the field (not col-name), as follows:
Long id = employees.get(0).getLong("id"); String name = employees.get(0).getString("name");
The data type, to which the field should be converted, can be found from the field-type
mapping XML like we saw in the previous recipe.
We can read data from a database, when certain conditions are satisfied, using the findByAnd
method:
List<GenericValue> employees = delegator.findByAnd("Employee", MapBuilder.build("company", "J-Tricks"));
This will return all the records where the company name is J-Tricks. You can enforce more complex conditions using the findByCondition
method and select only the interested fields, as follows:
List<String> fieldList = new ArrayList<String>(); fieldList.add("id"); fieldList.add("name"); List<GenericValue> employees = this.delegator.findByCondition("Employee", new EntityExpr("id", EntityOperator.LESS_THAN, "15000"), fieldList);
Here, we find all employee records with an id less than 15000 and we retrieve only the id and name of the employees.
The findListIteratorByCondition
method can be used to add more options such as the orderBy
clause, the where conditions, and the having conditions, as follows:
EntityFindOptions entityFindOptions = new EntityFindOptions(); entityFindOptions.scrollInsensitive(); entityFindOptions.setResultSetConcurrency(ResultSet.CONCUR_READ_ONLY); entityFindOptions.setDistinct(true); OfBizListIterator iterator = this.delegator.findListIteratorByCondition("Employee", new EntityExpr("id", EntityOperator.LESS_THAN, "15000"), null, UtilMisc.toList("name"), UtilMisc.toList("name"), entityFindOptions); List<GenericValue> employees = iterator.getCompleteList(); iterator.close();
Here, we search for all records with an id less than 15000. We don't have a having
condition in this case and, so, we will leave it null
. The next two arguments specify that only the name field needs to be selected and the records should be ordered by the name field. The last argument specifies the EntityFindOptions
class.
Here, we define EntityFindOptions
with scrollInsensitive()
which is same as using both setSpecifyTypeAndConcur(true)
and setResultSetType(TYPE_SCROLL_INSENSITIVE)
. If specifyTypeAndConcur
is true, the following two parameters will be used to specify resultSetType
and resultSetConcurrency
. If false, the default values of the JDBC driver will be used. In our case, specifyTypeAndConcur
is true and, hence, resultSetType
is taken as TYPE_SCROLL_INSENSITIVE
and resultSetConcurrency
is taken as CONCUR_READ_ONLY
. More about this and the possible values can be found at http://download.oracle.com/javase/tutorial/jdbc/basics/retrieving.html.
As you can see, we have also set the distinct
option to be true. Apparently, this is the only way to make a distinct selection using Entity Engine. You will find more information about this in the Entity Engine cookbook at http://www.opensourcestrategies.com/ofbiz/ofbiz_entity_cookbook.txt.
Creating a new record in a table using OfBizDelegator
is pretty easy, as shown in the following code snippet:
Map<String, Object> employeeDetails = new HashMap<String, Object>(); employeeDetails.put("name", "Some Guy1"); employeeDetails.put("address", "Some Address1"); employeeDetails.put("company", "J-Tricks"); GenericValue employee = this.delegator.createValue("Employee", employeeDetails);
Writing a record is done by retrieving the record, modifying the values, and using the store()
method. Once the record is retrieved, as we saw earlier in this recipe, we can modify the fields as shown below:
employee.setString("name","New Name"); employee.store();
More useful methods can be found in the Java docs at https://docs.atlassian.com/jira/latest/com/atlassian/jira/ofbiz/OfBizDelegator.html.
3.145.177.115