cop.create("DevLayer").refineClass(lively.store.ObjectRepository, {
initialize: function(url) {
this.repoURL = url ? url : URL.root;
},
getRecords: function(querySpec, thenDo) {
var res = this.getServerInterfaceURL()
.withQuery({getRecords: encodeURIComponent(JSON.stringify(querySpec))})
.asWebResource().noProxy();
if (thenDo != null) {
res.withJSONWhenDone(function(json, status) {
if (JSON.prettyPrint(json) == "[]"){
throw new Error("Could not get records for:" + JSON.prettyPrint(querySpec))
}
thenDo(status.isSuccess() ? null : status, json); }).beAsync().get();
return this;
} else {
var content = res.beSync().get().content;
if (!res.status.isSuccess())
throw new Error(content);
var json;
try { json = JSON.parse(content); } catch(e) { json = {error: e} }
return json;
}
}
}).refineClass(lively.PartsBin.PartItem, {
guessRootForURL: function(url) {
if (url.isIn(URL.root)) {
return URL.root
}
if (url.pathname.match(/\/PartsBin\//)) {
return url.withPath(url.pathname.replace("PartsBin.*"))
}
return url.withPath("/")
},
loadPartVersions: function(isAsync) {
var url = this.getFileURL(),
root = this.guessRootForURL(url),
path = url.relativePathFrom(root),
self = this;
function processVersions(err, rows) {
if (err) show(err);
else self.partVersions = rows;
}
var result = new lively.store.ObjectRepository(root).getRecords({
paths: [path],
attributes: ['path', 'date', 'author', 'change', 'version']
}, isAsync ? processVersions : null);
if (!isAsync) processVersions(result.error ? result.error : null, result);
return this;
},
load: function(isAsync, rev) {
if(!isAsync){
var webR = new WebResource(this.getFileURL()).noProxy().forceUncached();
if (isAsync) webR.beAsync();
lively.bindings.connect(webR, 'content', this, 'json', {updater: function($upd, json) {
if (!this.sourceObj.status.isSuccess()) { $upd(null); return; }
if (!this.sourceObj.status.isDone()) { return; }
this.targetObj.lastModifiedDate = this.sourceObj.lastModified;
$upd(json);
}});
webR.get();
return this;
}
var url = this.getFileURL(),
root = this.guessRootForURL(url),
path = url.relativePathFrom(root),
self = this,
query = !!rev || rev === 0 ? {
paths: [path],
attributes: ['content'],
version: rev,
limit: 1
} : {
paths: [path],
attributes: ['content'],
newest: true,
limit: 1
};
new lively.store.ObjectRepository(root).getRecords(query, function(err, rows) {
if (err) { show(err); self.json = null; return; }
self.json = rows[0].content;
});
return this;
},
loadPartMetaInfo: function(isAsync, rev) {
if (!isAsync) {
var webR = new WebResource(this.getMetaInfoURL()).beSync();
webR.forceUncached().get();
if (webR.status.isSuccess()) {
var metaInfo = lively.persistence.Serializer.deserialize(webR.content);
metaInfo.lastModifiedDate = webR.lastModified;
this.loadedMetaInfo = metaInfo;
}
return this;
}
var url = this.getMetaInfoURL(),
root = this.guessRootForURL(url),
path = url.relativePathFrom(root),
self = this,
query = !!rev ? {
paths: [path],
attributes: ['content', 'date'],
version: rev,
limit: 1
} : {
paths: [path],
attributes: ['content', 'date'],
newest: true,
limit: 1
};
new lively.store.ObjectRepository(root).getRecords(query, function(err, rows) {
if (err) { show(err); self.loadedMetaInfo = null; return; }
var metaInfo = lively.persistence.Serializer.deserialize(rows[0].content)
metaInfo.lastModifiedDate = new Date(rows[0].date);
self.loadedMetaInfo = metaInfo;
});
},
}).beGlobal()