define(
        batch
        staticBatch
        copy
        saveJSON
            addIndexedFields
            convertArrayToObject
        removeServerCreatedObjects
        cleanUpSakaiDocObject
        loadJSON
        convertObjectToArray
        removeJSON
        createSearchString
define(

class Server
description Server communication and batch processing. This should only hold functions which are used across multiple pages, and does not constitute functionality related to a single area/page
namespace Server related convenience functions and communication


batch : function(_requests, _callback, _forcePOST, _async) {

Perform a batch request to the server
param {Array} requests The JSON object of requests
param {Function} callback Callback function, passes ({Boolean} success, {Object} data)
param {Boolean} forcePOST if we need to force a POST
param {Boolean} async If we should do an async request or not


staticBatch : function(_requests, _callback) {

Perform a batch request for a list of static files
param {Array} requests The static files that need to be requested
param {Function} callback Callback function, passes ({Boolean} success, {Object} data)


copy : function(config, callback) {

Copy one node to another node
param {Object} config JSON object which has several options - source: The source path, where you want to copy from - destination: The destination path, where you want to copy to - replace: If true, it will remove the destination branch before doing the copy
param {Function} callback A callback function which is executed at the end of the operation


saveJSON : function(i_url, i_data, callback, removeTree, indexFields, merge) {

Saves a specified JSON object to a specified URL in JCR. The structure of JSON data will be re-created in JCR as a node hierarchy.
param {String} i_url The path to the preference where it needs to be saved
param {Object} i_data A JSON object which we would like to save (max 200 child object of each object)
param {Function} callback A callback function which is executed at the end of the operation
param {Boolean} removeTree If we should replace the entire tree of saved data or just update it
param {Array} indexFields Fields to index in the data (used for widgets, and is optional)
param {Boolean} merge If false, it will truly replace the content during an import - the default is true
returns {Void}


var addIndexedFields = function(path, obj) {

param {Array} path The path to the data object, split out
param {Object} obj The data object to add the field to


var convertArrayToObject = function(obj) {

<p>Convert all the arrays in an object to an object with a unique key.<br /> Mixed arrays (arrays with multiple types) are not supported. </p> <code> { 'boolean': true, 'array_object': [{ 'key1': 'value1', 'key2': 'value2'}, { 'key1': 'value1', 'key2': 'value2'}] } </code> to <code> { 'boolean': true, 'array_object': { '__array__0__': { 'key1': 'value1', 'key2': 'value2'}, '__array__1__': { 'key1': 'value1', 'key2': 'value2'} } } </code>
param {Object} obj The Object that you want to use to convert all the arrays to objects
return {Object} An object where all the arrays are converted into objects


removeServerCreatedObjects : function(obj, namespace, notToRemove) {

Removes any objects with a given namespace
param {Object} the object to clean
param {Array} an array containing a string for each namespace to move


cleanUpSakaiDocObject: function(pagestructure) {

Take a Sakai Doc object retrieved from the server and clean it so it only has the real data. This function will filter out properties that have been added on the server side during saving
param {Object} the object to clean


loadJSON : function(i_url, callback, data) {

Loads structured preference data from a specified URL (and it's node subtree)
param {String} i_url The path to the preference which needs to be loaded
param {Function} callback A callback function which is executed at the end of the operation
param {Object} data The data to pass to the url
returns {Void}


convertObjectToArray : function(specficObj, globalObj, objIndex) {

<p>Convert all the objects with format __array__?__ in an object to an array</p> <code> { 'boolean': true, 'array_object': { '__array__0__': { 'key1': 'value1', 'key2': 'value2' } } } </code> to <code> { 'boolean': true, 'array_object': [ { 'key1': 'value1', 'key2': 'value2' } ] } </code>
param {Object} specficObj The Object that you want to use to convert all the objects with the special format to arrays
param {Object} [globalObj] The parent object, we need this to run over the elements recursively
param {Object} [objIndex] The index of the parent object
return {Object} An object where all the objects with the special format are converted into arrays


removeJSON : function(i_url, callback) {

Remove the JSON for a specific node in JCR
param {String} i_url The path of the node you want to remove
param {Function} callback Callback function which is executed at the end of the operation
returns {Void}


createSearchString : function(searchString, handlePhrases, joinOn, tagString) {

Create a search string for the server This method exists to transform a user's search string which they type in into the string we should pass to the server Strings with AND, OR, '"', '-', '_' are treated as advanced search queries and left alone. Those without are transformed into term* AND term2*
param {String} searchString The user's search
param {Boolean} handlePhrases If we should split on ,\s instead of \s to better handle phrases
param {String} joinOn String to join keywords on, defaults to AND
param {Boolean} tagString True if we are processing a directory/tag string
return {String} The string to send to the server