Services

From JRapid

(Redirected from Service)
Jump to: navigation, search

Contents

Description

Services are an integral part of a JRapid application. They are automatically generated, extendable and provide access to and manipulation of the underlying entities. Understanding how JRapid applications create these services, and what methods are created when you add functionality will explain how the application organizes, displays and persists information.

Applications automatically generate a single MainController class that handles CRUD (Create/Retrieve/Update/Delete) operations and provides other services for each entity modeled. The main controller delegates calls to the specific service for that entity. There are several default CRUD methods generated for every entity, but based on whether the entity uses defaultsets, subsets, filters or auto-suggest functionality there may be additional methods created.

Creating a an entity will automatically create two classes in the services package, <EntityName>Services and <EntityName>ServicesAbstract. The services class extends the abstract class, and can be overwritten to provide additional functionality. The methods contained in the abstract class should not be edited. They are automatically generated and changes there will be lost.

The methods will be exported as REST web services from the main controller. You may read more about how these methods are exported in the controller article.

The below methods are created for a simple entity with a unique name, a boolean marking whether it is active, and a date for when it expires. The id value parameters below are strings representing numerical values uniquely defining the object on the backend.


Auto-Generated Methods

  • Note, the id value here is a String, and Entity refers to your particular entity.
public Boolean remove(String id)
A service to remove a particular entity by id.
public Boolean removeMany(Collection<String> ids)
This service allows many entities to be removed at once passing in a collection of string ids.
public Entity find(String id)
Return the entity object referenced by a particular entity id.
public Object store(String id, Entity voobj)
Persist the entity instance to the database.
protected Object parse(String filter, String value)
Parse is used to filter out results, but without a filter simply returns null.
public Collection<Entity> findPage(Map<String, String> params)
Used to support pagination, showing results in pages.
public Boolean checkUniqueForName(String value, String id)
Provided because the entity object is uniquely identified by its name. This is only necessary when unique is checked for a property.


Filters

When a filter is added on an entity, the parse method is updated allowing the service to check for the property's value.

protected Object parse(String filter, String value) {
   if ("activeFilter".equals(filter)) {
      return new com.jrapid.controller.servers.castor.BooleanHandler().convertUponSet(value);        
   }
   return null;
}

DefaultSets

Creating a defaultset adds two new methods in the. These methods will be called store<Name of the defaultset> and find<Name of the defaultset> in camel-case.

public Object storeDefaultEntity(Entity voobj)
Ensures the entity instance has an id value before persisting. It will call the store method above.
public Entity findDefaultEntity()
Returns an entity where the default values are set.

Subsets

An additional service method is created when a subset called byActiveNotExpired is created for the entity by adding the prefix “findSubset”:

public Collection<Entity> findSubsetByActiveNotExpired(String entityActiveId, Map<String, String> params)

I created a subset that takes a boolean parameter for whether or not the entity is active and then checks where the expiration date is in the future. The service filters the results to match my subset based on the entityActiveId and the map of parameters.

Auto-Suggest

Auto-suggest will also add code to the abstract service class for looking up the suggestions in the UI. Adding the following will bring back entities with a similar name to what is being entered:

<property display="primary" label="Name" name="name" required="required" type="string"     unique="unique">
   <suggest expr="SELECT e.name FROM Entity as e WHERE e.name LIKE concat('%', ?, '%')"/>
</property>

This will create the following method that will return a collection of entity names to suggest to the user and the method will be name suggest<Name of field>.

public Collection<String> suggestName(String prefix)

Example

    <entity label="Company" menu="menu" name="Company">
        <subset name="byActiveNotExpired">
            <param name="companyActive" type="boolean"/>
            <condition field="active" value="companyActive"/>
            <condition field="expiration" ge="now()"/>
        </subset>

        <defaultset name="defaultCompanies">
            <default name="active" value="true"/>
        </defaultset>

        <property display="primary" label="Name" name="name" required="required" type="string" unique="unique">
        <suggest expr="SELECT c.name FROM Company as c WHERE c.name LIKE concat('%', ?, '%')"/>
        </property>
        <property label="Address" name="address" type="text"/>
        <property label="Expiration" name="expiration" type="date"/>
        <property label="Active" name="active" type="boolean"/>

        <filter display="primary" label="Active Filter" name="activeFilter" property="active"/>
    </entity>

This entity containing a filter, subset and defaultset auto creates the following:

public abstract class CompanyServicesAbstract extends com.jrapid.services.Services {
	// find by id and remove
	public Boolean remove(String id)...
	
	// remove multiples with a list of ids
	public Boolean removeMany(Collection<String> ids)...

	// find the entity by id
	public Company find(String id)...
	
	// persist the entity
	public Object store(String id, Company voobj)...
	
	// for pagination
	public Collection<Company> findPage(Map<String, String> params)...
	
	// for subset
	public Collection<Company> findSubsetByActiveNotExpired(String companyActiveId, Map<String, String> params)...
	
	// code for filters, returns null when no filter exists
	protected Object parse(String filter, String value)...
	
	// code for suggest
	public Collection<String> suggestName(String prefix)...
			
	// code for unique
	public Boolean checkUniqueForName(String value, String id)...
	
	// code for defaultsets	
	public Object storeDefaultCompanies(Company voobj)...
	public Company findDefaultCompanies()...
}

The code generated for the basic methods makes use of the DAO classes and methods to perform the operations that require access to the persistence layer.

See also

Personal tools