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

  1. Create a new enterprise bean

    1. Select File > Import . In the Import wizard:

      1. Select EJB > EJB JAR file, and click Next.

      2. 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.

      3. Click Finish. The project is created.

    2. Right-click the Enablement-RelationshipManagementData project and select New > Enterprise Bean (1.x-2.x). The Enterprise Bean (1.x-2.x) wizard opens.

    3. In the Create an Enterprise Bean window:

      1. In the EJB Project list, the Enablement-RelationshipManagementData is already selected.

      2. Select Entity bean with container-managed persistence (CMP) fields

      3. In the Bean name field, enter an appropriate name for the bean. For this example, enter MySubTC.

      4. In the Source folder field, leave the default value that is specified (ejbModule).

      5. In the Default package field, enter com.ibm.commerce.contract.objects.

      6. In the CMP vlist, select 1.x.

      7. Click Next.

    4. In the Enterprise Bean Details window:

      1. From the Bean supertype list, select TermCondition.

      2. Click Add to add a new CMP attribute. The Create CMP Attribute window opens. In this window:

        1. In the Name field, enter an appropriate name for the new CMP field. For this example, enter attr1.

        2. In the Type field, enter the appropriate data type for the field. For this example, enter String.

        3. Select the Access with getter and setter methods check box.

        4. Select Promote getter and setter methods to remote interface check box.

        5. Clear the Make getter read-only check box.

        6. Click Apply.

        7. Create another attribute. In the Name field, enter attr2.

        8. In the Type field, enter the appropriate data type for the field. For this example, enter Integer.

        9. Clear the Make getter read-only check box.

        10. Click Apply.

        11. Click Close to close this window.

    5. Click Finish. The bean will be created.

    6. A class diagram may appear in RAD. Close this without saving.

  2. 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:

    1. Switch to the Navigator view.

    2. Expand the following folders:

      • Enablement-RelationshipManagementData > ejbModule > META-INF

      • Enablement-RelationshipManagementData > ejbModule > META-INF > backend > database

    3. Double-click the Map.mapxmi file.

    4. In the Enterprise Beans pane, expand the TermCondition bean, then expand the MySubTC bean, so that its attributes can be viewed.

    5. In the Tables pane, expand the TERMCOND table so that its columns can be viewed.

    6. Drag the MySubTC bean onto the TERMCOND table.

    7. Drag the attr1 field from the MySubTC onto the STRINGFIELD3 column in the TERMCOND table.

    8. Drag the attr2 field from the MySubTC onto the INTEGERFIELD3 column in the TERMCOND table.

    9. Save the changes.

  3. Add a new ejbCreate method

    1. 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.

    2. The file contains an error. Replace the implementation of the ejbRemove() method with the following code:

          public void ejbRemove() throws javax.ejb.RemoveException {
              _removeLinks();     }
                  
      

    3. 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;     }
                  
      

    4. Save the code changes.

    5. 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:

      1. Right-click the ejbCreate(Long, Element) method in the Outline view and select Enterprise Bean (1.x-2.x) > Promote to Home Interface.

    6. 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;        
      

  4. 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:

    1. Double-click the MySubTCBean class to open it and view its source code.

    2. 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 {
          
          }
                  
      

    3. Save the code changes.

  5. Add a parseXMLElement method

    1. Double-click the MySubTCBean class to open it and view its source code.

    2. Add the following import statements:

          import org.w3c.dom.Element;     import com.ibm.commerce.contract.helper.ContractUtil;         
      

    3. 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);     }
                  
      

    4. Save the work.

  6. Add a createNewVersion method

    1. Double-click the MySubTCBean class to open it and view its source code.

    2. 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;     }
                  
      

    3. 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.

    4. Save the work.

  7. Add a getXMLString method

    1. Double-click the MySubTCBean class to open it and view its source code.

    2. 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;     }
                  
      

    3. Save the work.

  8. Add a markForDelete method

    1. Double-click the MySubTCBean class to open it and view its source code.

    2. 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
          }
                  
      

    3. Save the work.

  9. Update the remote interface

    1. Double-click the MySubTC class to open it and view its source code.

    2. 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;             
      

    3. Save the changes.


Previous topic: Register the new terms and conditions object


Next topic: Create an access bean and deploying


+

Search Tips   |   Advanced Search