Store Opening Times – the Groovy Solution

Modelling the opening times for physical retail stores can be a bit challenging, at least I spent quite a few cycles thinking about it recently. Each store will have individual opening times, time zones and public holidays. The perfect solution will always be a customized one, but here is one approach that is greatly simplified by the use of Groovy scripting and the Ranges that Groovy supplies.

Let’s look how the weekly opening times for a single store could be defined:

In the example above, the information about the ‘normal’ opening hours are defined as a Map. The keys are the 3 letter abbreviations for the weekdays, the values are Arrays of Groovy Range objects. The Range objects model the timeframes in which the store is open. This allows you to easily specify times that a store may be closed during the day (like a lunch time closing time for an hour from 1200 to 1400). That brings up the data type of the upper and lower limits of the timeframes: I simply chose to convert the hour and minute to a integer value – so 9AM in the morning would be represented as 900. 2PM in the afternoon (in 24h format) would be 1400.

Next, let’s assume you got a few stores. A store is defined like this below in Groovy syntax.

Being a bit lazy, I return a List of all stores via the getAllStores() method. Also, the store data model is of course over-simplified, each store just having a name and opening hours. Teh weekdayHours and crazyHours opening times are defined similar the opening times above.

So how would you find all open stores? The first step involves getting a Java Calendar object with the time zone that the opening times were defined in:

We get a reference to the current Calendar object, set the timezone and then simply resolve the DAY_OF_WEEK from an integer value to our 3 letter key for the days in a week (‘mon’, ‘tue’, etc.).
Finding the open stores is now trivial due to some Groovy magic:

The filtering is done in a single line in the closure given to the findAll method. It will check if the currentTime (an integer such as 905 for 9:05AM) is in any of the defined timeframes we defined. If there is a match, the current store we are iterating over is in the list of results.

While this is a neat solution, there will be more work of course in real life:

  • Once we know a store is open, we would need to check against public holidays, too
  • Sometimes, special opening hours will be required – so another check / list needs to be maintained for special events (typically closes at 8PM on Thursdays, but THAT specific day we are open till 10PM

 

It would be great to receive comments from your experiences with modelling / checking store opening times. My approach may be simplistic, but it fits a lot of use cases I think. Let me know!

Writing a simple API server with Java 6 HttpServer

This post is about writing a super simple web server using Java 6’s HttpServer class. Ironically I was using the Groovy scripting language to write my own code that creates and starts the server. Being able to quickly setup a small, lightweight server can be very helpful – in my case I needed to trigger some lokal batch script from a web browser. So I used AJAX to call the local web server which would in turn execute a local process.

Below is the complete script, which starts a server under localhost:8000 and makes two handlers available under /test and /kill. The latter one can be used to stop the server from within – you don’t have to kill the java process or so… just call localhost:8000/kill in your browser. By the way: this is not a lesson on how to design RESTful resources, so let’s forget that GET should never be used for something like stopping a server…

The setup is really quite simple. First, we create a new HttpServer which is bound to an InetSocketAddress, here running on port 8000. Next, we can register contextes using the createContext method. Each context will have a handler, which simply is used to accept the request, do something, then respond with a response.

Groovy was helping a great deal here to create a basic json response which I expect for my web browser AJAX calls.

Simple, right?
 

HMAC-SHA1 in Groovy

Some APIs are nice and let you dance OAuth2 in order to play with them. Some others require you to calculate HMAC-SHA1 headers in order to play with them. HMAC-What? Yeah, so did I think. So here is a little snippet of Groovy code that I want to share. It allows you to calculate this hash-based message authentication code (HMAC) in Groovy, which is my favorite tool for quick experimentation with APIs.

The HTTP header I had to include in each call to a Mashape API was specified like this:

Public and private key are of course given by the API provider. The hard part is to create the HMAC_SHA1. It also turned out, that there seem to be a few variations, but this piece of code should get you at least started:

The entire response of the Hex-Encoded HMAC-SHA1 had to concatenated with the public key, seperated by colon and then again be Base64-Encoded (and converted from byte-array to String). Luckily, Groovy turned out to be a big helper with these steps:

This is obviously not a mind-blowing post but I hope that it helps a few of you looking to implement a HMAC-SHA1 in Java or Groovy.

 

Using Groovy and Recursion to find required extensions

The hybris eCommerce platform is very extensible, which is a very good thing as it provides our partners with a lot of flexibility when it comes to specific customer projects. Each extension is stored in its own directory and has a file called extensioninfo.xml. The following file is the extensioninfo.xml file from the ycommercewebservices extension which provides part of the hybris Omni-Commerce Connect Web Services API (shortened):

If you wanted to find out what extensions you totally require – for example to run the ycommercewebservices – you needed to follow these dependencies and add all these up so you know how the localextensions.xml file (lists all extensions required) needs to look.

This morning, I finally became tired of doing this and quickly sat down and wrote a script in Groovy using recursion. The following will work with trunk-versions of our platform (as all extensions are in one directory):

This will simply recursively add all required extensions and print them:

That’s it. My ‘good thing of the day’…