first version of erp5 to erp5 storage
Showing
/* | ||
* JIO extension for resource replication. | ||
* Copyright (C) 2013, 2015 Nexedi SA | ||
* | ||
* This library is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/*jslint nomen: true*/ | ||
/*global jIO, RSVP, console, indexedDB*/ | ||
var DATABASE = "erp5toerp5"; | ||
(function (jIO, RSVP) { | ||
"use strict"; | ||
function MappingStorage(spec) { | ||
this._local_mapping = {}; | ||
this._local_sub_storage = jIO.createJIO(spec.sub_storage); | ||
this._portal_type = spec.portal_type; | ||
this._index = spec.index; | ||
} | ||
MappingStorage.prototype.get = function (data) { | ||
console.log("MappingStorage.get data : " + data); | ||
if (!data.startsWith('_replicate_')) { | ||
data = this._local_mapping[data]; | ||
} | ||
return this._local_sub_storage.get(data); | ||
}; | ||
MappingStorage.prototype.put = function () { | ||
console.log("MappingStorage.put arguments " + arguments); | ||
return this._local_sub_storage.put.apply(this._local_sub_storage, | ||
arguments); | ||
}; | ||
MappingStorage.prototype.allAttachments = function () { | ||
return this._local_sub_storage.allAttachments.apply(this._local_sub_storage, | ||
arguments); | ||
}; | ||
MappingStorage.prototype.hasCapacity = function () { | ||
return this._local_sub_storage.hasCapacity.apply(this._local_sub_storage, | ||
arguments); | ||
}; | ||
MappingStorage.prototype.buildQuery = function () { | ||
console.log("MappingStorage.buildQuery"); | ||
var context = this, result_list = [], i; | ||
return this._local_sub_storage.allDocs( | ||
{ | ||
select_list : [context._index], //, | ||
query : 'portal_type:"' + context._portal_type + '"' | ||
} | ||
) | ||
.push(function (result) { | ||
for (i = 0; i < result.data.total_rows; i += 1) { | ||
result_list.push({id: result.data.rows[i].value.reference, | ||
value: ''}); | ||
context._local_mapping[result.data.rows[i].value.reference] = | ||
result.data.rows[i].id; | ||
} | ||
console.log("MappingStorage.buildQuery result_list " + result_list); | ||
console.log("MappingStorage.buildQuery local mapping " + | ||
context._local_mapping); | ||
return result_list; | ||
}); | ||
}; | ||
MappingStorage.prototype.remove = function (data) { | ||
console.log("MappingStorage.remove data " + data); | ||
//return this._local_sub_storage.remove.apply(this._local_sub_storage, | ||
// [data,]); | ||
}; | ||
MappingStorage.prototype.removeAttachment = function (data) { | ||
console.log("MappingStorage.removeAttachment data " + data); | ||
//return this._local_sub_storage.remove.apply(this._local_sub_storage, | ||
// [data,]); | ||
}; | ||
MappingStorage.prototype.putAttachment = function (data) { | ||
console.log("MappingStorage.putAttachment data " + data); | ||
//return this._local_sub_storage.remove.apply(this._local_sub_storage, | ||
// [data,]); | ||
}; | ||
function QueryERP5Storage(spec) { | ||
this._local_sub_storage = jIO.createJIO(spec.sub_storage); | ||
this._index = spec.index; | ||
this._source_portal_type = spec.source_portal_type; | ||
this._source_parent_relative_url = spec.source_parent_relative_url; | ||
this._destination_portal_type = spec.destination_portal_type; | ||
this._destination_parent_relative_url = | ||
spec.destination_parent_relative_url; | ||
this._local_mapping = {}; | ||
} | ||
QueryERP5Storage.prototype.get = function (data) { | ||
|
||
var context = this; | ||
console.log("QueryERP5.get data : " + data); | ||
return this._local_sub_storage.allDocs({ | ||
query : 'portal_type:"' + this._destination_portal_type + | ||
'" AND ' + this._index + ':"' + data + '"' | ||
}) | ||
.push(function (result) { | ||
if (result.data.total_rows > 0) { | ||
console.log("QueryERP5.get.push " + result.data.rows[0].id); | ||
context._local_mapping[data] = result.data.rows[0].id; | ||
return result.data.rows[0].id; | ||
} | ||
return; | ||
}) | ||
.push(function (id) { | ||
if (id) { | ||
return context._local_sub_storage.get(id); | ||
} | ||
}).push(function (data) { | ||
if (data) { | ||
data.portal_type = context._source_portal_type; | ||
data.parent_relative_url = context._source_parent_relative_url; | ||
} | ||
return data; | ||
}); | ||
}; | ||
QueryERP5Storage.prototype.post = function (data) { | ||
data.portal_type = this._destination_portal_type; | ||
data.parent_relative_url = this._destination_parent_relative_url; | ||
console.log("QuertERP5Storage.post data " + data); | ||
return this._local_sub_storage.post(data); | ||
}; | ||
QueryERP5Storage.prototype.put = function (id, data) { | ||
id = this._local_mapping[id]; | ||
console.log("QuertERP5Storage.put id " + id + ", data " + data); | ||
return this._local_sub_storage.put(id, data); | ||
}; | ||
jIO.addStorage('mapping_storage', MappingStorage); | ||
jIO.addStorage('query_erp5_storage', QueryERP5Storage); | ||
function ERP5ToERP5Storage(spec) { | ||
indexedDB.deleteDatabase('jio:' + DATABASE); | ||
this._locale_jio_instance = jIO.createJIO({ | ||
type: "replicate", | ||
query: spec.source_query, | ||
use_remote_post: true, | ||
conflict_handling: 1, | ||
check_local_modification: false, | ||
check_local_creation: false, | ||
check_local_deletion: false, | ||
check_remote_modification: false, | ||
check_remote_creation: true, | ||
check_remote_deletion: false, | ||
local_sub_storage: { | ||
type: "query", | ||
sub_storage: { | ||
type: "uuid", | ||
sub_storage: { | ||
type: "indexeddb", | ||
database: DATABASE | ||
} | ||
} | ||
}, | ||
remote_sub_storage: { | ||
type: "erp5", | ||
url: spec.source_url, | ||
default_view_reference: spec.source_view_reference | ||
} | ||
}); | ||
this._remote_jio_instance = jIO.createJIO({ | ||
type: "replicate", | ||
query: { | ||
query: 'portal_type: "' + spec.source_portal_type + '"' | ||
}, | ||
use_remote_post: 1, | ||
conflict_handling: 1, | ||
check_local_modification: false, | ||
check_local_creation: true, | ||
check_local_deletion: false, | ||
check_remote_modification: false, | ||
check_remote_creation: false, | ||
check_remote_deletion: false, | ||
local_sub_storage: { | ||
type: "query", | ||
sub_storage: { | ||
type: "mapping_storage", | ||
portal_type: spec.source_portal_type, | ||
index: spec.index, | ||
sub_storage: { | ||
type: "query", | ||
sub_storage: { | ||
type: "uuid", | ||
sub_storage: { | ||
type: "indexeddb", | ||
database: DATABASE | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
remote_sub_storage: { | ||
type: 'query_erp5_storage', | ||
index: spec.index, | ||
source_portal_type: spec.source_portal_type, | ||
source_parent_relative_url: spec.source_parent_relative_url, | ||
destination_portal_type: spec.destination_portal_type, | ||
destination_parent_relative_url: spec.destination_parent_relative_url, | ||
sub_storage: { | ||
type: "erp5", | ||
url: spec.destination_url, | ||
default_view_reference: spec.destination_view_reference | ||
} | ||
} | ||
}); | ||
} | ||
ERP5ToERP5Storage.prototype.repair = function () { | ||
var context = this; | ||
RSVP.Queue() | ||
.push(function () { | ||
return context._locale_jio_instance.repair(); | ||
}) | ||
.push(function () { | ||
return context._remote_jio_instance.repair(); | ||
}).push( | ||
function () {console.log('Synch is done'); }, | ||
function (error) {console.warn(error); throw error; } | ||
); | ||
}; | ||
jIO.addStorage('erp5_to_erp5', ERP5ToERP5Storage); | ||
}(jIO, RSVP)); |