//>>built
define("mill/queryBuilder/treeModel",["mill/queryBuilder/provider","mill/mill"],function(){window.mill||(mill={});mill.queryBuilder||(mill.queryBuilder={});mill.queryBuilder.provider||(mill.queryBuilder.provider={});dojo.declare("mill.queryBuilder.treeModel",null,{rootTableID:null,_items:null,_root:null,_rootTable:null,constructor:function(a){dojo.mixin(this,a);this._items=[]},getRoot:function(a,e){this._root||(this._rootTable=mill.queryBuilder.provider.tables(this.rootTableID),this._root={ID:this._rootTable.ID,
Caption:this._rootTable.Caption,Name:this._rootTable.Name,IsTable:!0,AutoExpand:!0,IsRoot:!0,PrimaryKeyID:this._rootTable.PrimaryKeyID,UplinkID:this._rootTable.UplinkID,HasPersistentClause:this._rootTable.HasPersistentClause,id:this._items.length},this._items.push(this._root));this._root&&a(this._root)},mayHaveChildren:function(a){return a.IsCategory||a.IsTable||a.IsExpandableField},_fetchTableChildren:function(a,e){var k=[],f=mill.queryBuilder.provider.tables(a),g=mill.queryBuilder.provider.fields(a);
f=f.preConditions;mill.queryBuilder.provider.types();var d=[],b;for(b in g)g[b].Category&&g[b].DisplayMode!=mill.queryBuilder.provider.dmNever&&-1==dojo.indexOf(d,g[b].Category)&&d.push(g[b].Category);e||dojo.forEach(f,function(c){c.Category&&-1==dojo.indexOf(d,c.Category)&&d.push(c.Category)});d.length&&(d=dojo.map(d,function(c){return{Caption:c}}),d.sort(mill.queryBuilder.provider._compareCaptions),dojo.forEach(d,dojo.hitch(this,function(c){c={Caption:c.Caption,IsCategory:!0,TableID:a,FieldsOnly:e,
id:this._items.length};this._items.push(c);k.push(c)})));return k.concat(this._fetchCategory(a,null,e))},_fetchCategory:function(a,e,k){var f=[],g=mill.queryBuilder.provider.tables(a);a=mill.queryBuilder.provider.fields(a);g=g.preConditions;var d=mill.queryBuilder.provider.types(),b;for(b in a)if((a[b].Category||"")==(e||"")&&a[b].DisplayMode!=mill.queryBuilder.provider.dmNever){var c={ID:b,Caption:a[b].Caption,Name:a[b].Name,CanCheck:d[a[b].TypeInfoID].SimpleType!=mill.queryBuilder.provider.dtReference,
TypeInfoID:a[b].TypeInfoID,DisplayMode:a[b].DisplayMode,IsExpandableField:d[a[b].TypeInfoID].SimpleType==mill.queryBuilder.provider.dtReference,IsField:!0,id:this._items.length};if(c.IsExpandableField){var l=mill.queryBuilder.provider.fields(d[a[b].TypeInfoID].TableID),m;for(m in l)l[m].DisplayMode==mill.queryBuilder.provider.dmAlways&&(c.AutoExpand=!0)}this._items.push(c);f.push(c)}k||dojo.forEach(g,dojo.hitch(this,function(h){(h.Category||"")==(e||"")&&(h={IsPreCondition:!0,ID:h.ID,CanCheck:!0,
Caption:h.Caption,Parameters:dojo.clone(h.Parameters),id:this._items.length},this._items.push(h),f.push(h))}));return f},getChildren:function(a,e,k){a.children||(this.mayHaveChildren(a)?(a.children=a.IsTable?this._fetchTableChildren(a.ID,!1):a.IsCategory?this._fetchCategory(a.TableID,a.Caption,a.FieldsOnly):a.IsField?this._fetchTableChildren(mill.queryBuilder.provider.types()[a.TypeInfoID].TableID,!0):[],dojo.forEach(a.children,dojo.hitch(this,function(f){dojo.mixin(f,{parent:a,model:this})}))):a.children=
[]);e&&e(a.children)},isItem:function(a){return!0},getIdentity:function(a){return a.id},getLabel:function(a){return a.Caption}})});