The properties of a given enterprise object (its attributes and relationships) do not usually provide all the values or data an application needs to be useful. To be of any value to your business, you usually need to add custom business logic in an application. The data in database tables usually stores raw business data. In order to make meaningful results from that data, you need to write business logic, usually in the form of business methods.
A business method in an enterprise object class returns a value based on data in the enterprise object’s properties. In the Real Estate model, you could write a business method to return the number of listings above a certain selling price that were sold by a particular agent.
This business method seeks information regarding a particular agent, so it should be implemented in the Agent business logic class. The method requires an Agent enterprise object and uses an Agent’s listings relationship and two attributes of a Listing enterprise object, isSold and sellingPrice, to determine the desired business information. Examples of this method appear in “Listing 4-1,” which assumes Listing is an EOGenericRecord subclass and in “Listing 4-2,” which assumes Listing is an EOCustomObject subclass.
Listing 3-1 Business method to determine information about the properties sold by a particular agent (assuming EOGenericRecord)
public int listingsSoldAbovePrice(String targetSellingPrice) { |
int hitCount = 0; |
NSArray listings = listings(); |
java.util.Enumeration enum = listings.objectEnumerator(); |
while (enum.hasMoreElements()) { |
webobjectsexamples.realestate.common.Listing listing = (webobjectsexamples.realestate.common.Listing)enum.nextElement(); |
int isSold = ((Integer)listing.valueForKey("isSold")).intValue(); |
int sellingPrice = ((Integer)listing.valueForKey("sellingPrice")).intValue(); |
if ((isSold == 1) && (targetSellingPrice >= sellingPrice)) { |
hitCount++; |
} |
} |
return hitCount; |
} |
Listing 3-2 Business method to determine information about the properties sold by a particular agent (assuming EOCustomObject)
public int listingsSoldAbovePrice(BigDecimal targetSellingPrice) { |
int hitCount = 0; |
NSArray listings = listings(); |
java.util.Enumeration enum = listings.objectEnumerator(); |
while (enum.hasMoreElements()) { |
webobjectsexamples.realestate.common.Listing listing = (webobjectsexamples.realestate.common.Listing)enum.nextElement(); |
Boolean isSold = listing.isSold(); |
int sellingPrice = (listing.sellingPrice()).intValue(); |
if ((isSold) && (targetSellingPrice.intValue() >= sellingPrice)) { |
hitCount++; |
} |
} |
return hitCount; |
} |
Within the Enterprise Objects frameworks, there are a number of other mechanisms you can use to derive business data. At the model level, you can specify custom read and write formats for particular attributes to coerce their values when they are read from the database and written back to the database. Also at the model level, you can define derived attributes that use custom SQL you write to derive business data. Both of these techniques are discussed in EOModeler User Guide.
The Foundation framework also provides mechanisms that help you write business logic. The class NSArray.Operator provides a number of operators that provide common information about a set of business data. These operators are listed in “Table 4-1.”
In the Real Estate business logic framework (located in /Developer/Examples/JavaWebObjects/Frameworks/JavaRealEstate), the webobjectsexamples.realestate.server.Agent class includes a method that uses the @avg operator. It is shown in “Listing 4-3.”
Listing 3-3 A business method using an NSArray operator
public Number averageRating() { |
if (_averageRating == null) { |
_averageRating = (Number)(ratings().valueForKey("@avg.rating.rating")); |
} |
return _averageRating; |
} |
Each of the operators requires an array of objects whose data type is a java.lang.Number (which includes the concrete classes java.lang.Integer and java.lang.BigDecimal). As shown in “Listing 4-3,” you use the operators within an invocation of the key-value coding method valueForKey. All the operators except @count require you to specify both an array of objects and the element within the array on which to apply the operator.
Last updated: 2007-07-11