Commerce, Java

Display component for users within geo fence

Overview:

This blog is to integrate Hybris with Google Maps. Business users can save geo fences in Hybris.

Using geo fences they can do the following things.

  • Show special promotions to users.
  • Warehouse Allocations.
  • Restrict Hazardious products.

In the following PoC, We integrated geo fencing such that users from certain geo fence is popped up with an offer for some product. (Note: We didn’t integrate this with promotions engine. So, users who doesn’t belong to this geo fence can also avail this promotion. Only thing is they won’t be popped up with offer in Home Page).

Step 1: Modify Data Model such that it saves the fences

This step will create table structure in Hybris Data Access layer. To acheive geo fencing, we need to save latitude and longitude values (As a list) in Hybris. As the table structure is not available, we used the following snippet to create the table structure.

<collectiontype code="TechaspectLatLongList"
elementtype="TechaspectLatLong" autocreate="true" generate="true" type="list" />
<typegroup name="Techaspect">
<itemtype code="TechaspectLatLong" autocreate="true"
generate="true">
<deployment table="TechaspectLatLong" typecode="20002" />
<attributes>
<attribute qualifier="code" type="java.lang.String">
<modifiers unique="true" />
<persistence type="property" />
</attribute>
<attribute qualifier="latitude" type="java.lang.String">
<persistence type="property" />
</attribute>
<attribute qualifier="longitude" type="java.lang.String">
<persistence type="property" />
</attribute>
</attributes>
</itemtype>
<itemtype code="TechaspectGeoFence" autocreate="true" generate="true">
<deployment table="TechaspectGeoFence" typecode="20001" />
<attributes>
<attribute qualifier="code" type="java.lang.String">
<modifiers unique="true" />
<persistence type="property" />
</attribute>
<attribute qualifier="fences" type="TechaspectLatLongList">
<persistence type="property" />
</attribute>
</attributes>
</itemtype>
</typegroup>

 

Step 2: Create a Controller to connect with UI

This controller exposes a service (You can use this as REST with small changes) which will take latitude and longitude list as input and performs a business logic to save those data in table created in previous step.

@Controller
@Scope("tenant")
@RequestMapping(value = "/geofence")
public class GeoFenceController extends AbstractPageController{

@RequestMapping(method = RequestMethod.POST)
public String saveFence(final Model model) throws CMSItemNotFoundException
{
//Business logic which will call service layer to save the latitude and longitude values
}

 

Step 3: Send the values from front end.

As a service is exposed from Controller layer, UI can call that service with Latitude and Longitude values to save the fences.

 

Step 4: Create a custom cmscomponent

We need to create a custom cms component and integrate with geo fence so that this component will be displayed only if user accesses web page from the geofence.

In component.jsp, write the logic such that component is displayed if user is within geo fence. In below example, we used componenent.getProduct() to display if location  falls with the fence

 

<itemtype code="TechaspectGeoFenceComponent" extends="SimpleCMSComponent"
autocreate="true" generate="true"
jaloclass="com.techaspect.jalo.components.TechaspectGeoFenceComponent">
<deployment table="TechaspectGeoFenceComp" typecode="20003" />
<attributes>
<attribute qualifier="fence" type="TechaspectGeoFence">
<persistence type="property" />
</attribute>
<attribute qualifier="product" type="Product">
<persistence type="property" />
</attribute>
</attributes>
</itemtype>

 

 

Notes: Integration with geofence api 

To create polyline,

https://developers.google.com/maps/documentation/javascript/examples/polyline-complex

 

To check whether point is within polyline,

https://developers.google.com/maps/documentation/javascript/examples/poly-containsLocation

SVN URL for code: https://github.com/TechAspect-Solutions/Hybris-TA_Components/tree/geofencing/DefaultB2CAccelerator/techaspect

About The Author

Leave a Reply

*