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.
Attaching a Need Info Link
To determine whether further information is required before continuing the purchasing process, Cortex API kernel scans the order representation's links for need info links. If the kernel finds a need info link, it blocks the purchase form submission until all need info links are addressed. In this tutorial, a need info link appears in the Terms and Conditions resource when the customer has not agreed to the order's terms.
To insert the need info link, the Terms And Conditions resource defines a link strategy:
<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>command<span class="j-pathsep">/</span>impl<span class="j-pathsep">/</span>LinkToTermsAndConditionsStrategy.java</pre> <pre class="java"><span class="j-bkg"> </span><span class="j-jdoc">/** * Create links kernel will use to attach to order representation. * * </span><span class="j-jdoc-key">@param </span><span class="j-jdoc">representation the representation to attach links too * </span><span class="j-jdoc-key">@return </span><span class="j-jdoc">Links to attach to order representation */ </span>@Override <span class="j-key">public </span>Collection<ResourceLink> createLinks<span class="j-sym">(</span><span class="j-key">final </span>Representation representation<span class="j-sym">) { </span>OrderRepresentation order = ResourceTypeFactory.adaptRepresentation<span class="j-sym">(</span>representation, OrderRepresentation.<span class="j-key">class</span><span class="j-sym">)</span>; <span class="j-blk">/* * Create the link to the terms and conditions form by correctly concatenating the root resource name (termsandconditions) * with URI from order representation (orders/scope/orderId) and /form. */ </span>String resourceUri = URIUtil.format<span class="j-sym">(</span>resourceServerName, RepresentationUtil.getSelfUri<span class="j-sym">(</span>order<span class="j-sym">)</span>, Form.URI_PART<span class="j-sym">)</span>; Collection<ResourceLink> linksToAdd = <span class="j-key">new </span>ArrayList<ResourceLink><span class="j-sym">(</span><span class="j-num">2</span><span class="j-sym">)</span>; ResourceLink termsAndAgreementsLink = ResourceLinkFactory.createNoRev<span class="j-sym">(</span>resourceUri, TermsAndConditionsRepresentation.TYPE, TermsAndConditionsResourceRels.TERMS_AND_CONDITIONS_REL<span class="j-sym">)</span>; linksToAdd.add<span class="j-sym">(</span>termsAndAgreementsLink<span class="j-sym">)</span>; <span class="j-blk">/* * Check to see if a user has agreed to the terms and conditions yet. If not, attach a need info link to alert both the kernel and user that * information is required before proceeding to purchase. */ </span>ExecutionResult<Boolean> hasAgreedToTerms = termsAndConditionsLookup.hasAgreedToTerms<span class="j-sym">(</span>order.getScope<span class="j-sym">()</span>, order.getOrderId<span class="j-sym">())</span>; <span class="j-key">if </span><span class="j-sym">(</span>hasAgreedToTerms.isSuccessful<span class="j-sym">() </span>&& !hasAgreedToTerms.getData<span class="j-sym">()) { </span>ResourceLink needInfoLink = ResourceLinkFactory.createNoRev<span class="j-sym">(</span>resourceUri, TermsAndConditionsRepresentation.TYPE, NeedInfoRels.NEEDINFO<span class="j-sym">)</span>; linksToAdd.add<span class="j-sym">(</span>needInfoLink<span class="j-sym">)</span>; <span class="j-sym">} </span><span class="j-key">return </span>linksToAdd; <span class="j-sym">}</span></pre> </body></html>
As you can see, a conditional statement wraps around the code that creates the need info link. This statement checks whether the customer has agreed to the terms and conditions. If the customer has agreed to the terms, the need info link isn't added.
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<TermsAndConditionsRepresentation> 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<TermsAndConditionsRepresentation> 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<Representation> 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<Representation> 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.