Methods in the service layer, upon performing all the business logic, call methods in the repository layer to persist or lookup data i.e. it interacts with the configured data store. For executing the queries, JdbcTemplate class is used. JdbcTemplate takes care of the creation and release of resources such as creating and closing the connection etc. All database operations namely insert, update, search and delete can be performed on the database using methods of JdbcTemplate class.
Steps
On DIGIT the create and update operations are handled asynchronously.
The persister service listens on the topic to which service applications are pushed for insertion and updation. Persister then takes care of executing insert and update operations on the database without clogging the application’s threads.
The execution of search queries on the database returns applications as per the search parameters provided by the user.
Implement Repository Layer
Define POJOs - The Address object is defined in the common contract (refer to the API spec). Link it to the birth registration table via the registrationId as defined in the DB schema.
Create packages - Add thequerybuilder and rowmapper packages within the repository folder.
Create a class - by the name of BirthApplicationQueryBuilder in querybuilder folder and annotate it with @Component annotation.
Insert the following content in BirthApplicationQueryBuilder class -
packagedigit.repository.querybuilder;importdigit.web.models.BirthApplicationSearchCriteria;importorg.springframework.stereotype.Component;importorg.springframework.util.CollectionUtils;importorg.springframework.util.ObjectUtils;importjava.util.List;@ComponentpublicclassBirthApplicationQueryBuilder { private static final String BASE_BTR_QUERY = " SELECT btr.id as bid, btr.tenantid as btenantid, btr.applicationnumber as bapplicationnumber, btr.babyfirstname as bbabyfirstname, btr.babylastname as bbabylastname, btr.fatherid as bfatherid, btr.motherid as bmotherid, btr.doctorname as bdoctorname, btr.hospitalname as bhospitalname, btr.placeofbirth as bplaceofbirth, btr.timeofbirth as btimeofbirth, btr.createdby as bcreatedby, btr.lastmodifiedby as blastmodifiedby, btr.createdtime as bcreatedtime, btr.lastmodifiedtime as blastmodifiedtime, ";
private static final String ADDRESS_SELECT_QUERY = " add.id as aid, add.tenantid as atenantid, add.doorno as adoorno, add.latitude as alatitude, add.longitude as alongitude, add.buildingname as abuildingname, add.addressid as aaddressid, add.addressnumber as aaddressnumber, add.type as atype, add.addressline1 as aaddressline1, add.addressline2 as aaddressline2, add.landmark as alandmark, add.street as astreet, add.city as acity, add.locality as alocality, add.pincode as apincode, add.detail as adetail, add.registrationid as aregistrationid ";
privatestaticfinalString FROM_TABLES =" FROM eg_bt_registration btr LEFT JOIN eg_bt_address add ON btr.id = add.registrationid ";privatefinalString ORDERBY_CREATEDTIME =" ORDER BY btr.createdtime DESC ";publicStringgetBirthApplicationSearchQuery(BirthApplicationSearchCriteria criteria,List<Object> preparedStmtList){StringBuilder query =newStringBuilder(BASE_BTR_QUERY);query.append(ADDRESS_SELECT_QUERY);query.append(FROM_TABLES);if(!ObjectUtils.isEmpty(criteria.getTenantId())){addClauseIfRequired(query, preparedStmtList);query.append(" btr.tenantid = ? ");preparedStmtList.add(criteria.getTenantId()); }if(!ObjectUtils.isEmpty(criteria.getStatus())){addClauseIfRequired(query, preparedStmtList);query.append(" btr.status = ? ");preparedStmtList.add(criteria.getStatus()); }if(!CollectionUtils.isEmpty(criteria.getIds())){addClauseIfRequired(query, preparedStmtList);query.append(" btr.id IN ( ").append(createQuery(criteria.getIds())).append(" ) ");addToPreparedStatement(preparedStmtList,criteria.getIds()); }if(!ObjectUtils.isEmpty(criteria.getApplicationNumber())){addClauseIfRequired(query, preparedStmtList);query.append(" btr.applicationnumber = ? ");preparedStmtList.add(criteria.getApplicationNumber()); }// order birth registration applications based on their createdtime in latest first mannerquery.append(ORDERBY_CREATEDTIME);returnquery.toString(); }privatevoidaddClauseIfRequired(StringBuilder query,List<Object> preparedStmtList){if(preparedStmtList.isEmpty()){query.append(" WHERE "); }else{query.append(" AND "); } }privateStringcreateQuery(List<String> ids) {StringBuilder builder =newStringBuilder();int length =ids.size();for (int i =0; i < length; i++) {builder.append(" ?");if (i != length -1)builder.append(","); }returnbuilder.toString(); }privatevoidaddToPreparedStatement(List<Object> preparedStmtList,List<String> ids) {ids.forEach(id -> {preparedStmtList.add(id); }); }}
Create a class - by the name of BirthApplicationRowMapper within the rowmapper package and annotate it with @Component.