Extending Customer For Heat Clinic Tutorial

In this tutorial, we're going to cover how to extend the actual CustomerImpl object so that we can keep track of a couple of new attributes directly on the object. We're going to create some variables to keep track of the average heat level of sauces that a customer buys so that we can display some special text for our hottest sauce eaters.

Extending CustomerImpl

There are a few steps to extending an entity in Broadleaf:

Create the extension class

Notify the persistence unit about your new class

Set this class as the default for the parent interface in the Broadleaf Entity Configuration

Create the extension class

Since customers are shared between the admin application and the site application, we want to create our objects in the core submodule so that both admin and site can share them.

Set the default Entity Configuration

We now need to instruct the EntityConfiguration to load our new overriden Customer class when it wants to create a Customer. The first thing to do is create a new file to hold our new entity configurations. Again, since we want this to be shared between admin and site, the most appropriate place to put the new file is in core.

I chose the path core/src/main/resources/applicationContext-entity.xml. Here are the contents of the file:

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"><!-- This file will contain bean overrides for extensions of Broadleaf entities --><!-- For example, if you have a custom class that extends CustomerImpl, you would note that here --><beanid="org.broadleafcommerce.profile.core.domain.Customer"class="com.heatclinic.profile.core.domain.HCCustomerImpl"scope="prototype"/></beans>

Note: If you're using our bundled workspace, we already reference the applicationContext-entity file. You can see this definition in applicationContext.xml.

And there we go! Whenever we create a new Customer via Broadleaf (for example: customerService.createCustomer(), an HCCustomerImpl instance will be generated instead of the typical CustomerImpl. This instance obviously has the extra fields that we added.

Subsequent modifications to other Broadleaf entities would be handled in the same way.

Displaying the new average heat level

Let's show the customer their average hot sauce heat level! For our example, we'll show them this information in the header bar, next to their name.

The welcome message is in site/src/main/webapp/templates/partials/header.html.

Of course, right now, all customers would have an average heat rating of 0 since we're not saving information anywhere. That's demonstrated in our next tutorial, hooking into the order submit workflow, so go check it out!