/*
 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.arcgis.Portal"]){dojo._hasResource["esri.arcgis.Portal"]=true;dojo.provide("esri.arcgis.Portal");dojo.require("dojox.socket");dojo.require("dojox.socket.Reconnect");dojo.require("dojo.DeferredList");dojo.require("esri.IdentityManager");(function(){var _1={"options":{"disableIdentityLookup":true},"requestParams":{"f":"json"}};dojo.declare("esri.arcgis.Portal",null,{onLoad:function(){},constructor:function(_2){dojo.mixin(this,{"url":_2});this.init(_2).then(dojo.hitch(this,"onLoad",this));},init:function(_3,_4){_3=(_3||this.portalUrl);this.portalUrl=_3+((_3.indexOf("/sharing")||_3.indexOf("/sharing/rest"))!==-1?"":"/sharing/rest/");return this._getSelf(this.portalUrl).then(dojo.hitch(this,function(_5){var _6=_5;var _7=_5.user;if(_7){_7.portal=this;_7=new esri.arcgis.PortalUser(_7);}_1.self=dojo.mixin({},_6);dojo.mixin(this,_1.self);_1.loggedInUser=dojo.mixin({},dojo.mixin(_7,{"credential":_4}));this.thumbnailUrl=esri.arcgis.PortalUtil.formatUrl(this.portalUrl+"accounts/self/resources/"+this.thumbnail);this.isOrganization=(this.access&&this.access.length)||false;this.created=new Date(this.created);this.modified=new Date(this.modified);this.defaultExtent=this.defaultExtent;return this;}));},signIn:function(){_1.options.disableIdentityLookup=false;return esri.id.getCredential(this.url).then(dojo.hitch(this,"init",this.url)).then(function(){return _1.loggedInUser;},function(_8){_1.options.disableIdentityLookup=true;throw _8;});},signOut:function(){_1.loggedInUser.credential&&_1.loggedInUser.credential.destroy&&_1.loggedInUser.credential.destroy();_1.loggedInUser=null;_1.options.disableIdentityLookup=true;esri.arcgis.PortalUtil.clearFieldsFromObject(_1.self,this);_1.self=null;return this.init(this.url);},queryGroups:function(_9){var _a=this.portalUrl+"community/groups";return this._queryPortal(_a,esri.arcgis.PortalUtil.formatQueryParams({},_9),esri.arcgis.PortalGroup);},queryItems:function(_b){var _c=this.portalUrl+"search";return this._queryPortal(_c,esri.arcgis.PortalUtil.formatQueryParams({},_b),esri.arcgis.PortalItem);},queryUsers:function(_d){var _e=this.portalUrl+"community/users";return this._queryPortal(_e,esri.arcgis.PortalUtil.formatQueryParams({"sortField":"username"},_d),esri.arcgis.PortalUser);},_getSelf:function(_f){var _10=_f+"accounts/self?"+"culture="+dojo.locale;return esri.arcgis.PortalUtil.request(_10);},_queryPortal:function(url,_11,_12){var _13=dojo.mixin({"num":10,"start":0,"sortField":"title","sortOrder":"asc"},_11),_14=["start","query","num","nextStart"],dfd=esri.arcgis.PortalUtil.request(url,_13).then(dojo.hitch(this,function(_15){_15.results=esri.arcgis.PortalUtil.resultsToTypedArray(_12,{"portal":this},_15.results);_15.queryParams=dojo.mixin({},_13);_15.nextQueryParams=dojo.mixin(_13,{"start":_15.nextStart});return esri.arcgis.PortalUtil.clearFieldsFromObject(_14,_15);}));dfd=dojo.delegate(dfd);dfd.queryParams=dojo.mixin({},_13);dfd.nextQueryParams=dojo.when(dfd,function(_16){return _16.nextQueryParams;});return esri.arcgis.PortalResult(dfd);}});esri.arcgis.PortalResult=function(_17){if(!_17){return _17;}if(_17.then){_17=dojo.delegate(_17);}if(!_17.total){_17.total=dojo.when(_17,function(_18){return esri._isDefined(_18.total)?_18.total:(_18.length||0);});}function _19(_1a){if(!_17[_1a]){_17[_1a]=function(){var _1b=arguments;return dojo.when(_17,function(_1c){Array.prototype.unshift.call(_1b,(_1c.results||_1c));return esri.arcgis.PortalResult(dojo[_1a].apply(dojo,_1b));});};}};_19("forEach");_19("filter");_19("map");_19("some");_19("every");return _17;};dojo.declare("esri.arcgis.PortalFolder",null,{constructor:function(_1d){dojo.mixin(this,_1d);this.url=(this.portal&&this.portal.portalUrl)+"content/users/"+this.username+"/"+this.id;this.created=new Date(this.created);},getItems:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.requestToTypedArray(this.url,null,null,esri.arcgis.PortalItem,{"portal":this.portal}));}});dojo.declare("esri.arcgis.PortalGroup",null,{constructor:function(_1e){dojo.mixin(this,_1e);this.url=(this.portal&&this.portal.portalUrl)+"community/groups/"+this.id;this.thumbnailUrl=esri.arcgis.PortalUtil.formatUrl(this.url+"/info/"+this.thumbnail);this.modified=new Date(this.modified);this.created=new Date(this.created);},getMembers:function(){return esri.arcgis.PortalUtil.request(this.url+"/users");},queryItems:function(_1f){_1f=esri.arcgis.PortalUtil.formatQueryParams({},_1f);_1f.q="group:"+this.id+(_1f.q?(" "+_1f.q):"");return this.portal.queryItems(_1f);}});dojo.declare("esri.arcgis.PortalItem",null,{constructor:function(_20){dojo.mixin(this,_20);this.itemUrl=(this.portal&&this.portal.portalUrl)+"content/items/"+this.id;this.userItemUrl=_1.loggedInUser?this.itemUrl.replace("content",("content/users/"+_1.loggedInUser.username)):"";this.itemDataUrl=esri.arcgis.PortalUtil.formatUrl(this.itemUrl+"/data");this.thumbnailUrl=esri.arcgis.PortalUtil.formatUrl(this.itemUrl+"/info/"+this.thumbnail);this.extent=this.extent;this.spatialReference=this.spatialReference;this.created=new Date(this.created);this.uploaded=new Date(this.uploaded);this.modified=new Date(this.modified);},addComment:function(_21){var _22=dojo.isString(_21)?{"comment":_21}:_21;return esri.arcgis.PortalUtil.request(this.itemUrl+"/addComment",_22,{"usePost":true}).then(dojo.hitch(this,function(_23){return new esri.arcgis.PortalComment(dojo.mixin(_22,{"id":_23.commentId,"item":this}));}));},updateComment:function(_24){if(_24&&_24.url&&_24.comment){var _25={"comment":_24.comment};return esri.arcgis.PortalUtil.request(_24.url+"/update",_25,{"usePost":true}).then(function(_26){_24.id=_26.commentId;return _24;});}else{throw new Error();}},getComments:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.requestToTypedArray(this.itemUrl+"/comments",null,null,esri.arcgis.PortalComment,{"item":this}));},deleteComment:function(_27){if(_27&&_27.url){return esri.arcgis.PortalUtil.request(_27.url+"/delete",null,{"usePost":true});}else{throw new Error();}},addRating:function(_28){var _29=dojo.isObject(_28)?_28:{"rating":parseFloat(_28)};return esri.arcgis.PortalUtil.request(this.itemUrl+"/addRating",_29,{"usePost":true}).then(dojo.hitch(this,function(_2a){return new esri.arcgis.PortalRating(dojo.mixin(_29,{"id":_2a.ratingId,"item":this}));}));},getRating:function(){return esri.arcgis.PortalUtil.request(this.itemUrl+"/rating").then(dojo.hitch(this,function(_2b){return new esri.arcgis.PortalRating(dojo.mixin(_2b,{"item":this}));}));},deleteRating:function(){return esri.arcgis.PortalUtil.request(this.itemUrl+"/deleteRating",null,{"usePost":true});}});dojo.declare("esri.arcgis.PortalComment",null,{constructor:function(_2c){dojo.mixin(this,_2c);this.url=this.item.itemUrl+"/comments/"+this.id;this.created=new Date(this.created);}});dojo.declare("esri.arcgis.PortalRating",null,{constructor:function(_2d){dojo.mixin(this,_2d);this.url=this.item.itemUrl+"/rating";this.created=new Date(this.created);}});dojo.declare("esri.arcgis.PortalUser",null,{constructor:function(_2e){dojo.mixin(this,_2e);this.url=(this.portal&&this.portal.portalUrl)+"community/users/"+this.username;this.thumbnailUrl=esri.arcgis.PortalUtil.formatUrl(this.url+"/info/"+this.thumbnail);this.extent=this.extent;this.modified=new Date(this.modified);this.created=new Date(this.created);},getGroups:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.requestToTypedArray(this.url,null,null,esri.arcgis.PortalGroup,{"portal":this.portal}));},getNotifications:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.request(this.url+"/notifications"));},getGroupInvitations:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.request(this.url+"/invitations"));},getTags:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.request(this.url+"/tags"));},getFolders:function(){var dfd=this.getContent().then(function(_2f){return _2f.folders;});return esri.arcgis.PortalResult(dfd);},getItems:function(_30){var dfd=this.getContent(_30).then(function(_31){return _31.items;});return esri.arcgis.PortalResult(dfd);},getContent:function(_32){var url=(this.url.replace("community","content"))+(_32?("/"+_32):"");return esri.arcgis.PortalUtil.request(url).then(dojo.hitch(this,function(_33){_33.folders=esri.arcgis.PortalUtil.resultsToTypedArray(esri.arcgis.PortalFolder,{"portal":this.portal},_33.folders);_33.items=esri.arcgis.PortalUtil.resultsToTypedArray(esri.arcgis.PortalItem,{"portal":this.portal},_33.items);return _33;}));}});esri.arcgis.PortalUtil={useSSL:function(_34,url){return (_34.indexOf("https:")!==-1||(_1.self&&_1.self.allSSL))?url.replace("http:","https:"):url;},formatUrl:function(url){var _35=_1.loggedInUser&&_1.loggedInUser.credential&&_1.loggedInUser.credential.token;return url.indexOf("null")!==-1?null:(_35?url+(url.indexOf("?")!==-1?"&":"?")+("token="+_35):url);},resultsToTypedArray:function(_36,_37,_38){_38=_38?(_38.items||_38.groups||_38.comments||_38.results||_38):[];return dojo.map(_38,function(_39){_39=dojo.mixin(_39,_37);return _36?new _36(_39):_39;});},clearFieldsFromObject:function(_3a,obj){if(!dojo.isArray(_3a)){for(var fld in _3a){delete obj[fld];}}else{for(var i=0,l=_3a.length;i<l;i++){delete obj[_3a[i]];}}return obj;},getSocket:function(url,_3b){_3b=_3b||{};var _3c=dojox.socket({url:url,transport:_3b.transport||esri.arcgis.PortalUtil.request,interval:_3b.interval||60000});if(!_3b.reconnect===false){_3c=dojox.socket.Reconnect(_3c);}return _3c;},requestToTypedArray:function(url,_3d,_3e,_3f,_40){return esri.arcgis.PortalUtil.request(url,_3d,_3e).then(dojo.partial(esri.arcgis.PortalUtil.resultsToTypedArray,_3f,_40));},request:function(url,_41,_42){_41&&_41.portal&&delete _41.portal;var _43=dojo.mixin(dojo.mixin({},_41||{}),_1.requestParams);var _44=dojo.mixin(_42||{},_1.options);return esri.request({url:esri.arcgis.PortalUtil.useSSL(window.location.protocol,(url.url||url)),content:_43,callbackParamName:"callback",timeout:(_44&&_44.timeout)||0},_44).promise;},formatQueryParams:function(_45,_46){return dojo.mixin(dojo.mixin({},_45),(dojo.isString(_46)?{"q":_46}:(_46||{})));}};})();}