-@queryStructure = @queryStructure || {}
-
-
-queryStructure.createContainer= (parent, json) ->
-  _ret = null
-  _children = []
-  if(json["and"])
-    _ret =  new queryStructure.And(parent,[])
-    _children = json["and"]
-  else if(json["or"])
-    _ret =   new queryStructure.Or(parent,[])
-    _children = json["or"]
-  else if(json["not"])
-    _ret =   new queryStructure.Not(parent,[])
-    _children = json["not"]
-
-  for item in _children
-     _ret.add(queryStructure.createContainer(_ret,item))
-  
-  _ret
+@queryStructure ||= {}
 
 
 class queryStructure.Query

##############

class queryStructure.Container
  constructor: (@parent, @children = [], @name, @negate = false) ->
    @children ||= []

  add: (element, after) ->

            
  clear: ->
    children = []
    
  childrenToJson: ->
     childJson = [];
     for child in @children
       childJson.push(child.toJson())
     return childJson
      

class queryStructure.Or extends queryStructure.Container
  toJson: ->
    childJson = this.childrenToJson()
    return { "name" : @name, "or" : childJson, "negate" : @negate, "title" : @title }
  
  test: (patient) -> 
    if (@children.length == 0)
      return true;
    retval = false  
    for child in @children
      if (child.test(patient)) 
        retval true
        break
    return if @negate then !retval else retval;


class queryStructure.And extends queryStructure.Container
  toJson: ->
    childJson = this.childrenToJson()
    return { "name" : @name, "and" : childJson, "negate" : @negate, "title" : @title }


  test: (patient) ->
    if (@children.length == 0)
      return true;
    retval = true  
    for child in @children
      if (!child.test(patient)) 
        retval =  false
        break
        
    return if @negate then !retval else retval



  

class queryStructure.CountN extends queryStructure.Container
