www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

libraries.js (9533B)


      1 /*
      2     ***** BEGIN LICENSE BLOCK *****
      3     
      4     Copyright © 2009 Center for History and New Media
      5                      George Mason University, Fairfax, Virginia, USA
      6                      http://zotero.org
      7     
      8     This file is part of Zotero.
      9     
     10     Zotero is free software: you can redistribute it and/or modify
     11     it under the terms of the GNU Affero General Public License as published by
     12     the Free Software Foundation, either version 3 of the License, or
     13     (at your option) any later version.
     14     
     15     Zotero is distributed in the hope that it will be useful,
     16     but WITHOUT ANY WARRANTY; without even the implied warranty of
     17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18     GNU Affero General Public License for more details.
     19     
     20     You should have received a copy of the GNU Affero General Public License
     21     along with Zotero.  If not, see <http://www.gnu.org/licenses/>.
     22     
     23     ***** END LICENSE BLOCK *****
     24 */
     25 
     26 Zotero.Libraries = new function () {
     27 	let _userLibraryID;
     28 	Zotero.defineProperty(this, 'userLibraryID', {
     29 		get: function() { 
     30 			if (_userLibraryID === undefined) {
     31 				throw new Error("Library data not yet loaded");
     32 			}
     33 			return _userLibraryID;
     34 		}
     35 	});
     36 	
     37 	Zotero.defineProperty(this, 'userLibrary', {
     38 		get: function () {
     39 			return Zotero.Libraries.get(_userLibraryID);
     40 		}
     41 	})
     42 	
     43 	/**
     44 	 * Manage cache
     45 	 */
     46 	this._cache = null;
     47 	
     48 	this._makeCache = function() {
     49 		return {};
     50 	}
     51 	
     52 	this.register = function(library) {
     53 		if (!this._cache) throw new Error("Zotero.Libraries cache is not initialized");
     54 		Zotero.debug("Zotero.Libraries: Registering library " + library.libraryID, 5);
     55 		this._addToCache(this._cache, library);
     56 	};
     57 	
     58 	this._addToCache = function(cache, library) {
     59 		if (!library.libraryID) throw new Error("Cannot register an unsaved library");
     60 		cache[library.libraryID] = library;
     61 	}
     62 	
     63 	this.unregister = function(libraryID) {
     64 		if (!this._cache) throw new Error("Zotero.Libraries cache is not initialized");
     65 		Zotero.debug("Zotero.Libraries: Unregistering library " + libraryID, 5);
     66 		delete this._cache[libraryID];
     67 	};
     68 	
     69 	/**
     70 	 * Loads all libraries from DB. Groups, Feeds, etc. should not maintain an
     71 	 * independent cache.
     72 	 */
     73 	this.init = Zotero.Promise.coroutine(function* () {
     74 		let specialLoading = ['feed', 'group'];
     75 		
     76 		// Invalidate caches until we're done loading everything
     77 		let libTypes = ['library'].concat(specialLoading);
     78 		let newCaches = {};
     79 		for (let i=0; i<libTypes.length; i++) {
     80 			let objs = Zotero.DataObjectUtilities.getObjectsClassForObjectType(libTypes[i]);
     81 			delete objs._cache;
     82 			
     83 			newCaches[libTypes[i]] = objs._makeCache();
     84 		}
     85 		
     86 		let sql = Zotero.Library._rowSQL
     87 			// Exclude libraries that require special loading
     88 			+ " WHERE type NOT IN "
     89 			+ "(" + Array(specialLoading.length).fill('?').join(',') + ")";
     90 		let rows = yield Zotero.DB.queryAsync(sql, specialLoading);
     91 		
     92 		for (let i=0; i<rows.length; i++) {
     93 			let row = rows[i];
     94 			
     95 			let library;
     96 			switch (row._libraryType) {
     97 				case 'user':
     98 					library = new Zotero.Library();
     99 					library._loadDataFromRow(row); // Does not call save()
    100 					break;
    101 				default:
    102 					throw new Error('Unhandled library type "' + row._libraryType + '"');
    103 			}
    104 			
    105 			if (library.libraryType == 'user') {
    106 				_userLibraryID = library.libraryID;
    107 			}
    108 			
    109 			this._addToCache(newCaches.library, library);
    110 		}
    111 		
    112 		// Load other libraries
    113 		for (let i=0; i<specialLoading.length; i++) {
    114 			let libType = specialLoading[i];
    115 			let LibType = Zotero.Utilities.capitalize(libType);
    116 			
    117 			let libs = yield Zotero.DB.queryAsync(Zotero[LibType]._rowSQL);
    118 			for (let j=0; j<libs.length; j++) {
    119 				let lib = new Zotero[LibType]();
    120 				lib._loadDataFromRow(libs[j]);
    121 				
    122 				this._addToCache(newCaches.library, lib);
    123 				Zotero[lib._ObjectTypePlural]._addToCache(newCaches[libType], lib);
    124 			}
    125 		}
    126 		
    127 		// Set new caches
    128 		for (let libType in newCaches) {
    129 			Zotero.DataObjectUtilities.getObjectsClassForObjectType(libType)
    130 				._cache = newCaches[libType];
    131 		}
    132 	});
    133 	
    134 	/**
    135 	 * @param {Integer} libraryID
    136 	 * @return {Boolean}
    137 	 */
    138 	this.exists = function(libraryID) {
    139 		if (!this._cache) throw new Error("Zotero.Libraries cache is not initialized");
    140 		return this._cache[libraryID] !== undefined;
    141 	}
    142 	
    143 	
    144 	this._ensureExists = function(libraryID) {
    145 		if (!this.exists(libraryID)) {
    146 			throw new Error("Invalid library ID " + libraryID);
    147 		}
    148 	}
    149 	
    150 	
    151 	/**
    152 	 * @return {Zotero.Library[]} - All libraries
    153 	 */
    154 	this.getAll = function () {
    155 		if (!this._cache) throw new Error("Zotero.Libraries cache is not initialized");
    156 		var libraries = Object.keys(this._cache).map(v => Zotero.Libraries.get(parseInt(v)));
    157 		var collation = Zotero.getLocaleCollation();
    158 		// Sort My Library, then others by name
    159 		libraries.sort(function (a, b) {
    160 			if (a.libraryID == _userLibraryID) return -1;
    161 			if (b.libraryID == _userLibraryID) return 1;
    162 			return collation.compareString(1, a.name, b.name);
    163 		}.bind(this))
    164 		return libraries;
    165 	}
    166 	
    167 	
    168 	/**
    169 	 * Get an existing library
    170 	 *
    171 	 * @param {Integer} libraryID
    172 	 * @return {Zotero.Library[] | Zotero.Library}
    173 	 */
    174 	this.get = function(libraryID) {
    175 		return this._cache[libraryID] || false;
    176 	}
    177 	
    178 	
    179 	/**
    180 	 * @deprecated
    181 	 */
    182 	this.getName = function (libraryID) {
    183 		Zotero.debug("Zotero.Libraries.getName() is deprecated. Use Zotero.Library.prototype.name instead");
    184 		this._ensureExists(libraryID);
    185 		return Zotero.Libraries.get(libraryID).name;
    186 	}
    187 	
    188 	
    189 	/**
    190 	 * @deprecated
    191 	 */
    192 	this.getType = function (libraryID) {
    193 		Zotero.debug("Zotero.Libraries.getType() is deprecated. Use Zotero.Library.prototype.libraryType instead");
    194 		this._ensureExists(libraryID);
    195 		return Zotero.Libraries.get(libraryID).libraryType;
    196 	}
    197 	
    198 	
    199 	/**
    200 	 * @deprecated
    201 	 * 
    202 	 * @param {Integer} libraryID
    203 	 * @return {Integer}
    204 	 */
    205 	this.getVersion = function (libraryID) {
    206 		Zotero.debug("Zotero.Libraries.getVersion() is deprecated. Use Zotero.Library.prototype.libraryVersion instead");
    207 		this._ensureExists(libraryID);
    208 		return Zotero.Libraries.get(libraryID).libraryVersion;
    209 	}
    210 	
    211 	
    212 	/**
    213 	 * @deprecated
    214 	 *
    215 	 * @param {Integer} libraryID
    216 	 * @param {Integer} version
    217 	 * @return {Promise}
    218 	 */
    219 	this.setVersion = Zotero.Promise.method(function(libraryID, version) {
    220 		Zotero.debug("Zotero.Libraries.setVersion() is deprecated. Use Zotero.Library.prototype.libraryVersion instead");
    221 		this._ensureExists(libraryID);
    222 		
    223 		let library = Zotero.Libraries.get(libraryID);
    224 		library.libraryVersion = version;
    225 		return library.saveTx();
    226 	});
    227 	
    228 	/**
    229 	 * @deprecated
    230 	 */
    231 	this.getLastSyncTime = function (libraryID) {
    232 		Zotero.debug("Zotero.Libraries.getLastSyncTime() is deprecated. Use Zotero.Library.prototype.lastSync instead");
    233 		this._ensureExists(libraryID);
    234 		return Zotero.Libraries.get(libraryID).lastSync;
    235 	};
    236 	
    237 	
    238 	/**
    239 	 * @deprecated
    240 	 * 
    241 	 * @param {Integer} libraryID
    242 	 * @param {Date} lastSyncTime
    243 	 * @return {Promise}
    244 	 */
    245 	this.setLastSyncTime = Zotero.Promise.method(function (libraryID, lastSyncTime) {
    246 		Zotero.debug("Zotero.Libraries.setLastSyncTime() is deprecated. Use Zotero.Library.prototype.lastSync instead");
    247 		this._ensureExists(libraryID);
    248 		
    249 		let library = Zotero.Libraries.get(libraryID);
    250 		library.lastSync = lastSyncTime;
    251 		return library.saveTx();
    252 	});
    253 	
    254 	/**
    255 	 * @deprecated
    256 	 */
    257 	this.isEditable = function (libraryID) {
    258 		Zotero.debug("Zotero.Libraries.isEditable() is deprecated. Use Zotero.Library.prototype.editable instead");
    259 		this._ensureExists(libraryID);
    260 		return Zotero.Libraries.get(libraryID).editable;
    261 	}
    262 	
    263 	/**
    264 	 * @deprecated
    265 	 *
    266 	 * @return {Promise}
    267 	 */
    268 	this.setEditable = Zotero.Promise.method(function(libraryID, editable) {
    269 		Zotero.debug("Zotero.Libraries.setEditable() is deprecated. Use Zotero.Library.prototype.editable instead");
    270 		this._ensureExists(libraryID);
    271 		
    272 		let library = Zotero.Libraries.get(libraryID);
    273 		library.editable = editable;
    274 		return library.saveTx();
    275 	});
    276 	
    277 	/**
    278 	 * @deprecated
    279 	 */
    280 	this.isFilesEditable = function (libraryID) {
    281 		Zotero.debug("Zotero.Libraries.isFilesEditable() is deprecated. Use Zotero.Library.prototype.filesEditable instead");
    282 		this._ensureExists(libraryID);
    283 		return Zotero.Libraries.get(libraryID).filesEditable;
    284 	};
    285 	
    286 	/**
    287 	 * @deprecated
    288 	 * 
    289 	 * @return {Promise}
    290 	 */
    291 	this.setFilesEditable = Zotero.Promise.coroutine(function* (libraryID, filesEditable) {
    292 		Zotero.debug("Zotero.Libraries.setFilesEditable() is deprecated. Use Zotero.Library.prototype.filesEditable instead");
    293 		this._ensureExists(libraryID);
    294 		
    295 		let library = Zotero.Libraries.get(libraryID);
    296 		library.filesEditable = filesEditable;
    297 		return library.saveTx();
    298 	});
    299 	
    300 	/**
    301 	 * @deprecated
    302 	 */
    303 	this.isGroupLibrary = function (libraryID) {
    304 		Zotero.debug("Zotero.Libraries.isGroupLibrary() is deprecated. Use Zotero.Library.prototype.isGroup instead");
    305 		this._ensureExists(libraryID);
    306 		return !!Zotero.Libraries.get(libraryID).isGroup;
    307 	}
    308 	
    309 	/**
    310 	 * @deprecated
    311 	 */
    312 	this.hasTrash = function (libraryID) {
    313 		Zotero.debug("Zotero.Libraries.hasTrash() is deprecated. Use Zotero.Library.prototype.hasTrash instead");
    314 		this._ensureExists(libraryID);
    315 		return Zotero.Libraries.get(libraryID).hasTrash;
    316 	}
    317 	
    318 	/**
    319 	 * @deprecated
    320 	 */
    321 	this.updateLastSyncTime = Zotero.Promise.method(function(libraryID) {
    322 		Zotero.debug("Zotero.Libraries.updateLastSyncTime() is deprecated. Use Zotero.Library.prototype.updateLastSyncTime instead");
    323 		this._ensureExists(libraryID);
    324 		
    325 		let library = Zotero.Libraries.get(libraryID);
    326 		library.updateLastSyncTime();
    327 		return library.saveTx()
    328 			.return();
    329 	})
    330 }