//>>built
define("mill/queryBuilder/queryDocument",["mill/mill","mill/queryBuilder/provider"],function(){window.mill||(mill={});mill.queryBuilder||(mill.queryBuilder={});mill.queryBuilder.queryDocument||(mill.queryBuilder.queryDocument={});dojo.mixin(mill.queryBuilder.queryDocument,{_buildItem:function(a){var b=[];for(id in a)b.push(""+id+"\x3d"+mill.enquoteForAttribute(a[id]));return"\x3citem "+b.join(" ")+"/\x3e"},_buildFields:function(a,b,e,d,k,l){var f=[];dojo.forEach(a.children,dojo.hitch(this,function(c){if(c.IsCategory)dojo.forEach(c.children,
dojo.hitch(this,arguments.callee));else if(c.IsField){var h=!0,g=!0;if(e)dojo.isArray(e)?(h=0<=dojo.indexOf(e,"F"+c.id),g=!1):h="F"+c.id==e;else{h=c.Checked;for(var m in l)h=!1}d["F"+c.id]&&f.push(this._buildItem({part:1,name:"F"+c.id,value:d["F"+c.id],subQuery:'"'+b+'"."'+c.Name+'"'}));l["F"+c.id]?f.push(this._buildItem({part:0,name:"F"+c.id,subQuery:'"'+b+'"."'+c.Name+'"',special:l["F"+c.id]})):h?(f.push(this._buildItem({part:0,name:g&&e?"Description":"F"+c.id,subQuery:'"'+b+'"."'+c.Name+'"'})),
k["F"+c.id]&&f.push(this._buildItem({part:2,name:"F"+c.id,special:"asc"==k["F"+c.id]?null:k["F"+c.id],subQuery:'"'+b+'"."'+c.Name+'"'})),g&&e&&(f.push(this._buildItem({part:3,name:"F"+c.id,subQuery:'"'+b+'"."'+c.Name+'"'})),f.push(this._buildItem({part:1,name:"F"+c.id,value:"*",subQuery:'"'+b+'"."'+c.Name+'"'})))):c.PartialChecked&&(h="T"+c.id,g=mill.queryBuilder.provider.tables(mill.queryBuilder.provider.types()[c.TypeInfoID].TableID),f.push(this._buildItem({part:6,name:"T"+c.id,subQuery:'"'+b+'"."'+
c.Name+'"',joinKey:g.fields[g.PrimaryKeyID].Name,value:g.Name})),f=f.concat(this._buildFields(c,h,e,d,k,l)))}}));return f},_buildPreConditions:function(a){var b=[];dojo.forEach(a.children,dojo.hitch(this,function(e){if(e.IsCategory)dojo.forEach(e.children,dojo.hitch(this,arguments.callee));else if(e.IsPreCondition&&e.Checked){var d=[];dojo.forEach(e.Parameters,function(k){d.push("\x3cparameter\x3e"+k.Value+"\x3c/parameter\x3e")});b.push("\x3cprecondition ID\x3d"+mill.enquoteForAttribute(e.ID)+"\x3e"+
d.join("")+"\x3c/precondition\x3e")}}));return b.join("")},_buildTable:function(a,b,e,d,k,l){var f=[];if(!e&&!l){f.push(this._buildItem({part:0,special:1}));var c=mill.queryBuilder.provider.fields(a.ID),h;for(h in c){var g=c[h];a.PrimaryKeyID!=g.ID&&g.DisplayMode==mill.queryBuilder.provider.dmNever&&f.push(this._buildItem({part:0,name:g.ID,subQuery:'"'+b+'"."'+g.Name+'"'}))}}!e&&a.HasPersistentClause&&f.push(this._buildItem({part:1,special:4,value:mill.queryBuilder.provider.persistentClause(a.ID,
b)}));return f.concat(this._buildFields(a,b,e,d,k,l||{}))},_options:function(){var a=[];dojo.forEach(mill.queryBuilder.provider.options(),function(b){a.push("\x3coption ID\x3d"+mill.enquoteForAttribute(b.ID)+"\x3e"+b.Value+"\x3c/option\x3e")});return a.join("")},buildFilterDatasetDocument:function(a,b,e){b=[];b.push("ID\x3d"+mill.enquoteForAttribute(a.ID));b.push("entityName\x3d"+mill.enquoteForAttribute(a.Name));b.push("mainEntityAlias\x3d"+mill.enquoteForAttribute("MAIN"));return"\x3cQuery "+b.join(" ")+
"\x3e"+this._buildTable(a,"MAIN",e,{},{},null).join("")+this._buildPreConditions(a)+this._options()+"\x3c/Query\x3e"},buildGroupDocument:function(a,b,e){var d=[];d.push("ID\x3d"+mill.enquoteForAttribute(a.ID));d.push("entityName\x3d"+mill.enquoteForAttribute(a.Name));d.push("mainEntityAlias\x3d"+mill.enquoteForAttribute("MAIN"));b=this._buildTable(a,"MAIN",e,b||{},{},null);b.push(this._buildItem({part:0,special:15}));return"\x3cQuery "+d.join(" ")+"\x3e"+b.join("")+this._buildPreConditions(a)+this._options()+
"\x3c/Query\x3e"},buildAggregateDocument:function(a,b,e){var d=[];d.push("ID\x3d"+mill.enquoteForAttribute(a.ID));d.push("entityName\x3d"+mill.enquoteForAttribute(a.Name));d.push("mainEntityAlias\x3d"+mill.enquoteForAttribute("MAIN"));b=this._buildTable(a,"MAIN",null,b||{},{},e);return"\x3cQuery "+d.join(" ")+"\x3e"+b.join("")+this._buildPreConditions(a)+this._options()+"\x3c/Query\x3e"},buildDocument:function(a,b,e){var d=[];d.push("ID\x3d"+mill.enquoteForAttribute(a.ID));d.push("entityName\x3d"+
mill.enquoteForAttribute(a.Name));d.push("mainEntityAlias\x3d"+mill.enquoteForAttribute("MAIN"));d.push("subQueryAlias\x3d"+mill.enquoteForAttribute("SUB"));a.PrimaryKeyID&&d.push("keyFieldName\x3d"+mill.enquoteForAttribute(mill.queryBuilder.provider.fields(a.ID)[a.PrimaryKeyID].Name));a.UplinkID&&(d.push("parentFieldName\x3d"+mill.enquoteForAttribute(mill.queryBuilder.provider.fields(a.ID)[a.UplinkID].Name)),d.push("hasChildrenFieldName\x3d"+mill.enquoteForAttribute("HASCHILDREN$")));return"\x3cQuery "+
d.join(" ")+"\x3e"+this._buildTable(a,"MAIN",null,b||{},e||{},null).join("")+this._buildPreConditions(a)+this._options()+"\x3c/Query\x3e"}})});