Tutorials > Program model > Create new business logic

< Previous | Next >


Modify MyNewTaskCmd

In this step of the tutorial, you modify MyNewTaskCmd to determine if the user name included in the URL is that of a registered user. You also modify MyNewDataBean to handle fields from the task command, for example, the user name. Correspondingly, you modify MyNewJSPTemplate to display whether or not the user is registered.

In this section of the tutorial, we will learn how to use URL parameters and access existing WebSphere Commerce information from within the own customized code.

Modify the new task command for user name validation

Modify the new task command to validate that the user name passed in through the URL is that of a registered user:


Procedure

  1. Open the MyNewTaskCmd.java interface to view its source code.

  2. Uncomment Section 3 to introduce the following code into the interface:

    /// Section //////////
    
      public void setFoundUserId(java.lang.String inputUserId);
      public java.lang.String getFoundUserId();
      
      public void setUserRegistryAccessBean(UserRegistryAccessBean rrb);
      
    /// End of section/////////
    

  3. Save the work.

  4. Open the MyNewTaskCmdImpl.java class to view its source code. Uncomment Import section 1 to introduce the following import statement into the code:

    /// Import section ///////////
    import com.ibm.commerce.user.objects.UserRegistryAccessBean
    ///  End of Import section ////../images/locale/screensnap////
    

  5. Uncomment Sections 2A and 2B to create the new fields and getter and setter methods that correspond to the methods added into the interface. You introduce the following code into the class:

  6. In the Outline view, select the validateParameters method and examine its source code. Uncomment Section 1, to introduce the following code into the method:

    // section /////////////
    
    // use UserRegistryAccessBean to check user Id
    
      try {
         
        if (rrb!=null){
          setFoundUserId(rrb.getUserId()); 
        } else {
          rrb =new UserRegistryAccessBean();
          rrb=rrb.findByUserLogonId(getInputUserName());  
          setFoundUserId(rrb.getUserId()); 
        }
         
      } catch (javax.ejb.FinderException e) {
          return;     
          
      } catch (java.rmi.RemoteException e) {
        throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION,       this.getClass().getName(), "validateParameters");
      } catch (javax.naming.NamingException e) {
        throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION,       this.getClass().getName(), "validateParameters");       
      } catch (javax.ejb.CreateException e) {
        throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION,       this.getClass().getName(), "validateParameters");
      }
    
    // end of section /////////
    

  7. Save the work.

  8. Modify MyNewControllerCmdImpl to create an object for the task command

    In order to promote the efficient use of objects, the controller command creates a UserRegistryAccessBean object instance variable. This object is also available to the task command. By taking this approach, the task command does not need to create a separate instance of the object. Later in this step, you use the UserRegistryAccessBean objects in the task command to determine if the user name is that of a registered user.

    To modify MyNewControllerCmdImpl:

    1. Open the MyNewControllerCmdImpl.java class to view its source code.

    2. In the main body of this class, uncomment Section 2 to introduce the following code into the class:

      /// Section //////////
      /// create a user registry accessbean resource instance variable
      
          private UserRegistryAccessBean rrb = null;
      
      /// End of Section ////////
      

    3. In the Outline view, select the performExecute method.

    4. Uncomment Sections 4D and 4F to pass the instance variable to the task command and then make the returned user ID available in the response properties object. You introduce the following code into the method:

      // Section 4////////
      /// pass rrb instance variable to the task command
      
          cmd.setUserRegistryAccessBean(rrb);
      
      // End of section 4////../images/locale/screensnap//
      
      
      // Section 4///////////
      ///using access bean to get information from database
          if (cmd.getFoundUserId() != null) {
            rspProp.put("taskOutputUserId", cmd.getFoundUserId());
          }
      // End of section 4////../images/locale/screensnap//
      

    5. At this point, there are two remaining compilation errors.

      To correct them, click the red indicator to the right of the editor's scroll bar. Your cursor highlights the compilation error.

    6. Right-click the highlighted text and select Source > Organize Imports.

    7. Save the work.

  9. Modify MyNewJSPTemplate for user name validation

    Modify the MyNewJSPTemplate.jsp file to display the user name validation information.

    To modify this file:

    1. Open both the MyNewJSPTemplate_All.jsp and MyNewJSPTemplate.jsp files.

    2. Copy SECTION 8 from the MyNewJSPTemplate_All.jsp file into the MyNewJSPTemplate.jsp file to introduce the following text into the JSP template:

      <!-- SECTION 8 -->
      
      <c:if test="${!empty taskOutputUserId}">
        
      <fmt:message key="UserId" bundle="${tutorial}" />
        
      <c:out value="${taskOutputUserId}"/>
      <br />
        
      <fmt:message key="FirstInput" bundle="${tutorial}" />
        
      <b><c:out value="${userName}"/></b>
        
      <fmt:message key="RegisteredUser" bundle="${tutorial}" />
      <br />
        
      <fmt:message key="ReferenceNumber" bundle="${tutorial}" /> 
        
      <b><c:out value="${taskOutputUserId}"/></b>
      <br />
      <br />
      </c:if>
      
      <c:if test="${empty taskOutputUserId}">
        
      <fmt:message key="FirstInput" bundle="${tutorial}" />
        
      <b><c:out value="${userName}"/></b> 
        
      <fmt:message key="NotRegisteredUser" bundle="${tutorial}" />
      <br />
      </c:if>
      
      
      <!-- END OF SECTION 8 -->
      

    3. Save the changes made to the MyNewJSPTemplate.jsp file.

  10. Test user name validation

    To test the user name validation logic:

    1. Restart the test environment.

    2. Navigate to the Stores > WebContent > ConsumerDirect_name directory.

    3. Select the index.jsp file and from its pop-up menu select Run As > Run on Server. The store home page displays in the Web browser.

    4. Create a new registered user:

      1. If applicable, choose a locale, for example: United States English.

      2. Click Sign In.

      3. In the registration form, enter appropriate values into all of the mandatory fields. For example, in the logon id field enter myNewLogonId. Make note of the logon ID and password you choose: _________________________.

      4. Click Submit.

    5. Enter a valid logon id as the value for input1 by entering the following URL:

      http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1=new_logon_Id&input2=1000
      

      where new_logon_Id is the logon ID for the user created in the previous step.

      if global security (LDAP) is enabled, instead of the logon_id, you need to use the name registered in the LDAP server. This name must be properly URL encoded. For example, if the name registered to the LDAP server is uid=myName,cn=users,dc=ibm,dc=com, the URL encoded LDAP string is: uid%3DmyName%2Ccn%3Dusers%2Cdc%3Dibm%2Cdc%3Dcom.

      The MyNewJSPTemplate displays as in the following screen capture, indicating that the value for input1 is a valid user name:

    6. Next, enter the following URL in which the value for the user name is invalid:

      http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1=abc&input2=1000
      

      The following page displays:

< Previous | Next >


+

Search Tips   |   Advanced Search