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
This commit is contained in:
Dan Stillman 2011-11-07 13:32:19 -05:00
parent 7338e5e40f
commit 83216d6125
4 changed files with 54 additions and 16 deletions

View file

@ -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);
}
//

View file

@ -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);
}

View file

@ -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 = <relation/>;
xml.@libraryID = this.libraryID;
xml.subject = this.subject;
xml.predicate = this.predicate;
xml.object = this.object;

View file

@ -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);