PATH |
Describes how to create a new instances of custom objects and EOGenericRecords, both types of enterprise objects.
New enterprise objects are created when your application adds a new instance of an entity. For example,
Enterprise objects encapsulate the attributes and behaviors that define the object. When you only need to get and set the object's attributes, the default EOGenericRecord suffices as the business object. EOGenericRecords are part of EOF, and provide functionality to create, read, update, and delete information from a variety of data stores. When your EO must have its own behavior in addition to the get and set behaviors, you need to create a custom enterprise object and add the extra functionality to it.
The programming examples below outline how new instances of EOGenericRecord and custom enterprise objects are created in Java, Objective-C, and WebScript. The Objective-C and WebScript code is identical. Each example assumes you have used EOModeler to define both your application's business objects and the mapping between the object's attributes and the data store schema's tables and columns. In the code examples, the Movie entity has two attributes: title and revenue.
Only basic get and set behavior is supported in this application.
import com.apple.yellow.foundation.*; import com.apple.yellow.webobjects.*; import com.apple.yellow.eocontrol.*; public class Main extends WOComponent { /** @TypeInfo Movie */ protected EOEnterpriseObject aMovie; public WOComponent makeAMovie() { String entName = "Movie"; // Name of entity within eomodeld EOEditingContext ec = session().defaultEditingContext(); EOClassDescription movieClassDesc; // create a Movie instance movieClassDesc = EOClassDescription(). classDescriptionForEntityName(entName); aMovie = (EOEnterpriseObject) movieClassDesc. createInstanceWithEditingContext(ec, null); // set some default values. aMovie.takeValueForKey("DefaultTitle", "title"); aMovie.takeValueForKey("1000000", "revenue"); return null; // updates the current component } }
/** @TypeInfo Movie */ id aMovie; - (WOComponent *)makeAMovie { EOEditingContext *ec = [[self session] defaultEditingContext]; aMovie = [[EOClassDescription classDescriptionForEntityName: @"Movie"] createInstanceWithEditingContext: ec globalID: nil zone: nil]; [aMovie takeValue:@"DefaultTitle" forKey: @"title"]; [aMovie takeValue:@"1000000" forKey: @"revenue"]; return nil; }
You can add your own business logic to this code.
import com.apple.yellow.foundation.*; import com.apple.yellow.webobjects.*; import com.apple.yellow.eocontrol.*; import Movie; public class Main extends WOComponent { Movie aMovie; public WOComponent makeAMovie() { // create an instance of a Movie aMovie = new Movie(null,null,null); // set default values aMovie.setTitle("DefaultTitle"); aMovie.setRevenue(new Float("1000000")); return null; // return the updated component. } }
Also link Movie.java into the application by adding it to the Classes folder within Project Builder. Generating Movie.java from EOModeler provides you with the common get and set methods. Additional business logic can be added directly to the Movie.java file.
// // Created on Mon Oct 26 22:43:39 1998 by Apple EOModeler Version 377 import com.apple.yellow.foundation.*; import com.apple.yellow.eocontrol.*; import java.util.*; import java.math.BigDecimal; public class Movie extends EOCustomObject { protected Number revenue; protected String title; // This is the constructor that EOF uses. // Later (perhaps upon a willRead()) it will be populated with // values via EOCustomObject's takeValueForKey() method. public Movie(EOEditingContext context, EOClassDescription classDesc, EOGlobalID gid) { super(context, classDesc, gid); } public String category() { willRead(); return category; } public void setCategory(String value) { willChange(); category = value; } public Number revenue() { willRead(); return revenue; } public void setRevenue(Number value) { willChange(); revenue = value; } public String title() { willRead(); return title; } public void setTitle(String value) { willChange(); title = value; } } }
//************** // Main.wos //************** // Assumes Movie.h and Movie.m have been linked into your application. // (Movie.h was generated from EOModeler and is shown below) // Movie *aMovie; - (WOComponent *)makeAMovie { // Create new instance of a Movie aMovie = [[Movie alloc] init]; // Set some of the attrs of the Movie [aMovie setTitle: @"Default Title"]; [aMovie setRevenue: @"1000000"]; return nil; }
//************************************* // Movie.h - generated from EOModeler //************************************* #import <EOControl/EOControl.h> @interface Movie : NSObject <NSCoding> { NSNumber *revenue; NSString *title; } - (void)setRevenue:(NSNumber *)value; - (NSNumber *)revenue; - (void)setTitle:(NSString *)value; - (NSString *)title; @end
9 July 1998, Rich Flewelling. First Draft.
28 October 1998. Clif Liu. Second Draft.