From ef57b4e0167073d3f83b6ec1ca009de1baff3e2b Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sun, 24 May 2015 03:08:22 -0400 Subject: [PATCH] Relations fixes and cleanup Relations need a complete overhaul, but this makes them generally work again. --- chrome/content/zotero/xpcom/data/creators.js | 10 +- chrome/content/zotero/xpcom/data/group.js | 26 +- chrome/content/zotero/xpcom/data/relation.js | 260 ------------------ chrome/content/zotero/xpcom/data/relations.js | 117 ++------ components/zotero-service.js | 1 - test/tests/collectionTreeViewTest.js | 2 + 6 files changed, 44 insertions(+), 372 deletions(-) delete mode 100644 chrome/content/zotero/xpcom/data/relation.js diff --git a/chrome/content/zotero/xpcom/data/creators.js b/chrome/content/zotero/xpcom/data/creators.js index 7290f52146..e65d6e0507 100644 --- a/chrome/content/zotero/xpcom/data/creators.js +++ b/chrome/content/zotero/xpcom/data/creators.js @@ -28,7 +28,7 @@ Zotero.Creators = new function() { this.fields = ['firstName', 'lastName', 'fieldMode']; this.totes = 0; - var _creatorCache = {}; + var _cache = {}; /* * Returns creator data in internal format for a given creatorID @@ -38,8 +38,8 @@ Zotero.Creators = new function() { throw new Error("creatorID not provided"); } - if (_creatorCache[creatorID]) { - return this.cleanData(_creatorCache[creatorID]); + if (_cache[creatorID]) { + return this.cleanData(_cache[creatorID]); } var sql = "SELECT * FROM creators WHERE creatorID=?"; @@ -47,7 +47,7 @@ Zotero.Creators = new function() { if (!row) { throw new Error("Creator " + creatorID + " not found"); } - return _creatorCache[creatorID] = this.cleanData({ + return _cache[creatorID] = this.cleanData({ firstName: row.firstName, // avoid "DB column 'name' not found" warnings from the DB row Proxy lastName: row.lastName, fieldMode: row.fieldMode @@ -128,7 +128,7 @@ Zotero.Creators = new function() { if (toDelete.length) { // Clear creator entries in internal array for (let i=0; i. - - ***** END LICENSE BLOCK ***** -*/ - -Zotero.Relation = function () { - this._id = null; - this._libraryID = null; - this._subject = null; - this._predicate = null; - this._object = null; - this._clientDateModified = null; - - this._loaded = false; -} - -Zotero.Relation.prototype.__defineGetter__('objectType', function () 'relation'); -Zotero.Relation.prototype.__defineGetter__('id', function () this._id); -Zotero.Relation.prototype.__defineSetter__('id', function (val) { this._set('id', val); }); -Zotero.Relation.prototype.__defineGetter__('libraryID', function () this._get('libraryID')); -Zotero.Relation.prototype.__defineSetter__('libraryID', function (val) { return this._set('libraryID', val); }); -Zotero.Relation.prototype.__defineGetter__('key', function () this._id); -//Zotero.Relation.prototype.__defineSetter__('key', function (val) { this._set('key', val) }); -Zotero.Relation.prototype.__defineGetter__('dateModified', function () this._get('dateModified')); -Zotero.Relation.prototype.__defineGetter__('subject', function () this._get('subject')); -Zotero.Relation.prototype.__defineSetter__('subject', function (val) { this._set('subject', val); }); -Zotero.Relation.prototype.__defineGetter__('predicate', function () this._get('predicate')); -Zotero.Relation.prototype.__defineSetter__('predicate', function (val) { this._set('predicate', val); }); -Zotero.Relation.prototype.__defineGetter__('object', function () this._get('object')); -Zotero.Relation.prototype.__defineSetter__('object', function (val) { this._set('object', val); }); - - -Zotero.Relation.prototype._get = function (field) { - if (!this._loaded) { - throw new Error("Data not loaded for relation " + this._id); - } - return this['_' + field]; -} - - -Zotero.Relation.prototype._set = function (field, val) { - switch (field) { - case 'id': - case 'libraryID': - if (field == 'libraryID' && !val) { - throw new Error("libraryID cannot be empty in Zotero.Relation._set()"); - } - - if (val == this['_' + field]) { - return; - } - - if (this._loaded) { - throw ("Cannot set " + field + " after object is already loaded in Zotero.Relation._set()"); - } - - if (field == 'libraryID') { - val = parseInt(val); - } - this['_' + field] = val; - return; - } - - if (this.id) { - if (!this._loaded) { - this.load(); - } - } - else { - this._loaded = true; - } - - if (this['_' + field] != val) { - //this._prepFieldChange(field); - - switch (field) { - default: - this['_' + field] = val; - } - } -} - - -/** - * Check if search exists in the database - * - * @return bool TRUE if the relation exists, FALSE if not - */ -Zotero.Relation.prototype.exists = Zotero.Promise.coroutine(function* () { - if (this.id) { - var sql = "SELECT COUNT(*) FROM relations WHERE relationID=?"; - return !!(yield Zotero.DB.valueQueryAsync(sql, this.id)); - } - - if (this.libraryID && this.subject && this.predicate && this.object) { - var sql = "SELECT COUNT(*) FROM relations WHERE libraryID=? AND " - + "subject=? AND predicate=? AND object=?"; - var params = [this.libraryID, this.subject, this.predicate, this.object]; - return !!(yield Zotero.DB.valueQueryAsync(sql, params)); - } - - throw ("ID or libraryID/subject/predicate/object not set in Zotero.Relation.exists()"); -}); - - - -Zotero.Relation.prototype.load = Zotero.Promise.coroutine(function* () { - var id = this._id; - if (!id) { - throw new Error("ID not set"); - } - - var sql = "SELECT * FROM relations WHERE ROWID=?"; - var row = yield Zotero.DB.rowQueryAsync(sql, id); - if (!row) { - return; - } - - this._libraryID = row.libraryID; - this._subject = row.subject; - this._predicate = row.predicate; - this._object = row.object; - this._clientDateModified = row.clientDateModified; - this._loaded = true; - - return true; -}); - - -// TODO: async -Zotero.Relation.prototype.save = Zotero.Promise.coroutine(function* () { - if (this.id) { - throw ("Existing relations cannot currently be altered in Zotero.Relation.save()"); - } - - if (!this.subject) { - throw ("Missing subject in Zotero.Relation.save()"); - } - if (!this.predicate) { - throw ("Missing predicate in Zotero.Relation.save()"); - } - if (!this.object) { - throw ("Missing object in Zotero.Relation.save()"); - } - - Zotero.Relations.editCheck(this); - - var sql = "INSERT INTO relations " - + "(libraryID, subject, predicate, object, clientDateModified) " - + "VALUES (?, ?, ?, ?, ?)"; - try { - var insertID = yield Zotero.DB.queryAsync( - sql, - [ - this.libraryID, - this.subject, - this.predicate, - this.object, - Zotero.DB.transactionDateTime - ] - ); - } - catch (e) { - // If above failed, try deleting existing row, in case libraryID has changed - yield Zotero.DB.executeTransaction(function* () { - var sql2 = "SELECT COUNT(*) FROM relations WHERE subject=? AND predicate=? AND object=?"; - if (yield Zotero.DB.valueQueryAsync(sql2, [this.subject, this.predicate, this.object])) { - // Delete - sql2 = "DELETE FROM relations WHERE subject=? AND predicate=? AND object=?"; - yield Zotero.DB.queryAsync(sql2, [this.subject, this.predicate, this.object]); - - // Insert with original query - var insertID = yield Zotero.DB.queryAsync( - sql, - [ - this.libraryID, - this.subject, - this.predicate, - this.object, - Zotero.DB.transactionDateTime - ] - ); - } - }.bind(this)); - } - return insertID; -}); - - -Zotero.Relation.prototype.erase = Zotero.Promise.coroutine(function* () { - if (!this.id) { - throw ("ID not set in Zotero.Relation.erase()"); - } - - var deleteData = {}; - deleteData[this.id] = { - libraryID: this.libraryID, - key: Zotero.Utilities.Internal.md5(this.subject + "_" + this.predicate + "_" + this.object) - } - - var sql = "DELETE FROM relations WHERE ROWID=?"; - yield Zotero.DB.queryAsync(sql, [this.id]); - - Zotero.Notifier.trigger('delete', 'relation', [this.id], deleteData); -}); - - -Zotero.Relation.prototype.toXML = function (doc) { - var relationXML = doc.createElement('relation'); - relationXML.setAttribute('libraryID', this.libraryID); - - var elem = doc.createElement('subject'); - elem.appendChild(doc.createTextNode(this.subject)); - relationXML.appendChild(elem); - - var elem = doc.createElement('predicate'); - elem.appendChild(doc.createTextNode(this.predicate)); - relationXML.appendChild(elem); - - var elem = doc.createElement('object'); - elem.appendChild(doc.createTextNode(this.object)); - relationXML.appendChild(elem); - - return relationXML; -} - - -Zotero.Relation.prototype.serialize = function () { - // Use a hash of the parts as the object key - var key = Zotero.Utilities.Internal.md5(this.subject + "_" + this.predicate + "_" + this.object); - - var obj = { - libraryID: this.libraryID, - key: key, - subject: this.subject, - predicate: this.predicate, - object: this.object - }; - return obj; -} diff --git a/chrome/content/zotero/xpcom/data/relations.js b/chrome/content/zotero/xpcom/data/relations.js index 3538301135..4ba998e17c 100644 --- a/chrome/content/zotero/xpcom/data/relations.js +++ b/chrome/content/zotero/xpcom/data/relations.js @@ -23,25 +23,15 @@ ***** END LICENSE BLOCK ***** */ -Zotero.Relations = function () { - this.constructor = null; - - this._ZDO_object = 'relation'; - this._ZDO_idOnly = true; - +Zotero.Relations = new function () { Zotero.defineProperty(this, 'relatedItemPredicate', {value: 'dc:relation'}); Zotero.defineProperty(this, 'linkedObjectPredicate', {value: 'owl:sameAs'}); Zotero.defineProperty(this, 'deletedItemPredicate', {value: 'dc:isReplacedBy'}); - this.get = function (id) { - if (typeof id != 'number') { - throw ("id '" + id + "' must be an integer in Zotero.Relations.get()"); - } - - var relation = new Zotero.Relation; - relation.id = id; - return relation; - } + this._namespaces = { + dc: 'http://purl.org/dc/elements/1.1/', + owl: 'http://www.w3.org/2002/07/owl#' + }; /** @@ -71,19 +61,15 @@ Zotero.Relations = function () { params.push(object); } var rows = yield Zotero.DB.columnQueryAsync(sql, params); - if (!rows) { - return []; - } - var toReturn = []; - var loads = []; for (let i=0; i