Entity Abstract

From JRapid

Jump to: navigation, search

Contents

Description

Inheritance is a powerful tool for reusing and compartmentalizing code in object-oriented programming. JRapid provides a flexible way for you to extend classes you create through the designer while keeping your additional code separate from the auto-generated code. Just about anywhere code is automatically created, JRapid creates an abstract class and then gives you a class that inherits from the abstract one. In this way the necessary functionality is separated from any customized code you place in the extending class.

All entities implement jrapid.entities.Entity, a serializable interface. Once an entity is created, you will wind up with the abstract one implementing Entity and the one you can extend. Be careful not to change the abstract class as changes will be overwritten.

Abstract Class Contents

In addition to the getters and setters, the abstract class will have several additional items.

  • A static method for accessing the particular entity's Data Access Object (DAO)
  • A protected method for accessing a com.jrapid.dao.DAO whose generic type is a class implementing Entity
  • A public toString method determining how the object is displayed in the application when printed as text.
  • A getStyle method returning a string that can be used to specify stylesheet settings for font, color etc.
  • A remove method that will delete this object from the DAO accessible above
  • A store method that will save the object in the DAO. If the object is persistent and linked to a record in the database it will return its ID. If not, it will return 1.
  • A long id value and a getter and setter providing access to it.

Example

For a newly generated class called Company, you will have CompanyAbstract.java and Company.java created in the entities folder of your application. The CompanyAbstract class should not be edited. It is auto-generated and anything you place in that class will be overwritten the next time the java files are created. However, with an understanding of the structure of these classes, anything you might want to do for the company object can be accomplished with simple changes to the class contained in Company.java.

The company class I created has a name which is a string, an address which is also a string, an expiration date and a boolean called active. These properties are created and placed in the CompanyAbstract.java class. They are created as private variables and have getters and setters automatically created allowing access to the underlying data. The names of the getters and setters are standard camel-case format.


    <entity label="Company" menu="menu" name="Company">
        <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>

To implement behavior for the company, one can simply override a method in the abstract class to either add or replace behavior. For example, if I wanted to make the company active any time that a user adds an expiration date in the future, I could simply override the setExpirationDate(Calendar expiration) method in the Company class:

public class Company extends CompanyAbstract  {    
   /*** Override in CompanyAbstract. */
   public void setExpiration(java.util.GregorianCalendar expiration) {
       super.setExpiration(expiration); // Set the expiration date
       
       // Create a calendar to compare against the expiration
       GregorianCalendar gc = new GregorianCalendar();
       
       // If the expiration date is in the future, set the company to active
       if(expiration.after(gc)) {
           setActive(true);
       }
   }
}

In this particular example, note that I still need to call the super class setExpiration, so that the updated expiration date is not discarded. Once that value is saved I check the date against the date right now, and set the company to active if the expiration is in the future.

See also

Personal tools