John N's colleague's approach to problem solving is best described as follows. The solution to any given problem is to introduce two additional problems. While this approach generated quite a many interesting implementations, it rarely produced the needed results: simple, custom software for their clients. Ultimately, this approach led towards his de-hiring, which meant that John would have to maintain his old systems.

One project that John inherited was a fairly simple web application that tracked overtime. Simple, that is, from the front end. The back end PHP code was responsible for querying a MySQL database with queries similar to this:

SELECT * FROM bean_overtime_availability 
 WHERE `startDateTime` >= DATE(NOW()) AND `available` = 1;

Seems simple enough, right? Here's the code used to generate that query.

public function getOvertimeAvailability() {
    $c = new Criteria();
    $c->leftParenthesis();
    $c->field('startDateTime');
    $c->greaterThanOrEqualTo();
    $c->quote($this->getToday());
    $c->rightParenthesis();
    $c->andCriteria(
        Criteria::newCriteria()->field('available')->
        equal()->quote(1)
    );
    $c->addString(' order by startDateTime;');
    return $this->getPersistenceLayer()->
       selectBeans($this->getOvertimeAvailabilityBeanDefinition(),$c);
}

Thinking that the code looked far too simple, John assumed the Criteria class was a kind of magical black box that performed some kind of SQL-fu to make things a lot easier / more efficient. And then he peeked into the Criteria class.

class Criteria extends Object {
       
    protected function add($c) {
        $this->getList()->add($c);
    }
       
    protected function getList() {
        if (is_null($this->list))
            $this->list = new ArrayList;
        return $this->list;
    }
       
    public function leftParenthesis() {
        $this->add('(');
        return $this;
    }
       
    public function rightParenthesis() {
        $this->add(')');
        return $this;
    }
       
    public function comma() {
        $this->add(',');
        return $this;
    }
       
    public function space() {
        $this->add(' ');
        return $this;
    }
       
    public function quote($text) {
        $this->add("\"$text\"");
        return $this;
    }
       
    public function field($text) {
        $this->add("`$text`");
        return $this;
    }
       
    public function equal() {
        $this->add('=');
        return $this;
    }
       
    public function null() {
        $this->add('null');
        return $this;
    }
       
    public function in() {
        $this->add('in');
        return $this;
    }
       
    public function not() {
        $this->add('!');
        return $this;
    }
       
    public function notEqual() {
        $this->not();
        $this->equals();
        return $this;
    }
       
    public function lessThan() {
        $this->add('<');
        return $this;
    }
       
    public function lassThanOrEqualTo() {
        $this->lessThan();
        $this->equals();
        return $this;
    }
       
    public function greaterThan() {
        $this->add('>');
        return $this;
    }
       
    public function greaterThanOrEqualTo() {
        $this->greaterThan();
        $this->equal();
        return $this;
    }
       
    public function addCriteria($c) {
        $this->leftParenthesis();
        $this->add($c->toString());
        $this->rightParenthesis();
        return $this;
    }
       
    public function operator($operator) {
        $this->add($operator);
        return $this;
    }
       
    public function andCriteria(&$c) {
        $this->add('and');
        $this->leftParenthesis();
        $this->add($c);
        $this->rightParenthesis();         
    }
       
    public function orCriteria(&$c) {
        $this->add('or');
        $this->leftParenthesis();
        $this->add($c);
        $this->rightParenthesis();
    }
       
    public function addString($str) {
        $this->add($str);
        return $this;
    }
       
    public function toString() {
        return implode('',$this->getList()->getItems());
    }
       
    static function newCriteria() {
        return new Criteria();
    }
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!