Tutorials > Contracts > Extend the existing contract model
Create the enterprise bean
To add a new terms and conditions object for use in WebSphere Commerce, create an Enterprise Java Bean to represent it. In this task, you create a new CMP enterprise bean to represent the term and condition subtype.
Note that typically when creating new enterprise beans, you would place the beans into the WebSphereCommerceServerExtensionsData project, rather than including them into one of the EJB groups that contain WebSphere Commerce entity beans. In this case however, since all new entity beans for terms and conditions must inherit from the WebSphere Commerce TermCondition bean, place the new term and condition beans into the Enablement-RelationshipManagementData project. The following sections describe how to create the new enterprise bean, using the tools in the WebSphere Commerce development environment.
Migration note: If you have created custom term and condition EJB beans and use an ejbPostCreate() method, ensure that you do not call the parseXMLElement() in that method.
Procedure
- Create a new enterprise bean
- Select File > Import . In the Import wizard:
- Select EJB > EJB JAR file, and click Next.
- For EJB Jar file, click Browse... and navigate to the WC_EAR directory. Then choose Enablement-RelationshipManagementData.jar and click Open. This will also put Enablement-RelationshipManagementData in the EJB project field.
- Click Finish. The project is created.
- Right-click the Enablement-RelationshipManagementData project and select New > Enterprise Bean (1.x-2.x). The Enterprise Bean (1.x-2.x) wizard opens.
- In the Create an Enterprise Bean window:
- In the EJB Project list, the Enablement-RelationshipManagementData is already selected.
- Select Entity bean with container-managed persistence (CMP) fields
- In the Bean name field, enter an appropriate name for the bean. For this example, enter MySubTC.
- In the Source folder field, leave the default value that is specified (ejbModule).
- In the Default package field, enter com.ibm.commerce.contract.objects.
- In the CMP vlist, select 1.x.
- Click Next.
- In the Enterprise Bean Details window:
- From the Bean supertype list, select TermCondition.
- Click Add to add a new CMP attribute. The Create CMP Attribute window opens. In this window:
- In the Name field, enter an appropriate name for the new CMP field. For this example, enter attr1.
- In the Type field, enter the appropriate data type for the field. For this example, enter String.
- Select the Access with getter and setter methods check box.
- Select Promote getter and setter methods to remote interface check box.
- Clear the Make getter read-only check box.
- Click Apply.
- Create another attribute. In the Name field, enter attr2.
- In the Type field, enter the appropriate data type for the field. For this example, enter Integer.
- Clear the Make getter read-only check box.
- Click Apply.
- Click Close to close this window.
- Click Finish. The bean will be created.
- A class diagram may appear in RAD. Close this without saving.
- Map the fields from the new bean into the TERMCOND table
The next step is to map the fields from the new bean to columns in the TERMCOND table.
To create this mapping:
- Switch to the Navigator view.
- Expand the following folders:
- Enablement-RelationshipManagementData > ejbModule > META-INF
- Enablement-RelationshipManagementData > ejbModule > META-INF > backend > database
- Double-click the Map.mapxmi file.
- In the Enterprise Beans pane, expand the TermCondition bean, then expand the MySubTC bean, so that its attributes can be viewed.
- In the Tables pane, expand the TERMCOND table so that its columns can be viewed.
- Drag the MySubTC bean onto the TERMCOND table.
- Drag the attr1 field from the MySubTC onto the STRINGFIELD3 column in the TERMCOND table.
- Drag the attr2 field from the MySubTC onto the INTEGERFIELD3 column in the TERMCOND table.
- Save the changes.
- Add a new ejbCreate method
- In the Enterprise explorer view, navigate to Enablement-RelationshipManagementData > ejbModule > com.ibm.commerce.objects and double-click the MySubTCBean class to open it and view its source code.
- The file contains an error. Replace the implementation of the ejbRemove() method with the following code:
public void ejbRemove() throws javax.ejb.RemoveException { _removeLinks(); }
- Create a new ejbCreate(Long, Element) method, by adding the following code into the class:
public com.ibm.commerce.contract.objects.TermConditionKey ejbCreate( java.lang.Long argTradingId, org.w3c.dom.Element argElement) throws javax.ejb.CreateException, javax.ejb.FinderException, javax.naming.NamingException, javax.ejb.RemoveException { _initLinks(); super.ejbCreate(argTradingId, argElement); this.attr1 = null; this.attr2 = null; return null; }
- Save the code changes.
- Add the new ejbCreate(Long, Element) method to the home interface. This makes the method available in the generated access bean.
To add the method to the home interface:
- Right-click the ejbCreate(Long, Element) method in the Outline view and select Enterprise Bean (1.x-2.x) > Promote to Home Interface.
- Edit MySubTCHome and remove the old create method:
public com.ibm.commerce.contract.objects.MySubTC create(java.lang.Long referenceNumber) throws javax.ejb.CreateException, java.rmi.RemoteException;
- Add a new ejbPostCreate method
Next create a new ejbPostCreate(Long, Element) method so that it has the same parameters as the ejbCreate(Long, Element) method, by doing the following:
- Double-click the MySubTCBean class to open it and view its source code.
- Create a new ejbPostCreate(Long, Element) method, by adding the following code into the class:
public void ejbPostCreate(java.lang.Long argTradingId, org.w3c.dom.Element argElement) throws javax.ejb.CreateException, javax.ejb.FinderException, javax.naming.NamingException, javax.ejb.RemoveException { }
- Save the code changes.
- Add a parseXMLElement method
- Double-click the MySubTCBean class to open it and view its source code.
- Add the following import statements:
import org.w3c.dom.Element; import com.ibm.commerce.contract.helper.ContractUtil;
- Add the method:
public void parseXMLElement(org.w3c.dom.Element argElement) throws javax.ejb.CreateException, javax.ejb.FinderException, javax.naming.NamingException, javax.ejb.RemoveException { super.parseXMLElement(argElement); if (argElement == null) { return; } String nodeName = argElement.getNodeName(); if (nodeName.equals("TCCopy")) { return; } this.attr1 = argElement.getAttribute("attr1").trim(); this.attr2 = new Integer(argElement.getAttribute("attr2").trim()); // get element "ProductSetPolicyRef" from "MySubTC" Element ePolicyReference = null; ePolicyReference = ContractUtil.getElementByTag(argElement, "ProductSetPolicyRef"); parseElementPolicyReference(ePolicyReference); }
- Save the work.
- Add a createNewVersion method
- Double-click the MySubTCBean class to open it and view its source code.
- Add the method:
public Long createNewVersion(Long argNewTradingId) throws javax.ejb.CreateException, javax.ejb.FinderException, javax.naming.NamingException, javax.ejb.RemoveException, org.xml.sax.SAXException, java.io.IOException { // Contract a seqElement since tcSequence cannot be null Element seqElement = ContractUtil.getSeqElementFromTCSequence(this.tcSequence); MySubTCAccessBean newTC = new MySubTCAccessBean(argNewTradingId, seqElement); Long newTCId = newTC.getReferenceNumber(); newTC.setInitKey_referenceNumber(newTCId); newTC.setMandatoryFlag(this.mandatoryFlag); newTC.setChangeableFlag(this.changeableFlag); // set columns for this specific TC newTC.setAttr1(this.attr1); newTC.setAttr2(this.attr2); newTC.commitCopyHelper(); return newTCId; }
- Note that this method will not compile until the access bean is created and additional methods are added to the access bean. You will create this access bean in the next task.
- Save the work.
- Add a getXMLString method
- Double-click the MySubTCBean class to open it and view its source code.
- Add the two methods:
public String getXMLString() throws javax.ejb.CreateException, javax.ejb.FinderException, javax.naming.NamingException { return getXMLString(false); } public String getXMLString(boolean tcdata) throws javax.ejb.CreateException, javax.ejb.FinderException, javax.naming.NamingException { String xmlTC = " <MySubTC %TC_DATA% " + " attr1=\"" + this.attr1 + "\" attr2=\"" + this.attr2.toString() + "\">" + "%TC_DESC%" + "%PARTICIPANT%" + "%XML_POLICYREFERENCE%" + " </MySubTC>"; xmlTC = ContractUtil.replace(xmlTC, "%TC_DATA%", getXMLStringForTCData(tcdata)); String xmlPolicy = getXMLStringForElementPolicyReference("ProductSet"); xmlTC = ContractUtil.replace(xmlTC, "%XML_POLICYREFERENCE%", xmlPolicy); xmlTC = ContractUtil.replaceAll(xmlTC, "%POLICY_REF_TYPE%", "ProductSetPolicyRef"); return xmlTC; }
- Save the work.
- Add a markForDelete method
- Double-click the MySubTCBean class to open it and view its source code.
- Add the method:
public void markForDelete() throws javax.ejb.CreateException, javax.ejb.FinderException, javax.naming.NamingException { // code: remove entries from associated tables which // cannot be deleted though delete cascade }
- Save the work.
- Update the remote interface
- Double-click the MySubTC class to open it and view its source code.
- Add the five interface methods:
public String getXMLString() throws javax.ejb.CreateException, javax.ejb.FinderException, java.rmi.RemoteException, javax.naming.NamingException; public String getXMLString(boolean tcdata) throws javax.ejb.CreateException, javax.ejb.FinderException, java.rmi.RemoteException, javax.naming.NamingException; public void parseXMLElement(org.w3c.dom.Element argElement) throws javax.ejb.CreateException, javax.ejb.FinderException, java.rmi.RemoteException, javax.naming.NamingException, javax.ejb.RemoveException; public Long createNewVersion(Long argNewTradingId) throws javax.ejb.CreateException, javax.ejb.FinderException, java.rmi.RemoteException, javax.naming.NamingException, javax.ejb.RemoveException, org.xml.sax.SAXException, java.io.IOException; public void markForDelete() throws javax.ejb.CreateException, javax.ejb.FinderException, java.rmi.RemoteException, javax.naming.NamingException;
- Save the changes.
Previous topic: Register the new terms and conditions object
Next topic: Create an access bean and deploying