From 83216d6125a8cd3f1800943f1a6646cf2c21ef46 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 7 Nov 2011 13:32:19 -0500 Subject: [PATCH] Relations libraryID fixes - For cross-library links, if both group libraries, store relation with source group. Otherwise, store with personal library. - Send libraryID when syncing - Update libraryID properly when syncing updated relation from server - Increment sync API version so server can send relation libraryIDs safely --- .../content/zotero/xpcom/data/collection.js | 7 ++- chrome/content/zotero/xpcom/data/item.js | 7 ++- chrome/content/zotero/xpcom/data/relation.js | 51 ++++++++++++++----- chrome/content/zotero/xpcom/sync.js | 5 +- 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js index 0e2a90c1fb..985dcbef34 100644 --- a/chrome/content/zotero/xpcom/data/collection.js +++ b/chrome/content/zotero/xpcom/data/collection.js @@ -1180,7 +1180,12 @@ Zotero.Collection.prototype.addLinkedCollection = function (collection) { Zotero.debug("Collections " + this.key + " and " + collection.key + " are already linked"); return false; } - Zotero.Relations.add(null, url1, predicate, url2); + + // If both group libraries, store relation with source group. + // Otherwise, store with personal library. + var libraryID = (this.libraryID && item.libraryID) ? this.libraryID : null; + + Zotero.Relations.add(libraryID, url1, predicate, url2); } // diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index db02ae09b1..5523683ec5 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -3621,7 +3621,12 @@ Zotero.Item.prototype.addLinkedItem = function (item) { Zotero.debug("Items " + this.key + " and " + item.key + " are already linked"); return false; } - Zotero.Relations.add(null, url1, predicate, url2); + + // If both group libraries, store relation with source group. + // Otherwise, store with personal library. + var libraryID = (this.libraryID && item.libraryID) ? this.libraryID : null; + + Zotero.Relations.add(libraryID, url1, predicate, url2); } diff --git a/chrome/content/zotero/xpcom/data/relation.js b/chrome/content/zotero/xpcom/data/relation.js index 9c1e81b34c..c6997732ea 100644 --- a/chrome/content/zotero/xpcom/data/relation.js +++ b/chrome/content/zotero/xpcom/data/relation.js @@ -158,20 +158,46 @@ Zotero.Relation.prototype.save = function () { throw ("Missing object in Zotero.Relation.save()"); } - var sql = "INSERT OR IGNORE INTO relations " + var sql = "INSERT INTO relations " + "(libraryID, subject, predicate, object, clientDateModified) " + "VALUES (?, ?, ?, ?, ?)"; - var insertID = Zotero.DB.query( - sql, - [ - this.libraryID, - this.subject, - this.predicate, - this.object, - Zotero.DB.transactionDateTime - ] - ); - + try { + var insertID = Zotero.DB.query( + 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 + Zotero.DB.beginTransaction(); + + var sql2 = "SELECT COUNT(*) FROM relations WHERE subject=? AND predicate=? AND object=?"; + if (Zotero.DB.valueQuery(sql2, [this.subject, this.predicate, this.object])) { + // Delete + sql2 = "DELETE FROM relations WHERE subject=? AND predicate=? AND object=?"; + Zotero.DB.query(sql2, [this.subject, this.predicate, this.object]); + + // Insert with original query + var insertID = Zotero.DB.query( + sql, + [ + this.libraryID, + this.subject, + this.predicate, + this.object, + Zotero.DB.transactionDateTime + ] + ); + } + + Zotero.DB.commitTransaction(); + } return insertID; } @@ -199,6 +225,7 @@ Zotero.Relation.prototype.erase = function () { Zotero.Relation.prototype.toXML = function () { var xml = ; + xml.@libraryID = this.libraryID; xml.subject = this.subject; xml.predicate = this.predicate; xml.object = this.object; diff --git a/chrome/content/zotero/xpcom/sync.js b/chrome/content/zotero/xpcom/sync.js index 310b746f85..87b4ecee3b 100644 --- a/chrome/content/zotero/xpcom/sync.js +++ b/chrome/content/zotero/xpcom/sync.js @@ -1209,7 +1209,7 @@ Zotero.Sync.Server = new function () { this.manualSyncRequired = false; this.upgradeRequired = false; this.nextLocalSyncDate = false; - this.apiVersion = 8; + this.apiVersion = 9; default xml namespace = ''; @@ -2203,7 +2203,8 @@ Zotero.Sync.Server = new function () { Zotero.userID = userID; Zotero.libraryID = libraryID; - // Update userID in relations + // Update libraryID in relations, which we store for the local + // for some reason. All other objects use null for the local library. if (lastUserID && lastLibraryID) { Zotero.Relations.updateUser(lastUserID, lastLibraryID, userID, libraryID);