c8osdkjscore
Advanced tools
Comparing version 0.0.0-beta2 to 0.0.0-beta3
{ | ||
"name": "c8osdkjscore", | ||
"version": "0.0.0-beta2", | ||
"version": "0.0.0-beta3", | ||
"description": "convertigo's sdk js core", | ||
@@ -5,0 +5,0 @@ "main": "bundle/index.umd.js", |
import {C8oCore} from "./c8oCore"; | ||
import {C8oResponseListener, C8oResponseJsonListener} from "./c8oResponse"; | ||
import {C8oExceptionListener} from "./Exception/c8oExceptionListener"; | ||
import {C8oFullSync} from "./c8oFullSync"; | ||
import {C8oFullSync, C8oFullSyncCbl} from "./c8oFullSync"; | ||
import {C8oException} from "./Exception/c8oException"; | ||
@@ -12,4 +12,3 @@ import {C8oExceptionMessage} from "./Exception/c8oExceptionMessage"; | ||
import {C8oHttpRequestException} from "./Exception/c8oHttpRequestException"; | ||
import {C8oUnavailableLocalCacheException} from "./Exception/c8oUnavailableLocalCacheException"; | ||
import {C8oFullSyncCbl} from "./c8oFullSyncCbl"; | ||
import {C8oUnavailableLocalCacheException} from "./Exception/c8oUnavailableLocalCacheException" | ||
@@ -16,0 +15,0 @@ export class C8oCallTask { |
import { C8oBase } from "./c8oBase"; | ||
import { C8oHttpInterface } from "./c8oHttpInterface"; | ||
import { C8oLogger } from "./c8oLogger"; | ||
import { C8oFullSync } from "./c8oFullSync"; | ||
import { C8oLogLevel } from "./c8oLogLevel"; | ||
import { C8oSettings } from "./c8oSettings"; | ||
import { C8oFullSync } from "./c8oFullSync"; | ||
import { C8oResponseListener } from "./c8oResponse"; | ||
@@ -8,0 +8,0 @@ import { C8oPromise } from "./c8oPromise"; |
@@ -5,3 +5,2 @@ import {C8oBase} from "./c8oBase"; | ||
import {C8oLogger} from "./c8oLogger"; | ||
import {C8oFullSync} from "./c8oFullSync"; | ||
import {C8oLogLevel} from "./c8oLogLevel"; | ||
@@ -11,3 +10,3 @@ import {C8oSettings} from "./c8oSettings"; | ||
import {C8oExceptionMessage} from "./Exception/c8oExceptionMessage"; | ||
import {C8oFullSyncCbl} from "./c8oFullSyncCbl"; | ||
import {C8oFullSync, C8oFullSyncCbl} from "./c8oFullSync"; | ||
import {C8oResponseListener, C8oResponseJsonListener} from "./c8oResponse"; | ||
@@ -14,0 +13,0 @@ |
@@ -0,3 +1,8 @@ | ||
import { C8oFullSyncDatabase } from "./fullSyncDatabase"; | ||
import { FullSyncPolicy } from "./fullsyncpolicy"; | ||
import { C8oResponseListener } from "./c8oResponse"; | ||
import { FullSyncDefaultResponse, FullSyncDocumentOperationResponse } from "./fullSyncResponse"; | ||
import { C8oLocalCacheResponse } from "./c8oLocalCacheResponse"; | ||
import { C8oFullSyncChangeListener } from "./c8oFullSyncChangeListener"; | ||
import { C8oCore } from "./c8oCore"; | ||
import { C8oResponseListener } from "./c8oResponse"; | ||
export declare class C8oFullSync { | ||
@@ -17,1 +22,32 @@ private static FULL_SYNC_URL_PATH; | ||
} | ||
export declare class C8oFullSyncCbl extends C8oFullSync { | ||
private static ATTACHMENT_PROPERTY_KEY_CONTENT_URL; | ||
private fullSyncDatabases; | ||
private fullSyncChangeListeners; | ||
private cblChangeListeners; | ||
constructor(c8o: C8oCore); | ||
getOrCreateFullSyncDatabase(databaseName: string): C8oFullSyncDatabase; | ||
handleFullSyncResponse(response: any, listener: C8oResponseListener): any; | ||
handleGetAttachmentUrlRequest(fullSyncDatabaseName: string, docid: string, parameters: Object): Promise<any>; | ||
handleGetDocumentRequest(fullSyncDatabaseName: string, docid: string, parameters: Object): Promise<any>; | ||
handleDeleteDocumentRequest(DatabaseName: string, docid: string, parameters: Object): Promise<any>; | ||
handlePostDocumentRequest(databaseName: string, fullSyncPolicy: FullSyncPolicy, parameters: Object): Promise<FullSyncDocumentOperationResponse>; | ||
handlePutAttachmentRequest(databaseName: string, docid: string, attachmentName: string, attachmentType: string, attachmentContent: any): Promise<any>; | ||
handleDeleteAttachmentRequest(databaseName: string, docid: string, attachmentName: string): Promise<any>; | ||
handleAllDocumentsRequest(databaseName: string, parameters: Object): Promise<any>; | ||
handleGetViewRequest(databaseName: string, ddocName: string, viewName: string, parameters: Object): Promise<any>; | ||
handleSyncRequest(databaseName: string, parameters: Object, c8oResponseListener: C8oResponseListener): Promise<any>; | ||
handleReplicatePullRequest(databaseName: string, parameters: Object, c8oResponseListener: C8oResponseListener): Promise<any>; | ||
handleReplicatePushRequest(databaseName: string, parameters: Object, c8oResponseListener: C8oResponseListener): Promise<any>; | ||
handleResetDatabaseRequest(databaseName: string): Promise<FullSyncDefaultResponse>; | ||
handleCreateDatabaseRequest(databaseName: string): FullSyncDefaultResponse; | ||
handleDestroyDatabaseRequest(databaseName: string): Promise<FullSyncDefaultResponse>; | ||
static mergeProperties(newProperties: Object, oldProperties: Object): void; | ||
static mergeArrayProperties(newArray: any, oldArray: any): void; | ||
getDocucmentFromDatabase(c8o: C8oCore, databaseName: string, documentId: string): Promise<any>; | ||
overrideDocument(document: any, properties: Object, databaseName: any): void; | ||
getResponseFromLocalCache(c8oCallRequestIdentifier: string): Promise<any>; | ||
saveResponseToLocalCache(c8oCallRequestIdentifier: string, localCacheResponse: C8oLocalCacheResponse): Promise<any>; | ||
addFullSyncChangeListener(db: string, listener: C8oFullSyncChangeListener): void; | ||
removeFullSyncChangeListener(db: string, listener: C8oFullSyncChangeListener): void; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -38,7 +48,16 @@ return new (P || (P = Promise))(function (resolve, reject) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var fullSyncRequestable_1 = require("./fullSyncRequestable"); | ||
var fullSyncDatabase_1 = require("./fullSyncDatabase"); | ||
var fullSyncDeleteDocumentParameter_1 = require("./fullSyncDeleteDocumentParameter"); | ||
var c8oUnavailableLocalCacheException_1 = require("./Exception/c8oUnavailableLocalCacheException"); | ||
var c8oExceptionMessage_1 = require("./Exception/c8oExceptionMessage"); | ||
var c8oException_1 = require("./Exception/c8oException"); | ||
var c8oCouchBaseLiteException_1 = require("./Exception/c8oCouchBaseLiteException"); | ||
var c8oFullSyncTranslator_1 = require("./c8oFullSyncTranslator"); | ||
var c8oRessourceNotFoundException_1 = require("./Exception/c8oRessourceNotFoundException"); | ||
var c8oResponse_1 = require("./c8oResponse"); | ||
var fullSyncResponse_1 = require("./fullSyncResponse"); | ||
var c8oLocalCacheResponse_1 = require("./c8oLocalCacheResponse"); | ||
var c8oUtilsCore_1 = require("./c8oUtilsCore"); | ||
var c8oCore_1 = require("./c8oCore"); | ||
var c8oUtilsCore_1 = require("./c8oUtilsCore"); | ||
var c8oException_1 = require("./Exception/c8oException"); | ||
var c8oExceptionMessage_1 = require("./Exception/c8oExceptionMessage"); | ||
var fullSyncRequestable_1 = require("./fullSyncRequestable"); | ||
var C8oFullSync = (function () { | ||
@@ -120,2 +139,517 @@ function C8oFullSync(c8o) { | ||
exports.C8oFullSync = C8oFullSync; | ||
var C8oFullSyncCbl = (function (_super) { | ||
__extends(C8oFullSyncCbl, _super); | ||
function C8oFullSyncCbl(c8o) { | ||
var _this = _super.call(this, c8o) || this; | ||
_this.fullSyncChangeListeners = []; | ||
_this.cblChangeListeners = []; | ||
_this.fullSyncDatabases = {}; | ||
return _this; | ||
} | ||
C8oFullSyncCbl.prototype.getOrCreateFullSyncDatabase = function (databaseName) { | ||
var localDatabaseName = databaseName + this.localSuffix; | ||
if (this.fullSyncDatabases[localDatabaseName] == null) { | ||
this.fullSyncDatabases[localDatabaseName] = new fullSyncDatabase_1.C8oFullSyncDatabase(this.c8o, databaseName, this.fullSyncDatabaseUrlBase, this.localSuffix); | ||
} | ||
return this.fullSyncDatabases[localDatabaseName]; | ||
}; | ||
C8oFullSyncCbl.prototype.handleFullSyncResponse = function (response, listener) { | ||
response = _super.prototype.handleFullSyncResponse.call(this, response, listener); | ||
if (listener instanceof c8oResponse_1.C8oResponseJsonListener) { | ||
if (response instanceof fullSyncResponse_1.FullSyncDocumentOperationResponse) { | ||
return c8oFullSyncTranslator_1.C8oFullSyncTranslator.fullSyncDocumentOperationResponseToJson(response); | ||
} | ||
else if (response instanceof fullSyncResponse_1.FullSyncDefaultResponse) { | ||
return c8oFullSyncTranslator_1.C8oFullSyncTranslator.fullSyncDefaultResponseToJson(response); | ||
} | ||
else if (response instanceof Object) { | ||
return response; | ||
} | ||
else { | ||
throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.illegalArgumentIncompatibleListener(listener.toString(), typeof response)); | ||
} | ||
} | ||
}; | ||
C8oFullSyncCbl.prototype.handleGetAttachmentUrlRequest = function (fullSyncDatabaseName, docid, parameters) { | ||
var fullSyncDatabase = null; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(fullSyncDatabaseName); | ||
var attachmentName = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, "attachment_name", false); | ||
return new Promise(function (resolve) { | ||
fullSyncDatabase.getdatabase.getAttachment(docid, attachmentName).then(function (buffer) { | ||
resolve(buffer); | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.handleGetDocumentRequest = function (fullSyncDatabaseName, docid, parameters) { | ||
var fullSyncDatabase = null; | ||
var dictDoc = {}; | ||
var param; | ||
param = parameters["attachments"] ? { "attachments": true } : {}; | ||
parameters["binary"] ? param["binary"] = true : {}; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(fullSyncDatabaseName); | ||
return new Promise(function (resolve, reject) { | ||
fullSyncDatabase.getdatabase.get(docid, param).then(function (document) { | ||
if (document != null) { | ||
dictDoc = document; | ||
var attachments = document[C8oFullSync.FULL_SYNC__ATTACHMENTS]; | ||
if (attachments != null) { | ||
for (var _i = 0, attachments_1 = attachments; _i < attachments_1.length; _i++) { | ||
var attachmentName = attachments_1[_i]; | ||
var url = attachments["url"]; | ||
var attachmentDesc = attachments[attachmentName]; | ||
attachmentDesc[C8oFullSyncCbl.ATTACHMENT_PROPERTY_KEY_CONTENT_URL] = url.toString(); | ||
var dictAny = {}; | ||
dictAny[attachmentName] = attachmentDesc; | ||
dictDoc[C8oFullSyncCbl.FULL_SYNC__ATTACHMENTS] = dictAny; | ||
} | ||
} | ||
} | ||
else { | ||
throw new c8oRessourceNotFoundException_1.C8oRessourceNotFoundException((c8oExceptionMessage_1.C8oExceptionMessage.ressourceNotFound("requested document \"" + docid + "\""))); | ||
} | ||
if (dictDoc === null) { | ||
dictDoc = {}; | ||
} | ||
resolve(dictDoc); | ||
}) | ||
.catch(function (error) { | ||
reject(error); | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.handleDeleteDocumentRequest = function (DatabaseName, docid, parameters) { | ||
var fullSyncDatabase = null; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(DatabaseName); | ||
var revParameterValue = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, fullSyncDeleteDocumentParameter_1.FullSyncDeleteDocumentParameter.REV.name, false); | ||
var documentRevision; | ||
if (revParameterValue === null) { | ||
return new Promise(function (resolve, reject) { | ||
fullSyncDatabase.getdatabase.get(docid).then(function (doc) { | ||
if (doc === null) { | ||
throw new c8oRessourceNotFoundException_1.C8oRessourceNotFoundException(c8oExceptionMessage_1.C8oExceptionMessage.toDo()); | ||
} | ||
documentRevision = doc._rev; | ||
return fullSyncDatabase.getdatabase.remove(doc); | ||
}).then(function (result) { | ||
resolve(new fullSyncResponse_1.FullSyncDocumentOperationResponse(docid, documentRevision, result.ok)); | ||
}).catch(function (err) { | ||
reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.couchRequestDeleteDocument(), err)); | ||
}); | ||
}); | ||
} | ||
else { | ||
return new Promise(function (resolve, reject) { | ||
fullSyncDatabase.getdatabase.remove(docid, revParameterValue) | ||
.then(function (result) { | ||
resolve(new fullSyncResponse_1.FullSyncDocumentOperationResponse(docid, documentRevision, result.ok)); | ||
}).catch(function (err) { | ||
reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.couchRequestDeleteDocument(), err)); | ||
}); | ||
}); | ||
} | ||
}; | ||
C8oFullSyncCbl.prototype.handlePostDocumentRequest = function (databaseName, fullSyncPolicy, parameters) { | ||
var fullSyncDatabase; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
var subkeySeparatorParameterValue = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, c8oCore_1.C8oCore.FS_SUBKEY_SEPARATOR, false); | ||
if (subkeySeparatorParameterValue == null) { | ||
subkeySeparatorParameterValue = "."; | ||
} | ||
var newProperties = {}; | ||
for (var i = 0; i < Object.keys(parameters).length; i++) { | ||
var parameterName = Object.keys(parameters)[i]; | ||
if (!parameterName.startsWith("__") && !parameterName.startsWith("_use_")) { | ||
var objectParameterValue = parameters[Object.keys(parameters)[i]]; | ||
var paths = parameterName.split(subkeySeparatorParameterValue); | ||
if (paths.length > 1) { | ||
parameterName = paths[0]; | ||
var count = paths.length - 1; | ||
while (count > 0) { | ||
var tmpObject = {}; | ||
tmpObject[paths[count]] = objectParameterValue; | ||
objectParameterValue = tmpObject; | ||
count--; | ||
} | ||
var existProperty = newProperties[parameterName]; | ||
if (existProperty != null) { | ||
C8oFullSyncCbl.mergeProperties(objectParameterValue, existProperty); | ||
} | ||
} | ||
newProperties[parameterName] = objectParameterValue; | ||
} | ||
} | ||
var db = fullSyncDatabase.getdatabase; | ||
return new Promise(function (resolve, reject) { | ||
fullSyncPolicy.action(db, newProperties).then(function (createdDocument) { | ||
var fsDocOpeResp = new fullSyncResponse_1.FullSyncDocumentOperationResponse(createdDocument.id, createdDocument.rev, createdDocument.ok); | ||
resolve(fsDocOpeResp); | ||
}).catch(function (error) { | ||
reject(error); | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.handlePutAttachmentRequest = function (databaseName, docid, attachmentName, attachmentType, attachmentContent) { | ||
var document = null; | ||
var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return new Promise(function (resolve) { | ||
fullSyncDatabase.getdatabase.get(docid).then(function (result) { | ||
document = result; | ||
if (document !== null) { | ||
fullSyncDatabase.getdatabase.putAttachment(docid, attachmentName, result._rev, attachmentContent, attachmentType) | ||
.then(function (result) { | ||
resolve(new fullSyncResponse_1.FullSyncDocumentOperationResponse(result._id, result._rev, result.ok)); | ||
}).catch(function (err) { | ||
throw new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException("Unable to put the attachment " + attachmentName + " to the document " + docid + ".", err); | ||
}); | ||
} | ||
else { | ||
throw new c8oRessourceNotFoundException_1.C8oRessourceNotFoundException(c8oExceptionMessage_1.C8oExceptionMessage.toDo()); | ||
} | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.handleDeleteAttachmentRequest = function (databaseName, docid, attachmentName) { | ||
var document = null; | ||
var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return new Promise(function (resolve) { | ||
fullSyncDatabase.getdatabase.get(docid).then(function (result) { | ||
document = result; | ||
}).then(function () { | ||
if (document !== null) { | ||
fullSyncDatabase.getdatabase.removeAttachment(docid, attachmentName, document._rev).catch(function (err) { | ||
throw new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException("Unable to delete the attachment " + attachmentName + " to the document " + docid + ".", err); | ||
}); | ||
} | ||
else { | ||
throw new c8oRessourceNotFoundException_1.C8oRessourceNotFoundException(c8oExceptionMessage_1.C8oExceptionMessage.toDo()); | ||
} | ||
resolve(new fullSyncResponse_1.FullSyncDocumentOperationResponse(document._id, document._rev, true)); | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.handleAllDocumentsRequest = function (databaseName, parameters) { | ||
var fullSyncDatabase = null; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return new Promise(function (resolve, reject) { | ||
fullSyncDatabase.getdatabase | ||
.allDocs(parameters) | ||
.then(function (res) { | ||
resolve(res); | ||
}) | ||
.catch(function () { | ||
reject(new c8oException_1.C8oException("TODO")); | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.handleGetViewRequest = function (databaseName, ddocName, viewName, parameters) { | ||
var _this = this; | ||
var fullSyncDatabase = null; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
var attachments; | ||
var binary; | ||
var include_docs; | ||
if (parameters["attachments"] && window["cblite"] !== undefined) { | ||
attachments = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, "attachments", false); | ||
binary = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, "binary", false); | ||
include_docs = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, "include_docs", false); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
fullSyncDatabase.getdatabase.query(ddocName + "/" + viewName, parameters) | ||
.then(function (result) { | ||
if (attachments) { | ||
var array_1 = []; | ||
var len_1 = 0; | ||
for (var _i = 0, _a = result.rows; _i < _a.length; _i++) { | ||
var resp = _a[_i]; | ||
_this.handleGetDocumentRequest(databaseName, resp.id, { | ||
"attachments": true, | ||
"binary": true, | ||
"include_docs": true | ||
}).then(function (getResult) { | ||
array_1.push(getResult); | ||
len_1++; | ||
if (len_1 === result.rows.length) { | ||
result.rows = array_1; | ||
resolve(result); | ||
} | ||
}); | ||
} | ||
} | ||
else { | ||
resolve(result); | ||
} | ||
}).catch(function (error) { | ||
reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.couchRequestGetView(), error)); | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.handleSyncRequest = function (databaseName, parameters, c8oResponseListener) { | ||
var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return fullSyncDatabase.startAllReplications(parameters, c8oResponseListener); | ||
}; | ||
C8oFullSyncCbl.prototype.handleReplicatePullRequest = function (databaseName, parameters, c8oResponseListener) { | ||
var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return fullSyncDatabase.startPullReplication(parameters, c8oResponseListener); | ||
}; | ||
C8oFullSyncCbl.prototype.handleReplicatePushRequest = function (databaseName, parameters, c8oResponseListener) { | ||
var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return fullSyncDatabase.startPushReplication(parameters, c8oResponseListener); | ||
}; | ||
C8oFullSyncCbl.prototype.handleResetDatabaseRequest = function (databaseName) { | ||
var _this = this; | ||
return new Promise(function (resolve) { | ||
_this.handleDestroyDatabaseRequest(databaseName).then(function () { | ||
resolve(_this.handleCreateDatabaseRequest(databaseName)); | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.handleCreateDatabaseRequest = function (databaseName) { | ||
this.getOrCreateFullSyncDatabase(databaseName); | ||
return new fullSyncResponse_1.FullSyncDefaultResponse(true); | ||
}; | ||
C8oFullSyncCbl.prototype.handleDestroyDatabaseRequest = function (databaseName) { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var localDatabaseName = databaseName + _this.localSuffix; | ||
_this.getOrCreateFullSyncDatabase(databaseName).deleteDB().then(function (response) { | ||
if (_this.fullSyncDatabases[localDatabaseName] !== null) { | ||
delete _this.fullSyncDatabases[localDatabaseName]; | ||
} | ||
resolve(new fullSyncResponse_1.FullSyncDefaultResponse(response.ok)); | ||
}).catch(function (err) { | ||
reject(new c8oException_1.C8oException("TODO", err)); | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.mergeProperties = function (newProperties, oldProperties) { | ||
for (var i = 0; i < Object.keys(oldProperties).length; i++) { | ||
var oldPropertyKey = Object.keys(oldProperties)[i]; | ||
var oldPropertyValue = oldProperties[Object.keys(oldProperties)[i]]; | ||
if (newProperties[oldPropertyKey] !== null && newProperties[oldPropertyKey] !== undefined) { | ||
var newDocumentValue = newProperties[oldPropertyKey]; | ||
if (Object.prototype.toString.call(newDocumentValue) === "[object Array]" && Object.prototype.toString.call(oldPropertyValue) === "[object Array]") { | ||
C8oFullSyncCbl.mergeArrayProperties(newDocumentValue, oldPropertyValue); | ||
} | ||
else if (typeof (newDocumentValue) === "object" && typeof (oldPropertyValue) === "object") { | ||
C8oFullSyncCbl.mergeProperties(newDocumentValue, oldPropertyValue); | ||
} | ||
else { | ||
} | ||
} | ||
else { | ||
newProperties[oldPropertyKey] = oldPropertyValue; | ||
} | ||
} | ||
}; | ||
C8oFullSyncCbl.mergeArrayProperties = function (newArray, oldArray) { | ||
var newArraySize = newArray.length; | ||
var oldArraySize = oldArray.length; | ||
for (var i = 0; i < oldArraySize; i++) { | ||
var newArrayValue = null; | ||
if (i < newArraySize) { | ||
newArrayValue = newArray[i]; | ||
} | ||
var oldArrayValue = oldArray[i]; | ||
if (newArrayValue !== null) { | ||
if (newArrayValue instanceof Array && oldArrayValue instanceof Array) { | ||
C8oFullSyncCbl.mergeArrayProperties(newArrayValue, oldArrayValue); | ||
} | ||
else if (typeof (newArrayValue) === "object" && typeof (oldArrayValue) === "object") { | ||
C8oFullSyncCbl.mergeProperties(newArrayValue, oldArrayValue); | ||
} | ||
else { | ||
} | ||
} | ||
else { | ||
newArray.push(oldArrayValue); | ||
} | ||
} | ||
}; | ||
C8oFullSyncCbl.prototype.getDocucmentFromDatabase = function (c8o, databaseName, documentId) { | ||
var c8oFullSyncDatabase; | ||
try { | ||
c8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
} | ||
catch (err) { | ||
throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncGetOrCreateDatabase(databaseName)); | ||
} | ||
return new Promise(function (resolve) { | ||
c8oFullSyncDatabase.getdatabase().get(documentId).then(function (result) { | ||
resolve(result); | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.overrideDocument = function (document, properties, databaseName) { | ||
properties[C8oFullSync.FULL_SYNC__REV] = document._rev; | ||
var c8oFullSyncDatabase; | ||
try { | ||
c8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
} | ||
catch (err) { | ||
throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncGetOrCreateDatabase(databaseName)); | ||
} | ||
c8oFullSyncDatabase.getdatabase().put(properties).catch(function () { | ||
throw new c8oException_1.C8oException("TODO"); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.getResponseFromLocalCache = function (c8oCallRequestIdentifier) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var fullSyncDatabase, localCacheDocument; | ||
return __generator(this, function (_a) { | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(c8oCore_1.C8oCore.LOCAL_CACHE_DATABASE_NAME); | ||
localCacheDocument = null; | ||
return [2, new Promise(function (resolve, reject) { | ||
fullSyncDatabase.getdatabase.get(c8oCallRequestIdentifier).then(function (result) { | ||
localCacheDocument = result; | ||
var response = JSON.stringify(localCacheDocument[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE]); | ||
var responseType = localCacheDocument[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE_TYPE]; | ||
var expirationDate = localCacheDocument[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_EXPIRATION_DATE]; | ||
var responseString = null; | ||
var responseTypeString = null; | ||
var expirationDateNumber = -1; | ||
if (response != null) { | ||
if (typeof response === "string") { | ||
responseString = response; | ||
} | ||
else { | ||
throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.InvalidLocalCacheResponseInformation()); | ||
} | ||
} | ||
if (responseType != null) { | ||
if (typeof responseType === "string") { | ||
responseTypeString = responseType; | ||
} | ||
else { | ||
throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.InvalidLocalCacheResponseInformation()); | ||
} | ||
} | ||
if (expirationDate != null) { | ||
if (typeof expirationDate === "number") { | ||
expirationDateNumber = expirationDate; | ||
var currentTime = new Date().getTime(); | ||
if (expirationDateNumber < currentTime) { | ||
throw new c8oUnavailableLocalCacheException_1.C8oUnavailableLocalCacheException(c8oExceptionMessage_1.C8oExceptionMessage.timeToLiveExpired()); | ||
} | ||
} | ||
else { | ||
throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.InvalidLocalCacheResponseInformation()); | ||
} | ||
} | ||
resolve(new c8oLocalCacheResponse_1.C8oLocalCacheResponse(responseString, responseTypeString, expirationDateNumber)); | ||
}).catch(function (err) { | ||
if (err.status === 404) { | ||
resolve(new c8oUnavailableLocalCacheException_1.C8oUnavailableLocalCacheException(c8oExceptionMessage_1.C8oExceptionMessage.localCacheDocumentJustCreated())); | ||
} | ||
else { | ||
reject(err); | ||
} | ||
}); | ||
})]; | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.saveResponseToLocalCache = function (c8oCallRequestIdentifier, localCacheResponse) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var fullSyncDatabase; | ||
return __generator(this, function (_a) { | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(c8oCore_1.C8oCore.LOCAL_CACHE_DATABASE_NAME); | ||
return [2, new Promise(function (resolve) { | ||
fullSyncDatabase.getdatabase.get(c8oCallRequestIdentifier).then(function (localCacheDocument) { | ||
var properties = {}; | ||
properties[C8oFullSync.FULL_SYNC__ID] = c8oCallRequestIdentifier; | ||
properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE] = localCacheResponse.getResponse(); | ||
properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE_TYPE] = localCacheResponse.getResponseType(); | ||
if (localCacheResponse.getExpirationDate() > 0) { | ||
properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_EXPIRATION_DATE] = localCacheResponse.getExpirationDate(); | ||
} | ||
var currentRevision = localCacheDocument._rev; | ||
if (currentRevision !== null) { | ||
properties[C8oFullSyncCbl.FULL_SYNC__REV] = currentRevision; | ||
} | ||
fullSyncDatabase.getdatabase.put(properties).then(function (result) { | ||
resolve(result); | ||
}); | ||
}).catch(function (error) { | ||
if (error.status === 404) { | ||
var properties = {}; | ||
properties[C8oFullSync.FULL_SYNC__ID] = c8oCallRequestIdentifier; | ||
properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE] = localCacheResponse.getResponse(); | ||
properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE_TYPE] = localCacheResponse.getResponseType(); | ||
if (localCacheResponse.getExpirationDate() > 0) { | ||
properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_EXPIRATION_DATE] = localCacheResponse.getExpirationDate(); | ||
} | ||
fullSyncDatabase.getdatabase.put(properties).then(function (result) { | ||
resolve(result); | ||
}); | ||
} | ||
else { | ||
resolve(error); | ||
} | ||
}); | ||
})]; | ||
}); | ||
}); | ||
}; | ||
C8oFullSyncCbl.prototype.addFullSyncChangeListener = function (db, listener) { | ||
if (db === null || db === "") { | ||
db = this.c8o.defaultDatabaseName; | ||
} | ||
var listeners = []; | ||
if (this.fullSyncChangeListeners[db] != null) { | ||
listeners[0] = this.fullSyncChangeListeners[db]; | ||
} | ||
else { | ||
listeners[0] = []; | ||
this.fullSyncChangeListeners[db] = listeners[0]; | ||
var evtHandler = this.getOrCreateFullSyncDatabase(db).getdatabase | ||
.changes({ | ||
since: "now", | ||
live: true, | ||
include_docs: true | ||
}).on("change", function (change) { | ||
var changes = {}; | ||
var docs = []; | ||
var doc = {}; | ||
doc["id"] = change["doc"]["_id"]; | ||
doc["rev"] = change["doc"]["_rev"]; | ||
doc["isConflict"] = change.doc._conflicts; | ||
if (change.source != null) { | ||
doc["sourceUrl"] = change.source; | ||
} | ||
docs.push(doc); | ||
changes["changes"] = docs; | ||
for (var _i = 0, _a = listeners[0]; _i < _a.length; _i++) { | ||
var handler = _a[_i]; | ||
if (handler !== undefined) { | ||
handler.onChange(changes); | ||
} | ||
} | ||
}); | ||
this.cblChangeListeners[db] = evtHandler; | ||
} | ||
listeners[0].push(listener); | ||
}; | ||
C8oFullSyncCbl.prototype.removeFullSyncChangeListener = function (db, listener) { | ||
if (db === null || db === "") { | ||
db = this.c8o.defaultDatabaseName; | ||
} | ||
if (this.fullSyncChangeListeners[db] != null) { | ||
var listeners = this.fullSyncChangeListeners[db]; | ||
for (var list in listeners) { | ||
if (listeners[list] === listener) { | ||
delete listeners[list]; | ||
} | ||
} | ||
if (listeners.length === 0 || listeners == null) { | ||
this.getOrCreateFullSyncDatabase(db).getdatabase.cancel(); | ||
this.cblChangeListeners[db].cancel(); | ||
delete this.fullSyncChangeListeners[db]; | ||
delete this.cblChangeListeners[db]; | ||
} | ||
} | ||
}; | ||
C8oFullSyncCbl.ATTACHMENT_PROPERTY_KEY_CONTENT_URL = "content_url"; | ||
return C8oFullSyncCbl; | ||
}(C8oFullSync)); | ||
exports.C8oFullSyncCbl = C8oFullSyncCbl; | ||
//# sourceMappingURL=c8oFullSync.js.map |
@@ -0,7 +1,18 @@ | ||
import {FullSyncRequestable} from "./fullSyncRequestable"; | ||
import {C8oFullSyncDatabase} from "./fullSyncDatabase"; | ||
import {FullSyncDeleteDocumentParameter} from "./fullSyncDeleteDocumentParameter"; | ||
import {C8oUnavailableLocalCacheException} from "./Exception/c8oUnavailableLocalCacheException"; | ||
import {C8oExceptionMessage} from "./Exception/c8oExceptionMessage"; | ||
import {C8oException} from "./Exception/c8oException"; | ||
import {C8oCouchBaseLiteException} from "./Exception/c8oCouchBaseLiteException"; | ||
import {C8oFullSyncTranslator} from "./c8oFullSyncTranslator"; | ||
import {C8oRessourceNotFoundException} from "./Exception/c8oRessourceNotFoundException"; | ||
import {FullSyncPolicy} from "./fullsyncpolicy"; | ||
import {C8oResponseJsonListener, C8oResponseListener} from "./c8oResponse"; | ||
import {FullSyncDefaultResponse, FullSyncDocumentOperationResponse} from "./fullSyncResponse"; | ||
import {C8oLocalCacheResponse} from "./c8oLocalCacheResponse"; | ||
import {C8oFullSyncChangeListener} from "./c8oFullSyncChangeListener"; | ||
import {C8oUtilsCore} from "./c8oUtilsCore"; | ||
import {C8oBase} from "./c8oBase"; | ||
import {C8oCore} from "./c8oCore"; | ||
import {C8oResponseListener} from "./c8oResponse"; | ||
import {C8oUtilsCore} from "./c8oUtilsCore"; | ||
import {C8oException} from "./Exception/c8oException"; | ||
import {C8oExceptionMessage} from "./Exception/c8oExceptionMessage"; | ||
import {FullSyncRequestable} from "./fullSyncRequestable"; | ||
@@ -113,2 +124,577 @@ export class C8oFullSync { | ||
} | ||
export class C8oFullSyncCbl extends C8oFullSync { | ||
private static ATTACHMENT_PROPERTY_KEY_CONTENT_URL: string = "content_url"; | ||
private fullSyncDatabases: Object; | ||
private fullSyncChangeListeners: Array<Array<C8oFullSyncChangeListener>> = []; | ||
private cblChangeListeners: Array<any> = []; | ||
constructor(c8o: C8oCore) { | ||
super(c8o); | ||
this.fullSyncDatabases = {}; | ||
} | ||
/** | ||
* Returns the database with this name in the list.<br/> | ||
* If it does not already exist yet then creates it and adds it to the list. | ||
* | ||
* @param databaseName | ||
* @return C8oFullSyncDatabase | ||
* @throws C8oException Failed to create a new fullSync database. | ||
*/ | ||
public getOrCreateFullSyncDatabase(databaseName: string): C8oFullSyncDatabase { | ||
let localDatabaseName: string = databaseName + this.localSuffix; | ||
if (this.fullSyncDatabases[localDatabaseName] == null) { | ||
this.fullSyncDatabases[localDatabaseName] = new C8oFullSyncDatabase(this.c8o, databaseName, this.fullSyncDatabaseUrlBase, this.localSuffix); | ||
} | ||
return this.fullSyncDatabases[localDatabaseName]; | ||
} | ||
handleFullSyncResponse(response: any, listener: C8oResponseListener): any { | ||
response = super.handleFullSyncResponse(response, listener); | ||
if (listener instanceof C8oResponseJsonListener) { | ||
if (response instanceof FullSyncDocumentOperationResponse) { | ||
return C8oFullSyncTranslator.fullSyncDocumentOperationResponseToJson(response as FullSyncDocumentOperationResponse); | ||
} | ||
else if (response instanceof FullSyncDefaultResponse) { | ||
return C8oFullSyncTranslator.fullSyncDefaultResponseToJson(response as FullSyncDefaultResponse); | ||
} | ||
else if (response instanceof Object) { | ||
return response as JSON; | ||
} | ||
else { | ||
throw new C8oException(C8oExceptionMessage.illegalArgumentIncompatibleListener(listener.toString(), typeof response)); | ||
} | ||
} | ||
} | ||
handleGetAttachmentUrlRequest(fullSyncDatabaseName: string, docid: string, parameters: Object): Promise<any> { | ||
let fullSyncDatabase: C8oFullSyncDatabase = null; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(fullSyncDatabaseName); | ||
let attachmentName = C8oUtilsCore.getParameterStringValue(parameters, "attachment_name", false); | ||
return new Promise((resolve) => { | ||
fullSyncDatabase.getdatabase.getAttachment(docid, attachmentName).then((buffer) => { | ||
resolve(buffer); | ||
}); | ||
}); | ||
} | ||
handleGetDocumentRequest(fullSyncDatabaseName: string, docid: string, parameters: Object): Promise<any> { | ||
let fullSyncDatabase: C8oFullSyncDatabase = null; | ||
let dictDoc: Object = {}; | ||
let param: Object; | ||
param = parameters["attachments"] ? {"attachments": true} : {}; | ||
parameters["binary"] ? param["binary"] = true : {}; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(fullSyncDatabaseName); | ||
return new Promise((resolve, reject) => { | ||
fullSyncDatabase.getdatabase.get(docid, param).then((document) => { | ||
if (document != null) { | ||
dictDoc = document; | ||
let attachments = document[C8oFullSync.FULL_SYNC__ATTACHMENTS]; | ||
if (attachments != null) { | ||
for (let attachmentName of attachments) { | ||
let url = attachments["url"]; | ||
let attachmentDesc: Object = attachments[attachmentName]; | ||
attachmentDesc[C8oFullSyncCbl.ATTACHMENT_PROPERTY_KEY_CONTENT_URL] = url.toString(); | ||
let dictAny: Object = {}; | ||
dictAny[attachmentName] = attachmentDesc; | ||
dictDoc[C8oFullSyncCbl.FULL_SYNC__ATTACHMENTS] = dictAny; | ||
} | ||
} | ||
} | ||
else { | ||
throw new C8oRessourceNotFoundException((C8oExceptionMessage.ressourceNotFound("requested document \"" + docid + "\""))); | ||
} | ||
if (dictDoc === null) { | ||
dictDoc = {}; | ||
} | ||
resolve(dictDoc); | ||
}) | ||
.catch((error) => { | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
handleDeleteDocumentRequest(DatabaseName: string, docid: string, parameters: Object): Promise<any> { | ||
let fullSyncDatabase: C8oFullSyncDatabase = null; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(DatabaseName); | ||
let revParameterValue: string = C8oUtilsCore.getParameterStringValue(parameters, FullSyncDeleteDocumentParameter.REV.name, false); | ||
let documentRevision: string; | ||
if (revParameterValue === null) { | ||
return new Promise((resolve, reject) => { | ||
fullSyncDatabase.getdatabase.get(docid).then((doc) => { | ||
if (doc === null) { | ||
throw new C8oRessourceNotFoundException(C8oExceptionMessage.toDo()); | ||
} | ||
documentRevision = doc._rev; | ||
return fullSyncDatabase.getdatabase.remove(doc); | ||
}).then((result) => { | ||
resolve(new FullSyncDocumentOperationResponse(docid, documentRevision, result.ok)); | ||
}).catch((err) => { | ||
reject(new C8oException(C8oExceptionMessage.couchRequestDeleteDocument(), err)); | ||
}); | ||
}); | ||
} | ||
else { | ||
return new Promise((resolve, reject) => { | ||
fullSyncDatabase.getdatabase.remove(docid, revParameterValue) | ||
.then((result) => { | ||
resolve(new FullSyncDocumentOperationResponse(docid, documentRevision, result.ok)); | ||
}).catch((err) => { | ||
reject(new C8oException(C8oExceptionMessage.couchRequestDeleteDocument(), err)); | ||
}); | ||
}); | ||
} | ||
} | ||
handlePostDocumentRequest(databaseName: string, fullSyncPolicy: FullSyncPolicy, parameters: Object): Promise<FullSyncDocumentOperationResponse> { | ||
let fullSyncDatabase: C8oFullSyncDatabase; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
let subkeySeparatorParameterValue: string = C8oUtilsCore.getParameterStringValue(parameters, C8oCore.FS_SUBKEY_SEPARATOR, false); | ||
if (subkeySeparatorParameterValue == null) { | ||
subkeySeparatorParameterValue = "."; | ||
} | ||
let newProperties = {}; | ||
for (let i = 0; i < Object.keys(parameters).length; i++) { | ||
let parameterName: string = Object.keys(parameters)[i]; | ||
if (!parameterName.startsWith("__") && !parameterName.startsWith("_use_")) { | ||
let objectParameterValue: any = parameters[Object.keys(parameters)[i]]; | ||
let paths: Array<string> = parameterName.split(subkeySeparatorParameterValue); | ||
if (paths.length > 1) { | ||
parameterName = paths[0]; | ||
let count = paths.length - 1; | ||
while (count > 0) { | ||
let tmpObject: Object = {}; | ||
tmpObject[paths[count]] = objectParameterValue; | ||
objectParameterValue = tmpObject; | ||
count--; | ||
} | ||
let existProperty = newProperties[parameterName]; | ||
if (existProperty != null) { | ||
C8oFullSyncCbl.mergeProperties(objectParameterValue, existProperty); | ||
} | ||
} | ||
newProperties[parameterName] = objectParameterValue; | ||
} | ||
} | ||
let db = fullSyncDatabase.getdatabase; | ||
return new Promise((resolve, reject) => { | ||
fullSyncPolicy.action(db, newProperties).then((createdDocument) => { | ||
let fsDocOpeResp: FullSyncDocumentOperationResponse = new FullSyncDocumentOperationResponse(createdDocument.id, createdDocument.rev, createdDocument.ok); | ||
resolve(fsDocOpeResp); | ||
}).catch((error) => { | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
handlePutAttachmentRequest(databaseName: string, docid: string, attachmentName: string, attachmentType: string, attachmentContent: any): Promise<any> { | ||
let document: any = null; | ||
let fullSyncDatabase: C8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return new Promise((resolve) => { | ||
fullSyncDatabase.getdatabase.get(docid).then((result) => { | ||
document = result; | ||
if (document !== null) { | ||
fullSyncDatabase.getdatabase.putAttachment(docid, attachmentName,result._rev, attachmentContent, attachmentType) | ||
.then((result) => { | ||
// handle result | ||
resolve(new FullSyncDocumentOperationResponse(result._id, result._rev, result.ok)); | ||
}).catch((err) => { | ||
throw new C8oCouchBaseLiteException("Unable to put the attachment " + attachmentName + " to the document " + docid + ".", err); | ||
}); | ||
} else { | ||
throw new C8oRessourceNotFoundException(C8oExceptionMessage.toDo()); | ||
} | ||
}); | ||
}); | ||
} | ||
handleDeleteAttachmentRequest(databaseName: string, docid: string, attachmentName: string): Promise<any> { | ||
let document: any = null; | ||
let fullSyncDatabase: C8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return new Promise((resolve) => { | ||
fullSyncDatabase.getdatabase.get(docid).then((result) => { | ||
document = result; | ||
}).then(() => { | ||
if (document !== null) { | ||
fullSyncDatabase.getdatabase.removeAttachment(docid, attachmentName, document._rev).catch((err) => { | ||
throw new C8oCouchBaseLiteException("Unable to delete the attachment " + attachmentName + " to the document " + docid + ".", err); | ||
}); | ||
} | ||
else { | ||
throw new C8oRessourceNotFoundException(C8oExceptionMessage.toDo()); | ||
} | ||
resolve(new FullSyncDocumentOperationResponse(document._id, document._rev, true)); | ||
}); | ||
}); | ||
} | ||
public handleAllDocumentsRequest(databaseName: string, parameters: Object): Promise<any> { | ||
let fullSyncDatabase = null; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return new Promise((resolve, reject) => { | ||
fullSyncDatabase.getdatabase | ||
.allDocs(parameters) | ||
.then((res) => { | ||
resolve(res); | ||
}) | ||
.catch(() => { | ||
reject(new C8oException("TODO")); | ||
}); | ||
}); | ||
} | ||
handleGetViewRequest(databaseName: string, ddocName: string, viewName: string, parameters: Object): Promise<any> { | ||
let fullSyncDatabase = null; | ||
fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
let attachments; | ||
let binary; | ||
let include_docs; | ||
if (parameters["attachments"] && window["cblite"] !== undefined) { | ||
attachments = C8oUtilsCore.getParameterStringValue(parameters, "attachments", false); | ||
binary = C8oUtilsCore.getParameterStringValue(parameters, "binary", false); | ||
include_docs = C8oUtilsCore.getParameterStringValue(parameters, "include_docs", false); | ||
} | ||
return new Promise((resolve, reject) => { | ||
fullSyncDatabase.getdatabase.query(ddocName + "/" + viewName, parameters) | ||
.then((result) => { | ||
if (attachments) { | ||
let array: Array<Object> = []; | ||
let len = 0; | ||
for (let resp of result.rows) { | ||
this.handleGetDocumentRequest(databaseName, resp.id, { | ||
"attachments": true, | ||
"binary": true, | ||
"include_docs": true | ||
}).then((getResult) => { | ||
array.push(getResult); | ||
len++; | ||
if (len === result.rows.length) { | ||
result.rows = array; | ||
resolve(result); | ||
} | ||
}); | ||
} | ||
} | ||
else { | ||
resolve(result); | ||
} | ||
}).catch((error) => { | ||
reject(new C8oException(C8oExceptionMessage.couchRequestGetView(), error)); | ||
}); | ||
}); | ||
} | ||
handleSyncRequest(databaseName: string, parameters: Object, c8oResponseListener: C8oResponseListener): Promise<any> { | ||
let fullSyncDatabase: C8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return fullSyncDatabase.startAllReplications(parameters, c8oResponseListener); | ||
} | ||
handleReplicatePullRequest(databaseName: string, parameters: Object, c8oResponseListener: C8oResponseListener): Promise<any> { | ||
let fullSyncDatabase: C8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return fullSyncDatabase.startPullReplication(parameters, c8oResponseListener); | ||
} | ||
handleReplicatePushRequest(databaseName: string, parameters: Object, c8oResponseListener: C8oResponseListener): Promise<any> { | ||
let fullSyncDatabase: C8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
return fullSyncDatabase.startPushReplication(parameters, c8oResponseListener); | ||
} | ||
handleResetDatabaseRequest(databaseName: string): Promise<FullSyncDefaultResponse> { | ||
return new Promise((resolve) => { | ||
this.handleDestroyDatabaseRequest(databaseName).then(() => { | ||
resolve(this.handleCreateDatabaseRequest(databaseName)); | ||
}); | ||
}); | ||
} | ||
handleCreateDatabaseRequest(databaseName: string): FullSyncDefaultResponse { | ||
this.getOrCreateFullSyncDatabase(databaseName); | ||
return new FullSyncDefaultResponse(true); | ||
} | ||
handleDestroyDatabaseRequest(databaseName: string): Promise<FullSyncDefaultResponse> { | ||
return new Promise((resolve, reject) => { | ||
let localDatabaseName = databaseName + this.localSuffix; | ||
this.getOrCreateFullSyncDatabase(databaseName).deleteDB().then((response) => { | ||
if (this.fullSyncDatabases[localDatabaseName] !== null) { | ||
delete this.fullSyncDatabases[localDatabaseName]; | ||
} | ||
resolve(new FullSyncDefaultResponse(response.ok)); | ||
}).catch((err) => { | ||
reject(new C8oException("TODO", err)); | ||
}); | ||
}); | ||
} | ||
static mergeProperties(newProperties: Object, oldProperties: Object) { | ||
for (let i = 0; i < Object.keys(oldProperties).length; i++) { | ||
let oldPropertyKey = Object.keys(oldProperties)[i]; | ||
let oldPropertyValue = oldProperties[Object.keys(oldProperties)[i]]; | ||
// Checks if the new document contains the same key | ||
if (newProperties[oldPropertyKey] !== null && newProperties[oldPropertyKey] !== undefined) { | ||
let newDocumentValue = newProperties[oldPropertyKey]; | ||
if (Object.prototype.toString.call( newDocumentValue ) === "[object Array]" && Object.prototype.toString.call( oldPropertyValue ) === "[object Array]") { | ||
C8oFullSyncCbl.mergeArrayProperties(newDocumentValue, oldPropertyValue); | ||
} | ||
else if (typeof(newDocumentValue) === "object" && typeof(oldPropertyValue) === "object") { | ||
C8oFullSyncCbl.mergeProperties(newDocumentValue, oldPropertyValue); | ||
} | ||
else { | ||
// If the new document has the same key but its value is not the same type than the old one or if their type are "simple" | ||
// Does nothing cause the right value is the new one | ||
} | ||
} | ||
else { | ||
// If the new document does not contain the key then adds it | ||
newProperties[oldPropertyKey] = oldPropertyValue; | ||
} | ||
} | ||
} | ||
static mergeArrayProperties(newArray: any, oldArray: any) { | ||
let newArraySize = newArray.length; | ||
let oldArraySize = oldArray.length; | ||
for (let i = 0; i < oldArraySize; i++) { | ||
let newArrayValue: any = null; | ||
if (i < newArraySize) { | ||
newArrayValue = newArray[i]; | ||
} | ||
let oldArrayValue = oldArray[i]; | ||
if (newArrayValue !== null) { | ||
if (newArrayValue instanceof Array && oldArrayValue instanceof Array) { | ||
C8oFullSyncCbl.mergeArrayProperties(newArrayValue, oldArrayValue); | ||
} | ||
else if (typeof(newArrayValue) === "object" && typeof(oldArrayValue) === "object") { | ||
C8oFullSyncCbl.mergeProperties(newArrayValue, oldArrayValue); | ||
} | ||
else { | ||
// If the new array value is not the same type than the old one or if their type are "simple" | ||
// Does nothing cause the right value is the new one | ||
} | ||
} | ||
else { | ||
// If the new array value is null then it means that it size is reach so we can add objects at its end | ||
newArray.push(oldArrayValue); | ||
} | ||
} | ||
} | ||
//noinspection JSUnusedLocalSymbols | ||
getDocucmentFromDatabase(c8o: C8oCore, databaseName: string, documentId: string): Promise<any> { | ||
let c8oFullSyncDatabase: C8oFullSyncDatabase; | ||
try { | ||
c8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
} | ||
catch (err) { | ||
throw new C8oException(C8oExceptionMessage.fullSyncGetOrCreateDatabase(databaseName)); | ||
} | ||
return new Promise((resolve) => { | ||
c8oFullSyncDatabase.getdatabase().get(documentId).then((result) => { | ||
resolve(result); | ||
}); | ||
}); | ||
} | ||
overrideDocument(document: any, properties: Object, databaseName) { | ||
properties[C8oFullSync.FULL_SYNC__REV] = document._rev; | ||
let c8oFullSyncDatabase: C8oFullSyncDatabase; | ||
try { | ||
c8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); | ||
} | ||
catch (err) { | ||
throw new C8oException(C8oExceptionMessage.fullSyncGetOrCreateDatabase(databaseName)); | ||
} | ||
c8oFullSyncDatabase.getdatabase().put(properties).catch(() => { | ||
throw new C8oException("TODO"); | ||
}); | ||
} | ||
async getResponseFromLocalCache(c8oCallRequestIdentifier: string): Promise<any> { | ||
let fullSyncDatabase = this.getOrCreateFullSyncDatabase(C8oCore.LOCAL_CACHE_DATABASE_NAME); | ||
let localCacheDocument = null; | ||
return new Promise((resolve, reject) => { | ||
fullSyncDatabase.getdatabase.get(c8oCallRequestIdentifier).then((result) => { | ||
localCacheDocument = result; | ||
let response = JSON.stringify(localCacheDocument[C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE]); | ||
let responseType = localCacheDocument[C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE_TYPE]; | ||
let expirationDate = localCacheDocument[C8oCore.LOCAL_CACHE_DOCUMENT_KEY_EXPIRATION_DATE]; | ||
let responseString: string = null; | ||
let responseTypeString: string = null; | ||
let expirationDateNumber: number = -1; | ||
if (response != null) { | ||
if (typeof response === "string") { | ||
responseString = response; | ||
} | ||
else { | ||
throw new C8oException(C8oExceptionMessage.InvalidLocalCacheResponseInformation()); | ||
} | ||
} | ||
if (responseType != null) { | ||
if (typeof responseType === "string") { | ||
responseTypeString = responseType; | ||
} | ||
else { | ||
throw new C8oException(C8oExceptionMessage.InvalidLocalCacheResponseInformation()); | ||
} | ||
} | ||
if (expirationDate != null) { | ||
if (typeof expirationDate === "number") { | ||
expirationDateNumber = expirationDate; | ||
let currentTime = new Date().getTime(); | ||
if (expirationDateNumber < currentTime) { | ||
throw new C8oUnavailableLocalCacheException(C8oExceptionMessage.timeToLiveExpired()); | ||
} | ||
} | ||
else { | ||
throw new C8oException(C8oExceptionMessage.InvalidLocalCacheResponseInformation()); | ||
} | ||
} | ||
resolve(new C8oLocalCacheResponse(responseString, responseTypeString, expirationDateNumber)); | ||
}).catch((err) => { | ||
if (err.status === 404) { | ||
resolve(new C8oUnavailableLocalCacheException(C8oExceptionMessage.localCacheDocumentJustCreated())); | ||
} | ||
else { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
} | ||
async saveResponseToLocalCache(c8oCallRequestIdentifier: string, localCacheResponse: C8oLocalCacheResponse): Promise<any> { | ||
let fullSyncDatabase: C8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(C8oCore.LOCAL_CACHE_DATABASE_NAME); | ||
return new Promise((resolve) => { | ||
fullSyncDatabase.getdatabase.get(c8oCallRequestIdentifier).then((localCacheDocument) => { | ||
let properties = {}; | ||
properties[C8oFullSync.FULL_SYNC__ID] = c8oCallRequestIdentifier; | ||
properties[C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE] = localCacheResponse.getResponse(); | ||
properties[C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE_TYPE] = localCacheResponse.getResponseType(); | ||
if (localCacheResponse.getExpirationDate() > 0) { | ||
properties[C8oCore.LOCAL_CACHE_DOCUMENT_KEY_EXPIRATION_DATE] = localCacheResponse.getExpirationDate(); | ||
} | ||
let currentRevision = localCacheDocument._rev; | ||
if (currentRevision !== null) { | ||
properties[C8oFullSyncCbl.FULL_SYNC__REV] = currentRevision; | ||
} | ||
fullSyncDatabase.getdatabase.put(properties).then((result) => { | ||
resolve(result); | ||
}); | ||
}).catch((error) => { | ||
if (error.status === 404) { | ||
let properties = {}; | ||
properties[C8oFullSync.FULL_SYNC__ID] = c8oCallRequestIdentifier; | ||
properties[C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE] = localCacheResponse.getResponse(); | ||
properties[C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE_TYPE] = localCacheResponse.getResponseType(); | ||
if (localCacheResponse.getExpirationDate() > 0) { | ||
properties[C8oCore.LOCAL_CACHE_DOCUMENT_KEY_EXPIRATION_DATE] = localCacheResponse.getExpirationDate(); | ||
} | ||
fullSyncDatabase.getdatabase.put(properties).then((result) => { | ||
resolve(result); | ||
}); | ||
} | ||
else { | ||
resolve(error); | ||
} | ||
}); | ||
}); | ||
} | ||
public addFullSyncChangeListener(db: string, listener: C8oFullSyncChangeListener) { | ||
if (db === null || db === "") { | ||
db = this.c8o.defaultDatabaseName; | ||
} | ||
let listeners: Array<Array<C8oFullSyncChangeListener>> = []; | ||
if (this.fullSyncChangeListeners[db] != null) { | ||
listeners[0] = this.fullSyncChangeListeners[db]; | ||
} | ||
else { | ||
listeners[0] = []; | ||
this.fullSyncChangeListeners[db] = listeners[0]; | ||
//noinspection UnnecessaryLocalVariableJS | ||
let evtHandler = this.getOrCreateFullSyncDatabase(db).getdatabase | ||
.changes({ | ||
since: "now", | ||
live: true, | ||
include_docs: true | ||
}).on("change", function (change) { | ||
let changes: Object = {}; | ||
let docs: Array<Object> = []; | ||
// docs["isExternal"] = false; | ||
let doc: Object = {}; | ||
doc["id"] = change["doc"]["_id"]; | ||
doc["rev"] = change["doc"]["_rev"]; | ||
doc["isConflict"] = change.doc._conflicts; | ||
if (change.source != null) { | ||
doc["sourceUrl"] = change.source; | ||
} | ||
docs.push(doc); | ||
changes["changes"] = docs; | ||
for (let handler of listeners[0]) { | ||
if (handler !== undefined) { | ||
handler.onChange(changes); | ||
} | ||
} | ||
}); | ||
this.cblChangeListeners[db] = evtHandler; | ||
} | ||
listeners[0].push(listener); | ||
} | ||
public removeFullSyncChangeListener(db: string, listener: C8oFullSyncChangeListener) { | ||
if (db === null || db === "") { | ||
db = this.c8o.defaultDatabaseName; | ||
} | ||
if (this.fullSyncChangeListeners[db] != null) { | ||
let listeners: Array<C8oFullSyncChangeListener> = this.fullSyncChangeListeners[db]; | ||
for (let list in listeners) { | ||
if (listeners[list] === listener) { | ||
delete listeners[list]; | ||
} | ||
} | ||
if (listeners.length === 0 || listeners == null) { | ||
this.getOrCreateFullSyncDatabase(db).getdatabase.cancel(); | ||
this.cblChangeListeners[db].cancel(); | ||
delete this.fullSyncChangeListeners[db]; | ||
delete this.cblChangeListeners[db]; | ||
} | ||
} | ||
} | ||
} |
@@ -5,3 +5,2 @@ "use strict"; | ||
var c8oFullSync_1 = require("./c8oFullSync"); | ||
var c8oFullSyncCbl_1 = require("./c8oFullSyncCbl"); | ||
var c8oCouchBaseLiteException_1 = require("./Exception/c8oCouchBaseLiteException"); | ||
@@ -111,3 +110,3 @@ var c8oCore_1 = require("./c8oCore"); | ||
database.get(documentId_2).then(function (doc) { | ||
c8oFullSyncCbl_1.C8oFullSyncCbl.mergeProperties(newProperties, doc); | ||
c8oFullSync_1.C8oFullSyncCbl.mergeProperties(newProperties, doc); | ||
database.put(newProperties).then(function (createdDocument) { | ||
@@ -114,0 +113,0 @@ resolve(createdDocument); |
import {C8oExceptionMessage} from "./Exception/c8oExceptionMessage"; | ||
import {C8oFullSync} from "./c8oFullSync"; | ||
import {C8oFullSyncCbl} from "./c8oFullSyncCbl" | ||
import {C8oFullSync, C8oFullSyncCbl} from "./c8oFullSync"; | ||
import {C8oCouchBaseLiteException} from "./Exception/c8oCouchBaseLiteException"; | ||
@@ -5,0 +4,0 @@ import {C8oCore} from "./c8oCore"; |
@@ -1,3 +0,2 @@ | ||
import { C8oFullSync } from "./c8oFullSync"; | ||
import { C8oFullSyncCbl } from "./c8oFullSyncCbl"; | ||
import { C8oFullSync, C8oFullSyncCbl } from "./c8oFullSync"; | ||
import { C8oResponseListener } from "./c8oResponse"; | ||
@@ -4,0 +3,0 @@ export declare class FullSyncRequestable { |
@@ -1,3 +0,2 @@ | ||
import {C8oFullSync} from "./c8oFullSync"; | ||
import {C8oFullSyncCbl} from "./c8oFullSyncCbl"; | ||
import {C8oFullSync, C8oFullSyncCbl} from "./c8oFullSync"; | ||
import { | ||
@@ -4,0 +3,0 @@ C8oResponseListener, |
@@ -6,3 +6,2 @@ export * from "./c8o/c8oCore"; | ||
export * from "./c8o/c8oFullSyncChangeListener"; | ||
export * from "./c8o/c8oFullSyncCbl"; | ||
export * from "./c8o/fullsyncpostdocumentparameter"; | ||
@@ -9,0 +8,0 @@ export * from "./c8o/c8oFullSync"; |
@@ -11,3 +11,2 @@ "use strict"; | ||
__export(require("./c8o/c8oFullSyncChangeListener")); | ||
__export(require("./c8o/c8oFullSyncCbl")); | ||
__export(require("./c8o/fullsyncpostdocumentparameter")); | ||
@@ -14,0 +13,0 @@ __export(require("./c8o/c8oFullSync")); |
@@ -6,3 +6,2 @@ export * from "./c8o/c8oCore"; | ||
export * from "./c8o/c8oFullSyncChangeListener"; | ||
export * from "./c8o/c8oFullSyncCbl"; | ||
export * from "./c8o/fullsyncpostdocumentparameter"; | ||
@@ -9,0 +8,0 @@ export * from "./c8o/c8oFullSync"; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
567738
149
9198