1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public License Version
5
* 1.1 (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
7
* http://www.mozilla.org/MPL/
9
* Software distributed under the License is distributed on an "AS IS" basis,
10
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
* for the specific language governing rights and limitations under the
14
* The Original Code is Weave.
16
* The Initial Developer of the Original Code is Mozilla.
17
* Portions created by the Initial Developer are Copyright (C) 2008
18
* the Initial Developer. All Rights Reserved.
21
* Dan Mills <thunder@mozilla.com>
23
* Alternatively, the contents of this file may be used under the terms of
24
* either the GNU General Public License Version 2 or later (the "GPL"), or
25
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26
* in which case the provisions of the GPL or the LGPL are applicable instead
27
* of those above. If you wish to allow use of your version of this file only
28
* under the terms of either the GPL or the LGPL, and not to allow others to
29
* use your version of this file under the terms of the MPL, indicate your
30
* decision by deleting the provisions above and replace them with the notice
31
* and other provisions required by the GPL or the LGPL. If you do not delete
32
* the provisions above, a recipient may use your version of this file under
33
* the terms of any one of the MPL, the GPL or the LGPL.
35
* ***** END LICENSE BLOCK ***** */
37
const EXPORTED_SYMBOLS = ['WBORecord', 'RecordManager', 'Records'];
39
const Cc = Components.classes;
40
const Ci = Components.interfaces;
41
const Cr = Components.results;
42
const Cu = Components.utils;
44
Cu.import("resource://weave/log4moz.js");
45
Cu.import("resource://weave/resource.js");
46
Cu.import("resource://weave/util.js");
48
function WBORecord(uri) {
54
WBORecord.prototype = {
55
_logName: "Record.WBO",
57
// NOTE: baseUri must have a trailing slash, or baseUri.resolve() will omit
58
// the collection name
60
return Utils.makeURI(this.baseUri.resolve(encodeURI(this.id)));
63
if (typeof(value) != "string")
65
let foo = value.split('/');
67
this.baseUri = Utils.makeURI(foo.join('/') + '/');
71
if (this.data.sortindex)
72
return this.data.sortindex;
76
deserialize: function deserialize(json) {
77
this.data = json.constructor.toString() == String ? JSON.parse(json) : json;
80
// The payload is likely to be JSON, but if not, keep it as a string
81
this.payload = JSON.parse(this.payload);
86
toJSON: function toJSON() {
87
// Copy fields from data to be stringified, making sure payload is a string
89
for (let [key, val] in Iterator(this.data))
90
obj[key] = key == "payload" ? JSON.stringify(val) : val;
94
toString: function WBORec_toString() "{ " + [
96
"index: " + this.sortindex,
97
"modified: " + this.modified,
98
"payload: " + JSON.stringify(this.payload)
102
Utils.deferGetSet(WBORecord, "data", ["id", "modified", "sortindex", "payload"]);
104
Utils.lazy(this, 'Records', RecordManager);
106
function RecordManager() {
107
this._log = Log4Moz.repository.getLogger(this._logName);
110
RecordManager.prototype = {
111
_recordType: WBORecord,
112
_logName: "RecordMgr",
114
import: function RecordMgr_import(url) {
115
this._log.trace("Importing record: " + (url.spec ? url.spec : url));
117
// Clear out the last response with empty object if GET fails
119
this.response = new Resource(url).get();
121
// Don't parse and save the record on failure
122
if (!this.response.success)
125
let record = new this._recordType();
126
record.deserialize(this.response);
129
return this.set(url, record);
132
this._log.debug("Failed to import record: " + Utils.exceptionStr(ex));
137
get: function RecordMgr_get(url) {
138
// Use a url string as the key to the hash
139
let spec = url.spec ? url.spec : url;
140
if (spec in this._records)
141
return this._records[spec];
142
return this.import(url);
145
set: function RegordMgr_set(url, record) {
146
let spec = url.spec ? url.spec : url;
147
return this._records[spec] = record;
150
contains: function RegordMgr_contains(url) {
151
if ((url.spec || url) in this._records)
156
clearCache: function recordMgr_clearCache() {
160
del: function RegordMgr_del(url) {
161
delete this._records[url];