Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. The Developer Center continues to support Commerce 6.13.0 through 7.4.1.Visit new site

A Closer Look at the Stop Condition Application

A Closer Look at the Stop Condition Application

In this section, we take a closer look at how to attach a need info link to a resource and the commands used to check whether a client has agreed to the terms and conditions.

Processing the Terms and Conditions Resource

The resource operator for the Terms and Conditions Resource is shown below:

               <html><body>
<pre class="j-path">rest-resource-stopcondition<span class="j-pathsep">/</span>src<span class="j-pathsep">/</span>main<span class="j-pathsep">/</span>java<span class="j-pathsep">/</span>com<span class="j-pathsep">/</span>extension<span class="j-pathsep">/</span>rest<span class="j-pathsep">/</span>resources<span class="j-pathsep">/</span>termsandconditions<span class="j-pathsep">/</span>impl<span class="j-pathsep">/</span>TermsAndConditionsResourceOperatorImpl.java</pre>
<pre class="java"><span class="j-bkg">  </span><span class="j-jdoc">/**
   * Handles a request to the terms and conditions form.
   *
   * </span><span class="j-jdoc-key">@param </span><span class="j-jdoc">resourceUri the resource uri
   * </span><span class="j-jdoc-key">@param </span><span class="j-jdoc">operation The Resource Operation
   * </span><span class="j-jdoc-key">@return </span><span class="j-jdoc">the operation result
   */
  </span>@Path<span class="j-sym">({ </span>ResourceUri.PATH_PART, Form.PATH_PART <span class="j-sym">})
  </span>@OperationType<span class="j-sym">(</span>Operation.READ<span class="j-sym">)
  </span><span class="j-key">public </span>OperationResult processReadTermsAndConditionsForm<span class="j-sym">(
      </span>@ResourceUri <span class="j-key">final </span>String resourceUri,
      <span class="j-key">final </span>ResourceOperation operation<span class="j-sym">) {

    </span>Command&lt;TermsAndConditionsRepresentation&gt; cmd = buildTacFormCommandBuilder.get<span class="j-sym">()
      </span>.setOrderUri<span class="j-sym">(</span>URIUtil.normalize<span class="j-sym">(</span>resourceUri<span class="j-sym">))
      </span>.build<span class="j-sym">()</span>;

    ExecutionResult&lt;TermsAndConditionsRepresentation&gt; result = cmd.execute<span class="j-sym">()</span>;

    <span class="j-key">return </span>OperationResultFactory.create<span class="j-sym">(</span>result, operation<span class="j-sym">)</span>;
  <span class="j-sym">}

  </span><span class="j-jdoc">/**
   * Handles a post to agree to the terms and conditions of an order.
   *
   * </span><span class="j-jdoc-key">@param </span><span class="j-jdoc">resourceUri the resource uri
   * </span><span class="j-jdoc-key">@param </span><span class="j-jdoc">operation The Resource Operation
   * </span><span class="j-jdoc-key">@return </span><span class="j-jdoc">an operation result.
   */
  </span>@Path<span class="j-sym">(</span>ResourceUri.PATH_PART<span class="j-sym">)
  </span>@OperationType<span class="j-sym">(</span>Operation.CREATE<span class="j-sym">)
  </span><span class="j-key">public </span>OperationResult processAgreeToTermsAndConditions<span class="j-sym">(
      </span>@ResourceUri <span class="j-key">final </span>String resourceUri,
      <span class="j-key">final </span>ResourceOperation operation<span class="j-sym">) {

    </span>AgreeToTermsAndConditionsCommand.Builder builder = agreeToTacCommandBuilder.get<span class="j-sym">()</span>;
    Command&lt;Representation&gt; cmd = builder
      .setOrderUri<span class="j-sym">(</span>URIUtil.normalize<span class="j-sym">(</span>resourceUri<span class="j-sym">))
      </span>.build<span class="j-sym">()</span>;

    ExecutionResult&lt;Representation&gt; result = cmd.execute<span class="j-sym">()</span>;

    <span class="j-key">return </span>OperationResultFactory.create<span class="j-sym">(</span>result, operation<span class="j-sym">)</span>;
  <span class="j-sym">}</span></pre>
</body></html>
            

The resource operator's methods processReadTermsAndConditionsForm and processAgreeToTermsAndConditions handle the resources' GET and POST requests. Cortex API uses OperationTypes and the @OperationType annotation to map HTTP requests to CRUD operations. As you can see, GET request handlers use the Operation.READ operation type, while POST request handlers use Operation.CREATE operation type.

  • The processReadTermsAndConditionsForm method retrieves the terms and conditions form when GET requests are made to the resource. This method functions similarly to the processRead methods seen in the previous tutorials.
  • The processAgreeToTermsAndConditions method sets the hasAgreedToTerms property to true when the Terms And Conditions resource receives a POST request. Once hasAgreedToTerms is set to true, the link strategy no longer adds the need info link to the order, which allows the purchasing process to continue.