Service Layer - List/Search Service Example

The following example is using the Country Hibernate Object to perform a Search/List on the database(if you have not created the Hibernate Object click here). The developer has to define the Business Class and the xml that will plug it to the application

Normally you should receive from an analyst a descriptive document like the following

Name CountryListBC
Package org.jprovocateur.archetype.businesslayer.country
Description Search for countries based on object input parameters and the predefined conditions in InitMetadata
Input Object Country
Execution type LIST
Transactional false
InitMetadata name: startwith
Order it desc based on the name
initialize Default
validate Default
execute Default

The above contains all the business rules that the developer needs to know in order to create the specific service.

Implementation of the Service

To implement a service

  • Extend the BusinessLogic class and define the type of the object that is required as input
  • Use spring annotations to define it as a service and if transaction is required. The scope of the service is always prototype.
  • define the genericDao you want to use (GenericDAO unless you implement your own) .
  • define the execution type(LIST/CREATE/UPDATE/DELETE/GET).

 

CountryListService.java
package org.jprovocateur.archetype.businesslayer.country;

import javax.annotation.Resource;

import org.jprovocateur.archetype.objectmodel.country.Country;
import org.jprovocateur.basis.datalayer.GenericDaoDBInt;
import org.jprovocateur.basis.error.BasisException;
import org.jprovocateur.basis.serviceslayer.BusinessLogicInt;
import org.jprovocateur.basis.serviceslayer.impl.BusinessLogic;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



@Service("countryListService")
@Scope("prototype")
@Transactional(readOnly = true)
public class CountryListService extends BusinessLogic{
	
	@Resource(name="genericDaoDB")
	private GenericDaoDBInt dao;
	

	public GenericDaoDBInt getDao() {
		return dao;
	}

	public String getExecutionType() {
		return BusinessLogicInt.LIST;
	}	
	
	public void initMetadata(Country country) throws BasisException {
		this.metaData.restrictionStartsWith("name", country.getName());
	 
	}
	

}

How it Works

You have defined a class called CountryListBC that extends BasisLogic. The above class receives an Object as input and based on the specified metadata defined in initMetadata method, will query the database.

    You can define multiple criteria for a hibernate object in the metadata object. Bellow you can find a full list of methods to define metadata :
  1. restrictionGreaterEqual ( String field , Object value ):
  2. restrictionLessEqual ( String field , Object value )
  3. restrictionEndsWith ( String field , String value )
  4. restrictionEndsWith ( String field , String value, boolean ignoreCase )
  5. orderAsc ( String field )
  6. orderDesc ( String field )
  7. restrictionStartsWith ( String field , String value )
  8. restrictionStartsWith ( String field , String value, boolean ignoreCase )
  9. restrictionContains ( String field , String value )
  10. restrictionContains ( String field , String value, boolean ignoreCase )
  11. restrictionIn ( String field , String value )
  12. restrictionIn ( String field , List value )
  13. restrictionIsEmpty ( String field )
  14. restrictionIsNotEmptyNotNul ( String field )
  15. restrictionIsNull ( String field )
  16. restrictionIsNotNul ( String field )
  17. restrictionIsNotEmpty ( String field )
  18. restrictionBetween ( String field , Object lowValue , Object hiValue )

Declaring the Service and defining Access Rights

In order to make the service available you have to register it in the database and defined its basic characteristics. All services are registered in table BasisClass.

Column Value Description
CLASSNAME com.exampleServices.businesslayer.businesslogic.country.CountryListBC the class of the service
DISABLED 0 defines if the service is disabled, possible values 0(false)/1(true)
Defining the Access Rights

Security is based on roles. Each service are accessible only if the user has one of the configured roles. Jprovocateur is offering a web interface to manage user, roles, service and access rights.

    Important tables in the database are
  • Users:users of the application
  • Roles:roles configured in the application
  • UserRights:assigned roles to the user.
  • Access_Rights: Services and roles relation (which role can access which serice)
  • Service: Your services

JUnit Client Creation

The following JUnit class will run the service.

package com.exampleServices.businesslayer.businesslogic.country;

import java.util.List;
import java.util.Vector;

import org.jprovocateur.businesslayer.businesslogic.BasisObject;
import org.jprovocateur.businesslayer.businesslogic.Services;
import org.jprovocateur.datalayer.data.MetaData;
import org.jprovocateur.user.User;

import com.exampleServices.businesslayer.objectmodel.country.Country;

import junit.framework.TestCase;

public class CountryListBCTest extends TestCase {
	
	
	Long id = null;

	MetaData metaData;
	User user;
	Services service = new Services();

	protected void setUp() throws Exception {
		super.setUp();

		// create an empty instance of the metaData object
		metaData = new MetaData();
		
		// Create a user object and assign roles, in order to simulate an
		// authenticated user
		user = new User();
		List roles = new Vector();
		roles.add("ADMIN");
		user.setRoles(roles);
		
		

	}
	
	
	
	
	
	
	

	public void testCountryListService() {

		System.out
				.println("=========================countryListService===================================");


		Country country = new Country();
	
		BasisObject basisObject = service.getBC("countryListService", country, metaData,
				user, false);

		System.out.println(basisObject.getXML());
	
		assertEquals("0", basisObject.result);

	}

}

 
Design downloaded from free website templates.
 
Web Templates created with FlashMint. .