Detecting Date Range Conflicts

I’m working on an application for which one of the requirements is to not allow double-booking of rooms. Events in the system each have a start and end date and time, and when a new event is saved the system needs to tell the user if there are any overlaps with existing events in the same room.

This seems simple enough on the face of it but once I started thinking about all the possibilities around this I realized it was a lot more complex than I had initially thought. After some good old-fashioned “sledge hammer approach to get it working and to help gain understanding that will hopefully lead to eventual refinement” I think I have it licked.
I’m sure this is one of those classic problems that I just haven’t had to deal with before which are always fun to think through, and whenever I run into one of these I resist the urge to search for a solution until I’ve wrapped my head around the problem and am ready to admit defeat. (And I really try never to admit defeat unless time constraints force me to.)
My first phase on solving this problem was to consider all the possible conflict states, which in plain english are:
  1. Since an event is assumed to have a non-zero duration, if either the start date/time or end date/time is exactly the same as the start date/time or end date/time of another event in the same room, that indicates a conflict. Note that one event’s start date/time can be the same as another event’s end date/time.
  2. If an event has a start date/time that is between the start and end date/time of another event, that indicates a conflict.
  3. If an event has an end date/time that is between the start and end date/time of another event, that indicates a conflict.
  4. If an event’s start date/time is after that of another event but its end date/time is before that of another event, that indicates a conflict.
Granted some of these overlap, are redundant, or are the inverse of one another, but it was helpful as a first pass to simply think through all the scenarios to start forming a picture in my head of the various possibilities.
I’ll spare you the messy middle step here and just say I then started coding all these scenarios (and anything else I thought of) and as I went through that exercise, I realized that this all boils down to some pretty simple logic.
Assume that we have two events and each one has a start and end date/time. We’ll use start1 and end1 for the first event’s dates, and start2 and end2 for the second event’s dates. Here’s what I came up with after a lot of head banging that I believe handles all the scenarios:

Consider yourself lucky I spared you the big hairy mess I had before I arrived at that solution. I believe that covers all the bases, however, and at least in the testing I did it certainly seems to.

The only other wrinkle in the case of this system is making sure that an event itself isn’t detected as a conflict if someone updates the event and either doesn’t change the dates or changes the dates in such a way that it would be considered a conflict with that event’s state that’s already in the database. To handle that case I still run the function to detect conflicts but if I only get back 1 and the ID is the same as the one I’m trying to save, I ignore it.

So that’s how I spent more time than I care to admit this weekend. I’m curious if other people have solved this differently, and definitely would love to hear if this won’t address some scenario I didn’t consider.

Polyglot Web Development With the Grails Framework #s2gx

Jeff Brown – SpringSource

Polyglot?

  • "many languages"
  • writing software in multiple languages
  • some people would say if you do any web development, you're doing polyglot
    • javascript, css, html, java, etc.
  • in the context of this talk, we'll be talking about implementing the actual business logic with multiple languages

Languages on the JVM

  • 200+ languages available on the JVM
  • many of these aren't exactly practical, but many are
  • at least 10-12 programming languages available on the JVM that could be used for serious development
  • big players are java, groovy, clojure, scala, jruby, jython
  • which of these is the best? no answer of course
    • personal preference, best tool for the job, etc.
  • many of these languages solve specific problems really well
  • all these languages are turing complete, so anything you can do in one you can do in another
  • but depending on the problem you're trying to solve, you may find one language or another is ideally suited to the task at hand
  • reached a point with CPU development where the speed of light is a factor in terms of increasing the speed
    • can't really make processors any faster with the current method of developing processors
    • instead of making faster processors, we're using more processors and multiple cores
  • concurrency is becoming more and more important
  • OO languages don't lend themselves to managing concurrency very well
    • allocate objects on the heap, objects in a shared mutable state
    • best we can do in OO languages is use locks so multiple threads can't access things at the same time
    • problem with locking is it's opt-in
  • functional languages make the concurrency problem almost disappear
    • no such thing as destructive assignment in a pure functional language
    • clojure and scala *do* allow destructive assignment
    • but in clojure, for example, you have to do this in a transaction
      • get a snapshot of the heap, and nothing can change on the heap while you're making changes
  • because of the advantages in terms of concurrency, it will be more common to use polyglot programming moving forward
    • e.g. write much of the application in groovy, but build parts of the application using a functional language
  • ultimately all this will run on the jvm, but we can take advantage of the best things each language has to offer
  • pretty different from the past–would have been rather unusual to write a C++ program that had other languages mixed in

Grails?

  • full stack MVC platform for the JVM
    • build system down to ORM, etc.
  • leverages proven staples
    • spring, hibernate, groovy, quartz, java, sitemesh
  • extensible plugin system
    • e.g. can pull out hibernate and use a different persistence mechanism, or can write your own plugins
  • since grails is built on the jvm you can take advantage of any language that will run on the jvm

Demo

  • showing how to write a grails app that uses a groovy controller, but a "math helper" class can be written in groovy, or java, or clojure
  • as long as there's a bean in the spring context, regardless of language, it can be injected into grails controllers
  • the grails controller doesn't care what language the classes it uses are written in

Clojure

  • core grails doesn't have clojure support, but there's a clojure plugin
  • plugin creates an src/clj file for clojure source
  • code in the grails controller doesn't have to change to take advantage of the math helper written in the different languages


(ns grails)
(defn addNumbers [x y]
  (+ x y ))

Using the Clojure Plugin

  • call via clj.mathHelper.addNumbers(x, y)
  • clj is the same as calling getClj()
  • the cloure plugin adds the getCjl method to all of the grails classes
    • classes*.metaClass*."getClj" = { return proxy } — this is done in the withDynamicProperties method in the plugin config
    • the proxy is an instance of the clojure proxy class (grails.clojure.ClojureProxy)
  • no addNumbers method in the proxy class — uses methodMissing
  • plugin looks for clojure methods in the grails namespace by default
    • if you need another namespace, clj['mynamespace'].methodName() will handle this
  • clojure plugin declares that it wants to watch all the files in src/clj
    • gets notified when files change and compiles the files if they change
  • if your plugin is adding things to the metaclasses on application startup, then you need to make sure your plugin also modifies controllers, services, etc. as they're changed while the application is running
  • can also observe only specific plugins for changes, e.g. only notify me when something involved with hibernate changes
  • can swap out other view technologies in grails
  • for taglibs, they have to be written in groovy, but inside the taglib you could be making calls to code in other languages

Who gets the credit?

  • grails? groovy? clojure? java? the jvm?
  • really it's the combination of all of them
  • don't have to walk away from grails to take advantage of any of the languages that run on the jvm

In Seattle? Into Groovy and Grails? Join the Seattle Groovy/Grails Meetup!

If you're in the Seattle area and interested in Groovy and Grails, make sure and join us at next month's Seattle Groovy/Grails Meetup on February 11. We get together every month at the Elephant & Castle, and it's a casual mix of Groovy, Grails, other technology, and whatever else comes up.

All are welcome so even if you're not using Groovy and Grails yet, come join us and we can all learn from each other. See you there!

Software Development: Doing It Scared | Our Blog | Box UK

In software, we are often faced with things that can seem intimidating. We may be assigned a project that we know nothing about. We may have to use a new tool or environment. We may be working with people who can seem intimidating. The natural temptation is to retreat to a place of safety, but that won’t get the job done. So how can we, as software developers, overcome the instinct to cower when projects seem daunting? I’ll go over a few things that have worked for me under these circumstances; your mileage may vary

This is an absolutely fantastic post outlining how to get your lazy self (hey, I’m including myself here) outside your comfort zone. When I first started working with Grails I tweeted something like, “I have no idea what I’m doing and it’s a great feeling,” and this blog posts captures that same sentiment in a more practical way. I really love the “do it scared” mantra. Words we developers should live by more often than we do.

Pragmatic Dictator » Tech is for Sissies

So often I see companies create job specs for engineers where the key requirement is to hire someone who can hit the deck coding like mad using whatever tools have been selected. To that end they load the specs up with endless tech hubris and at interview ask the details of this or that bit of syntax or API call. But what about the next project within the company where the tech is different? All those engineers that just got hired are now useless, they don’t have the skills and we lose time whilst they learn. Or we could fire them and hire another lot?

Couldn’t agree more. The longer I’m in this business the more weary I grow of one-trick technologists. Yes, I think it’s important to spend a lot of time with a few select tools so you don’t fall into the “jack of all trades master of none” camp, but frankly better that than complete inflexibility or unwillingness to try new things.

Groovy – UltraEdit Plugin

UltraEdit is a nice little text editor when working on Windows. It is very much suited to handle all kinds of resource files and some little scripts, when starting your IDE just takes too long.

You can get it from http://www.ultraedit.com/

Now that UltraEdit is on Linux (the column select capabilities alone are worth the $50!), I hunted for a Groovy plugin for UltraEdit and it didn’t take long. There’s a link in this article to an addition to UltraEdit’s wordfile, so you copy/paste and UltraEdit is Groovy aware. Nice.

EtherPad Blog: EtherPad Open Source Release

As promised,
we hereby release all the source code to EtherPad.

http://code.google.com/p/etherpad/

Our goal with this release is to let the world run their own
etherpad servers so that the functionality can live on even after we
shut down etherpad.com.

If you are just interested in running an etherpad server, these
instructions
should get you up and running.

Running EtherPad behind a firewall is rather intriguing. Might have to do some experimentation.

Coding Horror: Version 1 Sucks, But Ship It Anyway

What’s important isn’t so much the initial state of the software — in fact, some say if you aren’t embarrassed by v1.0 you didn’t release it early enough — but what you do after releasing the software.

Couldn’t agree more with this, and lately on some projects I’ve had to remind myself of this mantra on a daily basis. This is right in line with the “worse is better” principle ( http://en.wikipedia.org/wiki/Worse_is_better ), and since by nature software developers tend to be perfectionists, it’s often difficult to let go, realize it will never be done, and focus on the bare minimum to get the application out the door.

A commenter on this post does have a good point, namely that you shouldn’t ship it so early that your customers lose faith. So focus on the bare minimum of features that make the application usable, make sure that small set of features is rock solid, and launch the damn thing! After launch you’ll impress your users with all the cool features you add one by one, not to mention in the meantime you’re getting real feedback from real users, and before you know it you have an amazing application on your hands.

The Framework is the Language | Eric Lamb

You can’t approach learning a framework as anything less than you would when learning a new language. Anything less and you’re in for pain. After this realization the framework came together pretty quickly though it still hurt like hell.

Although I’ve never experienced the “hurt like hell” part when learning a framework, this is a decent reminder that a framework is written in/targeted to a language, but also has a dialect all its own. Understanding that fact will probably make things go a lot more smoothly. I disagree with the author’s doubt surrounding whether or not using a framework is a good idea but it’s food for thought nonetheless.