/*
 COPYRIGHT 2009 ESRI

 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts and Legal Services Department
 380 New York Street
 Redlands, California, 92373
 USA

 email: contracts@esri.com
 */

if(!dojo._hasResource["esri.tasks.query"]){dojo._hasResource["esri.tasks.query"]=true;dojo.provide("esri.tasks.query");dojo.require("esri.tasks._task");dojo.require("esri._time");dojo.declare("esri.tasks.QueryTask",esri.tasks._Task,{constructor:function(_1,_2){this._handler=dojo.hitch(this,this._handler);this._relationshipQueryHandler=dojo.hitch(this,this._relationshipQueryHandler);this._executeForIdsHandler=dojo.hitch(this,this._executeForIdsHandler);this._countHandler=dojo.hitch(this,this._countHandler);this.source=_2&&_2.source;this.gdbVersion=_2&&_2.gdbVersion;},__msigns:[{n:"execute",c:4,a:[{i:0,p:["geometry"]}],e:2},{n:"executeForIds",c:3,a:[{i:0,p:["geometry"]}],e:2},{n:"executeForCount",c:3,a:[{i:0,p:["geometry"]}],e:2}],onComplete:function(){},onExecuteRelationshipQueryComplete:function(){},onExecuteForIdsComplete:function(){},onExecuteForCountComplete:function(){},execute:function(_3,_4,_5,_6,_7){var _8=_7.assembly,_9=this._encode(dojo.mixin({},this._url.query,{f:"json"},_3.toJson(_8&&_8[0]))),_a=this._handler,_b=this._errorHandler;if(this.source){var _c={source:this.source.toJson()};_9.layer=dojo.toJson(_c);}if(this.gdbVersion){_9.gdbVersion=this.gdbVersion;}return esri.request({url:this._url.path+"/query",content:_9,callbackParamName:"callback",load:function(r,i){_a(r,i,_4,_5,_7.dfd);},error:function(r){_b(r,_5,_7.dfd);},callbackSuffix:_6});},executeRelationshipQuery:function(_d,_e,_f){var _10=this._encode(dojo.mixin({},this._url.query,{f:"json"},_d.toJson())),_11=this._relationshipQueryHandler,_12=this._errorHandler;if(this.gdbVersion){_10.gdbVersion=this.gdbVersion;}var dfd=new dojo.Deferred(esri._dfdCanceller);dfd._pendingDfd=esri.request({url:this._url.path+"/queryRelatedRecords",content:_10,callbackParamName:"callback",load:function(r,i){_11(r,i,_e,_f,dfd);},error:function(r){_12(r,_f,dfd);}});return dfd;},executeForIds:function(_13,_14,_15,_16){var _17=_16.assembly,_18=this._encode(dojo.mixin({},this._url.query,{f:"json",returnIdsOnly:true},_13.toJson(_17&&_17[0]))),_19=this._executeForIdsHandler,_1a=this._errorHandler;if(this.source){var _1b={source:this.source.toJson()};_18.layer=dojo.toJson(_1b);}if(this.gdbVersion){_18.gdbVersion=this.gdbVersion;}return esri.request({url:this._url.path+"/query",content:_18,callbackParamName:"callback",load:function(r,i){_19(r,i,_14,_15,_16.dfd);},error:function(r){_1a(r,_15,_16.dfd);}});},executeForCount:function(_1c,_1d,_1e,_1f){var _20=_1f.assembly,_21=this._encode(dojo.mixin({},this._url.query,{f:"json",returnIdsOnly:true,returnCountOnly:true},_1c.toJson(_20&&_20[0]))),_22=this._countHandler,_23=this._errorHandler;if(this.source){var _24={source:this.source.toJson()};_21.layer=dojo.toJson(_24);}if(this.gdbVersion){_21.gdbVersion=this.gdbVersion;}return esri.request({url:this._url.path+"/query",content:_21,callbackParamName:"callback",load:function(r,i){_22(r,i,_1d,_1e,_1f.dfd);},error:function(r){_23(r,_1e,_1f.dfd);}});},_handler:function(_25,io,_26,_27,dfd){try{var _28=new esri.tasks.FeatureSet(_25);this._successHandler([_28],"onComplete",_26,dfd);}catch(err){this._errorHandler(err,_27,dfd);}},_relationshipQueryHandler:function(_29,io,_2a,_2b,dfd){try{var gt=_29.geometryType,sr=_29.spatialReference,_2c={};dojo.forEach(_29.relatedRecordGroups,function(gr){var _2d={};_2d.geometryType=gt;_2d.spatialReference=sr;_2d.features=gr.relatedRecords;var _2e=new esri.tasks.FeatureSet(_2d);_2c[gr.objectId]=_2e;});this._successHandler([_2c],"onExecuteRelationshipQueryComplete",_2a,dfd);}catch(err){this._errorHandler(err,_2b,dfd);}},_executeForIdsHandler:function(_2f,io,_30,_31,dfd){try{this._successHandler([_2f.objectIds],"onExecuteForIdsComplete",_30,dfd);}catch(err){this._errorHandler(err,_31,dfd);}},_countHandler:function(_32,io,_33,_34,dfd){try{var _35,_36=_32.features,ids=_32.objectIds;if(ids){_35=ids.length;}else{if(_36){throw new Error(esri.bundle.tasks.query.invalid);}else{_35=_32.count;}}this._successHandler([_35],"onExecuteForCountComplete",_33,dfd);}catch(err){this._errorHandler(err,_34,dfd);}}});esri._createWrappers("esri.tasks.QueryTask");dojo.declare("esri.tasks.Query",null,{constructor:function(){this.spatialRelationship=esri.tasks.Query.SPATIAL_REL_INTERSECTS;},text:null,where:"",geometry:null,groupByFieldsForStatistics:null,objectIds:null,returnGeometry:false,orderByFields:null,outSpatialReference:null,outFields:null,outStatistics:null,timeExtent:null,relationParam:null,toJson:function(_37){var _38={text:this.text,where:this.where,returnGeometry:this.returnGeometry,spatialRel:this.spatialRelationship,maxAllowableOffset:this.maxAllowableOffset},g=_37&&_37["geometry"]||this.geometry,ids=this.objectIds,_39=this.outFields,_3a=this.outSpatialReference,_3b=this.groupByFieldsForStatistics,_3c=this.orderByFields,_3d=this.outStatistics;if(g){_38.geometry=g;_38.geometryType=esri.geometry.getJsonType(g);_38.inSR=g.spatialReference.wkid||dojo.toJson(g.spatialReference.toJson());}if(ids){_38.objectIds=ids.join(",");}if(_39){_38.outFields=_39.join(",");}if(_3b){_38.groupByFieldsForStatistics=_3b.join(",");}if(_3c){_38.orderByFields=_3c.join(",");}if(_3d){var _3e=[];dojo.forEach(_3d,function(_3f,idx){_3e.push(_3f.toJson());});_38.outStatistics=dojo.toJson(_3e);}if(_3a!==null){_38.outSR=_3a.wkid||dojo.toJson(_3a.toJson());}else{if(g){_38.outSR=g.spatialReference.wkid||dojo.toJson(g.spatialReference.toJson());}}var _40=this.timeExtent;_38.time=_40?_40.toJson().join(","):null;var _41=this.relationParam;if(_41&&this.spatialRelationship===esri.tasks.Query.SPATIAL_REL_RELATION){_38.relationParam=_41;}_38._ts=this._ts;return _38;}});dojo.mixin(esri.tasks.Query,esri.tasks._SpatialRelationship);dojo.declare("esri.tasks.RelationshipQuery",null,{definitionExpression:"",relationshipId:null,returnGeometry:false,objectIds:null,outSpatialReference:null,outFields:null,toJson:function(){var _42={definitionExpression:this.definitionExpression,relationshipId:this.relationshipId,returnGeometry:this.returnGeometry,maxAllowableOffset:this.maxAllowableOffset},_43=this.objectIds,_44=this.outFields,_45=this.outSpatialReference;if(_43){_42.objectIds=_43.join(",");}if(_44){_42.outFields=_44.join(",");}if(_45){_42.outSR=_45.toJson();}_42._ts=this._ts;return _42;}});dojo.declare("esri.tasks.StatisticDefinition",null,{statisticType:null,onStatisticField:null,outStatisticFieldName:null,toJson:function(){var _46={statisticType:this.statisticType,onStatisticField:this.onStatisticField};if(this.outStatisticFieldName){_46.outStatisticFieldName=this.outStatisticFieldName;}return _46;}});}