Composite entity pattern: Difference between revisions

Content deleted Content added
Sample code: format code
Line 28:
==Sample code==
Sample code for a Professional Service Automation application (PSA) in which the resource object is implemented via composite entity pattern, may look like as follows (entity implements coarse-grained object):
<source lang="Javajava">
package corepatterns.apps.psa.ejb;
 
Line 40:
 
public class ResourceEntity implements EntityBean {
public String employeeId;
public String lastName;
public String firstName;
public String departmentId;
public String practiceGroup;
public String title;
public String grade;
public String email;
public String phone;
public String cell;
public String pager;
public String managerId;
// Collection of BlockOutTime Dependent objects
public Collection blockoutTimes;
 
// Collection of SkillSet Dependent objects
public Collection skillSets;
 
...
 
private EntityContext context;
// Entity Bean methods implementation
public String ejbCreate(ResourceTO resource) throws
CreateException {
try {
this.employeeId = resource.employeeId;
setResourceData(resource);
getResourceDAO().create(resource);
} catch(Exception ex) {
throw new EJBException("Reason:" + ...);
}
return this.employeeId;
}
public String ejbFindByPrimaryKey(String primaryKey)
throws FinderException {
boolean result;
try {
ResourceDAO resourceDAO = getResourceDAO();
result =
resourceDAO.selectByPrimaryKey(primaryKey);
} catch(Exception ex) {
throw new EJBException("Reason:" + ...);
}
if (result) {
return primaryKey;
}
else {
throw new ObjectNotFoundException(...);
}
}
if(result) {
public void ejbRemove() {
return primaryKey;
try {
// Remove dependent objects
if (this.skillSets != null) {
 
SkillSetDAO skillSetDAO = getSkillSetDAO();
skillSetDAO.setResourceID(employeeId);
skillSetDAO.deleteAll();
skillSets = null;
}
if (this.blockoutTime != null) {
BlockOutTimeDAO blockouttimeDAO =
getBlockOutTimeDAO();
blockouttimeDAO.setResourceID(employeeId);
blockouttimeDAO.deleteAll();
blockOutTimes = null;
}
 
// Remove the resource from the persistent store
ResourceDAO resourceDAO = new
ResourceDAO(employeeId);
resourceDAO.delete();
} catch(ResourceException ex) {
throw new EJBException("Reason:"+...);
} catch(BlockOutTimeException ex) {
throw new EJBException("Reason:"+...);
} catch(Exception exception) {
...
}
}
public void setEntityContext(EntityContext context)
else {
{
throw new ObjectNotFoundException(...);
this.context = context;
}
public void unsetEntityContext() {
context = null;
}
public void ejbActivate() {
employeeId = (String)context.getPrimaryKey();
}
public void ejbPassivate() {
employeeId = null;
}
public void ejbLoad() {
try {
// load the resource info from
ResourceDAO resourceDAO = getResourceDAO();
setResourceData((ResourceTO)
resourceDAO.load(employeeId));
// Load other dependent objects, if necessary
...
} catch(Exception ex) {
throw new EJBException("Reason:" + ...);
}
}
}
public void ejbStore() {
public void ejbRemove() try {
// Store resource information
try {
getResourceDAO().update(getResourceData());
// Remove dependent objects
if(this.skillSets != null) {
 
// Store dependent objects as needed
SkillSetDAO skillSetDAO = getSkillSetDAO();
...
skillSetDAO.setResourceID(employeeId);
} catch(SkillSetException ex) {
skillSetDAO.deleteAll();
throw new EJBException("Reason:" + ...);
skillSets = null;
} catch(BlockOutTimeException ex) {
}
throw new EJBException("Reason:" + ...);
if(this.blockoutTime != null) {
}
BlockOutTimeDAO blockouttimeDAO =
getBlockOutTimeDAO();...
blockouttimeDAO.setResourceID(employeeId);
blockouttimeDAO.deleteAll();
blockOutTimes = null;
}
 
// Remove the resource from the persistent store
ResourceDAO resourceDAO = new
ResourceDAO(employeeId);
resourceDAO.delete();
} catch(ResourceException ex) {
throw new EJBException("Reason:"+...);
} catch(BlockOutTimeException ex) {
throw new EJBException("Reason:"+...);
} catch(Exception exception) {
...
}
public void ejbPostCreate(ResourceTO resource) {
}
public void setEntityContext(EntityContext context)
{
this.context = context;
}
public void unsetEntityContext() {
context = null;
}
public void ejbActivate() {
employeeId = (String)context.getPrimaryKey();
}
public void ejbPassivate() {
employeeId = null;
}
public void ejbLoad() {
try {
// load the resource info from
ResourceDAO resourceDAO = getResourceDAO();
setResourceData((ResourceTO)
resourceDAO.load(employeeId));
// Load other dependent objects, if necessary
...
} catch(Exception ex) {
throw new EJBException("Reason:" + ...);
}
}
public void ejbStore() {
try {
// Store resource information
getResourceDAO().update(getResourceData());
 
// StoreMethod to dependentGet objectsResource asTransfer neededObject
public ResourceTO getResourceTO() {
...
// create a new Resource Transfer Object
} catch(SkillSetException ex) {
ResourceTO resourceTO = new
throw new EJBException("Reason:" + ...);
ResourceTO(employeeId);
} catch(BlockOutTimeException ex) {
 
throw new EJBException("Reason:" + ...);
// copy all values
resourceTO.lastName = lastName;
resourceTO.firstName = firstName;
resourceTO.departmentId = departmentId;
...
return resourceTO;
}
...
}
public void ejbPostCreate(ResourceTO resource) {
}
 
public void setResourceData(ResourceTO resourceTO) {
// Method to Get Resource Transfer Object
// copy values from Transfer Object into entity bean
public ResourceTO getResourceTO() {
employeeId = resourceTO.employeeId;
// create a new Resource Transfer Object
lastName = resourceTO.lastName;
ResourceTO resourceTO = new
ResourceTO(employeeId);...
}
 
// Method to get dependent Transfer Objects
// copy all values
public Collection getSkillSetsData() {
resourceTO.lastName = lastName;
// If skillSets is not loaded, load it first.
resourceTO.firstName = firstName;
// See Lazy Load strategy implementation.
resourceTO.departmentId = departmentId;
 
return skillSets;
}
...
return resourceTO;
}
 
// other get and set methods as needed
public void setResourceData(ResourceTO resourceTO) {
// copy values from Transfer Object into entity bean
employeeId = resourceTO.employeeId;
lastName = resourceTO.lastName;
...
}
 
// Entity bean business methods
// Method to get dependent Transfer Objects
public Collectionvoid getSkillSetsDataaddBlockOutTimes(Collection moreBOTs) {
throws BlockOutTimeException {
// If skillSets is not loaded, load it first.
// Note: moreBOTs is a collection of
// See Lazy Load strategy implementation.
// BlockOutTimeTO objects
 
return skillSets; try {
Iterator moreIter = moreBOTs.iterator();
}
while (moreIter.hasNext()) {
...
BlockOutTimeTO botTO = (BlockOutTimeTO)
 
moreIter.next();
// other get and set methods as needed
if (! (blockOutTimeExists(botTO))) {
...
// add BlockOutTimeTO to collection
 
botTO.setNew();
// Entity bean business methods
blockOutTime.add(botTO);
public void addBlockOutTimes(Collection moreBOTs)
} else {
throws BlockOutTimeException {
// BlockOutTimeTO already exists, cannot add
// Note: moreBOTs is a collection of
throw new BlockOutTimeException(...);
// BlockOutTimeTO objects
try { }
}
Iterator moreIter = moreBOTs.iterator();
while } catch(moreIter.hasNext()Exception exception) {
throw new EJBException(...);
BlockOutTimeTO botTO = (BlockOutTimeTO)
moreIter.next();
if (! (blockOutTimeExists(botTO))) {
// add BlockOutTimeTO to collection
botTO.setNew();
blockOutTime.add(botTO);
} else {
// BlockOutTimeTO already exists, cannot add
throw new BlockOutTimeException(...);
}
}
} catch(Exception exception) {
throw new EJBException(...);
}
}
 
public void addSkillSet(Collection moreSkills)
throws SkillSetException {
// similar to addBlockOutTime() implementation
...
}
 
...
}
 
public void updateBlockOutTime(Collection updBOTs)
...
throws BlockOutTimeException {
 
try {
public void updateBlockOutTime(Collection updBOTs)
Iterator botIter = blockOutTimes.iterator();
throws BlockOutTimeException {
Iterator updIter = updBOTs.iterator();
try {
Iterator botIter = blockOutTimes while (updIter.iteratorhasNext();) {
BlockOutTimeTO botTO = (BlockOutTimeTO)
Iterator updIter = updBOTs.iterator();
while ( updIter.hasNextnext()) {;
BlockOutTimeTO botTO = while (BlockOutTimeTObotIter.hasNext()) {
updIter.next(); BlockOutTimeTO existingBOT =
while (BlockOutTimeTO) botIter.hasNextnext()) {;
// compare key values to locate BlockOutTime
BlockOutTimeTO existingBOT =
(BlockOutTimeTO) botIter if (existingBOT.nextequals(botTO);) {
// compare key values to locate // Found BlockOutTime in collection
// replace old BlockOutTimeTO with new one
if (existingBOT.equals(botTO)) {
botTO.setDirty(); //modified old dependent
// Found BlockOutTime in collection
// replace old BlockOutTimeTO with botTO.resetNew(); //not a new onedependent
botTO.setDirty(); //modified old dependent existingBOT = botTO;
botTO.resetNew(); //not a new dependent }
existingBOT = botTO; }
}
} catch (Exception exc) {
throw new EJBException(...);
}
}
} catch (Exception exc) {
throw new EJBException(...);
}
}
 
public void updateSkillSet(Collection updSkills)
throws CommitmentException {
// similar to updateBlockOutTime...
...
}
 
...
}
 
...
 
}
 
</source><ref name=":o_cjp"/>