/**
 * 
 * All content on this website (including text, images, source
 * code and any other original works), unless otherwise noted,
 * is licensed under a Creative Commons License.
 * 
 * http://creativecommons.org/licenses/by-nc-sa/2.5/
 * 
 * Copyright (C) 2004-2008 Open-Xchange, Inc.
 * Mail: info@open-xchange.com 
 * 
 * @author Jan Finsel <jan.finsel@open-xchange.com>
 * 
 */




  /////////////////////////////////////
 //   Key and Small Key             //
/////////////////////////////////////
/***
 * Creates a new Key
 * @class Key
 * The class key is needed for MRUKeyList, every key has two functions.
 * hashCode and equals   
 */
function Key() {
}
Key.prototype.equals = function(keyObject1) {
	return (this.module==keyObject1.module && this.folder_id == keyObject1.folder_id && this.id == keyObject1.id && this.recurrence_position == keyObject1.recurrence_position)
}
Key.prototype.hashCode = function() {
    if(this.calculatedHash) { return this.calculatedHash; }
	if(this.recurrence_position || (this.recurrence_position == 0)) {
		if(!isNaN(this.folder_id) && !isNaN(this.id) && !isNaN(this.recurrence_position)) {
		  this.calculatedHash=parseInt(this.folder_id)+parseInt(this.id)+parseInt(this.recurrence_position);
          return this.calculatedHash;		 	
		} else {
		  this.calculatedHash=(this.folder_id+this.id+this.recurrence_position);
          return this.calculatedHash;	
		}
	} else {
		if(!isNaN(this.folder_id) && !isNaN(this.id)) {
			this.calculatedHash= (parseInt(this.folder_id)+parseInt(this.id));
			return this.calculatedHash;
	    } else {
		    this.calculatedHash=(this.folder_id+this.id);
            return this.calculatedHash;
		}
	}
}
Key.createfromObject= function(keyObject) {
	var element=new Key();
	element.module=keyObject.module;
	element.folder_id=keyObject.folder_id;
	element.id=keyObject.id;
	element.recurrence_position=keyObject.recurrence_position;
	return element;
}

function SmallKey(module,id) {
	this.module=module;
	this.id=id;
}
SmallKey.prototype.equals = function(keyObject1) {
    return this.module==keyObject1.module && this.id==keyObject1.id;
}
SmallKey.prototype.hashCode = function() {
    return this.id;
}
  /////////////////////////////////////
 //   List-Recently-Used Key list   //
/////////////////////////////////////
/**
 * Creates an LRU list.
 * @class List-recently-used list (LRU list).
 * An MRU list limits the number of stored items by discarding
 * the least-recently referenced item, when storing a new item would otherwise
 * exceed the limit.
 * @param {Int} size The maximum size of the list.
 */
function LRUKeyList(size) {
	this.free = size;
	if(!this.free) {
        this.free=-1;
    }
    this.cache = {};
    this.list = new LinkedList();
}

LRUKeyList.prototype = {
    /**
     * Retrieves an item from the list.loadconfig();
     * @param key The key of the item to retrieve.
     * @return The retrieved item or <code>undefined</code> if the item was not
     * found.
     */
    get: function(key,changeList,wholeobject) {
        var items = this.cache[key.hashCode()];
        var item;
        if(items) {
            for (var i=0;i<items.length;i++) {
                if(key.equals(items[i].key)) {
                    item=items[i];  
                    break;
                }
            }
        }
        if(!item) return undefined;
        if(changeList) {
            this.list.remove(item);
            this.list.addLast(item);
        }
        if(!wholeobject) return item.data;
        else { return item; }
    },
	
	/**
     * Stores an item in the list.
     * @param key The key under which to store the item.
     * @param value The item to store in the list.
     * @return A removed item if cache is full or <code>undefined</code>
     * found.
     */
    set: function(key, value) {
        var item = this.get(key,false,true);
        if (item)
            item.data = value;
        else {
            item = {key: key, data: value};
            this.list.addLast(item);
            var hash = key.hashCode();
            if(!this.cache[hash]) this.cache[hash]=new Array();
			this.cache[hash].push(item);
            if (this.free) {
                this.free--;
            } else {
                var key1=this.list.removeFirst().key;
                var ret=this.get(key1,false,true);
                this.remove(key1);
				this.free--;
                return ret;
            } 
        }
    },


    /**
     * Removes an item from the list.
     * @param key The key of the item to remove.
     */
    remove: function(key) {
        var items = this.cache[key.hashCode()];
        var item;
        if(items) {
            for (var i=0;i<items.length;i++) {
                if(key.equals(items[i].key)) {
					this.list.remove(items[i]);
                    items.splice(i,1);
					if(!items.length) {
						this.cache[key.hashCode()]= undefined;
					}
					this.free++;
					break;  
                }
            }
        }
    },
	/**
     * Returns an array of keys.
     * @returns an Array of keys in LRUList
     */
	keys : function() {
	   var myKeys=new Array();
	   for(var hashcode in this.cache) {
	   	   if(this.cache[hashcode]) {
	           for(var i=0;i<this.cache[hashcode].length;i++) {
		          if(this.cache[hashcode][i].key) { myKeys.push(this.cache[hashcode][i].key) }	
		       }
		   }
	   }
	   return myKeys;
	}
};
  /////////////////////////////////////
 //   OX Collection                 //
/////////////////////////////////////

function OXCollection() {
    this.last_modified; //Maximum of last modified
    this.lastRequest;   //Last request of 
    this.criteria;      //Criteria
    this.order;         //Order 
    this.objects=new Array();
    this.map_objects=new LRUKeyList();
}

  /////////////////////////////////////
 //   HELP FUNCTIONS                //
/////////////////////////////////////
function switchStringObject(object) {
    var myObject=new Object();
    for(var i in object) {
        myObject[object[i]]=i;
    }
    return myObject;
}

  /////////////////////////////////////
 //   OXCACHE                       //
/////////////////////////////////////
function OXCache() {}
/**
 * Time {int} (in Milliseconds), at which an existing object in Cache is expired and ought
 * ought to requested again.
 * @value 0 existing objects in Cache never expire
 */
OXCache.updateTime       = 300000; //Time in Milliseconds for next update if is requested
/**
* Object size {int}, number of objects, which are maximum in Cache.
*/
OXCache.objectSize       = 20000;
/**
* Object size {int}, number of criteria list, which are maximum in Cache.
*/
OXCache.criteriaSize     = 10;
/**
 * @private
 * Sequence (int) of Cache 
 */
OXCache.sequence    = 0;
/**
* @private
* Returns next Sequence .
* @return The next sequence value {int}
*/
OXCache.getSequence = function() {
    return OXCache.sequence++;
}
/**
* @private 
* Registered module mappings.
*/
OXCache.moduleMappings   = new Object();
/**
* @private
* Cache for Objects.
*/
OXCache.cachedObjects    = new LRUKeyList(OXCache.objectSize);
/**
* @private
* Cache for Collections.
*/
OXCache.cachedCollections = new MRUList(OXCache.criteriaSize); //new Array();
/**
* @private 
* Temporary Trash for objects, which are removed out of cachedCollections between the last requests.
*/
OXCache.cachedObjectsTrash = new LRUKeyList();
/**
* @private 
* Temporary Trash for collections, which are removed out of cachedCollections between the last requests.
*/
OXCache.cachedCollectionsTrash = new Array();
/**
* @private 
* Short key mapping for objects.
*/
OXCache.objectShortKey   = new LRUKeyList();
/**
* @private 
* Request, which will be requested after execute all callbacks.
*/
OXCache.waitingrequests = new Array();
/**
* @private
* Registered modified callbacks
*/
OXCache.modifiedCallbacks = new Object();
/**
* @private 
* Registered callbacks which are executed just one time.
*/
OXCache.actualCallbacks = new Object();
/**
* @private
* List of modified Objects, which are changed since last request.
*/
OXCache.modifiedObjects= new Array();
/**
* @private
* List of possible deleted Objects in list requests, which are changed since last request.
*/
OXCache.deletedObjects= new Array();
/**
 * @private
 * List all all requests send to server
 */
OXCache.requested=new Array();
/**
 * @private
 * Checks if OXCache is in execution 
 */
OXCache.executeActive=false;

/**
* @private
* Adds request to waiting requests. Waiting requests will be inquired after all callbacks are executed.
* @param module {string}, String of module
* @param collection {OXCollection}, Collection object
* @param cb {function}, Callback 
*/
OXCache.requestSequence = 0;
OXCache.getRequestSequence = function() {
    return OXCache.requestSequence++;
}

OXCache.addRequest= function(module,collection,cb,uniqueName) {
	OXCache.waitingrequests.push({module : module ,collection : collection , cb : cb, uniqueName : uniqueName });
}
/**
* @private
* Join of Cache. Executes all execute functions in Cache, if all started requests are returned 
* and clears tmp variables at last step.
*/
OXCache.join = new Join(function() {
	OXCache.callbackArray=new Object();
	OXCache.modifiedArray=new Object();
	for(var i in OXCache.actualCallbacks) {
        OXCache.callbackArray[i]=true;
    }
    for(var i in OXCache.modifiedCallbacks) {
        OXCache.modifiedArray[i]=true;
    }
    
	for (var i in OXCache.moduleMappings) {
        if(OXCache.moduleMappings[i].execute) {
            OXCache.moduleMappings[i].execute();
        }
    }
    for(var i in OXCache.callbackArray) {
        delete OXCache.actualCallbacks[i];
    }
    for(var i in OXCache.modifiedObjects) {
       OXCache.modifiedObjects[i].modified=false;
    }
    OXCache.modifiedObjects=new Array();
    OXCache.deletedObjects=new Array();
    for(var i in OXCache.cachedCollections.cache) {
       OXCache.cachedCollections.cache[i].data.modified=false;
    }
	
	for(var i = 0;i<OXCache.waitingrequests.length;i++) {
        OXCache.actualCallbacks[OXCache.waitingrequests[i].uniqueName]={
			module : OXCache.waitingrequests[i].module , 
		    collection : OXCache.waitingrequests[i].collection ,
			fn : OXCache.waitingrequests[i].cb
		};
        OXCache.moduleMappings[OXCache.waitingrequests[i].module].get(OXCache.waitingrequests[i].uniqueName,OXCache.waitingrequests[i].module,OXCache.waitingrequests[i].collection,null,OXCache.waitingrequests[i].cb,true);		
    }   
    OXCache.waitingrequests=new Array();
    OXCache.cachedObjectsTrash=new LRUKeyList();
    OXCache.cachedCollectionsTrash=new Array();
	OXCache.requested=new Array();
});
OXCache.errorHandler = function(uniqueName,request,response,response2) {
	if(debug) { console.log("Error",uniqueName,request,response,response2); }
	if(response.error) {
		switch(response.category) {
			case  1: //USER IMPUT (SHOULD NOT EXIST) NOT IN CACHE
			    newServerError(response);
                OXCache.unregister(uniqueName);				
                break;
			case  2: //CONFIGURATION ERROR (SHOULD NOT EXIST) NOT IN CACHE
			    newServerError(response);
				OXCache.unregister(uniqueName);
                break;
			case  3: //MISSING RIGHTS
            case  4: //TRY AGAIN
			case  5: //SUBSYSTEM DOWN
			    newServerError(response);
                var col=OXCache.cachedCollections.get(uniqueName);
				if(col) { 
				    col.check=false;
					col.modified=false;
				} else {
					OXCache.unregister(uniqueName);
				}
				break;
			case  6: //SOCKET CONNECTION (CAN'T OCCUR)
                newServerError(response);
				OXCache.unregister(uniqueName);
				break;
			case  7: //INTERNAL ERROR (SERVER BUG)
                newServerError(response);
				OXCache.unregister(uniqueName);
				break;
			case  8: //CODE ERROR (SERVER BUG)
                newServerError(response);
				OXCache.unregister(uniqueName);
				break;
			case  9: //MODIFICATION CHANGES(TIMESTAMP EXPIRED)
                newServerError(response);
				OXCache.unregister(uniqueName);
				break;
			case 10: //CONFIG FILE ERROR(SERVER BUG)
                newServerError(response);
				OXCache.unregister(uniqueName);
				break;
			case 11: //RESSOURCE FULL e.g IMAP QUOTA
                newServerError(response);
				OXCache.unregister(uniqueName);
				break;
			case 12: //TO LONG VALUES IN FIELD (SHOULD NOT EXIST)
                newServerError(response);
				OXCache.unregister(uniqueName);
				break;
			case 13: //WARNING
                newServerError(response);
				OXCache.unregister(uniqueName);
                break;
			default:
			    newServerError(response);
                break;
		}
	}
	return;	
}
/**
* @private
* Starts a new JSON Request combined with OXCache join handling 
* @param url {String} Url of request
* @param data {object} Data of request
* @param cb {function} Callback function
*/
OXCache.getJSON = function(url,data,cb) {
    (new JSON()).put(url,data,null,OXCache.join.add(cb));   
}
/**
* @private
* Sets a new Mapping for ObjectCache 
* @param url {String} module of mapping
* @param data {OXMapping} An OX Mapping
*/
OXCache.setMapping = function(module,mapping) {
    if(OXCache.moduleMappings[module]) {
        if(debug) { alert("A module mapping for module >"+module+"< is still available in Object cache."); }
        return;
    } 
    OXCache.moduleMappings[module]=mapping;
}
/**
* Starts a OXCache request 
* @param uniqueName {String} Unique name for this request 
* @param module {String} Module of the request
* @param collection {OXCollection} see OXCollection for further description
* @param callbackModified {function} Callback Modified will be executed when Collection is modified (optional)
* @param callback {function} Callback which will be executed directly (optional)
* @param forceupdate {boolean} (optional) Ignore expire handling of object and start new request automatically (optional)
*        default <code>false</code>  
*/
OXCache.newRequest = function (uniqueName,module,collection,callbackModified,callback,forceupdate) {
    var returnobject;
	uniqueName="oxcache_external_"+OXCache.getRequestSequence();
	if(!OXCache.moduleMappings[module]) {
        if(debug) { alert("A module mapping for module >"+module+"< is not available in Object cache.");}
        return;
    }
	if(!uniqueName) {
        if(debug) { alert("No uniquename entered for modified function");}
        return;
    }
    if((OXCache.modifiedCallbacks[uniqueName] && 
        OXCache.modifiedCallbacks[uniqueName].fn && 
        callbackModified) || 
	   (OXCache.actualCallbacks[uniqueName] && 
       OXCache.actualCallbacks[uniqueName].fn && 
       callback))
	{
        if(debug) { alert("A modified function for name >"+uniqueName+"< is still available in Object cache.");}
    }
	if(callbackModified) {
	   OXCache.modifiedCallbacks[uniqueName]={module : module, collection : collection, fn : callbackModified };
	}
	if(callback) {
	   OXCache.actualCallbacks[uniqueName]={module : module , collection : collection ,fn : callback};
	}
	var returnobject=OXCache.moduleMappings[module].get(uniqueName,module,collection,callbackModified,callback,forceupdate);
	returnobject.uniqueName=uniqueName;
	return returnobject;
}

/**
* Unregister a modified callback 
* @param uniqueName {String} Unique name of the modified request
* */
OXCache.unregister = function(uniqueName) {
    var registered=false
	if(OXCache.modifiedCallbacks[uniqueName]) { 
        delete OXCache.modifiedCallbacks[uniqueName];  
		registered=true;
    }
	if(OXCache.actualCallbacks[uniqueName]) { 
        delete OXCache.modifiedCallbacks[uniqueName];
		registered=true;
    } 
	//if(debug && !registered) { alert("No function for name >"+uniqueName+"< is not available in Object cache.");}
    
}
/**
* Updates all modified callbacks 
* */

OXCache.update = function() {
    for(var counter in OXCache.modifiedCallbacks) {
        var uniqueObject=OXCache.newRequest(null,OXCache.modifiedCallbacks[counter].module,OXCache.modifiedCallbacks[counter].collection,null,function(data) {
	        if (uniqueObject) {
			  OXCache.unregister(uniqueObject.uniqueName);
			} else {
			    //debugger;
			}
	    },true);
    }   
}
  /////////////////////////////////////
 //   OX Mapping                    //
/////////////////////////////////////
/**
 * Abstract Mapping which has implemented functions for whole core modules
 */
function OXAbstractMapping() {}
/**
 * Returns function get for a OXMapping implementation.  
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} get function for OXMapping implementation. Handles requests of a module type in combination if object cache.
 * params of the function:
 *      uniqueName {String} Unique name of a request
 *      module {String} Module of a request
 *      collection {OXCollection} OXCollection of a request see OXCollection for further description
 *      (optional) callbackModified {function} A callback which has to be executed when Collection is modified 
 *      (optional) callback {function} A callback which will be executed directly 
 *      (optional) forceupdate {boolean}  A boolean ig expire handling of OXCache ought to be ignored. 
 *          
 */
OXAbstractMapping.get=function(oxMapping) {
    return function(uniquename,module,collection,callbackModified,callback,forceupdate) {
        //Check criteria search or list search
        if(collection.criteria) {
            oxMapping.criteriaRequest(uniquename,module,collection,callbackModified,callback,forceupdate);
        } else if(collection.objects) {
            return oxMapping.listRequest(uniquename,module,collection,callbackModified,callback,forceupdate);
        } else {
            if(debug) { alert("Illegal request: Neighter criteria nor objects are set"); }
        }
		return {};
    }
}
/**
 * Returns Execute function a OXMapping implementation.
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} execute function for OXMapping implementation. Excutes all needed callbacks of an module in OXCache
 *          
 */
OXAbstractMapping.execute=function(oxMapping) {
	return function() {
		for(var i in OXCache.actualCallbacks) {
			if(OXCache.callbackArray[i]) {
			   var tmpCallback=OXCache.actualCallbacks[i];
                if(!tmpCallback.executed) {
					tmpCallback.executed=true;
					if(tmpCallback.module==oxMapping.module) {
	                    var ret=oxMapping.buildResponse(tmpCallback,false,i);
	                    if(ret) {
	                        OXCache.actualCallbacks[i].fn(ret);
	                    }
	                }
					tmpCallback.executed=false;
			    }	
			}    
        }
		for(var i in OXCache.modifiedCallbacks) {
            if(OXCache.actualCallbacks[i]) { continue; }
			if(OXCache.modifiedArray[i]) {
				var tmpCallback=OXCache.modifiedCallbacks[i];
                if(!tmpCallback.executed) {
					tmpCallback.executed=true;
		            if(tmpCallback.module==oxMapping.module) {
		                var ret=oxMapping.buildResponse(tmpCallback,true,i);
						if(ret) {
							OXCache.modifiedCallbacks[i].fn(ret);	
						}
		            }
					tmpCallback.executed=false;
				}
			}
        }
	}
}
/**
 * @private 
 * This function is needed, if abstract function execute of an OXAbstractMapping is mapped to an OXMapping implementation
 * Return function builds response object, for a callback
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} buildResponse function for OXMapping implementation. 
 * params of the function:
 *      collection {OXCollection} OXCollection of an response
 *      isModified {boolean} boolean if callback is modified request
 *      callback {boolean} executable Callback          
 */
OXAbstractMapping.buildResponse = function(oxMapping) {
	return function(tmpCallback,isModified,uniqueName) {
		var collection=tmpCallback.collection;
		if(collection.criteria) {
			var tmpCollection=oxMapping.getCollection(collection,false,true);
			if(!tmpCollection || tmpCollection.check) {
				if(debug) { console.log("Missing or changed collection:",collection); }
				OXCache.addRequest(oxMapping.module,collection,tmpCallback.fn,uniqueName);
			} else if(!isModified || tmpCollection.modified) {
                var retCol=new OXCollection();
				retCol.timestamp=tmpCollection.timestamp;
                retCol.criteria=tmpCollection.criteria;
				retCol.search=tmpCollection.search;
				//TODO CHECK FOR ORDER 
                retCol.order=collection.order;         //Order
                var direction=1;
                if(collection.order && collection.order.length) {
					if(collection.order[0].order!=tmpCollection.order[0].order) {
						direction=-1;
					}
				} 
                retCol.objects=new Array(tmpCollection.objects.length);
                retCol.map_objects=new LRUKeyList();
				var counter= (direction==1) ? 0 : tmpCollection.objects.length-1;
				for(var i=0;i<tmpCollection.objects.length;i++) {
				   //var tmpObject=OXCache.cachedObjects.get(tmpCollection.objects[i]) || OXCache.cachedObjectsTrash.get(tmpCollection.objects[i]);
				   var tmpObject=tmpCollection.objects[i];
	               if(tmpObject) { 
	                   retCol.objects[counter]=tmpObject;
	                   retCol.map_objects.set(tmpObject,counter+1);
	               } else {
	                   if(debug) {console.log(tmpCollection.objects[i],i,"is missing");//TODO : OBJECT IS OUT OF CACHE CAUSED BY SIZE AND MODIFIED REQUEST NEED DATA 
					   }   
	               }
				   counter=counter+direction;
			    }
			}
			return retCol;
		} else if(collection.objects) {
			if(isModified) {
				if(!tmpCallback.modified) {
					var realmodified=false;
					for(var i=0;i<OXCache.modifiedObjects.length;i++) {
						for(var i2=0;i2<collection.objects.length;i2++) {
							if(OXCache.modifiedObjects[i].equals(collection.objects[i2])) {
							  realmodified=true;
							  break;
					        }
						}	
					}
					if(!realmodified) return;
				} else {
					tmpCallback.modified=false;
				}
			}
			var retCol=new OXCollection();
            retCol.objects=new Array();
            for(var i=0;i<collection.objects.length;i++) {
                //TODO
				if(!collection.objects[i].hashCode || !collection.objects[i].equals) {
					collection.objects[i]=Key.createfromObject(collection.objects[i]);
				}
                var tmpObject=OXCache.cachedObjects.get(collection.objects[i]) || OXCache.cachedObjectsTrash.get(collection.objects[i]);
                if(tmpObject) {
                    retCol.objects.push(tmpObject);
                } else {
					//TODO ??? IS OBJECT REAL DELETED ??? THROW ERROR
                    if(debug) {
						console.log(collection.objects[i],i,"is missing");//TODO : OBJECT IS OUT OF CACHE CAUSED BY SIZE AND MODIFIED REQUEST NEED DATA
					}   
                }
            }
			return retCol;	
        }
	} 
}
/**
 * @private 
 * This function is needed, if abstract function get of an OXAbstractMapping is mapped to an OXMapping implementation
 * Return function handles criteria requests of a OXMapping request
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} criteriaRequest function for OXMapping implementation. 
 *      params of the function:
 *      uniqueName {String} Unique name of a request
 *      module {String} Module of a request
 *      collection {OXCollection} OXCollection of a request see OXCollection for further description
 *      (optional) callbackModified {function} A callback which has to be executed when Collection is modified 
 *      (optional) callback {function} A callback which will be executed directly 
 *      (optional) forceupdate {boolean}  A boolean ig expire handling of OXCache ought to be ignored. 
 * 
 */
OXAbstractMapping.criteriaRequest =function(oxMapping) {
    return function(uniqueName,module,collection,callbackModified,callback,forceupdate) {
        var onlymandatory=false;
		if(!collection.columns) { 
            var tmpColumns=new Array();
            for(var value in oxMapping.stringmapping) {
                tmpColumns.push(value);
                collection.columns=tmpColumns;
            }
        }
		for(var i=0;i<collection.columns.length;i++) {
            for(var i2=0;i2<oxMapping.mandatoryfields.length;i2++) {
                if(collection.columns[i]==oxMapping.mandatoryfields[i2]) {
				    collection.columns.splice(i,1);
                }
            }
        }
		if(!collection.columns.length) {
			onlymandatory=true;
		}
        collection.columns=oxMapping.mandatoryfields.concat(collection.columns);             
		//Check criteria exists
        var myCollection=oxMapping.getCollection(collection,false);
        if(myCollection) { 
            //Check time expired
            if(!forceupdate && (myCollection.lastRequest && myCollection.lastRequest>(new Date().getTime()-OXCache.updateTime))) {
				if (!onlymandatory) {
					for (var i = 0; i < myCollection.objects.length; i++) {
						var tmpObject = OXCache.cachedObjects.get(myCollection.objects[i]);
						if (!(tmpObject && tmpObject.hasColumns(collection.columns))) {
							if (!tmpObject) {
								if (myCollection.objects[i].hashCode) {
									tmpObject = myCollection.objects[i];
								}
							}
							oxMapping.addCriteriaMissingColumn(collection.columns, tmpObject, uniqueName);
						}
					}
				}               
            } else {
                oxMapping.addCriteriaExpired(collection,myCollection.timestamp,uniqueName);
            }     
        } else {
            oxMapping.addCriteriaNotInCache(collection,uniqueName);
        }
        oxMapping.request();
		delete oxMapping.requestCriteriaNotInCache;
        delete oxMapping.requestCriteriaExpired;
        delete oxMapping.requestCriteriaMissingColumn;
       
    }
}
/**
 * @private 
 * This function is needed, if abstract function get of an OXAbstractMapping is mapped to an OXMapping implementation
 * Return function handles list requests of a OXMapping request
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} listRequest function for OXMapping implementation. 
 *      params of the function:
 *      uniqueName {String} Unique name of a request
 *      module {String} Module of a request
 *      collection {OXCollection} OXCollection of a request see OXCollection for further description
 *      (optional) callbackModified {function} A callback which has to be executed when Collection is modified 
 *      (optional) callback {function} A callback which will be executed directly 
 *      (optional) forceupdate {boolean}  A boolean if expire handling of OXCache ought to be ignored. 
 */
OXAbstractMapping.listRequest =function(oxMapping) {
    return function(uniqueName,module,collection,callbackModified,callback,forceupdate) {
        var returnobject={};
		returnobject.clear=[];
		returnobject.set=[];
		var isrequest=false;
		if(collection.objects) {
			if(!collection.columns) { 
                 var tmpColumns=new Array();
                 for(var value in oxMapping.stringmapping) {
                    tmpColumns.push(value);
                    collection.columns=tmpColumns;
                 }
				 if(oxMapping.alternative) {
				    for(var value in oxMapping.alternative) {
                        tmpColumns.push(value);
                        collection.columns=tmpColumns;
                    }	
				 }
            } 
			for(var i=0;i<collection.columns.length;i++) {
                for(var i2=0;i2<oxMapping.mandatoryfields.length;i2++) {
                    if(collection.columns[i]==oxMapping.mandatoryfields[i2]) {
                        collection.columns.splice(i,1);
                    }
                }
            }
			collection.columns=oxMapping.mandatoryfields.concat(collection.columns);
            for(var i=0;i<collection.objects.length;i++) {
                var tmpVar = collection.objects[i];
                var tmpObject = Key.createfromObject(tmpVar);
				var myCacheObject=OXCache.cachedObjects.get(tmpObject);
                if(myCacheObject) {
                    if(myCacheObject.hasColumns(collection.columns)) {
                        if(forceupdate || (!(myCacheObject.lastRequest>(new Date().getTime()-OXCache.updateTime)))) {
                           oxMapping.addListExpired(collection.columns,tmpObject,uniqueName);
						   isrequest=true;
                        } 
						returnobject.set.push(OXCache.cachedObjects.get(tmpObject));
                    } else {
                       oxMapping.addListMissingColumn(collection.columns,tmpObject,uniqueName);
					   returnobject.clear.push(tmpObject);
					   isrequest=true;
                    }
                } else {
                    oxMapping.addListNotInCache(collection.columns,tmpObject,uniqueName);
					returnobject.clear.push(tmpObject);
					isrequest=true;
                }
           }
       }   
       oxMapping.request();
	   delete oxMapping.requestListNotInCache;
       delete oxMapping.requestListExpired;
       delete oxMapping.requestListMissingColumn;
		return isrequest ? returnobject : {};
    }
}
/**
 * @private 
 * This function is needed, if abstract function criteria Request of OXAbstractMapping is mapped to an OXMapping implementation
 * Return function adds OXCollection to the to server request list (not in Cache)
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} addCriteriaNotInCache function for OXMapping implementation. 
 *      params of the function:
 *      collection {OXCollection} OXCollection for a request see OXCollection for further description
 */
OXAbstractMapping.addCriteriaNotInCache = function(oxMapping) {
    return function(collection, uniqueName) {
		if(!oxMapping.requestCriteriaNotInCache) {
            oxMapping.requestCriteriaNotInCache=new Array();
        }
		if(OXCache.requested && OXCache.requested["CNOT"]) {
            for(var i=0;i<OXCache.requested["CNOT"].length;i++) {
                if(oxMapping.equalsCollection(OXCache.requested["CNOT"][i].collection, collection)) {
                	return; 
                }
            }
        }
		if(!OXCache.requested) { OXCache.requested={}}
        if(!OXCache.requested["CNOT"]) { OXCache.requested["CNOT"]=[]}
        OXCache.requested["CNOT"].push({ collection : collection, uniqueName : uniqueName });
        oxMapping.requestCriteriaNotInCache.push({ collection : collection, uniqueName : uniqueName } );
    }
}
/**
 * @private 
 * This function is needed, if abstract function criteria Request of OXAbstractMapping is mapped to an OXMapping implementation
 * Return function adds OXCollection to the to server request list (in Cache but expired)
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} addCriteriaExpired function for OXMapping implementation. 
 *      params of the function:
 *      collection {OXCollection} OXCollection for a request see OXCollection for further description
 */
OXAbstractMapping.addCriteriaExpired = function(oxMapping) {
    return function(collection, last_modified, uniqueName) {
        if(!oxMapping.requestCriteriaExpired) {
            oxMapping.requestCriteriaExpired=new Array();
		}
		if(OXCache.requested && OXCache.requested["CEXP"]) {
            for(var i=0;i<OXCache.requested["CEXP"].length;i++) {
                if(oxMapping.equalsCollection(OXCache.requested["CEXP"][i].collection, collection)) { 
				    return; 
				}       
            }
        }
        if(!OXCache.requested) { OXCache.requested={}}
        if(!OXCache.requested["CEXP"]) { OXCache.requested["CEXP"]=[]}
        OXCache.requested["CEXP"].push({ collection : collection, last_modified : last_modified });
        oxMapping.requestCriteriaExpired.push({ collection : collection, last_modified : last_modified, uniqueName : uniqueName });
	}
}
/**
 * @private 
 * This function is needed, if abstract function criteria Request of OXAbstractMapping is mapped to an OXMapping implementation
 * Return function adds OXObjects to the to server request list (object in Cache but columns missing)
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} addCriteriaExpired function for OXMapping implementation. 
 *      params of the function:
 *      columns {Array of Strings} Needed columsn for a request
 *      object {Key} key of an object
 */
OXAbstractMapping.addCriteriaMissingColumn = function(oxMapping) {
    return function(columns,object,uniqueName) {
        if(!oxMapping.requestCriteriaMissingColumn) {
            oxMapping.requestCriteriaMissingColumn=new Array();
		}
		if(OXCache.requested && OXCache.requested["CMIS"]) {
            for(var i=0;i<OXCache.requested["CMIS"].length;i++) {
                if(equals(OXCache.requested["CMIS"][i],{ columns : columns , object : object })) { return; }
            }
        }
		if(!OXCache.requested) { OXCache.requested={}}
        if(!OXCache.requested["CMIS"]) { OXCache.requested["CMIS"]=[]}
        OXCache.requested["CMIS"].push({ columns : columns , object : object });
        oxMapping.requestCriteriaMissingColumn.push({ columns : columns , object : object , uniqueName : uniqueName });
    }
}
/**
 * @private 
 * This function is needed, if abstract function list Request of OXAbstractMapping is mapped to an OXMapping implementation
 * Return function adds OXObjects to the to server request list (object not in Cache)
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} addListNotInCache function for OXMapping implementation. 
 *      params of the function:
 *      columns {Array of Strings} Needed columns for a request
 *      object {Key} key of an object
 */
OXAbstractMapping.addListNotInCache = function(oxMapping) {
    return function(columns,object,uniqueName) {
        if(!oxMapping.requestListNotInCache) {
            oxMapping.requestListNotInCache=new Array();
        }
		if(OXCache.requested && OXCache.requested["LNOT"]) {
            for(var i=0;i<OXCache.requested["LNOT"].length;i++) {
                if(equals(OXCache.requested["LNOT"][i],{ columns : columns , object : object })) { return; }
            }
        }
		if(!OXCache.requested) { OXCache.requested={}}
	    if(!OXCache.requested["LNOT"]) { OXCache.requested["LNOT"]=[]}
	    OXCache.requested["LNOT"].push({ columns : columns , object : object });
	    oxMapping.requestListNotInCache.push({ columns : columns , object : object, uniqueName : uniqueName });
    }
}
/**
 * @private 
 * This function is needed, if abstract function list Request of OXAbstractMapping is mapped to an OXMapping implementation
 * Return function adds OXObjects to the to server request list (object in Cache but expired)
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} addListExpired function for OXMapping implementation. 
 *      params of the function:
 *      columns {Array of Strings} Needed columns for a request
 *      object {Key} key of an object
 */
OXAbstractMapping.addListExpired = function(oxMapping) {
    return function(columns,object,uniqueName) {
        if(!oxMapping.requestListExpired) {
            oxMapping.requestListExpired=new Array();
        }
		if(OXCache.requested && OXCache.requested["LEXP"]) {
            for(var i=0;i<OXCache.requested["LEXP"].length;i++) {
                if(equals(OXCache.requested["LEXP"][i],{ columns : columns , object : object })) { return; }
            }
        }
	    if(!OXCache.requested) { OXCache.requested={}}
        if(!OXCache.requested["LEXP"]) { OXCache.requested["LEXP"]=[]}
        OXCache.requested["LEXP"].push({ columns : columns , object : object });
        oxMapping.requestListExpired.push({ columns : columns , object : object, uniqueName : uniqueName });
    }
}
/**
 * @private 
 * This function is needed, if abstract function list Request of OXAbstractMapping is mapped to an OXMapping implementation
 * Return function adds OXObjects to the to server request list (object in Cache but missing columns)
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} addListMissingColumn function for OXMapping implementation. 
 *      params of the function:
 *      columns {Array of Strings} Needed columns for a request
 *      object {Key} key of an object
 */
OXAbstractMapping.addListMissingColumn = function(oxMapping) {
    return function(columns,object,uniqueName) {
        if(!oxMapping.requestListMissingColumn) {
            oxMapping.requestListMissingColumn=new Array();
        }
		if(OXCache.requested && OXCache.requested["LMIS"]) {
            for(var i=0;i<OXCache.requested["LMIS"].length;i++) {
                if(equals(OXCache.requested["LMIS"][i],{ columns : columns , object : object })) { return; }
            }
        }
        if(!OXCache.requested) { OXCache.requested={}}
        if(!OXCache.requested["LMIS"]) { OXCache.requested["LMIS"]=[]}
        OXCache.requested["LMIS"].push({ columns : columns , object : object });
        oxMapping.requestListMissingColumn.push({ columns : columns , object : object, uniqueName : uniqueName });
            
    }
}
/**
 * @private 
 * This function is needed, if abstract function list or criteria Request of OXAbstractMapping is mapped to an OXMapping implementation
 * Return function executes server requests.
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} request function for OXMapping implementation. 
 */
OXAbstractMapping.request = function(oxMapping) {
    return function() {
        var multipleRequests=new Array();
        if(oxMapping.requestCriteriaNotInCache) {
            for(var i = 0;i<oxMapping.requestCriteriaNotInCache.length;i++) {
                var tmpObject= {
                    action : "all", module : oxMapping.module, 
                    columns : oxMapping.convertColumns(oxMapping.requestCriteriaNotInCache[i].collection.columns).join(",")
                };
                for(var counter in oxMapping.requestCriteriaNotInCache[i].collection.criteria) {
                    switch(counter) {
                        case "start" :
                        case "end" :
                           tmpObject[counter]= oxMapping.requestCriteriaNotInCache[i].collection.criteria[counter];
                           break;
                        case "folder_id":
                           tmpObject["folder"]= oxMapping.requestCriteriaNotInCache[i].collection.criteria[counter];
                           break;
                    }
                }
				if(oxMapping.requestCriteriaNotInCache[i].collection.search) {
					var searchObject=oxMapping.requestCriteriaNotInCache[i].collection.search;
					var operator="or";
					switch(oxMapping.module) {
                        case "tasks" :
                           for(var key in searchObject) {
                                var value=searchObject[key];
                                if(key.toLowerCase() == "operator" && value.toLowerCase() == "or") {
                                    operator="or";
                                } else if(key == "title" || key =="categories" || key =="note") {
                                    tmpObject.action="search";
                                    tmpObject.data = { pattern : value };    
                                    break;
                                } else {
                                    alert("This search" + searchObject +"is not supported for tasks");
                                }
                            }
                            break;
                        case "contacts" :
                            if(searchObject["startletter"]) {
                                if(searchObject["last_name"]) {
                                    tmpObject.action="search";
                                    tmpObject.data = { pattern : searchObject["last_name"] , startletter : true };
                                    if(tmpObject.folder) { tmpObject.data["folder"] = tmpObject.folder }
                                } else {
                                    alert("This search" + searchObject +"is not supported for contacts")
                                } 
                            } else {
                               for(var key in searchObject) {
                                   var value=searchObject[key];
                                    if(key.toLowerCase() == "operator" && value.toLowerCase() == "or") {
                                        operator="or";
                                    } else if(key == "email1" || key =="email2" || key =="email3" || key =="display_name" || key =="first_name" || key =="last_name") {
                                        tmpObject.action="search";
                                        tmpObject.data = { pattern : value };
                                        if(tmpObject.folder) { tmpObject.data["folder"] = tmpObject.folder }
                                        break;    
                                    } else {
                                        alert("This search" + searchObject +"is not supported for contacts");
                                    }
                                }   
                            }
						    break;
						case "mail":
                           for(var key in searchObject) {
                                var value=searchObject[key];
                                if(key.toLowerCase() == "operator" && value.toLowerCase() == "or") {
                                    operator="or";
                                } else {
                                    tmpObject.action="search";
                                    if(!tmpObject.data) { tmpObject.data=[]; }
									var tmpCol=oxMapping.convertColumns([trimStr(key)])[0];
                                    if(tmpCol) { tmpObject.data.push({col:  parseInt(tmpCol), pattern : searchObject[key] }); }
                                }    
                            }
                            break;
                        case "infostore":
                            for(var key in searchObject) {
                                var value=searchObject[key];
                                if(key.toLowerCase() == "operator" && value.toLowerCase() == "or") {
                                    operator="or";
                                } else if(key == "title" || key =="url" || key =="filename" || key =="version_comment" || key=="categories" || key=="description") {
                                    tmpObject.action="search";
                                    tmpObject.data = { pattern : value };    
                                    break;
                                } else {
                                    alert("This search" + searchObject +"is not supported for tasks");
                                }
							}
                            break;
                    }
				}
				if(oxMapping.requestCriteriaNotInCache[i].collection.order){
                    var tmpOrder=oxMapping.requestCriteriaNotInCache[i].collection.order[0];
                    if(oxMapping.convertColumns([trimStr(tmpOrder.sort)])[0]) {
                       tmpObject["sort"]=oxMapping.convertColumns([trimStr(tmpOrder.sort)])[0];
                       tmpObject["order"]= (tmpOrder.order.toLowerCase()=="desc") ? "desc" : "asc";
                    }                   
                }
                multipleRequests.push(tmpObject);
            }
        }
        if(oxMapping.requestCriteriaExpired) {
            for(var i = 0;i<oxMapping.requestCriteriaExpired.length;i++) {
                var tmpObject= {
                    action : "all", module : oxMapping.module, 
                    columns : oxMapping.convertColumns(oxMapping.requestCriteriaExpired[i].collection.columns).join(",")
                };
                var tmpObject2= {
                    action : "updates", module : oxMapping.module, timestamp : (oxMapping.requestCriteriaExpired[i].last_modified+1) || 0,
                    columns :oxMapping.convertColumns(oxMapping.requestCriteriaExpired[i].collection.columns).join(","), ignore : "deleted"
                };
                for(var counter in oxMapping.requestCriteriaExpired[i].collection.criteria) {
                    switch(counter) {
                        case "start" :
                        case "end" :
                           tmpObject[counter]= oxMapping.requestCriteriaExpired[i].collection.criteria[counter];
                           tmpObject2[counter]= oxMapping.requestCriteriaExpired[i].collection.criteria[counter];
                           break;
                        case "folder_id":
                           tmpObject["folder"]= oxMapping.requestCriteriaExpired[i].collection.criteria[counter];
                           tmpObject2["folder"]= oxMapping.requestCriteriaExpired[i].collection.criteria[counter];
                           break;
                    }
                }
				if(oxMapping.requestCriteriaExpired[i].collection.search) {
                    var searchObject=oxMapping.requestCriteriaExpired[i].collection.search;
					var operator="or";
                    switch(oxMapping.module) {
                        case "tasks" :
                           for(var key in oxMapping.requestCriteriaExpired[i].collection.search) {
                                var value=oxMapping.requestCriteriaExpired[i].collection.search[key];
                                if(key.toLowerCase() == "operator" && value.toLowerCase() == "or") {
                                    operator="or";
                                } else if(key == "title" || key =="categories" || key =="note") {
                                    tmpObject.action="search";
                                    tmpObject.data = { pattern : value };  
									if(tmpObject.folder) { tmpObject.data["folder"] = tmpObject.folder }  
                                } else {
                                    alert("This search" + oxMapping.requestCriteriaExpired[i].collection.search +"is not supported for tasks");
                                }
                            }
                            break;
						case "contacts" :
                           if(searchObject["startletter"]) {
                                if(searchObject["last_name"]) {
                                    tmpObject.action="search";
                                    tmpObject.data = { pattern : searchObject["last_name"] , startletter : true };
                                    if(tmpObject.folder) { tmpObject.data["folder"] = tmpObject.folder }
                                } else {
                                    alert("This search" + searchObject +"is not supported for contacts")
                                } 
                            } else {
                               for(var key in searchObject) {
                                   var value=searchObject[key];
                                    if(key.toLowerCase() == "operator" && value.toLowerCase() == "or") {
                                        operator="or";
                                    } else if(key == "email1" || key =="email2" || key =="email3" || key =="display_name" || key =="first_name" || key =="last_name") {
                                        tmpObject.action="search";
                                        tmpObject.data = { pattern : value };
                                        if(tmpObject.folder) { tmpObject.data["folder"] = tmpObject.folder }
                                        break;    
                                    } else {
                                        alert("This search" + searchObject +"is not supported for contacts");
                                    }
                                }   
                            }
                            break;
						case "mail":
                           for(var key in searchObject) {
                                var value=searchObject[key];
                                if(key.toLowerCase() == "operator" && value.toLowerCase() == "or") {
                                    operator="or";
                                } else {
                                    tmpObject.action="search";
                                    if(!tmpObject.data) { tmpObject.data=[]; }
                                    var tmpCol=oxMapping.convertColumns([trimStr(key)])[0];
                                    if(tmpCol) { tmpObject.data.push({col:  parseInt(tmpCol), pattern : searchObject[key] }); }
                                }    
                            }
                            break;
						case "infostore":
                            for(var key in searchObject) {
                                var value=searchObject[key];
                                if(key.toLowerCase() == "operator" && value.toLowerCase() == "or") {
                                    operator="or";
                                } else if(key == "title" || key =="url" || key =="filename" || key =="version_comment" || key=="categories" || key=="description") {
                                    tmpObject.action="search";
                                    tmpObject.data = { pattern : value };    
                                    break;
                                } else {
                                    alert("This search" + searchObject +"is not supported for tasks");
                                }
							}
                            break;
                    }
                }
				if(oxMapping.requestCriteriaExpired[i].collection.order){
                    var tmpOrder=oxMapping.requestCriteriaExpired[i].collection.order[0];
                    if(oxMapping.convertColumns([trimStr(tmpOrder.sort)])[0]) {
                       tmpObject["sort"]=oxMapping.convertColumns([trimStr(tmpOrder.sort)])[0];
                       tmpObject["order"]= (tmpOrder.order.toLowerCase()=="desc") ? "desc" : "asc";
                    }                   
                }
                
                multipleRequests.push(tmpObject);
				if(!tmpObject.search) {
                    multipleRequests.push(tmpObject2);
				}
            }
        }
        if(oxMapping.requestCriteriaMissingColumn && oxMapping.requestCriteriaMissingColumn.length) {
            var tmpData= [];
            var tmpColumns;
            for(var i = 0;i<oxMapping.requestCriteriaMissingColumn.length;i++) {
                if(!tmpColumns) {
                  tmpColumns = oxMapping.convertColumns(oxMapping.requestCriteriaMissingColumn[i].columns).join(",");           
                }
                var tmpString=oxMapping.requestCriteriaMissingColumn[i].object;
                tmpData.push({ folder : tmpString.folder , id : tmpString.id});
            }
            var tmpObject= { action : "list", module : oxMapping.module, data : tmpData , columns : tmpColumns 
            };
            multipleRequests.push(tmpObject);
        }
        if(oxMapping.requestListNotInCache && oxMapping.requestListNotInCache.length) {
            var tmpData= [];
            var tmpColumns;
			var tmpAction="list";
			var view="text";
            for(var i = 0;i<oxMapping.requestListNotInCache.length;i++) {
                if(!tmpColumns) {
                  tmpColumns = oxMapping.convertColumns(oxMapping.requestListNotInCache[i].columns);
				  for(var i2=0;i2<tmpColumns.length;i2++) {
                      if(!tmpColumns[i2]) { 
                            tmpAction="get";
                            var stringcolumn=oxMapping.requestListNotInCache[i].columns[i2];
                            if (stringcolumn == "attachments_plain") { view="text"; break; }
                            else if (stringcolumn == "attachments_html") { view="html"; break; }
                            else if (stringcolumn == "attachments_html_noimage") { view="noimg"; break; }
                      }
                  }
                  if(tmpAction=="list") { tmpColumns=tmpColumns.join(","); }           
                }
                var tmpString=oxMapping.requestListNotInCache[i].object;
				if(tmpAction=="get") {
                    var tmpObject= { action : tmpAction, module : oxMapping.module, id : tmpString.id, folder : tmpString.folder_id, view : view};
                    multipleRequests.push(tmpObject); 
                } else {
                    tmpData.push({ folder : tmpString.folder_id , id : tmpString.id});
                }
            }
			if(tmpAction=="list") {
                var tmpObject= { action : tmpAction, module : oxMapping.module, data : tmpData , columns : tmpColumns};
				 multipleRequests.push(tmpObject);
			}
        }
        if(oxMapping.requestListExpired && oxMapping.requestListExpired.length) {
            var tmpData= [];
            var tmpColumns;
			var tmpAction="list";
            for(var i = 0;i<oxMapping.requestListExpired.length;i++) {
                if(!tmpColumns) {
                  tmpColumns = oxMapping.convertColumns(oxMapping.requestListExpired[i].columns);
				  for(var i2=0;i2<tmpColumns.length;i2++) {
                      if(!tmpColumns[i2]) { 
                            tmpAction="get";
                            var stringcolumn=oxMapping.requestListExpired[i].columns[i2];
                            if (stringcolumn == "attachments_plain") { view="text"; break; }
                            else if (stringcolumn == "attachments_html") { view="html"; break; }
                            else if (stringcolumn == "attachments_html_noimage") { view="noimg"; break; }
                      }
                  }
                  if(tmpAction=="list") { tmpColumns=tmpColumns.join(","); }             
                }
                var tmpString=oxMapping.requestListExpired[i].object;
                if(tmpAction=="get") {
                    var tmpObject= { action : tmpAction, module : oxMapping.module, id : tmpString.id, folder : tmpString.folder_id , view : view };
                    multipleRequests.push(tmpObject); 
                } else {
                    tmpData.push({ folder : tmpString.folder_id , id : tmpString.id});
                }
            }
            if(tmpAction=="list") {
                var tmpObject= { action : tmpAction, module : oxMapping.module, data : tmpData , columns : tmpColumns};
				multipleRequests.push(tmpObject);
            }
            
        }
        if(oxMapping.requestListMissingColumn && oxMapping.requestListMissingColumn.length) {
            var tmpData= [];
            var tmpColumns;
			var tmpAction="list";
			var view="text";
            for(var i = 0;i<oxMapping.requestListMissingColumn.length;i++) {
                if(!tmpColumns) {
                    tmpColumns = oxMapping.convertColumns(oxMapping.requestListMissingColumn[i].columns);
                    for(var i2=0;i2<tmpColumns.length;i2++) {
                        if(!tmpColumns[i2]) { 
                            tmpAction="get";
                            var stringcolumn=oxMapping.requestListMissingColumn[i].columns[i2];
                            if (stringcolumn == "attachments_plain") { view="text"; break; }
                            else if (stringcolumn == "attachments_html") { view="html"; break; }
                            else if (stringcolumn == "attachments_html_noimage") { view="noimg"; break; }
                        }  
                    }
                    if(tmpAction=="list") { tmpColumns=tmpColumns.join(","); }            
                }
			    var tmpString=oxMapping.requestListMissingColumn[i].object;
                if(tmpAction=="get") {
                    var tmpObject= { action : tmpAction, module : oxMapping.module, id : tmpString.id, folder : tmpString.folder_id , view: view};
                    multipleRequests.push(tmpObject); 
                } else {
                    tmpData.push({ folder : tmpString.folder_id , id : tmpString.id});
                }
            }
            if(tmpAction=="list") {
                var tmpObject= { action : tmpAction, module : oxMapping.module, data : tmpData , columns : tmpColumns};
				multipleRequests.push(tmpObject);
            }
        }
	    var tmpFn=OXCache.join.add();
		if(multipleRequests.length) {
			if(debug) { console.log("Requests",multipleRequests); }
			(function (
                requestCriteriaNotInCache,
                requestCriteriaExpired,
                requestCriteriaMissingColumn,
                requestListNotInCache,
                requestListExpired,
                requestListMissingColumn) 
            {
           
                tmptime=new Date().getTime();
        
                OXCache.getJSON(AjaxRoot + "/multiple?session="+session +"&continue=true", multipleRequests, function(response) {
                   oxMapping.handleResponse(response,requestCriteriaNotInCache,requestCriteriaExpired,requestCriteriaMissingColumn,requestListNotInCache,requestListExpired,requestListMissingColumn);
                });
            })(clone(oxMapping.requestCriteriaNotInCache),clone(oxMapping.requestCriteriaExpired),
               clone(oxMapping.requestCriteriaMissingColumn),clone(oxMapping.requestListNotInCache),
               clone(oxMapping.requestListExpired),clone(oxMapping.requestListMissingColumn));
       }
	   tmpFn();
    }
}
/**
 * @private 
 * This function is needed, if abstract function request of OXAbstractMapping is mapped to an OXMapping implementation
 * Return function handles response of server.
 * @param oxMapping {OXMapping} OXMapping implementation
 * @return {function} handleResponse function for OXMapping implementation. 
 *      params of the function:
 *      uniqueName {Object} Response object of the server
 *      requestCriteriaNotInCache {Array} Array of Criteria Requests not in Cache.
 *      requestCriteriaExpired {Array} Array of Criteria Requests which are expired.
 *      requestCriteriaMissingColumn {Array} Array of Objects in Criteria Requests with missing columns.
 *      requestListNotInCache {Array} Array of Objects in List Requests which are not in Cache.
 *      requestListExpired {Array} Array of Objects in List Requests which are in Cache but expired.
 *      requestListMissingColumn {Array} Array of Objects in List Requests which are in Cache but have missing columns.
 */
 OXAbstractMapping.handleResponse = function(oxMapping) {
	return function(response,requestCriteriaNotInCache,requestCriteriaExpired,requestCriteriaMissingColumn,requestListNotInCache,requestListExpired,requestListMissingColumn) {
		tmptime=new Date().getTime();
        var count=0;
		for(var i=0;requestCriteriaNotInCache && i<requestCriteriaNotInCache.length;i++) {
            oxMapping.handleCriteriaNotInCache(requestCriteriaNotInCache[i].collection,response[count],requestCriteriaNotInCache[i].uniqueName);
            count++;
        }
        for(var i=0;requestCriteriaExpired && i<requestCriteriaExpired.length;i++) {
			if(requestCriteriaExpired[i].search) {
				oxMapping.handleCriteriaExpired(requestCriteriaExpired[i].collection,response[count],null,requestCriteriaExpired[i].uniqueName);
				count++;
			} else {
                oxMapping.handleCriteriaExpired(requestCriteriaExpired[i].collection,response[count],response[count+1],requestCriteriaExpired[i].uniqueName);
                count++;		
                count++;
			}
        }
        if(requestCriteriaMissingColumn && requestCriteriaMissingColumn.length) {
            oxMapping.handleCriteriaMissingColumn(requestCriteriaMissingColumn,response[count],requestCriteriaMissingColumn.uniqueName);
            count++;
        }      
		if(requestListNotInCache && requestListNotInCache.length) {
            if(response[count].data && response[count].data.constructor == Array) {
                oxMapping.handleListNotInCache(requestListNotInCache,response[count],requestListNotInCache.uniqueName);
                count++;    
            } else {
                 for(var i=0;i<requestListNotInCache.length;i++) {
                    oxMapping.handleListNotInCache(requestListNotInCache[i],response[count],requestListNotInCache.uniqueName);
                    count++;
                 }
            }
        }
		if(requestListExpired && requestListExpired.length) {
            if(response[count].data && response[count].data.constructor == Array) {
                oxMapping.handleListExpired(requestListExpired,response[count],requestListExpired.uniqueName);
                count++;    
            } else {
                 for(var i=0;i<requestListExpired.length;i++) {
                    oxMapping.handleListExpired(requestListExpired[i],response[count],requestListExpired.uniqueName);
                    count++;
                 }
            }
        }
		if(requestListMissingColumn && requestListMissingColumn.length) {
            if(response[count].data && response[count].data.constructor == Array) {
                oxMapping.handleListMissingColumn(requestListMissingColumn,response[count],requestListMissingColumn.uniqueName);
                count++;    
            } else {
                 for(var i=0;i<requestListMissingColumn.length;i++) {
                    oxMapping.handleListMissingColumn(requestListMissingColumn[i],response[count],requestListMissingColumn.uniqueName);
                    count++;
                 }
            }
        }
        
		if (response[count] && response[count].error) {
			newServerError(response[count].error);
		}
        
	} 
}
OXAbstractMapping.handleCriteriaNotInCache = function(oxMapping) {
    return function(request,response,uniqueName) {
		if(debug) { console.log("Criteria not in cache:",request,response,uniqueName);	}
		oxMapping.handleCriteria(request,response,null,uniqueName);
    }
}
OXAbstractMapping.handleCriteriaExpired = function(oxMapping) {
    return function(request,response,response2,uniqueName) {
        if(debug) { console.log("Criteria expired:",request,response,response2,uniqueName); }
		oxMapping.handleCriteria(request,response,response2,uniqueName);
    }
}
OXAbstractMapping.handleCriteria= function (oxMapping) {
    return function(request,response,response2,uniqueName) {
        if(response && response.error || response2 && response2.error) {
            //OXCache.errorHandler(uniqueName,request,response,response2);
            oxMapping.errorHandler(uniqueName,request,response,response2);
            return;
        } 
        var tmpCollection=oxMapping.getCollection(request,false);
        if(!tmpCollection) {
            var tmpCollection=new OXCollection();
            tmpCollection.criteria=request.criteria;
			tmpCollection.search=request.search
            tmpCollection.order=request.order;
            tmpCollection.last_modified=response.timestamp;
			tmpCollection.timestamp=response.timestamp;
            tmpCollection.lastRequest=new Date().getTime();
            tmpCollection.objects=new Array(response.data.length);
            tmpCollection.map_objects=new LRUKeyList();
			tmpCollection.modified=true;
            for(var i=0;i<response.data.length;i++) {
                var tmpObject=oxMapping.createKeyFromData(response.data[i],response["timestamp"]);
                tmpCollection.objects[i]=tmpObject;
                tmpCollection.map_objects.set(tmpObject,i+1);
                oxMapping.createCacheObject(request.columns,response.data[i],response["timestamp"],tmpObject);
            }
            oxMapping.checkAddCriteria(tmpCollection.objects,request);
            var tmp=OXCache.cachedCollections.set(OXCache.getSequence(),tmpCollection);
            if(tmp) {
                OXCache.cachedCollectionsTrash.push({ data: tmp });
            }
            tmpCollection.check=false;
        } else {
            //TIMESTAMP CAN NOT CHECK LIST EQUALS CACHED LIST
            var tmpList=new LRUKeyList(response.data.length);
            var tmpArray=new Array(response.data.length);
            var changed=false;
            for(var i=0;i<response.data.length;i++) {
                var tmpObject =oxMapping.createKeyFromData(response.data[i],response.timestamp);
                tmpList.set(tmpObject,i+1);
                tmpArray[i]=tmpObject; 
            }
            if(response2 && response2.data.length) {
                for(var i=0;i<response2.data.length;i++) {
                    var tmpObject =oxMapping.createKeyFromData(response2.data[i],response2.timestamp);
                    oxMapping.createCacheObject(request.columns,response2.data[i],response2["timestamp"],tmpObject,true,tmpCollection);
                    changed=true;
                }  
            }
            for(var i=0;i<tmpCollection.objects.length;i++) {
                //DELETE
				if(!tmpList.get(tmpCollection.objects[i])) {
					changed=true;
                    oxMapping.deleteObject(tmpCollection.objects[i],tmpCollection);
                }
			}
		    if(oxMapping.module=="mail" || request.search) {
			     // NEW
				 for(var i=0;i<tmpArray.length;i++) {
				    if(!tmpCollection.map_objects.get(tmpArray[i])) {
					   oxMapping.createCacheObject(request.columns,response.data[i],response["timestamp"],tmpArray[i],true,tmpCollection);
                       changed=true;
					} 
				}
				//UPDATE
				//NOT POSSIBLE
			}
            tmpCollection.objects=tmpArray;
            tmpCollection.order=request.order;
            tmpCollection.map_objects=tmpList;
			if(changed) {
                tmpCollection.last_modified=response.timestamp;
				tmpCollection.timestamp=response.timestamp;
                tmpCollection.modified=true;
            }
			tmpCollection.check=false;
            tmpCollection.lastRequest=new Date().getTime();
        }
    }
}
OXAbstractMapping.checkAddCriteria=function(oxMapping) {
    return function(objects,request) {
        var collections=oxMapping.getSubCollections(request);
        for(var i=0;i<collections.length;i++) {
			if(!oxMapping.equalsCollection(collections[i],request)) {
                if(!collections[i].check) {
                    for(var i2=0;i2<objects.length;i2++) {
                        if(collections[i].map_objects.get(objects[i2])) {
						    collections[i].check=true;
                            break;
                        }
                    }
                }
            }
		}
    }
}
OXAbstractMapping.checkAddEditDeleteObject=function(oxMapping) {
    return function(object,executeCollection) {
        var collections=oxMapping.getSubCollectionsfromObject(object,true);
		for(var i=0;i<collections.length;i++) {
		   if(!executeCollection || !oxMapping.equalsCollection(collections[i],executeCollection)) {
		      if(!collections[i].check) {
                  collections[i].check=true;
              }
		   }
		}
		for(var i in OXCache.modifiedCallbacks) {
            if(OXCache.modifiedCallbacks[i].module == oxMapping.module && !(OXCache.modifiedCallbacks[i].collection.criteria)) {
                for(var count=0;count<OXCache.modifiedCallbacks[i].collection.objects.length;count++) {
                    if(object.equals(OXCache.modifiedCallbacks[i].collection.objects[count])) {
                        OXCache.modifiedCallbacks[i].collection.objects.splice(count,1);
                        count=count-1;
                        OXCache.modifiedCallbacks[i].modified=true;                 
                    }
                }
            }
        }
    }
}

OXAbstractMapping.handleCriteriaMissingColumn = function(oxMapping) {
    return function(request,response,uniqueName) {
		if(debug) { console.log("Criteria missing column:",request,response,uniqueName); }
		oxMapping.handleList(request,response,uniqueName);
    }
}
OXAbstractMapping.handleListNotInCache = function(oxMapping) {
    return function(request,response,uniqueName) {
	    if(debug) { console.log("Objects not in Cache:",request,response,uniqueName); }
		oxMapping.handleList(request,response,uniqueName);
    }
}
OXAbstractMapping.handleListExpired = function(oxMapping) {
    return function(request,response,uniqueName) {
        if(debug) { console.log("Objects expired:",request,response,uniqueName); }
		oxMapping.handleList(request,response,uniqueName);
    }
}
OXAbstractMapping.handleListMissingColumn = function(oxMapping) {
    return function(request,response,uniqueName) {
      if(debug) { console.log("Objects columns missing:",request,response,uniqueName); }
	  oxMapping.handleList(request,response,uniqueName);
	}
}
OXAbstractMapping.handleList = function(oxMapping) {
	return function(request,response,uniqueName) {
	   if(response && response.error) {
	   	    //OXCache.errorHandler(uniqueName,request,response);
	   	    oxMapping.errorHandler(uniqueName,request,response);
            return;
	   }
	   if(response.data && response.data.constructor == Array) {
		   if(request.length==response.data.length) {
	           for(var i=0;i<request.length;i++) {
	               oxMapping.createCacheObject(request[i].columns,response.data[i],response["timestamp"]);
	           }
	        }  else {
	            for(var i=0;i<request.length;i++) {
	                var deleted=true;
	                for(var i2=0;i2<response.data.length;i2++) {
	                    var key=oxMapping.createKeyFromData(response.data[i2]);
	                    if(key.folder_id == request[i].object.folder_id && key.id == request[i].object.id && request[i].object.recurrence_position == key.recurrence_position) {
	                        deleted=false;
	                        oxMapping.createCacheObject(request[i].columns,response.data[i2],response["timestamp"]);
	                        break;
	                    }   
	                }
	                if(deleted) {
	                    oxMapping.deleteObject(request[i].object);
	                } 
	            }  
	        }
		} else if (response.data) {
			for(var i in request.object) {
				if(response.data[i] === undefined) { response.data[i]=request.object[i]; }
			}
			oxMapping.createCacheObject(request.columns,response.data,response["timestamp"]);
		} else {
			// fatal error. something's completely broken on the server.
			triggerEvent("OX_New_Error", 4, _("Internal Fatal Error: Server response contains no or invalid data. Some functions may be disabled or not useable. (OC02=" + request + ")"));
		}
	}
}
OXAbstractMapping.deleteObject = function(oxMapping) {
	return function(object,collection) {
		var smallKeyObject=OXCache.objectShortKey.get(new SmallKey(oxMapping.module,object.id))
        if(smallKeyObject) {
            for(var zaehler=0;zaehler<smallKeyObject.length;zaehler++) {
	            var tmp1=OXCache.cachedObjects.get(smallKeyObject[zaehler]);
	            if(tmp1) {
	                oxMapping.checkAddEditDeleteObject(tmp1,collection);                                
	            } else {
					oxMapping.checkAddEditDeleteObject(smallKeyObject[zaehler],collection);
				}
	            OXCache.cachedObjects.remove(smallKeyObject[zaehler]);
	        }
		}
        OXCache.objectShortKey.remove(new SmallKey(oxMapping.module,object.id));
	}
}
OXAbstractMapping.createCacheObject = function(oxMapping) {
    return function(columns,data,timestamp,key,events,executeCollection) {
		var itsupdate=false;
		var seen=false; //MAIL ONLY
		if(!key) {
		  if(data.constructor == Array) {
		      key=oxMapping.createKeyFromData(data);
		  } else {
		  	  key=oxMapping.createKeyFromObject(data);
		  }
		}
		var tmpObject=key;
		var oldObject=OXCache.cachedObjects.get(key);
		if(!oldObject) {
		  //NEWOBJECT
          if(data.constructor == Array) { 
              for(var i=0;i<columns.length;i++) {
                  tmpObject[columns[i]]=data[i]; 
              }  
          } else {
              for(var i=0;i<columns.length;i++) {
                  if(data[columns[i]]|| data[columns[i]]==0 || data[columns[i]]=="") { tmpObject[columns[i]]=data[columns[i]] }
                  else if(data[columns[i]] === undefined) {
				  	if ((oxMapping.module == "mail") && (columns[i] == "attachments_plain" || columns[i] == "attachments_html" || columns[i] == "attachments_html_noimage")) {
                        tmpObject[columns[i]] = data["attachments"] || null;
						//IMAGES ARE BLOCKED
						if(data["modified"] && data["modified"] == 1) {
                            tmpObject["blocked_images"]=true;
                        } else {
                            tmpObject["blocked_images"]=false;
                        }
                    } else if(columns[i]=="blocked_images") {
                    } else {
                        tmpObject[columns[i]] = null;
                    } 
				  } 
              }
			  if(oxMapping.module == "mail" && (data["unread"] || data["unread"]== 0)) {
                    oUnreadCounterStorage.set(tmpObject["folder_id"],data["unread"]);
                }
          }
          tmpObject["timestamp"]=timestamp;
          tmpObject.lastRequest=new Date().getTime();
          var tmp=OXCache.cachedObjects.set(tmpObject,tmpObject);
		  if(tmp) { OXCache.cachedObjectsTrash.set(tmp.key,tmp.data); }
		  /*var myShortKey=new SmallKey(oxMapping.module,tmpObject.id);
		  if(!OXCache.objectShortKey.get(myShortKey)) {
		      OXCache.objectShortKey.set(myShortKey,new Array());
		  }
		  var tmpShortObject=OXCache.objectShortKey.get(myShortKey);
		  tmpShortObject.push(tmpObject);
		  OXCache.objectShortKey.set(myShortKey,tmpShortObject);
		  */
		  if(events) {
             oxMapping.checkAddEditDeleteObject(tmpObject,executeCollection); 
          }
          if(oxMapping.module == "mail" && data["unread"]) {
            oUnreadCounterStorage.set(tmpObject["folder_id"],data["unread"]);
          } 
	  
        } else {
            //EDIT OBJECT
            if(data.constructor == Array) { 
                for(var i=0;i<columns.length;i++) {
                    tmpObject[columns[i]]=data[i]; 
                }  
            } else {
                for(var i=0;i<columns.length;i++) {
				   if(data[columns[i]]|| data[columns[i]]==0 || data[columns[i]]=="") { tmpObject[columns[i]]=data[columns[i]] }
                   else if(data[columns[i]] === undefined) { 
				       if ((oxMapping.module == "mail") && (columns[i] == "attachments_plain" || columns[i] == "attachments_html" || columns[i] == "attachments_html_noimage")) {
                           tmpObject[columns[i]] = data["attachments"] || null;
						   if(data["modified"] && data["modified"] == 1) {
                               tmpObject["blocked_images"]=true;
                           } else {
                            tmpObject["blocked_images"]=false;
						   }
                       } else if(columns[i]=="blocked_images") {
					   } else {
                           tmpObject[columns[i]] = null;
                       } 
				   } 
                }
				//UNREAD COUNTER
                if(oxMapping.module == "mail" && (data["unread"] || data["unread"]== 0)) {
                    oUnreadCounterStorage.set(tmpObject["folder_id"],data["unread"]);
                } 
            }
			if(oldObject.last_modified==tmpObject.last_modified) {
                for(var i=0;i<columns.length;i++) {
					var value = oldObject[columns[i]];
					var newvalue = tmpObject[columns[i]];
					//FIX FOR MAIL , Arrays should'nt be tested
					if(value !== undefined && newvalue !==undefined && value != newvalue && !itsupdate && typeof(value) != "object") {
						tmpObject.modified=true;
                        OXCache.modifiedObjects.push(tmpObject);
                        itsupdate=true;
					}
					if(tmpObject[columns[i]] !== undefined) { oldObject[columns[i]]=tmpObject[columns[i]]; }
				}
			    if(!tmpObject.modified) {
				    oldObject.lastRequest=new Date().getTime();
				    if(oldObject.timestamp<timestamp) { oldObject["timestamp"]=timestamp; }
				    OXCache.cachedObjects.set(oldObject,oldObject);
				} else {
					//WORKAROUND FOR LEVEL PARAMETER IN MAIL
					if(oxMapping.module=="mail" && !tmpObject["level"] && (oldObject["level"] || oldObject["level"]==0)) {
						tmpObject["level"]=oldObject["level"];
					}
					tmpObject.lastRequest=new Date().getTime();
                    if(tmpObject.timestamp<timestamp) { tmpObject["timestamp"]=timestamp; }
                    OXCache.cachedObjects.set(tmpObject,tmpObject);
					//TODO ONLY CHECK CHANGES
					oxMapping.editObjectsInternal([tmpObject],tmpObject);
				}
            } else {
                //CHANGED OBJECT
				tmpObject.lastRequest=new Date().getTime();
				tmpObject.timestamp=timestamp;
                tmpObject.modified=true;
				OXCache.modifiedObjects.push(tmpObject);
                var tmp=OXCache.cachedObjects.set(tmpObject,tmpObject);
				if(tmp) {
                    OXCache.cachedObjectsTrash.set(tmp.key,tmp.data);
                }
                if(events) {
                    oxMapping.checkAddEditDeleteObject(tmpObject,executeCollection); 
                }
            }
        } 
        //TODO EXTRAS
    }  
}
OXAbstractMapping.getCollection = function(oxMapping) {
    return function(collection,first) {
        function search(myArray) {
            for(var colCount in myArray) {
                var tmpCollection=myArray[colCount].data;
                var same=true;
                //Check Criteria
                if(collection.criteria && tmpCollection.criteria) {
                    for(var criteriaCount in tmpCollection.criteria) {
                        if(collection.criteria[criteriaCount] != tmpCollection.criteria[criteriaCount]) {  
                            same=false; break;     
                        }
                    }
                    for(var criteriaCount in collection.criteria) {
                        if(tmpCollection.criteria[criteriaCount] != collection.criteria[criteriaCount]) {  
                            same=false; break;     
                        }
                    }
                } else if (!(!collection.criteria && !tmpCollection.criteria)) {
                    continue;
                }
                //Check Search
                if(collection.search && tmpCollection.search) {
                    for(var searchCount in tmpCollection.search) {
                        if(collection.search[searchCount] != tmpCollection.search[searchCount]) {  
                            same=false; break;     
                        }
                    }
                    for(var searchCount in collection.search) {
                        if(tmpCollection.search[searchCount] != collection.search[searchCount]) {  
                            same=false; break;     
                        }
                    }
                } else if (!(!collection.search && !tmpCollection.search)) {
                    continue;
                }
                
				if(!same) { continue; }
                
                //Check Order
                if(collection.order && tmpCollection.order) {
                    if(collection.order.length!=tmpCollection.order.length) {
                        continue;
                    }
                    var direction=0;
                    for(var i=0;i<collection.order.length;i++) {
                        var tmpObject=collection.order[i];
                        var tmpObject2=tmpCollection.order[i];
                        if(tmpObject.sort!=tmpObject2.sort) {
                            same=false;
                            break;
                        }
                        if(tmpObject.order!=tmpObject2.order) {
                            if(!direction) { direction=-1; }
                            if(direction != -1) { same=false; break; } 
                        } else {
                            if(!direction) { direction=1; }
                            if(direction != 1) { same=false; break; }
                        }
                        if(same!=true) { break; }
                    }
                    if(!same) { continue; }
                } else if (!(!collection.order && !tmpCollection.order)){
                    continue;
                }
                if(first) {
                    var tmp1=OXCache.cachedCollections.get(colCount);
                }
                return tmpCollection;
            }
        }
        return search(OXCache.cachedCollections.cache) || search(OXCache.cachedCollectionsTrash) 
    }
}
OXAbstractMapping.equalsCollection = function (oxMapping) {
	return function(collection1,collection2) {
	    if(collection1.criteria && collection2.criteria) {
            for(var criteriaCount in collection2) {
                if(collection1.criteria[criteriaCount] != collection2.criteria[criteriaCount]) {  return false;  }
            }
            for(var criteriaCount in collection1.criteria) {
                if(collection2.criteria[criteriaCount] != collection1.criteria[criteriaCount]) {  return false;  }
            }
        } else if (!(!collection1.criteria && !collection2.criteria)) { return false; }
        if(collection1.search && collection2.search) {
            for(var criteriaCount in collection2) {
                if(collection1.search[criteriaCount] != collection2.search[criteriaCount]) {  return false;  }
            }
            for(var criteriaCount in collection1.search) {
                if(collection2.search[criteriaCount] != collection1.search[criteriaCount]) {  return false;  }
            }
        } else if (!(!collection1.search && !collection2.search)) { return false; }
        
		if(collection1.order && collection2.order) {
            if(collection1.order.length!=collection2.order.length) { return false; }
            var direction=0;
            for(var i=0;i<collection1.order.length;i++) {
                var tmpObject=collection1.order[i];
                var tmpObject2=collection2.order[i];
                if(tmpObject.sort!=tmpObject2.sort) { return false; }
                if(tmpObject.order!=tmpObject2.order) {
                    if(!direction) { direction=-1; }
                    if(direction != -1) { return false; } 
                } else {
                    if(!direction) { direction=1; }
                    if(direction != 1) { return false; }
                }
            }
        }
		else if (!(!collection1.order && !collection2.order)){
            return false;
        }
        return true;	
	}
}
OXAbstractMapping.getSubCollections = function(oxMapping) {
    return function(collection,ignoreChecked) {
        function search(myArray) {
            var tmpArray=new Array();
            for(var colCount in myArray) {
                var tmpCollection=myArray[colCount].data;
                if(ignoreChecked && tmpCollection.check) { continue; }
                var same=true;
                if(collection.criteria && tmpCollection.criteria) {
                    //Check Criteria
                    for(var criteriaCount in tmpCollection.criteria) {
                        if(collection.criteria[criteriaCount] !== undefined && collection.criteria[criteriaCount] != tmpCollection.criteria[criteriaCount]) {  
                            same=false; break;     
                        }
                    }
                 }
				 if(collection.search && tmpCollection.search) {
                    //Check Criteria
                    for(var criteriaCount in tmpCollection.search) {
                        if(collection.search[criteriaCount] !== undefined && collection.search[criteriaCount] != tmpCollection.search[criteriaCount]) {  
                            same=false; break;     
                        }
                    }
                 }
                 if(!same) { continue; }
                tmpArray.push(tmpCollection);
            }
            return tmpArray;
        }
        return search(OXCache.cachedCollections.cache).concat(search(OXCache.cachedCollectionsTrash)); 
    }
}
OXAbstractMapping.getSubCollectionsfromObject = function(oxMapping) {
    return function(object,ignoreChecked) {
        function search(myArray) {
            var tmpArray=new Array();
            for(var colCount in myArray) {
                var tmpCollection=myArray[colCount].data;
                if(ignoreChecked && tmpCollection.check) { continue; }
                var same=true;
                if(tmpCollection.criteria) {
                   for(var criteriaCount in tmpCollection.criteria) {
                        if(same!=true) { break; }
                        switch(criteriaCount) {
                            default:
                                if(object[criteriaCount] !== undefined && object[criteriaCount] != tmpCollection.criteria[criteriaCount]) {  
                                    same=false;
                                    break;     
                                }
                                break;
                        }
                    }
                 }
                 if(!same) { continue; }
                tmpArray.push(tmpCollection);
            }
            return tmpArray;
        }
        return search(OXCache.cachedCollections.cache).concat(search(OXCache.cachedCollectionsTrash)); 
    }
}

OXAbstractMapping.convertColumns = function(oxMapping) {
   return function(columns) {
   	    var idcolumns=new Array();
		for(var i=0;i<columns.length;i++) {
			var tmpColumn=oxMapping.stringmapping[trimStr(columns[i])];
			if(!tmpColumn) {
				if(oxMapping.alternative) {
				    tmpColumn=oxMapping.alternative[trimStr(columns[i])];
				}
				if(tmpColumn==true) {
					tmpColumn=false;
				}
			}
			if(!tmpColumn && tmpColumn != false) { 
			    if(debug) { alert("Column "+columns[i]+" not found in Mapping")}
			}
			idcolumns.push(tmpColumn);
		}
		return idcolumns;
   } 
}
OXAbstractMapping.setTag = function(oxMapping) {
	return function(tag,ids) {
		var multipleObject = [];
		for(var i = 0; i < ids.length; i++) {
            multipleObject.push({ 
                 action    : "update", 
                 module    : ids[i].module, 
                 timestamp : ids[i].timestamp || 0, 
                 id        : ids[i].id, 
                 folder    : ids[i].folder_id, 
                 data      : { color_label : tag } 
            });
			if(ids[i].recurrence_position && ids[i].recurrence_position > 0) {
                multipleObject[i] = ids[i].recurrence_position;
            }
        }
        json.put(AjaxRoot + "/multiple?session=" + session + "&continue=true",multipleObject,null,
            function(callback){
            	var timestamp = 0;
                for(var i=0;i<callback.length;i++) {
                    if(callback[i].error) {
						newServerError(callback[i]);
                        return;
                    } else if (callback[i].timestamp) {
                    	if (callback[i].timestamp > timestamp)
                            timestamp = callback[i].timestamp;
                    }
                }
                oxMapping.editObjectsInternal(ids,{ color_label : tag , timestamp : timestamp });                       
            }
        );
 	}
}




  /////////////////////////////////////
 //   OX Mapping Implementation     //
/////////////////////////////////////
function OXTaskMapping() {}
OXTaskMapping.mandatoryfields=["folder_id","id","last_modified"];
OXTaskMapping.idmapping= {
    "1": "id", "2": "created_by", "3": "modified_by", "4": "creation_date", "5": "last_modified", 
    "20": "folder_id", "100": "categories", "101": "private_flag", "104" : "number_of_attachments", "200": "title", "201": "start_date", 
    "202": "end_date", "203": "note", "204": "alarm",   
    "209": "recurrence_type", "212": "days", "213": "days_in_month", 
    "214": "month", "215": "internal", "216": "until",  "220": "participants", "221" : "users",
    "300": "status", "301": "percent_completed", "302": "actual_costs", 
    "303": "actual_duration", "305": "billing_information", 
    "307": "target_costs", "308": "target_duration", "309": "priority", 
    "312": "currency", "313": "trip_meter", "314": "companies", 
    "315": "date_completed", "102": "color_label"
} 
//"207": "recurrence_position", 208": "recurrence_date_position","217": "notification", "306": "project_id"
// ???
// "310": "duration_type", "311": "type_of_costs"

OXTaskMapping.stringmapping=switchStringObject(OXTaskMapping.idmapping);
OXTaskMapping.module="tasks";
OXTaskMapping.createKeyFromData = function(myArray,timestamp) {
     var tmp=new OXTaskObjectCache();
     tmp.module = OXTaskMapping.module;
     tmp.folder_id=myArray[0];
     tmp.id=myArray[1];
	 if(timestamp) { tmp.timestamp = timestamp }
     return tmp;     
}
OXTaskMapping.createKeyFromObject = function(myObject) {
     var tmp=new OXTaskObjectCache();
     tmp.module = OXTaskMapping.module;
     tmp.folder_id=myObject["folder_id"];
     tmp.id=myObject["id"];
     return tmp;     
}

OXTaskMapping.objectConstructor=OXTaskObjectCache;
OXTaskMapping.get = OXAbstractMapping.get(OXTaskMapping);
OXTaskMapping.execute = OXAbstractMapping.execute(OXTaskMapping);
OXTaskMapping.buildResponse = OXAbstractMapping.buildResponse(OXTaskMapping);
OXTaskMapping.criteriaRequest= OXAbstractMapping.criteriaRequest(OXTaskMapping);
OXTaskMapping.listRequest= OXAbstractMapping.listRequest(OXTaskMapping);
OXTaskMapping.addCriteriaNotInCache = OXAbstractMapping.addCriteriaNotInCache(OXTaskMapping);
OXTaskMapping.addCriteriaExpired = OXAbstractMapping.addCriteriaExpired(OXTaskMapping);
OXTaskMapping.addCriteriaMissingColumn = OXAbstractMapping.addCriteriaMissingColumn(OXTaskMapping);
OXTaskMapping.addListNotInCache = OXAbstractMapping.addListNotInCache(OXTaskMapping);
OXTaskMapping.addListExpired = OXAbstractMapping.addListExpired(OXTaskMapping);
OXTaskMapping.addListMissingColumn = OXAbstractMapping.addListMissingColumn(OXTaskMapping);
OXTaskMapping.request = OXAbstractMapping.request(OXTaskMapping);
OXTaskMapping.handleResponse = OXAbstractMapping.handleResponse(OXTaskMapping);
OXTaskMapping.handleCriteriaNotInCache = OXAbstractMapping.handleCriteriaNotInCache(OXTaskMapping);
OXTaskMapping.handleCriteriaExpired = OXAbstractMapping.handleCriteriaExpired(OXTaskMapping);
OXTaskMapping.handleCriteriaMissingColumn = OXAbstractMapping.handleCriteriaMissingColumn(OXTaskMapping);
OXTaskMapping.handleCriteria=OXAbstractMapping.handleCriteria(OXTaskMapping);
OXTaskMapping.handleListNotInCache = OXAbstractMapping.handleListNotInCache(OXTaskMapping);
OXTaskMapping.handleListExpired = OXAbstractMapping.handleListExpired(OXTaskMapping);
OXTaskMapping.handleListMissingColumn = OXAbstractMapping.handleListMissingColumn(OXTaskMapping);
OXTaskMapping.handleList = OXAbstractMapping.handleList(OXTaskMapping);
OXTaskMapping.deleteObject = OXAbstractMapping.deleteObject(OXTaskMapping);
OXTaskMapping.getCollection= OXAbstractMapping.getCollection(OXTaskMapping);
OXTaskMapping.convertColumns = OXAbstractMapping.convertColumns(OXTaskMapping);
OXTaskMapping.createCacheObject = OXAbstractMapping.createCacheObject(OXTaskMapping);
OXTaskMapping.checkAddCriteria = OXAbstractMapping.checkAddCriteria(OXTaskMapping);
OXTaskMapping.checkAddEditDeleteObject= OXAbstractMapping.checkAddEditDeleteObject(OXTaskMapping);
OXTaskMapping.getSubCollectionsfromObject = OXAbstractMapping.getSubCollectionsfromObject(OXTaskMapping);
OXTaskMapping.getSubCollections = OXAbstractMapping.getSubCollections(OXTaskMapping);
OXTaskMapping.equalsCollection = OXAbstractMapping.equalsCollection(OXTaskMapping);
OXTaskMapping.errorHandler = OXCache.errorHandler;
OXTaskMapping.deleteObjectsInternal = function(objects) {
    var tmpFn=OXCache.join.add();
    var oxMapping=OXTaskMapping;
    var tmpCollect=[];
    for(var mail=0;mail<objects.length;mail++) {
        var collections=oxMapping.getSubCollectionsfromObject(objects[mail],true);
        for(var count=0;count<collections.length;count++) {
            if(collections[count].check) { continue; } 
            var index=collections[count].map_objects.get(objects[mail]);
            if(index) {
                 collections[count].modified=true;
                 if(!collections[count].tmpIndexes) { collections[count].tmpIndexes=[]; }
                 collections[count].tmpIndexes.push(index);
                 tmpCollect.push(collections[count]);
                 collections[count].modifiedtmp=true;
             }               
        }
    }
    
    for(var count=0;count<tmpCollect.length;count++) {
        if(tmpCollect[count].modifiedtmp) {
            var indexes=tmpCollect[count].tmpIndexes;
            indexes.sort(function(a,b) { return a-b; });
            for(var i=indexes.length-1;i>=0;i--) {
                tmpCollect[count].objects.splice(indexes[i]-1,1);
            }
            delete tmpCollect[count].tmpIndexes;
            tmpCollect[count].map_objects=new LRUKeyList();
            for(var index=0;index<tmpCollect[count].objects.length;index++) { 
                tmpCollect[count].map_objects.set(tmpCollect[count].objects[index],index+1) 
            }
            tmpCollect[count].modifiedtmp=false;
        }
    }
    tmpFn();
}

OXTaskMapping.deleteObjects = function(objects) {
    var oxMapping=OXTaskMapping;
    for(var i=0;i<objects.length;i++) {
        var tmpTime=objects[i]["timestamp"]; 
        if (!tmpTime) {
            var tmpObject = OXCache.cachedObjects.get(objects[i]);
            if(tmpObject) {
                tmpTime=tmpObject.timestamp;
            }
        }
		var request = new Array();
        for (var i in objects) {
            request.push({
                module: "tasks", 
                action: "delete",
                folder: objects[i]["folder_id"],
                timestamp: objects[i]["timestamp"],
                data: { folder : objects[i]["folder_id"] , id : objects[i]["id"] }
            });
        }
    }
    json.put(AjaxRoot + "/multiple?session=" + session + "&continue=true",
        request, null, function(reply) {
            var tmpFn=OXCache.join.add();
            var error=new Array();
            for(var i=0;i<reply.length;i++) {
                if(reply[i].error) {
                    newServerError(reply[i]);
                    error.push(i);   
                }  
            }
            if(!error.length) { oxMapping.deleteObjectsInternal(objects); }
            tmpFn();        
        }
    );
}
OXTaskMapping.editObjectsInternal = function(oldObjects,changes,checkExist) {
    var oxMapping=OXTaskMapping;
    var tmpFn=OXCache.join.add();
    for(var myObject in oldObjects) {
        var tmpKeyOld=oxMapping.createKeyFromObject(oldObjects[myObject]);
        var collections=oxMapping.getSubCollectionsfromObject(oldObjects[myObject],true);
        //TODO Check Changes in Collection
        for(var i=0;i<collections.length;i++) {
            if(!collections[i].check) {
                collections[i].check=true;
            } 
        }
        for(var i in OXCache.modifiedCallbacks) {
            if(OXCache.modifiedCallbacks[i].module == oxMapping.module && !(OXCache.modifiedCallbacks[i].collection.criteria)) {
                for(var count=0;count<OXCache.modifiedCallbacks[i].collection.objects.length;count++) {
                    if(tmpKeyOld.equals(OXCache.modifiedCallbacks[i].collection.objects[count])) {
                        OXCache.modifiedCallbacks[i].modified=true;
                    }
                }
            }
        }
        var tmpObjectOld=OXCache.cachedObjects.get(tmpKeyOld);
        if(tmpObjectOld) {
           if(changes) {
              for(var i in changes) {
                  if(!checkExist || tmpObjectOld[i]!== undefined) {
                      if(i != "notification") {
                          tmpObjectOld[i]=changes[i];
                      }
                  }
              }
           }
       }
       OXCache.cachedObjects.set(tmpKeyOld,tmpObjectOld);    
    }  
    //TODO CHECK CHANGE FOLDER
    tmpFn();
}

OXTaskMapping.editObjects = function(oldObjects,changes) {
    setTimeout(function() {
       var tmpFn=OXCache.join.add();
       OXTaskMapping.editObjectsInternal(oldObjects,changes);
       tmpFn(); 
    },0);
}
OXTaskMapping.createObject = function(newObject) {
    setTimeout(function() {
        var tmpFn=OXCache.join.add();
        OXTaskMapping.createObjectsInternal([newObject]);
        tmpFn();
    },0);
}
OXTaskMapping.createObjectsInternal = function(newObjects) {
    setTimeout(function() {
        var oxMapping=OXTaskMapping;
        var tmpFn=OXCache.join.add();
        for(var object in newObjects) {
            var collections=oxMapping.getSubCollectionsfromObject(newObjects[object],true);
            for(var i=0;i<collections.length;i++) {
                if(!collections[i].check) {
                    collections[i].check=true;
                }
            }
        }
        tmpFn();
    },0);
}
OXTaskMapping.setTag = OXAbstractMapping.setTag(OXTaskMapping);
OXCache.setMapping("tasks",OXTaskMapping);




function OXCalendarMapping() {}
OXCalendarMapping.mandatoryfields=["folder_id","id","recurrence_position","last_modified"];
OXCalendarMapping.idmapping= { 
    "1": "id", "2": "created_by", "3": "modified_by", "4": "creation_date", "5":"last_modified",
    "20": "folder_id", "100": "categories", "101": "private_flag", "102":"color_label", "200": "title", 
    "201": "start_date", "202": "end_date", "203": "note", "204": "alarm",  
    "207": "recurrence_position", "208": "recurrence_date_position", "209": "recurrence_type",
    "212": "days", "213": "days_in_month", "214": "month", "215": "interval", "216": "until",
    "220": "participants", "221": "users", "400": "location",
    "401": "full_time", "402": "shown_as"
}
//"217": notification"
OXCalendarMapping.module="calendar";
OXCalendarMapping.createKeyFromData = function(myArray,timestamp) {
     var tmp=new OXCalendarObjectCache();
     tmp.module = OXCalendarMapping.module;
     tmp.folder_id=myArray[0];
     tmp.id=myArray[1];
     tmp.recurrence_position=myArray[2];
	  if(timestamp) { tmp.timestamp = timestamp }
     return tmp;     
}
OXCalendarMapping.createKeyFromObject = function(myObject) {
     var tmp=new OXCalendarObjectCache();
     tmp.module = OXCalendarMapping.module;
     tmp.folder_id=myObject["folder_id"];
     tmp.id=myObject["id"];
	 tmp.recurrence_position=myObject.recurrence_position;
     return tmp;     
}

OXCalendarMapping.objectConstructor=OXCalendarObjectCache;
OXCalendarMapping.stringmapping=switchStringObject(OXCalendarMapping.idmapping);
OXCalendarMapping.get = OXAbstractMapping.get(OXCalendarMapping);
OXCalendarMapping.execute = OXAbstractMapping.execute(OXCalendarMapping);
OXCalendarMapping.buildResponse = OXAbstractMapping.buildResponse(OXCalendarMapping);
OXCalendarMapping.criteriaRequest= OXAbstractMapping.criteriaRequest(OXCalendarMapping);
OXCalendarMapping.listRequest= OXAbstractMapping.listRequest(OXCalendarMapping);
OXCalendarMapping.addCriteriaNotInCache = OXAbstractMapping.addCriteriaNotInCache(OXCalendarMapping);
OXCalendarMapping.addCriteriaExpired = OXAbstractMapping.addCriteriaExpired(OXCalendarMapping);
OXCalendarMapping.addCriteriaMissingColumn = OXAbstractMapping.addCriteriaMissingColumn(OXCalendarMapping);
OXCalendarMapping.addListNotInCache = OXAbstractMapping.addListNotInCache(OXCalendarMapping);
OXCalendarMapping.addListExpired = OXAbstractMapping.addListExpired(OXCalendarMapping);
OXCalendarMapping.addListMissingColumn = OXAbstractMapping.addListMissingColumn(OXCalendarMapping);
OXCalendarMapping.request = OXAbstractMapping.request(OXCalendarMapping);
OXCalendarMapping.handleResponse = OXAbstractMapping.handleResponse(OXCalendarMapping);
OXCalendarMapping.handleCriteriaNotInCache = OXAbstractMapping.handleCriteriaNotInCache(OXCalendarMapping);
OXCalendarMapping.handleCriteriaExpired = OXAbstractMapping.handleCriteriaExpired(OXCalendarMapping);
OXCalendarMapping.handleCriteriaMissingColumn = OXAbstractMapping.handleCriteriaMissingColumn(OXCalendarMapping);
OXCalendarMapping.handleCriteria=OXAbstractMapping.handleCriteria(OXCalendarMapping);
OXCalendarMapping.handleListNotInCache = OXAbstractMapping.handleListNotInCache(OXCalendarMapping);
OXCalendarMapping.handleListExpired = OXAbstractMapping.handleListExpired(OXCalendarMapping);
OXCalendarMapping.handleListMissingColumn = OXAbstractMapping.handleListMissingColumn(OXCalendarMapping);
OXCalendarMapping.handleList = OXAbstractMapping.handleList(OXCalendarMapping);
OXCalendarMapping.deleteObject = OXAbstractMapping.deleteObject(OXCalendarMapping);
OXCalendarMapping.getCollection= OXAbstractMapping.getCollection(OXCalendarMapping);
OXCalendarMapping.convertColumns = OXAbstractMapping.convertColumns(OXCalendarMapping);
OXCalendarMapping.createCacheObject = OXAbstractMapping.createCacheObject(OXCalendarMapping);
OXCalendarMapping.checkAddCriteria = OXAbstractMapping.checkAddCriteria(OXCalendarMapping);
OXCalendarMapping.checkAddEditDeleteObject= OXAbstractMapping.checkAddEditDeleteObject(OXCalendarMapping);
OXCalendarMapping.getSubCollectionsfromObject = OXAbstractMapping.getSubCollectionsfromObject(OXCalendarMapping);
OXCalendarMapping.getSubCollections = OXAbstractMapping.getSubCollections(OXCalendarMapping);
OXCalendarMapping.equalsCollection = OXAbstractMapping.equalsCollection(OXCalendarMapping);
OXCalendarMapping.errorHandler = OXCache.errorHandler;
OXCache.setMapping("calendar",OXCalendarMapping);

function OXContactMapping() {}
OXContactMapping.mandatoryfields=["folder_id","id","last_modified"];
OXContactMapping.idmapping= { 
    "1": "id", "2": "created_by", "3": "modified_by", "4": "creation_date", "5": "last_modified", 
    "20": "folder_id", "100": "categories", "101": "private_flag", "500": "display_name", "501": "first_name",
    "502": "last_name", "503": "second_name", "504": "suffix", "505": "title", "506": "street_home",
    "507": "postal_code_home", "508": "city_home", "509": "state_home", "510": "country_home", 
    "511": "birthday", "512": "marital_status", "513": "number_of_children", "514": "profession", 
    "515": "nickname", "516": "spouse_name", "517": "anniversary", "518": "note", "519": "department", 
    "520": "position", "521": "employee_type", "522": "room_number", "523": "street_business", 
    "525": "postal_code_business", "526": "city_business", "527": "state_business", "528": "country_business",
    "529": "number_of_employees", "530": "sales_volume", "531": "tax_id", "532": "commercial_register",
    "533": "branches", "534": "business_category", "535": "info", "536": "manager_name", 
    "537": "assistant_name", "538": "street_other", "539": "city_other", "540": "postal_code_other", 
    "541": "country_other", "542": "telephone_business1", "543": "telephone_business2", "544": "fax_business",
    "545": "telephone_callback", "546": "telephone_car", "547": "telephone_company", "548": "telephone_home1",
    "549": "telephone_home2", "550": "fax_home", "551": "cellular_telephone1", "552": "cellular_telephone2",
    "553": "telephone_other", "554": "fax_other", "555": "email1", "556": "email2", "557": "email3",
    "558": "url", "559": "telephone_isdn", "560": "telephone_pager", "561": "telephone_primary",
    "562": "telephone_radio", "563": "telephone_telex", "564": "telephone_ttytdd",
    "565": "instant_messenger1", "566": "instant_messenger2", "567": "telephone_ip",
    "568": "telephone_assistant", "569": "company", "570": "image1", "571": "userfield01",
    "572": "userfield02", "573": "userfield03", "574": "userfield04", "575": "userfield05",
    "576": "userfield06", "577": "userfield07", "578": "userfield08", "579": "userfield09",
    "580": "userfield10", "581": "userfield11", "582": "userfield12", "583": "userfield13",
    "584": "userfield14", "585": "userfield15", "586": "userfield16", "587": "userfield17",
    "588": "userfield18", "589": "userfield19", "590": "userfield20", "591": "links",
    "592": "distribution_list", "594": "number_of_distribution_list",
    "595": "number_of_links", "596": "contains_image1", "597": "image_last_modified", "598": "state_other",
    "599": "file_as", "104": "number_of_attachments", "601": "image1_content_type", "602": "mark_as_distributionlist",
    "605": "default_address", "102": "color_label", "524": "internal_userid"
}
OXContactMapping.stringmapping=switchStringObject(OXContactMapping.idmapping);
OXContactMapping.module="contacts";
OXContactMapping.createKeyFromData = function(myArray,timestamp) {
     var tmp=new OXContactObjectCache();
     tmp.module = OXContactMapping.module;
     tmp.folder_id=myArray[0];
     tmp.id=myArray[1];
     if(timestamp) { tmp.timestamp = timestamp }
     return tmp;     
}
OXContactMapping.createKeyFromObject = function(myObject) {
     var tmp=new OXContactObjectCache();
     tmp.module = OXContactMapping.module;
     tmp.folder_id=myObject["folder_id"];
     tmp.id=myObject["id"];
     return tmp;     
}


OXContactMapping.objectConstructor=OXContactObjectCache;
OXContactMapping.get = OXAbstractMapping.get(OXContactMapping);
OXContactMapping.execute = OXAbstractMapping.execute(OXContactMapping);
OXContactMapping.buildResponse = OXAbstractMapping.buildResponse(OXContactMapping);
OXContactMapping.criteriaRequest= OXAbstractMapping.criteriaRequest(OXContactMapping);
OXContactMapping.listRequest= OXAbstractMapping.listRequest(OXContactMapping);
OXContactMapping.addCriteriaNotInCache = OXAbstractMapping.addCriteriaNotInCache(OXContactMapping);
OXContactMapping.addCriteriaExpired = OXAbstractMapping.addCriteriaExpired(OXContactMapping);
OXContactMapping.addCriteriaMissingColumn = OXAbstractMapping.addCriteriaMissingColumn(OXContactMapping);
OXContactMapping.addListNotInCache = OXAbstractMapping.addListNotInCache(OXContactMapping);
OXContactMapping.addListExpired = OXAbstractMapping.addListExpired(OXContactMapping);
OXContactMapping.addListMissingColumn = OXAbstractMapping.addListMissingColumn(OXContactMapping);
OXContactMapping.request = OXAbstractMapping.request(OXContactMapping);
OXContactMapping.handleResponse = OXAbstractMapping.handleResponse(OXContactMapping);
OXContactMapping.handleCriteriaNotInCache = OXAbstractMapping.handleCriteriaNotInCache(OXContactMapping);
OXContactMapping.handleCriteriaExpired = OXAbstractMapping.handleCriteriaExpired(OXContactMapping);
OXContactMapping.handleCriteriaMissingColumn = OXAbstractMapping.handleCriteriaMissingColumn(OXContactMapping);
OXContactMapping.handleCriteria=OXAbstractMapping.handleCriteria(OXContactMapping);
OXContactMapping.handleListNotInCache = OXAbstractMapping.handleListNotInCache(OXContactMapping);
OXContactMapping.handleListExpired = OXAbstractMapping.handleListExpired(OXContactMapping);
OXContactMapping.handleListMissingColumn = OXAbstractMapping.handleListMissingColumn(OXContactMapping);
OXContactMapping.handleList = OXAbstractMapping.handleList(OXContactMapping);
OXContactMapping.deleteObject = OXAbstractMapping.deleteObject(OXContactMapping);
OXContactMapping.getCollection= OXAbstractMapping.getCollection(OXContactMapping);
OXContactMapping.convertColumns = OXAbstractMapping.convertColumns(OXContactMapping);
OXContactMapping.createCacheObject = OXAbstractMapping.createCacheObject(OXContactMapping);
OXContactMapping.checkAddCriteria = OXAbstractMapping.checkAddCriteria(OXContactMapping);
OXContactMapping.checkAddEditDeleteObject= OXAbstractMapping.checkAddEditDeleteObject(OXContactMapping);
OXContactMapping.getSubCollectionsfromObject = OXAbstractMapping.getSubCollectionsfromObject(OXContactMapping);
OXContactMapping.getSubCollections = OXAbstractMapping.getSubCollections(OXContactMapping);
OXContactMapping.equalsCollection = OXAbstractMapping.equalsCollection(OXContactMapping);
OXContactMapping.errorHandler = OXCache.errorHandler;

OXContactMapping.deleteObjectsInternal = function(objects) {
    var tmpFn=OXCache.join.add();
    var oxMapping=OXContactMapping;
    var tmpCollect=[];
    for(var mail=0;mail<objects.length;mail++) {
        var collections=oxMapping.getSubCollectionsfromObject(objects[mail],true);
        for(var count=0;count<collections.length;count++) {
            if(collections[count].check) { continue; } 
            var index=collections[count].map_objects.get(objects[mail]);
            if(index) {
                 collections[count].modified=true;
                 if(!collections[count].tmpIndexes) { collections[count].tmpIndexes=[]; }
                 collections[count].tmpIndexes.push(index);
                 tmpCollect.push(collections[count]);
                 collections[count].modifiedtmp=true;
             }               
        }
    }
    for(var count=0;count<tmpCollect.length;count++) {
        if(tmpCollect[count].modifiedtmp) {
                var indexes=tmpCollect[count].tmpIndexes;
                indexes.sort(function(a,b) { return a-b; });
                for(var i=indexes.length-1;i>=0;i--) {
                    tmpCollect[count].objects.splice(indexes[i]-1,1);
                }
                delete tmpCollect[count].tmpIndexes;
            tmpCollect[count].map_objects=new LRUKeyList();
            for(var index=0;index<tmpCollect[count].objects.length;index++) { 
                tmpCollect[count].map_objects.set(tmpCollect[count].objects[index],index+1) 
            }
            tmpCollect[count].modifiedtmp=false;
        }
    }
    tmpFn();
}

OXContactMapping.deleteObjects = function(objects) {
    var oxMapping=OXContactMapping;
    var request = new Array();
    for (var i in objects) {
		var tmpTime=objects[i]["timestamp"]; 
        if (!tmpTime) {
            var tmpObject = OXCache.cachedObjects.get(objects[i]);
            if(tmpObject) {
                tmpTime=tmpObject.timestamp;
            }
        }
        request.push({
            module: oxMapping.module, 
            action: "delete",
            folder: objects[i]["folder_id"],
            timestamp: tmpTime ,
            data: { folder : objects[i]["folder_id"] , id : objects[i]["id"] }
        });
    }
    json.put(AjaxRoot + "/multiple?session=" + session + "&continue=true",
        request, null, function(reply) {
            var tmpFn=OXCache.join.add();
            var error=new Array();
            for(var i=0;i<reply.length;i++) {
                if(reply[i].error) {
                    newServerError(reply[i]);
                    error.push(i);   
                }  
            }
            if(!error.length) {
                oxMapping.deleteObjectsInternal(objects);
            }
            tmpFn();            
        }
    );
}
OXContactMapping.editObjects = function(oldObjects,changes) {
    setTimeout(function() {
       var tmpFn=OXCache.join.add();
       OXContactMapping.editObjectsInternal(oldObjects,changes);
       tmpFn(); 
    },0);
}
OXContactMapping.editObjectsInternal = function(oldObjects,changes,checkExist) {
    var oxMapping=OXContactMapping;
    var tmpFn=OXCache.join.add();
    for(var myObject in oldObjects) {
        var tmpKeyOld=oxMapping.createKeyFromObject(oldObjects[myObject]);
        var collections=oxMapping.getSubCollectionsfromObject(oldObjects[myObject],true);
        //TODO Check Changes in Collection
        for(var i=0;i<collections.length;i++) {
            if(!collections[i].check) {
                collections[i].check=true;
            } 
        }
        for(var i in OXCache.modifiedCallbacks) {
            if(OXCache.modifiedCallbacks[i].module == oxMapping.module && !(OXCache.modifiedCallbacks[i].collection.criteria)) {
                for(var count=0;count<OXCache.modifiedCallbacks[i].collection.objects.length;count++) {
                    if(tmpKeyOld.equals(OXCache.modifiedCallbacks[i].collection.objects[count])) {
                        OXCache.modifiedCallbacks[i].modified=true;    
                    }
                }
            }
        }
        var tmpObjectOld=OXCache.cachedObjects.get(tmpKeyOld);
        if(tmpObjectOld) {
           if(changes) {
              for(var i in changes) {
                  if(!checkExist || tmpObjectOld[i]!== undefined) {
                      if(i != "notification") {
                          tmpObjectOld[i]=changes[i];
                      }
                  }
              }
           }
       }
       OXCache.cachedObjects.set(tmpKeyOld,tmpObjectOld);    
    }  
    //TODO CHECK CHANGE FOLDER
    tmpFn();
}

OXContactMapping.createObject = function(newObject) {
    setTimeout(function() {
        var tmpFn=OXCache.join.add();
        OXContactMapping.createObjectsInternal([newObject]);
        tmpFn();
    },0);
}
OXContactMapping.createObjectsInternal = function(newObjects) {
    setTimeout(function() {
        var oxMapping=OXContactMapping;
        var tmpFn=OXCache.join.add();
        for(var object in newObjects) {
            var collections=oxMapping.getSubCollectionsfromObject(newObjects[object],true);
            for(var i=0;i<collections.length;i++) {
                if(!collections[i].check) {
                    collections[i].check=true;
                }
            }
        }
        tmpFn();
    },0);
}
OXContactMapping.setTag = OXAbstractMapping.setTag(OXContactMapping);

OXCache.setMapping("contacts",OXContactMapping);

function OXMailMapping() {} 
OXMailMapping.mandatoryfields=["id","folder_id"];
OXMailMapping.idmapping = {
    "600": "id", "601": "folder_id", "602": "attachment", "603": "from", "604": "to", "605": "cc", 
    "606": "bcc", "607": "subject", "608": "size", "609": "sent_date", "610": "received_date", 
    "611": "flags", "612": "level", "614": "priority", "102": "color_label"  
}
OXMailMapping.alternative = {
	"user" : true , "attachments" : true , "nested_msgs" : true ,"attachments_html" : true ,
	"attachments_html_noimage" : true,"attachments_plain" : true , "blocked_images" : true ,
    "mailtext" : "-1", "disp_notification_to" : "613", "flags_sort" : "651", "headers" : true 
}
OXMailMapping.GETHTML = [
    "id","folder_id","attachment","attachments_html","from","to","cc","bcc","subject","size","sent_date","received_date","flags",
    "level","priority","color_label","user","nested_msgs","disp_notification_to",
    "flags_sort", "headers" 
];
OXMailMapping.GETPLAIN = [
    "id","folder_id","attachment","attachments_plain","from","to","cc","bcc","subject","size","sent_date","received_date","flags",
    "level","priority","color_label","user","nested_msgs","disp_notification_to",
    "flags_sort", "headers" 
];
OXMailMapping.GETHTMLNOIMAGE = [
    "id","folder_id","attachment","attachments_html_noimage","blocked_images","from","to","cc","bcc","subject","size","sent_date","received_date","flags",
    "level","priority","color_label","user","nested_msgs","disp_notification_to",
    "flags_sort", "headers"  
];

OXMailMapping.flagmapping = { "seen" : { flag : 32 , bool : true} , "unseen" : { flag : 32 , bool : false} ,
                            "answer" : { flag : 1 , bool : true} , "answered" : { flag : 1 , bool : false},
                            "delete" : { flag : 2 , bool : true} , "undelete" : { flag : 2 , bool : false},
                            "spam" : { flag : 128 , bool : true} , "ham" : { flag : 128 , bool : false} };

OXMailMapping.module="mail";
OXMailMapping.createKeyFromData = function(myArray,timestamp) {
     var tmp=new OXMailObjectCache();
     tmp.module = OXMailMapping.module;
     tmp.folder_id=myArray[1];
     tmp.id=myArray[0];
     return tmp;     
}
OXMailMapping.createKeyFromObject = function(myObject) {
     var tmp=new OXMailObjectCache();
     tmp.module = OXMailMapping.module;
     tmp.folder_id=myObject["folder_id"];
     tmp.id=myObject["id"];
     return tmp;     
}


OXMailMapping.stringmapping=switchStringObject(OXMailMapping.idmapping);
OXMailMapping.objectConstructor=OXMailObjectCache;
OXMailMapping.stringmapping=switchStringObject(OXMailMapping.idmapping);
OXMailMapping.get = OXAbstractMapping.get(OXMailMapping);
OXMailMapping.execute = OXAbstractMapping.execute(OXMailMapping);
OXMailMapping.buildResponse = OXAbstractMapping.buildResponse(OXMailMapping);
OXMailMapping.criteriaRequest= OXAbstractMapping.criteriaRequest(OXMailMapping);
OXMailMapping.listRequest= OXAbstractMapping.listRequest(OXMailMapping);
OXMailMapping.addCriteriaNotInCache = OXAbstractMapping.addCriteriaNotInCache(OXMailMapping);
OXMailMapping.addCriteriaExpired = OXAbstractMapping.addCriteriaExpired(OXMailMapping);
OXMailMapping.addCriteriaMissingColumn = OXAbstractMapping.addCriteriaMissingColumn(OXMailMapping);
OXMailMapping.addListNotInCache = OXAbstractMapping.addListNotInCache(OXMailMapping);
OXMailMapping.addListExpired = OXAbstractMapping.addListExpired(OXMailMapping);
OXMailMapping.addListMissingColumn = OXAbstractMapping.addListMissingColumn(OXMailMapping);
OXMailMapping.request = OXAbstractMapping.request(OXMailMapping);
OXMailMapping.handleResponse = OXAbstractMapping.handleResponse(OXMailMapping);
OXMailMapping.handleCriteriaNotInCache = OXAbstractMapping.handleCriteriaNotInCache(OXMailMapping);
OXMailMapping.handleCriteriaExpired = OXAbstractMapping.handleCriteriaExpired(OXMailMapping);
OXMailMapping.handleCriteriaMissingColumn = OXAbstractMapping.handleCriteriaMissingColumn(OXMailMapping);
OXMailMapping.handleCriteria=OXAbstractMapping.handleCriteria(OXMailMapping);
OXMailMapping.handleListNotInCache = OXAbstractMapping.handleListNotInCache(OXMailMapping);
OXMailMapping.handleListExpired = OXAbstractMapping.handleListExpired(OXMailMapping);
OXMailMapping.handleListMissingColumn = OXAbstractMapping.handleListMissingColumn(OXMailMapping);
OXMailMapping.handleList = OXAbstractMapping.handleList(OXMailMapping);
OXMailMapping.deleteObject = OXAbstractMapping.deleteObject(OXMailMapping);
OXMailMapping.getCollection= OXAbstractMapping.getCollection(OXMailMapping);
OXMailMapping.convertColumns = OXAbstractMapping.convertColumns(OXMailMapping);
OXMailMapping.createCacheObject = OXAbstractMapping.createCacheObject(OXMailMapping);
OXMailMapping.checkAddCriteria = OXAbstractMapping.checkAddCriteria(OXMailMapping);
OXMailMapping.checkAddEditDeleteObject= OXAbstractMapping.checkAddEditDeleteObject(OXMailMapping);
OXMailMapping.getSubCollectionsfromObject = OXAbstractMapping.getSubCollectionsfromObject(OXMailMapping);
OXMailMapping.getSubCollections = OXAbstractMapping.getSubCollections(OXMailMapping);
OXMailMapping.equalsCollection = OXAbstractMapping.equalsCollection(OXMailMapping);
OXMailMapping.errorHandler = function(uniqueName,request,response,response2) {
    if(debug) { 
    	console.log("Error",uniqueName,request,response,response2); 
    }
    if (response.error) {
    	// overwrite the default error handler at some special cases
        switch (response.code) {
        	 case "MSG-0032":
        	   // message has been already deleted so we remove it from the cache
        	   // and ignore the error  
    	       OXMailMapping.deleteObjectsInternal([ request.object ]);
        	   break;
        	 default:
        	   // calling the default error handler
        	   OXCache.errorHandler(uniqueName,request,response,response2);
        	   break;        	
        }
    }
    return;
};

OXMailMapping.deleteObjectsInternal = function(objects) {
	var objects_tmp = new Array(objects.length);
	for (var i=0; i<objects.length; i++) {
		objects_tmp[i] = objects[i];
	}
	
    var tmpFn=OXCache.join.add();
    var oxMapping=OXMailMapping;
    var tmpCollect=[];
    for(var mail=0;mail<objects_tmp.length;mail++) {
        var collections=oxMapping.getSubCollectionsfromObject(objects_tmp[mail],true);
        for(var count=0;count<collections.length;count++) {
            if(collections[count].check) { continue; } 
            var index=collections[count].map_objects.get(objects_tmp[mail]);
            if(index) {
                 collections[count].modified=true;
                 if(!collections[count].tmpIndexes) { collections[count].tmpIndexes=[]; }
                 collections[count].tmpIndexes.push(index);
                 tmpCollect.push(collections[count]);
                 collections[count].modifiedtmp=true;
             }               
        }
    }
    
    for(var count=0;count<tmpCollect.length;count++) {
        if(tmpCollect[count].modifiedtmp) {
                var indexes=tmpCollect[count].tmpIndexes;
                indexes.sort(function(a,b) { return a-b; });
                for(var i=indexes.length-1;i>=0;i--) {
                    tmpCollect[count].objects.splice(indexes[i]-1,1);
                }
                delete tmpCollect[count].tmpIndexes;
            tmpCollect[count].map_objects=new LRUKeyList();
            for(var index=0;index<tmpCollect[count].objects.length;index++) { 
                tmpCollect[count].map_objects.set(tmpCollect[count].objects[index],index+1) 
            }
            tmpCollect[count].modifiedtmp=false;
        }
    }

    for(var i2 in OXCache.modifiedCallbacks) {
        if (OXCache.modifiedCallbacks[i2].module == oxMapping.module && !(OXCache.modifiedCallbacks[i2].collection.criteria)) {
            for (var count=0;count<OXCache.modifiedCallbacks[i2].collection.objects.length;count++) {
                for (var i=0;i<objects_tmp.length;i++) {
                    if (OXCache.modifiedCallbacks[i2].collection.objects[count].equals(objects_tmp[i])) {
                       OXCache.modifiedCallbacks[i2].collection.objects.splice(count,1);
                       count=count-1;
                       OXCache.modifiedCallbacks[i2].modified=true;
                       break;
                   }
                }
            }
        }
    }
    tmpFn();
}
OXMailMapping.deleteObjects = function(mailObjects,harddelete,optCb) {
    var oxMapping=OXMailMapping;
    var request=new Array();
    var idArray=new Array();
    for(var i=0;i<mailObjects.length;i++) {
        idArray.push({id : mailObjects[i].id , folder : mailObjects[i].folder_id});
    }
    if(idArray.length) {
        request.push({
            module : "mail",
            action : "delete",
            folder : idArray[0].folder,
            data : idArray
        });
        if(harddelete) {
            request.harddelete=1;
        }
    }
    var tmpFn=OXCache.join.add();
    var callit=false;
	json.put(AjaxRoot + "/multiple?session=" + session + "&continue=true",
        request, null, function(reply) {
            var tmpCollect=new Array();
            for(var i=0;i<reply.length;i++) {
                if(reply[i].error) {
                    if (!status && reply.code == "MSG-0039") {
                        function cbyes() {
                            OXCache.deleteObjects(mailObjects, true, optCb);
                        }
                        newConfirm(_("Quota Exceed"),_("Permanently remove your deleted E-Mails?"),AlertPopup.YESNO,null,null,cbyes,null);          
                    } else {
                       newServerError(reply[i]);
                    }
                } else {
                    oxMapping.deleteObjectsInternal(mailObjects);
					callit=true;		
                }
            }
            tmpFn();            
			if(optCb && callit) optCb();
        }
    );
}
OXMailMapping.setMailFlags = function(flag,objects) {
    var oxMapping=OXMailMapping;
    var mailFlagsParam = OXMailMapping.flagmapping[flag];
    var Self = this;
    var tmpObject = {};
	var tmpFn=OXCache.join.add();
    if(objects.length != 0) {
        var multipleObject = [];
        for (var i = 0; i < objects.length; i++) {
            multipleObject.push({ 
                action : "update", module : "mail", timestamp : 0, id : objects[i].id, 
                folder : (objects[i].folder || objects[i].folder_id), data : { flags : mailFlagsParam.flag , value :  mailFlagsParam.bool }
            }); 
         }
         (new JSON).put(AjaxRoot + "/multiple?session=" + session + "&continue=true",multipleObject,null,
         function(daten){
            if (flag == "spam" || flag == "ham") {
                oxMapping.deleteObjectsInternal(objects);
            } else {
                oxMapping.editObjectsInternal(objects,null,true,[mailFlagsParam]);
            }
            tmpFn();

         });
     }
}
OXMailMapping.editObjectsInternal = function(oldObjects,changes,checkExist,optionalFlags) {
    var oxMapping=OXMailMapping;
    var tmpFn=OXCache.join.add();
    for(var myObject in oldObjects) {
        var tmpKeyOld=OXMailMapping.createKeyFromObject(oldObjects[myObject]);
        var collections=oxMapping.getSubCollectionsfromObject(oldObjects[myObject],true);
        //TODO Check Changes in Collection
        for(var i=0;i<collections.length;i++) {
            if(!collections[i].check) {
                //TODO SET CHECK IF CHANGES IN ORDER COLUMN
                //collections[i].check=true;
				collections[i].modified = true;
            } 
        }
        for(var i in OXCache.modifiedCallbacks) {
            if(OXCache.modifiedCallbacks[i].module == oxMapping.module && !(OXCache.modifiedCallbacks[i].collection.criteria)) {
                for(var count=0;count<OXCache.modifiedCallbacks[i].collection.objects.length;count++) {
                    if(tmpKeyOld.equals(OXCache.modifiedCallbacks[i].collection.objects[count])) {
						OXCache.modifiedCallbacks[i].modified=true;    
                    }
                }
            }
        }
        var tmpObjectOld=OXCache.cachedObjects.get(tmpKeyOld);
        if(tmpObjectOld) {
           if(changes) {
              for(var i in changes) {
                  if(!checkExist || tmpObjectOld[i]!== undefined) {
                      tmpObjectOld[i]=changes[i];
                  }
              }
           }
           if(optionalFlags) {
              if(!checkExist || tmpObjectOld["flags"]!== undefined) {
                 for(var i in optionalFlags) {
                      if((tmpObjectOld["flags"] & optionalFlags[i].flag) && !optionalFlags[i].bool) {
                        tmpObjectOld["flags"]=tmpObjectOld["flags"]-optionalFlags[i].flag;
                      } else if(!(tmpObjectOld["flags"] & optionalFlags[i].flag) && optionalFlags[i].bool) {
                        tmpObjectOld["flags"]=tmpObjectOld["flags"]+optionalFlags[i].flag;
                      }                        
                 }
              }
           }
       }
    }  
    OXCache.cachedObjects.set(tmpKeyOld,tmpObjectOld);
    //TODO CHECK CHANGE FOLDER
    tmpFn();
}
OXMailMapping.createObjectsInternal = function(newObjects) {
    setTimeout(function() {
        var oxMapping=OXMailMapping;
        var tmpFn=OXCache.join.add();
        for(var object in newObjects) {
            var collections=oxMapping.getSubCollectionsfromObject(newObjects[object],true);
            for(var i=0;i<collections.length;i++) {
                if(!collections[i].check) {
                    collections[i].check=true;
                }
            }
        }
        tmpFn();
    },0);
}
OXMailMapping.editObject = function(oldObject,newObject) {
    setTimeout(function() {
        var oxMapping=OXMailMapping;
        var tmpFn=OXCache.join.add();
        var tmpKeyOld=OXMailMapping.createKeyFromObject(oldObject);
        var collections=oxMapping.getSubCollectionsfromObject(oldObject,true);
        for(var i=0;i<collections.length;i++) {
            if(!collections[i].check) {
                collections[i].check=true;
            }
        }
        for(var i in OXCache.modifiedCallbacks) {
            if(OXCache.modifiedCallbacks[i].module == oxMapping.module && !(OXCache.modifiedCallbacks[i].collection.criteria)) {
                for(var count=0;count<OXCache.modifiedCallbacks[i].collection.objects.length;count++) {
                    if(tmpKeyOld.equals(OXCache.modifiedCallbacks[i].collection.objects[count])) {
                        OXCache.modifiedCallbacks[i].modified=true;  
                    }
                }
            }
        }
        var tmpObjectOld=OXCache.cachedObjects.get(tmpKeyOld);
        OXCache.cachedObjects.set(tmpKeyOld,tmpObjectOld);
        //TODO CHECK CHANGE FOLDER
        tmpFn();
    },0);
}
OXMailMapping.createObject = function(newObject) {
    setTimeout(function() {
        var oxMapping=OXTaskMapping;
        var tmpFn=OXCache.join.add();
        var collections=oxMapping.getSubCollectionsfromObject(newObject,true);
        for(var i=0;i<collections.length;i++) {
            if(!collections[i].check) {
                collections[i].check=true;
            }
        }
        tmpFn();
    },0);
}
OXMailMapping.setTag = OXAbstractMapping.setTag(OXMailMapping);

OXCache.setMapping("mail",OXMailMapping);




function OXInfoStoreMapping() {}
OXInfoStoreMapping.mandatoryfields=["folder_id","id","last_modified"];
OXInfoStoreMapping.idmapping= {
    "1": "id", "2": "created_by", "3": "modified_by", "4": "creation_date", "5": "last_modified", 
    "20": "folder_id", "100": "categories", "700": "title", "701": "url", 
    "702": "filename", "703": "file_mimetype", "704": "file_size", "705": "version", "706": "description", 
    "707": "locked_until", "708": "file_md5sum", "709": "version_comment", "710": "current_version", 
    "102": "color_label"
}
// "101": "private_flag",
OXInfoStoreMapping.stringmapping=switchStringObject(OXInfoStoreMapping.idmapping);
OXInfoStoreMapping.module="infostore";
OXInfoStoreMapping.createKeyFromData = function(myArray,timestamp) {
     var tmp=new OXInfoStoreObjectCache();
     tmp.module = OXInfoStoreMapping.module;
     tmp.folder_id=myArray[0];
     tmp.id=myArray[1];
     if(timestamp) { tmp.timestamp = timestamp }
     return tmp;     
}
OXInfoStoreMapping.createKeyFromObject = function(myObject) {
     var tmp=new OXInfoStoreObjectCache();
     tmp.module = OXInfoStoreMapping.module;
     tmp.folder_id=myObject["folder_id"];
     tmp.id=myObject["id"];
     return tmp;     
}

OXInfoStoreMapping.objectConstructor=OXInfoStoreObjectCache;
OXInfoStoreMapping.stringmapping=switchStringObject(OXInfoStoreMapping.idmapping);
OXInfoStoreMapping.get = OXAbstractMapping.get(OXInfoStoreMapping);
OXInfoStoreMapping.execute = OXAbstractMapping.execute(OXInfoStoreMapping);
OXInfoStoreMapping.buildResponse = OXAbstractMapping.buildResponse(OXInfoStoreMapping);
OXInfoStoreMapping.criteriaRequest= OXAbstractMapping.criteriaRequest(OXInfoStoreMapping);
OXInfoStoreMapping.listRequest= OXAbstractMapping.listRequest(OXInfoStoreMapping);
OXInfoStoreMapping.addCriteriaNotInCache = OXAbstractMapping.addCriteriaNotInCache(OXInfoStoreMapping);
OXInfoStoreMapping.addCriteriaExpired = OXAbstractMapping.addCriteriaExpired(OXInfoStoreMapping);
OXInfoStoreMapping.addCriteriaMissingColumn = OXAbstractMapping.addCriteriaMissingColumn(OXInfoStoreMapping);
OXInfoStoreMapping.addListNotInCache = OXAbstractMapping.addListNotInCache(OXInfoStoreMapping);
OXInfoStoreMapping.addListExpired = OXAbstractMapping.addListExpired(OXInfoStoreMapping);
OXInfoStoreMapping.addListMissingColumn = OXAbstractMapping.addListMissingColumn(OXInfoStoreMapping);
OXInfoStoreMapping.request = OXAbstractMapping.request(OXInfoStoreMapping);
OXInfoStoreMapping.handleResponse = OXAbstractMapping.handleResponse(OXInfoStoreMapping);
OXInfoStoreMapping.handleCriteriaNotInCache = OXAbstractMapping.handleCriteriaNotInCache(OXInfoStoreMapping);
OXInfoStoreMapping.handleCriteriaExpired = OXAbstractMapping.handleCriteriaExpired(OXInfoStoreMapping);
OXInfoStoreMapping.handleCriteriaMissingColumn = OXAbstractMapping.handleCriteriaMissingColumn(OXInfoStoreMapping);
OXInfoStoreMapping.handleCriteria=OXAbstractMapping.handleCriteria(OXInfoStoreMapping);
OXInfoStoreMapping.handleListNotInCache = OXAbstractMapping.handleListNotInCache(OXInfoStoreMapping);
OXInfoStoreMapping.handleListExpired = OXAbstractMapping.handleListExpired(OXInfoStoreMapping);
OXInfoStoreMapping.handleListMissingColumn = OXAbstractMapping.handleListMissingColumn(OXInfoStoreMapping);
OXInfoStoreMapping.handleList = OXAbstractMapping.handleList(OXInfoStoreMapping);
OXInfoStoreMapping.deleteObject = OXAbstractMapping.deleteObject(OXInfoStoreMapping);
OXInfoStoreMapping.getCollection= OXAbstractMapping.getCollection(OXInfoStoreMapping);
OXInfoStoreMapping.convertColumns = OXAbstractMapping.convertColumns(OXInfoStoreMapping);
OXInfoStoreMapping.createCacheObject = OXAbstractMapping.createCacheObject(OXInfoStoreMapping);
OXInfoStoreMapping.checkAddCriteria = OXAbstractMapping.checkAddCriteria(OXInfoStoreMapping);
OXInfoStoreMapping.checkAddEditDeleteObject= OXAbstractMapping.checkAddEditDeleteObject(OXInfoStoreMapping);
OXInfoStoreMapping.getSubCollectionsfromObject = OXAbstractMapping.getSubCollectionsfromObject(OXInfoStoreMapping);
OXInfoStoreMapping.getSubCollections = OXAbstractMapping.getSubCollections(OXInfoStoreMapping);
OXInfoStoreMapping.equalsCollection = OXAbstractMapping.equalsCollection(OXInfoStoreMapping);
OXInfoStoreMapping.errorHandler = OXCache.errorHandler;
OXInfoStoreMapping.editObjects = function(oldObjects,changes) {
    setTimeout(function() {
       var tmpFn=OXCache.join.add();
       OXInfoStoreMapping.editObjectsInternal(oldObjects,changes);
       tmpFn(); 
    },0);
}
OXInfoStoreMapping.editObjectsInternal = function(oldObjects,changes,checkExist) {
    var oxMapping=OXInfoStoreMapping;
    var tmpFn=OXCache.join.add();
    for(var myObject in oldObjects) {
        var tmpKeyOld=oxMapping.createKeyFromObject(oldObjects[myObject]);
        var collections=oxMapping.getSubCollectionsfromObject(oldObjects[myObject],true);
        //TODO Check Changes in Collection
        for(var i=0;i<collections.length;i++) {
            if(!collections[i].search && !collections[i].check) {
                collections[i].check=true;
            } 
        }
        for(var i in OXCache.modifiedCallbacks) {
            if(OXCache.modifiedCallbacks[i].module == oxMapping.module && !(OXCache.modifiedCallbacks[i].collection.criteria)) {
                for(var count=0;count<OXCache.modifiedCallbacks[i].collection.objects.length;count++) {
                    if(tmpKeyOld.equals(OXCache.modifiedCallbacks[i].collection.objects[count])) {
                        OXCache.modifiedCallbacks[i].modified=true; 
                    }
                }
            }
        }
        var tmpObjectOld=OXCache.cachedObjects.get(tmpKeyOld);
        if(tmpObjectOld) {
           if(changes) {
              for(var i in changes) {
                  if(!checkExist || tmpObjectOld[i]!== undefined) {
                      if(i != "notification") {
                          tmpObjectOld[i]=changes[i];
                      }
                  }
              }
           }
       }
       OXCache.cachedObjects.set(tmpKeyOld,tmpObjectOld);    
    }  
    //TODO CHECK CHANGE FOLDER
    tmpFn();
}
OXInfoStoreMapping.deleteObjects = function(objects) {
    var oxMapping=OXInfoStoreMapping;
    var request = new Array();
    for (var i in objects) {
        var tmpTime=objects[i]["timestamp"]; 
        if (!tmpTime) {
            var tmpObject = OXCache.cachedObjects.get(objects[i]);
            if(tmpObject) {
                tmpTime=tmpObject.timestamp;
            }
        }
		request.push({
            module: oxMapping.module, 
            action: "delete",
            timestamp: tmpTime,
            data: [{ folder : objects[i]["folder_id"] , id : objects[i]["id"] }]
        });
    }
    json.put(AjaxRoot + "/multiple?session=" + session + "&continue=true",
        request, null, function(reply) {
            var tmpFn=OXCache.join.add();
            var error=new Array();
            for(var i=0;i<reply.length;i++) {
                if(reply[i].error) {
                    newServerError(reply[i]);
                    error.push(i);   
                }  
            }
            if(!error.length) {
                oxMapping.deleteObjectsInternal(objects);
            }
            tmpFn();            
        }
    );
}
OXInfoStoreMapping.deleteObjectsInternal = function(objects) {
    var tmpFn=OXCache.join.add();
    var oxMapping=OXInfoStoreMapping;
    var tmpCollect=[];
    for(var mail=0;mail<objects.length;mail++) {
        var collections=oxMapping.getSubCollectionsfromObject(objects[mail],true);
        for(var count=0;count<collections.length;count++) {
            if(collections[count].check) { continue; } 
            var index=collections[count].map_objects.get(objects[mail]);
            if(index) {
                 collections[count].modified=true;
                 if(!collections[count].tmpIndexes) { collections[count].tmpIndexes=[]; }
                 collections[count].tmpIndexes.push(index);
                 tmpCollect.push(collections[count]);
                 collections[count].modifiedtmp=true;
             }               
        }
    }
    for(var count=0;count<tmpCollect.length;count++) {
        if(tmpCollect[count].modifiedtmp) {
                var indexes=tmpCollect[count].tmpIndexes;
                indexes.sort(function(a,b) { return a-b; });
                for(var i=indexes.length-1;i>=0;i--) {
                    tmpCollect[count].objects.splice(indexes[i]-1,1);
                }
                delete tmpCollect[count].tmpIndexes;
            tmpCollect[count].map_objects=new LRUKeyList();
            for(var index=0;index<tmpCollect[count].objects.length;index++) { 
                tmpCollect[count].map_objects.set(tmpCollect[count].objects[index],index+1) 
            }
            tmpCollect[count].modifiedtmp=false;
        }
    }
    tmpFn();
}
OXInfoStoreMapping.createObject = function(newObject) {
    setTimeout(function() {
        var tmpFn=OXCache.join.add();
        OXInfoStoreMapping.createObjectsInternal([newObject]);
        tmpFn();
    },0);
}
OXInfoStoreMapping.createObjectsInternal = function(newObjects) {
    setTimeout(function() {
        var oxMapping=OXContactMapping;
        var tmpFn=OXCache.join.add();
        for(var object in newObjects) {
            var collections=oxMapping.getSubCollectionsfromObject(newObjects[object],true);
            for(var i=0;i<collections.length;i++) {
                if(!collections[i].search && !collections[i].check) {
                    collections[i].check=true;
                }
            }
        }
        tmpFn();
    },0);
}
OXInfoStoreMapping.setTag = OXAbstractMapping.setTag(OXInfoStoreMapping);
OXCache.setMapping("infostore",OXInfoStoreMapping);
function OXAbstractObject() {   
    var Self=this;
    this.type="OXObject";
}
OXAbstractObject.prototype.hasColumns = function(stringobjects) {
	for(var i in stringobjects) {
		if(this[stringobjects[i]] === undefined) {
			return false;
		}
    }
	return true;
}
OXAbstractObject.prototype.getColumns = function() {
    var fields=new Array();
    if(this.mapping) {
	   for(var i in this.mapping.stringmapping) {
	       if(this.i) {
                filledfields.push(i);
            } 
        }
    }
	return filledfields;
}
function OXTaskObjectCache() {
    this.module="tasks";
	this.mapping=OXTaskMapping;
}
OXTaskObjectCache.prototype.equals=Key.prototype.equals;
OXTaskObjectCache.prototype.hashCode=Key.prototype.hashCode;
OXTaskObjectCache.prototype.hasColumns=OXAbstractObject.prototype.hasColumns;
OXTaskObjectCache.prototype.getColumns=OXAbstractObject.prototype.getColumns;
function OXCalendarObjectCache() {
    this.module="calendar";
	this.mapping=OXCalendarMapping;
}
OXCalendarObjectCache.prototype.equals=Key.prototype.equals;
OXCalendarObjectCache.prototype.hashCode=Key.prototype.hashCode;
OXCalendarObjectCache.prototype.hasColumns=OXAbstractObject.prototype.hasColumns;
OXCalendarObjectCache.prototype.getColumns=OXAbstractObject.prototype.getColumns;
function OXContactObjectCache() {
    this.module="contacts";
	this.mapping=OXContactMapping;
}
OXContactObjectCache.prototype.equals=Key.prototype.equals;
OXContactObjectCache.prototype.hashCode=Key.prototype.hashCode;
OXContactObjectCache.prototype.hasColumns=OXAbstractObject.prototype.hasColumns;
OXContactObjectCache.prototype.getColumns=OXAbstractObject.prototype.getColumns;
function OXMailObjectCache() {
    this.module="mail";
	this.mapping=OXMailMapping;
}
OXMailObjectCache.prototype.equals=Key.prototype.equals;
OXMailObjectCache.prototype.hashCode=Key.prototype.hashCode;

OXMailObjectCache.prototype.hasColumns=OXAbstractObject.prototype.hasColumns;
OXMailObjectCache.prototype.getColumns=OXAbstractObject.prototype.getColumns;
function OXInfoStoreObjectCache() {
    this.module="infostore";
	this.mapping=OXInfoStoreMapping;
}
OXInfoStoreObjectCache.prototype.equals=Key.prototype.equals;
OXInfoStoreObjectCache.prototype.hashCode=Key.prototype.hashCode;
OXInfoStoreObjectCache.prototype.hasColumns=OXAbstractObject.prototype.hasColumns;
OXInfoStoreObjectCache.prototype.getColumns=OXAbstractObject.prototype.getColumns;


  /////////////////////////////////////
 //   Tests and others              //
/////////////////////////////////////
var first=false;
function etest1(arg) {
    for(var i=0;i<arg;i++) {
    (new JSON()).put(AjaxRoot + "/tasks?action=new&session=" + session, {
        "title" : "Aufgabe Nr:"+i,
        "folder_id" : 3009
    }, null, function(cb){
    });
    }
}
var tmptime=new Date().getTime();
var times=new Array();   
var times2=new Array();  

register("OX_Refresh", function() {
	OXCache.update();
}) 
register("OX_Folder_Cleared",function(folder_id) {
	var tmpFn=OXCache.join.add();
	var allCollections=OXMailMapping.getSubCollectionsfromObject({ folder_id : folder_id });
	for (var i = 0; i < allCollections.length; i++) {
		OXMailMapping.deleteObjectsInternal(allCollections[i].objects);
	}	
	tmpFn();
});     
