/*
 *  Copyright 2013 Anyware Services
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * This class handles the access to page properties. See {@link Ametys.web.page.Page}
 */
Ext.define(
	"Ametys.web.sitemap.SitemapDAO", 
	{
		singleton: true,
		
		/**
		 * Retrieve a sitemap by its id
		 * @param {String} id The sitemap identifier. Cannot be null.
		 * @param {Function} callback The callback function called when the sitemap is retrieved. Can be null for synchronous mode (not recommended!). Parameters are
		 * @param {Ametys.web.sitemap.Sitemap} callback.sitemap The sitemap retrieved. Can be null if sitemap does not exist.
		 * @param {String} [workspaceName] The JCR workspace name
         * @param {String} [zoneName] A zone name to limit informations to that zone for performance purposes
         * @param {String} [zoneItemId] A zoneitem id to limit informations to that zoneitem for performance purposes
		 * @return {Ametys.web.sitemap.Sitemap} The sitemap retrieved if no callback function is specified or null otherwise.
		 */
		getSitemap: function (id, callback, workspaceName, zoneName, zoneItemId)
		{
			if (Ext.isEmpty(id))
			{
				callback(null);
				return;
			}
			this._sendRequest (id, zoneName, zoneItemId, Ext.bind(this._getSitemapCb, this, [callback], 1), workspaceName);
		},
		/**
		 * @private
		 * Callback function called after #getSitemap is processed
		 * @param {Ametys.web.sitemap.Sitemap} sitemap The sitemap retrieved
		 * @param {Function} callback The callback function called 
		 */
		_getSitemapCb: function (sitemap, callback)
		{
			callback(sitemap);
		},
		
		/**
		 * @private
		 * Send request to server to retrieved sitemap
		 * @param {String[]} id The id of sitemap to retrieve
         * @param {String} [zoneName] A zone name to limit informations to that zone for performance purposes
         * @param {String} [zoneItemId] A zoneitem id to limit informations to that zoneitem for performance purposes
		 * @param {Function} callback The callback function to be called after server process
		 * @param {String} [workspaceName] The JCR workspace name
		 */
		_sendRequest: function (id, zoneName, zoneItemId, callback, workspaceName)
		{
			Ametys.data.ServerComm.callMethod({
				role: "org.ametys.web.repository.page.SitemapDAO",
				methodName: "getSitemap",
				parameters: [id, zoneName, zoneItemId],
				callback: {
					scope: this,
					handler: this._getSitemapCB,
					arguments: [callback, id]
				},
				errorMessage: {
					category: this.self.getName(),
					msg: "{{i18n PLUGINS_WEB_DAOS_SITEMAP_ERROR}}"
				}
			});
		},
		
		/**
		 * @private
		 * Callback function called after #_sendRequest is processed
		 * @param {Object} data The sitemap properties
		 * @param {Array} arguments The callback arguments.
		 */
		_getSitemapCB: function (data, arguments)
		{
			if (!data)
			{
				Ametys.log.ErrorDialog.display({
					title: "{{i18n PLUGINS_WEB_DAOS_SITEMAP_NOT_FOUND}}", 
					text: "{{i18n PLUGINS_WEB_DAOS_SITEMAP_NOT_FOUND_HINT}}",
		    		details: args[1],
		    		category: "Ametys.web.sitemap.SitemapDAO"
				});
			}
			
			var sitemap = Ext.create ('Ametys.web.sitemap.Sitemap', data);

			if (typeof arguments[0] == 'function')
			{
				arguments[0] (sitemap);
			}
		}
		
		
	}
);