Adding Links to Your Resource
Adding Links to Your Resource
This tutorial assumes you have created the terms and conditions resource in Writing Your First Resource.
Links are a powerful way to enhance a resource's capability without having to change the resource's code. For example, to create an enhancement for the carts resource, you can create a new resource to provide this functionality and then attached a link on the carts resource that links to your new resource. Cortex uses links in many of its resources, items links to itemdefinitions where item descriptions are, carts link to totals where the total costs for the carts are, and so on.
- Add a link from one resource to another.
This tutorial enhances the terms and conditions resource you created in Writing Your First Resource by linking to it from the orders resource. After completing the tutorial, you can view the terms and conditions resource by following the link on the orders resource.
Linking from Orders to Terms and Conditions
{ "self": { "type": "elasticpath.orders.order", "uri": "/commerce-legacy/orders/{scope}/{order-id}", "href": "http://localhost:9080/cortex/orders/mobee/<an-order-id>" }, "links": [ ... { "rel":"terms", "type":"elasticpath.terms.terms-and-conditions", "uri":"/commerce-legacy/terms/id=", "href":"http://localhost:9080/cortex/terms/id=" }, ... ] }
Creating the Terms Link Handler
The ResourceStateLinkHandler creates links between resources. To attach our terms link to an order representation, we will develop a ResourceStateLinkHandler implementation that targets an OrderEntity.
- Open a command line and navigate to the extensions/tutorials directory.
- Run the following command to create the Cortex resource implementation project:
mvn clean package -Dtheme=cortex -Dpatch=generators/resource-link -Dproperties="resourceFamily=terms,resourceEntity=termsAndConditions,linkFamily=orders,linkEntity=order"
- In your IDE, open the extensions/cortex/resources/terms-and-conditions-resource project.
- Open the project's pom.xml and add a dependency to the artifact
containing the Orders API definition.
<dependencies> <dependency> <groupId>com.elasticpath.rest.definitions</groupId> <artifactId>ep-resource-orders-api</artifactId> <version>${cortex.version}</version> </dependency> </dependencies>
- Fix the compile errors in OrdersToTermsLinkHandler.java by adding an
import for
OrderEntity:
import com.elasticpath.rest.definition.orders.OrderEntity;
- Run the following command to build the terms and conditions resource
JAR
file:
mvn clean install
Testing the Terms Link
At this point, you've created all the necessary components for linking the terms and conditions resource to an order resource and the final result can be verified.
- Open a command line and navigate to the directory containing your Cortex web application.
- Run the following command to build and run the Cortex web
application:
mvn clean tomcat7:run-war
Open Cortex Studio in a browser at http://localhost:9080/studio/:
- Navigate to the default cart by clicking the "Shopper's Default Cart" link in the "Entry Points" panel at the right, or by submitting a GET Request for /carts/{scope}/default.
- Follow the order link.
- Check there is a link to terms on your order.
- Follow the terms link and ensure that the terms and conditions are successfully displayed
Congratulations, you just linked a resource to another resource. By following the same steps, you can now create resources that provide additional services to your client applications.