📄 customer.java
字号:
import java.util.*;
import org.jlf.log.*;
import org.jlf.dataMap.*;
/**
* This class hold information for a customer so we can track their
* recording purchases and interests.
*/
public class Customer extends DataMappedObject {
// --------------------------------------------------------------
// Section 1: DataMappedObject descriptors
// --------------------------------------------------------------
// Constants for attributes
public static final String ID_ATTRIBUTE = "CUSTOMER_ID";
public static final String CUSTOMER_TYPE_ID_ATTRIBUTE =
"CUSTOMER_TYPE_ID";
public static final String NICKNAME_ATTRIBUTE = "NICKNAME";
public static final String EMAIL_ADDRESS_ATTRIBUTE = "EMAIL_ADDRESS";
// Used for optimistic locking
public static final String VERSION_ATTRIBUTE = "VERSION";
// Constants for relationships
public static final String CUSTOMER_TYPE_RELATIONSHIP = "customerType";
public static final String CUSTOMER_TRACKED_RECORDINGS_RELATIONSHIP =
"customerTrackedRecordings";
/**
* Sets up a table of attribute descriptors for the Customer
* object.
*/
protected Hashtable basicGetAttributeDescriptors() {
// Always call the superclass!
Hashtable descriptors = super.basicGetAttributeDescriptors();
DataAttributeDescriptor descriptor;
// Then add class-specific attributes
descriptor = new DataAttributeDescriptor(ID_ATTRIBUTE,
LongAttribute.class, true);
descriptor.setIsKeyField(true);
descriptors.put(ID_ATTRIBUTE, descriptor);
descriptor = new DataAttributeDescriptor(CUSTOMER_TYPE_ID_ATTRIBUTE,
LongAttribute.class, true);
descriptors.put(CUSTOMER_TYPE_ID_ATTRIBUTE, descriptor);
descriptor = new DataAttributeDescriptor(NICKNAME_ATTRIBUTE,
StringAttribute.class, false);
descriptor.setMaximumLength(20);
descriptors.put(NICKNAME_ATTRIBUTE, descriptor);
descriptor = new DataAttributeDescriptor(EMAIL_ADDRESS_ATTRIBUTE,
StringAttribute.class, false);
descriptor.setMaximumLength(50);
descriptors.put(EMAIL_ADDRESS_ATTRIBUTE, descriptor);
descriptor = new DataAttributeDescriptor(VERSION_ATTRIBUTE,
LongAttribute.class, false);
descriptors.put(VERSION_ATTRIBUTE, descriptor);
return descriptors;
}
/**
* Sets up the relationships from this object to its
* customer, recording, and tracking type.
*/
protected Hashtable basicGetRelationshipDescriptors() {
// Always call the superclass!
Hashtable descriptors = super.basicGetRelationshipDescriptors();
// Add new relationships for this class
descriptors
.put(CUSTOMER_TRACKED_RECORDINGS_RELATIONSHIP,
new RelationshipDescriptor(CUSTOMER_TRACKED_RECORDINGS_RELATIONSHIP,
CustomerTrackedRecording.class));
descriptors.put(CUSTOMER_TYPE_RELATIONSHIP,
new RelationshipDescriptor(CUSTOMER_TYPE_RELATIONSHIP,
CustomerType.class));
return descriptors;
}
/**
* This class has a subclass hierarchy!
*/
protected boolean hasHierarchyTable() {
return true;
}
/**
* This method defines the attributes used in the hierarchy table.
*/
protected Vector basicGetHierarchyTableKeyAttributes() {
Vector hierarchyTableAttributes =
super.basicGetHierarchyTableKeyAttributes();
hierarchyTableAttributes.addElement(CUSTOMER_TYPE_ID_ATTRIBUTE);
return hierarchyTableAttributes;
}
/**
* This method defines the hierarchy mappings for this
* class and any subclasses. Use
* the <code>DataMappedObjectKey</code> as the key and a
* <code>Class</code> object as the value.<p>
*
* At this level in the hierarchy, no hierarchy table members
* are known. Return an empty hash table that can be populated
* by the subclass(es).
*/
protected Hashtable basicGetHierarchyTable() {
Hashtable hierarchyTable = super.basicGetHierarchyTable();
// Customer Type "Generic Customer" is this class.
// Construct an object key as such.
DataMappedObjectKey dmoKey =
new DataMappedObjectKey(CUSTOMER_TYPE_ID_ATTRIBUTE,
CustomerType.GENERIC_CUSTOMER_ID);
hierarchyTable.put(dmoKey, Customer.class);
// Customer Type "Frequent Buyer" is subclass FrequentBuyer.
// Construct an object key as such.
dmoKey = new DataMappedObjectKey(CUSTOMER_TYPE_ID_ATTRIBUTE,
CustomerType.FREQUENT_BUYER_ID);
hierarchyTable.put(dmoKey, FrequentBuyer.class);
// Customer Type "Corporate Customer" is subclass Corporation.
// Construct an object key as such.
dmoKey = new DataMappedObjectKey(CUSTOMER_TYPE_ID_ATTRIBUTE,
CustomerType.CORPORATE_CUSTOMER_ID);
hierarchyTable.put(dmoKey, Corporation.class);
// Customer Type "Non-Profit Corp." is subclass NonProfitCorporation.
// Construct an object key as such.
dmoKey = new DataMappedObjectKey(CUSTOMER_TYPE_ID_ATTRIBUTE,
CustomerType.NON_PROFIT_CUSTOMER_ID);
hierarchyTable.put(dmoKey, NonProfitCorporation.class);
return hierarchyTable;
}
/**
* Default constructor must be public for the data mapping
* framework! However, when creating a new object, look to use
* a paramterized constructor.
*/
public Customer() {}
// --------------------------------------------------------------
// Section 2: Java Bean methods
// --------------------------------------------------------------
/**
* Main constructor for the class. Takes a nickname and
* email address, categorizes the customer as a generic customer
* type.
*/
public Customer(String nickname, String emailAddress) {
setNickname(nickname);
setEmailAddress(emailAddress);
setCustomerType(CustomerType.GENERIC_CUSTOMER);
// Set initial object version to 1, increment every time the
// object is saved
setVersion(1);
}
/**
* Retrieves the database id of the Customer.
*/
public long getCustomerId() {
return getLongAttribute(ID_ATTRIBUTE);
}
/**
* Retrieves the customer's nickname.
*/
public String getNickname() {
return getStringAttribute(NICKNAME_ATTRIBUTE);
}
/**
* Sets the customer's nickname.
*/
public void setNickname(String nickname) {
setStringAttribute(NICKNAME_ATTRIBUTE, nickname);
}
/**
* Retrieves the customer's email address.
*/
public String getEmailAddress() {
return getStringAttribute(EMAIL_ADDRESS_ATTRIBUTE);
}
/**
* Sets the customer's email address.
*/
public void setEmailAddress(String emailAddress) {
setStringAttribute(EMAIL_ADDRESS_ATTRIBUTE, emailAddress);
}
/**
* Retrieves the version of the Customer object.
*/
public long getVersion() {
return getLongAttribute(VERSION_ATTRIBUTE);
}
/**
* Sets the version of the Customer object.
*/
private void setVersion(long version) {
setLongAttribute(VERSION_ATTRIBUTE, version);
}
/**
* Retrieves the Customer Type.
*/
public CustomerType getCustomerType() {
return (CustomerType) getRelatedObject(CUSTOMER_TYPE_RELATIONSHIP);
}
/**
* Sets the Customer Type.
*/
public void setCustomerType(CustomerType customerType) {
if (getRelatedObject(CUSTOMER_TYPE_RELATIONSHIP) != null) {
throw new DataMapError("Customer Type already set!" +
"Use resetCustomerType() method instead",
null, Log.ERROR_LEVEL);
}
addRelatedObject(CUSTOMER_TYPE_RELATIONSHIP, customerType);
setLongAttribute(CUSTOMER_TYPE_ID_ATTRIBUTE,
customerType.getCustomerTypeId());
}
/**
* Resets the Customer Type once set.
*/
protected void resetCustomerType(CustomerType customerType) {
Vector customerTypes = getRelatedObjects(CUSTOMER_TYPE_RELATIONSHIP);
customerTypes.removeAllElements();
setCustomerType(customerType);
}
/**
* Retrieves the CustomerTrackedRecording objects
* related to this object.
*/
public Vector getCustomerTrackedRecordings() {
return getRelatedObjects(CUSTOMER_TRACKED_RECORDINGS_RELATIONSHIP);
}
/**
* Adds a CustomerTrackedRecording object
* related to this object.
*/
public void addCustomerTrackedRecording(CustomerTrackedRecording ctr) {
addRelatedObject(CUSTOMER_TRACKED_RECORDINGS_RELATIONSHIP, ctr);
}
// --------------------------------------------------------------
// Section 3: Methods to map the class to the database
// --------------------------------------------------------------
/**
* Finds zero to many Customer objects that have a nickname
* like the input string, returning them in a Vector.
*/
public static Vector findByNickname(String likeNickname) {
// Create a new customer and populate the nickname to look for
Customer customer = new Customer();
customer.setNickname(likeNickname);
// Have the framework execute the query
return customer.findManyBySearchCriteria("findByNickname");
}
/**
* Finds zero to many Customer objects that have an email address
* like the input string, returning them in a Vector.
*/
public static Vector findByEmailAddress(String likeEmailAddress) {
// Create a new customer and populate the email address
// to look for
Customer customer = new Customer();
customer.setEmailAddress(likeEmailAddress);
// Have the framework execute the query
return customer.findManyBySearchCriteria("findByEmailAddress");
}
/**
* Finds zero to many Customer objects that have customer tracking
* records in the system that match the CustomerTrackedRecording
* object with a Recording artist and title like the title and artist in
* CustomerTrackedRecording's recording object, and a tracking type
* like the one in the CustomerTrackedRecording's tracking type object.
*/
public static Vector findByTrackingCriteria(CustomerTrackedRecording customerTrackedRecording) {
// Create a new customer and populate the title to look for
Customer customer = new Customer();
customer.addCustomerTrackedRecording(customerTrackedRecording);
// Have the framework execute the query
return customer.findManyBySearchCriteria("findByTrackingCriteria");
}
// --------------------------------------------------------------
// Section 4: Methods to make the class self-contained
// --------------------------------------------------------------
/**
* Prints the contents of all variables.
*/
public String toString() {
return "Customer: " + getCustomerId() + "\n Nickname: "
+ getNickname() + "\n Email: " + getEmailAddress();
}
/**
* Helper method to output a set of Customer objects
* in a Vector to System.out.
*/
public static void outputCustomers(Vector customers) {
Customer customer;
Enumeration e = customers.elements();
while (e.hasMoreElements()) {
customer = (Customer) e.nextElement();
// Print to output stream
System.out.println("\n" + customer);
}
}
/**
* Main method to test the capabilities above. CRUDs the
* database by creating 4 customers, reading them back,
* updates the nickname on one and reads it back, and
* deletes all 4.
*/
public static void main(String[] args) {
Customer cust1, cust2, cust3, cust4;
DataMapper dataMapper = null;
try {
cust1 = new Customer("shiney", "shiney@yahoo.com");
cust2 = new Customer("charlemange", "charle@hotmail.com");
cust3 = new Customer("choo", "choo@aol.com");
cust4 = new Customer("honey", "honey@yahoo.com");
dataMapper = cust1.getDefaultDataMapper();
// Create the customers in the database
cust1.write(dataMapper);
cust2.write(dataMapper);
cust3.write(dataMapper);
cust4.write(dataMapper);
dataMapper.commitWrites();
// Read the customers back and output
System.out.println("\nYou should see 4 customers:");
Vector cCustomers = Customer.findByNickname("c%");
outputCustomers(cCustomers);
Vector yahooCustomers = Customer.findByEmailAddress("%@yahoo.com");
outputCustomers(yahooCustomers);
// Update customer 1 nickname
cust1.setNickname("sunshine");
cust1.write(dataMapper);
dataMapper.commitWrites();
// Read record back and make sure update made it
System.out
.println("\nThis customer should have a nickname of sunshine");
Vector updatedCustomer = Customer.findByNickname("sunshine");
outputCustomers(updatedCustomer);
// Delete all customers
cust1.deleteOnWrite();
cust1.write(dataMapper);
cust2.deleteOnWrite();
cust2.write(dataMapper);
cust3.deleteOnWrite();
cust3.write(dataMapper);
cust4.deleteOnWrite();
cust4.write(dataMapper);
dataMapper.commitWrites();
// Read all customers back, shouldn't be any
System.out.println("\nDeleted all, shouldn't be any customers:");
Vector allCustomers = Customer.findByNickname("%");
outputCustomers(allCustomers);
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (dataMapper != null) {
dataMapper.close();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -