all files / src/ Container.js

47.45% Statements 65/137
25% Branches 14/56
38.1% Functions 8/21
47.45% Lines 65/137
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261                  262× 262× 262× 262× 262×       262×                                                                                                                                                                                                                                                                                                                                                                                
'use strict';
 
var log = require('./Log.js').Logger('libZotero:Container');
 
module.exports = function(){
	
};
 
module.exports.prototype.initSecondaryData = function(){
	
};
 
module.exports.prototype.addObject = function(object){
	log.debug('Zotero.Container.addObject', 4);
	var container = this;
	container.objectArray.push(object);
	container.objectMap[object.key] = object;
	Iif(container.owningLibrary){
		object.associateWithLibrary(container.owningLibrary);
	}
	
	return container;
};
 
module.exports.prototype.fieldComparer = function(field){
	if(Intl){
		var collator = new Intl.Collator();
		return function(a, b){
			return collator.compare(a.apiObj.data[field], b.apiObj.data[field]);
		};
	} else {
		return function(a, b){
			if(a.apiObj.data[field].toLowerCase() == b.apiObj.data[field].toLowerCase()){
				return 0;
			}
			if(a.apiObj.data[field].toLowerCase() < b.apiObj.data[field].toLowerCase()){
				return -1;
			}
			return 1;
		};
	}
};
 
module.exports.prototype.getObject = function(key){
	var container = this;
	Eif(container.objectMap.hasOwnProperty(key)){
		return container.objectMap[key];
	}
	else{
		return false;
	}
};
 
module.exports.prototype.getObjects = function(keys){
	var container = this;
	var objects = [];
	var object;
	for(var i = 0; i < keys.length; i++){
		object = container.getObject(keys[i]);
		if(object){
			objects.push(object);
		}
	}
	return objects;
};
 
module.exports.prototype.removeObject = function(key){
	var container = this;
	if(container.objectMap.hasOwnProperty(key)){
		delete container.objectmap[key];
		container.initSecondaryData();
	}
};
 
module.exports.prototype.removeObjects = function(keys){
	var container = this;
	//delete Objects from objectMap;
	for(var i = 0; i < keys.length; i++){
		delete container.objectMap[keys[i]];
	}
	
	//rebuild array
	container.initSecondaryData();
};
 
module.exports.prototype.writeObjects = function(objects){
	//TODO:implement
};
 
//generate keys for objects about to be written if they are new
module.exports.prototype.assignKeys = function(objectsArray){
	var object;
	for(var i = 0; i < objectsArray.length; i++){
		object = objectsArray[i];
		var key = object.get('key');
		if(!key) {
			var newObjectKey = Zotero.utils.getKey();
			object.set('key', newObjectKey);
			object.set('version', 0);
		}
	}
	return objectsArray;
};
 
//split an array of objects into chunks to write over multiple api requests
module.exports.prototype.chunkObjectsArray = function(objectsArray){
	var chunkSize = 50;
	var writeChunks = [];
	
	for(var i = 0; i < objectsArray.length; i = i + chunkSize){
		writeChunks.push(objectsArray.slice(i, i+chunkSize));
	}
	
	return writeChunks;
};
 
module.exports.prototype.rawChunks = function(chunks){
	var rawChunkObjects = [];
	
	for(var i = 0; i < chunks.length; i++){
		rawChunkObjects[i] = [];
		for(var j = 0; j < chunks[i].length; j++){
			rawChunkObjects[i].push(chunks[i][j].writeApiObj());
		}
	}
	return rawChunkObjects;
};
 
/**
 * Update syncState property on container to keep track of updates that occur during sync process.
 * Set earliestVersion to MIN(earliestVersion, version).
 * Set latestVersion to MAX(latestVersion, version).
 * This should be called with the modifiedVersion header for each response tied to this container
 * during a sync process.
 * @param  {int} version
 * @return {null}
 */
module.exports.prototype.updateSyncState = function(version) {
	var container = this;
	log.debug('updateSyncState: ' + version, 3);
	if(!container.hasOwnProperty('syncState')){
		log.debug('no syncState property');
		throw new Error('Attempt to update sync state of object with no syncState property');
	}
	if(container.syncState.earliestVersion === null){
		container.syncState.earliestVersion = version;
	}
	if(container.syncState.latestVersion === null){
		container.syncState.latestVersion = version;
	}
	if(version < container.syncState.earliestVersion){
		container.syncState.earliestVersion = version;
	}
	if(version > container.syncState.latestVersion){
		container.syncState.latestVersion = version;
	}
	log.debug('done updating sync state', 3);
};
 
module.exports.prototype.updateSyncedVersion = function(versionField) {
	var container = this;
	if(container.syncState.earliestVersion !== null &&
		(container.syncState.earliestVersion == container.syncState.latestVersion) ){
		container.version = container.syncState.latestVersion;
		container.synced = true;
	}
	else if(container.syncState.earliestVersion !== null) {
		container.version = container.syncState.earliestVersion;
	}
};
 
module.exports.prototype.processDeletions = function(deletedKeys) {
	var container = this;
	for(var i = 0; i < deletedKeys.length; i++){
		var localObject = container.get(deletedKeys[i]);
		if(localObject !== false){
			//still have object locally
			if(localObject.synced === true){
				//our object is not modified, so delete it as the server thinks we should
				container.removeObjects([deletedKeys[i]]);
			}
			else {
				//TODO: conflict resolution
			}
		}
	}
};
 
//update items appropriately based on response to multi-write request
//for success:
//  update objectKey if item doesn't have one yet (newly created item)
//  update itemVersion to response's Last-Modified-Version header
//  mark as synced
//for unchanged:
//  don't need to do anything? itemVersion should remain the same?
//  mark as synced if not already?
//for failed:
//  add the failure to the object under writeFailure
//  don't mark as synced
//  calling code should check for writeFailure after the written objects
//  are returned
module.exports.prototype.updateObjectsFromWriteResponse = function(objectsArray, response){
	log.debug('Zotero.Container.updateObjectsFromWriteResponse', 3);
	log.debug('statusCode: ' + response.status, 3);
	return new Promise((resolve, reject) => {
		Eif(response.status == 200) {
			response.json().then(data => {
				let lastModifiedVersion = response.headers.get('Last-Modified-Version');
				log.debug('newLastModifiedVersion: ' + lastModifiedVersion, 3);
				//make sure writes were actually successful and
				//update the itemKey for the parent
				if(data.hasOwnProperty('success') && Object.keys(data.success).length) {
					//update each successfully written item, possibly with new itemKeys
					Object.keys(data.success).forEach(function(ind){
						var i = parseInt(ind, 10);
						var key = data.success[ind];
						var object = objectsArray[i];
						//throw error if objectKey mismatch
						Iif(object.key !== '' && object.key !== key){
							throw new Error('object key mismatch in multi-write response');
						}
						Iif(object.key === ''){
							object.updateObjectKey(key);
						}
						object.set('version', lastModifiedVersion);
						object.synced = true;
						object.writeFailure = false;
					});
					resolve();
				} else Eif(data.hasOwnProperty('failed') && Object.keys(data.failed).length) {
					log.debug('updating objects with failed writes', 3);
					Object.keys(data.failed).forEach(function(ind){
						var failure = data.failed[ind];
						log.error('failed write ' + ind + ' - ' + failure);
						var i = parseInt(ind, 10);
						var object = objectsArray[i];
						object.writeFailure = failure;
					});
					reject();
				} else {
					resolve();
				}
			});
		}
		else if(response.status == 204){
			//single item put response, this probably should never go to this function
			objectsArray[0].synced = true;
			resolve();
		}
	});
};
 
//return the key as a string when passed an argument that 
//could be either a string key or an object with a key property
module.exports.prototype.extractKey = function(object){
	if(typeof object == 'string'){
		return object;
	}
	return object.get('key');
};