appsapp-cli
Version:
This command line program is the important counterpart of appsapp-module for connecting built-in or custom backend services.
1,439 lines (1,434 loc) • 125 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('class-validator'), require('class-transformer'), require('angular2-uuid'), require('object-hash'), require('rxjs'), require('unirest'), require('class-validator/decorator/decorators')) :
typeof define === 'function' && define.amd ? define(['exports', 'class-validator', 'class-transformer', 'angular2-uuid', 'object-hash', 'rxjs', 'unirest', 'class-validator/decorator/decorators'], factory) :
(factory((global['appsapp-cli'] = {}),global.classValidator,global.classTransformer,global.angular2Uuid,global.objectHash,global.rxjs,global.Unirest,global.decorators));
}(this, (function (exports,classValidator,classTransformer,angular2Uuid,objectHash,rxjs,Unirest,decorators) { 'use strict';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @record
*/
/**
* @record
*/
/**
* @record
*/
/**
* @record
*/
var PersistableModel = /** @class */ (function () {
/**
* PersistanceManager as an optional argument when changes were persisted to stable database
*/
function PersistableModel() {
var _this = this;
this.__isLoaded = false;
this.__isLoadedRequested = false;
this.__isAutosave = false;
this.uuid = '';
this.__firebaseDatabaseRoot = 'session';
this.__bindings = {};
this.__bindingsObserver = {};
this.__validator = {};
this.__validatorObserver = {};
this.__edited = {};
this.__editedObservableCallbacks = [];
this.__editedObservableObservers = [];
this.__temp = {};
this.__isOnline = true;
this.__validationErrors = {};
this.__hasValidationErrors = true;
this.__metadata = [];
this.__metadataCache = {};
this.__hasPendingChanges = false;
this.__conditionBindings = {};
this.__conditionActionIfMatches = {};
this.__conditionActionIfMatchesAction = {};
this.__conditionActionIfMatchesObserver = {};
this.__conditionActionIfMatchesRemovedProperties = {};
this.__conditionContraintsProperties = {};
this.__conditionContraintsPropertiesValue = {};
this.__conditionContraintsAffectedProperties = {};
this.tmp__hashedValues = {};
this.__listArrays = {};
this.__listArraysParentWatcher = {};
this.__isPersistableModel = true;
var /** @type {?} */ self = this;
this.__metadata = classValidator.getFromContainer(classValidator.MetadataStorage).getTargetValidationMetadatas(this.constructor, '');
// transform initally property values
this.transformAllProperties(true);
// check if all loaded metadata has corresponding properties
this.__metadata.forEach(function (metadata) {
if (_this[metadata.propertyName] == undefined) {
_this[metadata.propertyName] = null;
}
});
/**
* create observerable and observer for handling the models data changes
*/
this.__editedObservable = new rxjs.Observable(function (observer) {
self.__editedObserver = observer;
});
//this.loaded().then(() => {
self.__init();
// autovalidation
this.getChangesWithCallback(function () {
if (!_this.__isAutosave) {
self.removeConditionProperties();
classValidator.validate(self, { skipMissingProperties: true }).then(function (errors) {
if (errors.length) {
self.__hasValidationErrors = true;
}
else {
self.__hasValidationErrors = false;
}
});
}
});
// });
}
/**
*
* @return {?}
*/
PersistableModel.prototype.__init = /**
*
* @return {?}
*/
function () {
var _this = this;
var /** @type {?} */ self = this;
/**
* create observerable and observer for handling the models data changes
*/
this.__editedObservable = new rxjs.Observable(function (observer) {
self.__editedObserver = observer;
});
/**
* create observerable and observer for handling the models data changes
*/
this.__observable = new rxjs.Observable(function (observer) {
self.__observer = observer;
self.__observer.next(_this);
});
/**
* creates and update bindings for getProperty()-Method
*/
this.__observable.subscribe(function (next) {
if (self.__bindingsObserver) {
self.__editedObservableObservers.forEach(function (callback) {
if (next[callback.property] !== undefined) {
var /** @type {?} */ lastValue = null;
try {
lastValue = objectHash.sha1(next[callback.property]);
}
catch (/** @type {?} */ e) {
lastValue = next[callback.property];
}
if (lastValue !== callback.lastValue) {
callback.callback(next[callback.property]);
callback.lastValue = lastValue;
}
}
});
Object.keys(self.__bindingsObserver).forEach(function (property) {
if (next[property] !== undefined) {
self.executeConditionValidatorCircular(property);
self.__bindingsObserver[property].next(next[property]);
}
});
}
});
};
/**
* get http client
* @return {?} HttpClient
*/
PersistableModel.prototype.getHttpClient = /**
* get http client
* @return {?} HttpClient
*/
function () {
return this.__httpClient;
};
/**
* set http client
* @param {?} http
* @return {?}
*/
PersistableModel.prototype.setHttpClient = /**
* set http client
* @param {?} http
* @return {?}
*/
function (http) {
this.__httpClient = http;
return this;
};
/**
* call next method on observer
* @return {?}
*/
PersistableModel.prototype.emit = /**
* call next method on observer
* @return {?}
*/
function () {
if (this.__observer) {
this.__observer.next(this);
}
return this;
};
/**
* save with optional observable
* @param {?=} action
* @return {?}
*/
PersistableModel.prototype.saveWithPromise = /**
* save with optional observable
* @param {?=} action
* @return {?}
*/
function (action) {
var /** @type {?} */ self = this;
return new Promise(function (resolve, reject) {
self.save(action).subscribe(function (next) {
}, function (error) {
reject(error);
}, function () {
resolve();
});
});
};
/**
* execute cation
* @param {?} action
* @return {?}
*/
PersistableModel.prototype.action = /**
* execute cation
* @param {?} action
* @return {?}
*/
function (action) {
var /** @type {?} */ self = this;
var /** @type {?} */ observable = new rxjs.Observable(function (observer) {
if (self.__persistenceManager) {
self.__persistenceManager.action(self, observer, action).then(function (success) {
observer.complete();
}).catch(function (error) {
observer.error(error);
});
}
else {
observer.error('No persistence Manger provided');
}
});
return new Promise(function (resolve, reject) {
observable.subscribe(function (next) {
}, function (error) {
reject(error);
}, function () {
resolve();
});
});
};
/**
* trigger custom action
* @param {?} action
* @param {?=} interval
* @param {?=} maxExecutions
* @return {?}
*/
PersistableModel.prototype.trigger = /**
* trigger custom action
* @param {?} action
* @param {?=} interval
* @param {?=} maxExecutions
* @return {?}
*/
function (action, interval, maxExecutions) {
var /** @type {?} */ self = this;
return new rxjs.Observable(function (observer) {
self.loaded().then(function (model) {
model.loaded().then(function (model) {
model.getPersistenceManager().trigger(model, observer, {
name: 'custom',
data: {
name: action
}
}, interval, maxExecutions);
});
});
});
};
/**
* trigger a webhook url
* @param {?} url
* @param {?=} method
* @param {?=} type
* @return {?}
*/
PersistableModel.prototype.webhook = /**
* trigger a webhook url
* @param {?} url
* @param {?=} method
* @param {?=} type
* @return {?}
*/
function (url, method, type) {
var /** @type {?} */ self = this;
return new rxjs.Observable(function (observer) {
self.loaded().then(function (model) {
model.getPersistenceManager().trigger(model, observer, {
name: 'webhook',
data: {
url: url,
method: method,
type: type
}
});
});
});
};
/**
* save with optional observable
* @param {?=} action
* @return {?}
*/
PersistableModel.prototype.save = /**
* save with optional observable
* @param {?=} action
* @return {?}
*/
function (action) {
var /** @type {?} */ self = this;
return new rxjs.Observable(function (observer) {
if (typeof action === 'string') {
action = {
name: 'custom',
data: {
name: action
}
};
}
self.loaded().then(function (model) {
model.executeSave(action).subscribe(function (next) {
if (observer) {
observer.next(next);
}
}, function (error) {
if (observer) {
observer.error(error);
}
}, function () {
if (observer) {
observer.next(model);
observer.complete();
}
});
});
});
};
/**
* save model and persist if is persistable
* @param {?=} action
* @return {?}
*/
PersistableModel.prototype.executeSave = /**
* save model and persist if is persistable
* @param {?=} action
* @return {?}
*/
function (action) {
var /** @type {?} */ self = this;
Object.keys(self.__edited).forEach(function (property) {
self[property] = self.__edited[property];
});
return new rxjs.Observable(function (observer) {
self.setHasPendingChanges(true, action);
self.loaded().then(function (model) {
if (model.__persistenceManager) {
observer.next({ message: model.getMessage('submitted'), target: model });
model.__persistenceManager.save(self, observer, action).then(function (success) {
model.__edited = {};
observer.complete();
}).catch(function (error) {
model.__edited = {};
observer.error(error);
});
}
else {
observer.error('No persistence Manger provided');
model.__edited = {};
}
});
});
};
/**
* resets model
* @return {?}
*/
PersistableModel.prototype.reset = /**
* resets model
* @return {?}
*/
function () {
var /** @type {?} */ self = this;
Object.keys(self.getProperties()).forEach(function (property) {
self[property] = self.transformTypeFromMetadata(property, '');
});
self.__edited = {};
return this;
};
/**
* removes edited states
* @return {?}
*/
PersistableModel.prototype.removeEditedState = /**
* removes edited states
* @return {?}
*/
function () {
this.__edited = {};
return this;
};
/**
* get models observer
* @return {?}
*/
PersistableModel.prototype.getObserver = /**
* get models observer
* @return {?}
*/
function () {
return this.__observer;
};
/**
* get models obervable
* @return {?}
*/
PersistableModel.prototype.getObservable = /**
* get models obervable
* @return {?}
*/
function () {
return this.__observable;
};
/**
* set uuid
* @param {?=} uuid
* @return {?}
*/
PersistableModel.prototype.setUuid = /**
* set uuid
* @param {?=} uuid
* @return {?}
*/
function (uuid) {
this.uuid = uuid !== undefined ? uuid : angular2Uuid.UUID.UUID();
return this;
};
/**
* get uuid
* @return {?}
*/
PersistableModel.prototype.getUuid = /**
* get uuid
* @return {?}
*/
function () {
return this.uuid;
};
/**
* get models constructors name as an object identifier
* return {string}
* @return {?}
*/
PersistableModel.prototype.getObjectIdentifier = /**
* get models constructors name as an object identifier
* return {string}
* @return {?}
*/
function () {
return this.constructor.name;
};
/**
* set firebaseDatabase
* @param {?} firebaseDatabase
* @return {?}
*/
PersistableModel.prototype.setFirebaseDatabase = /**
* set firebaseDatabase
* @param {?} firebaseDatabase
* @return {?}
*/
function (firebaseDatabase) {
this.__firebaseDatabase = firebaseDatabase;
var /** @type {?} */ self = this;
var /** @type {?} */ connectedRef = this.__firebaseDatabase.app.database().ref(".info/connected");
connectedRef.on("value", function (snap) {
self.__isOnline = snap.val();
if (self.__persistenceManager && self.__isOnline) {
self.__persistenceManager.getObserver().next({ 'action': 'connected' });
}
if (self.__persistenceManager && !self.__isOnline) {
self.__persistenceManager.getObserver().next({ 'action': 'disconnected' });
}
});
if (this.getPersistenceManager() !== undefined) {
if (!this.getPersistenceManager()._isConnected) {
this.getPersistenceManager().getFirebase().getAuth().then(function (auth) {
auth.authState.subscribe(function (user) {
if (user && self.__persistenceManager) {
self.__persistenceManager.getObserver().next({ 'action': 'connected' });
}
self.emit();
});
});
this.getPersistenceManager().setIsConnected();
}
}
return this;
};
/**
* get firebase database
* @return {?}
*/
PersistableModel.prototype.getFirebaseDatabase = /**
* get firebase database
* @return {?}
*/
function () {
return this.__firebaseDatabase;
};
/**
* set firebase database path
* @param {?} path
* @return {?}
*/
PersistableModel.prototype.setFirebaseDatabasePath = /**
* set firebase database path
* @param {?} path
* @return {?}
*/
function (path) {
this.__firebaseDatabasePath = path;
this.registerConditionValidators(false);
return this;
};
/**
* get firebase database path
* @return {?}
*/
PersistableModel.prototype.getFirebaseDatabasePath = /**
* get firebase database path
* @return {?}
*/
function () {
return this.__firebaseDatabasePath;
};
/**
* get firebase session data path
* @param {?} path
* @return {?} string
*/
PersistableModel.prototype.getFirebaseDatabaseSessionPath = /**
* get firebase session data path
* @param {?} path
* @return {?} string
*/
function (path) {
var /** @type {?} */ a = path.split("/");
var /** @type {?} */ path = '';
var /** @type {?} */ i = 0;
a.forEach(function (segment) {
if (i == 3) {
path = path + '/data';
}
path = path + '/' + segment;
i++;
});
if (this.getFirebaseDatabasePath() !== undefined) {
return this.__firebaseDatabaseRoot + '/' + this.getFirebaseDatabasePath().substr(this.__firebaseDatabaseRoot.length + 1).split("/")[0] + '/' + this.getFirebaseDatabasePath().substr(this.__firebaseDatabaseRoot.length + 1).split("/")[1] + path.substr(1);
}
else {
return null;
}
};
/**
* set firebaseDatabaseObject
* @param {?} firebaseDatabaseObject
* @return {?}
*/
PersistableModel.prototype.setFirebaseDatabaseObject = /**
* set firebaseDatabaseObject
* @param {?} firebaseDatabaseObject
* @return {?}
*/
function (firebaseDatabaseObject) {
this.__angularFireObject = firebaseDatabaseObject;
return this;
};
/**
* get firebaseDatabaseObject
* @return {?}
*/
PersistableModel.prototype.getFirebaseDatabaseObject = /**
* get firebaseDatabaseObject
* @return {?}
*/
function () {
return this.__angularFireObject;
};
/**
* get firebaseDatabase prefix
* @return {?} string
*/
PersistableModel.prototype.getFirebaseDatabaseRoot = /**
* get firebaseDatabase prefix
* @return {?} string
*/
function () {
return this.__firebaseDatabaseRoot;
};
/**
* set firebase databse path prefix
* @param {?} path
* @return {?}
*/
PersistableModel.prototype.setFirebaseDatabaseRoot = /**
* set firebase databse path prefix
* @param {?} path
* @return {?}
*/
function (path) {
this.__firebaseDatabaseRoot = path;
return this;
};
/**
* get property
* @param {?} property
* @return {?}
*/
PersistableModel.prototype.getProperty = /**
* get property
* @param {?} property
* @return {?}
*/
function (property) {
var /** @type {?} */ self = this;
if (this.isInBackendMode()) {
return self.getPropertyValue(property);
}
else {
if (!self.__bindings[property]) {
self.__bindings[property] = new rxjs.Observable(function (observer) {
self.__bindingsObserver[property] = observer;
});
self.__bindings[property].subscribe();
if (self.__bindingsObserver[property] !== undefined) {
self.__bindingsObserver[property].next(self[property]);
}
}
return self.__bindings[property];
}
};
/**
* get observer property for using as an binding variable
* @param {?} property
* @return {?}
*/
PersistableModel.prototype.getPropertyObserver = /**
* get observer property for using as an binding variable
* @param {?} property
* @return {?}
*/
function (property) {
if (this.__bindingsObserver[property]) {
return this.__bindingsObserver[property];
}
else {
return null;
}
};
/**
* set module provider messages
* @param {?} messages
* @return {?}
*/
PersistableModel.prototype.setMessages = /**
* set module provider messages
* @param {?} messages
* @return {?}
*/
function (messages) {
this.__messages = messages;
return this;
};
/**
* get modules providers message
* @param {?} keyword
* @return {?}
*/
PersistableModel.prototype.getMessage = /**
* get modules providers message
* @param {?} keyword
* @return {?}
*/
function (keyword) {
if (this.__messages === undefined) {
return keyword;
}
return this.__messages[keyword] == undefined ? keyword : this.__messages[keyword];
};
/**
* set property value for using as an binding variable
* @param {?} property
* @param {?} value
* @return {?}
*/
PersistableModel.prototype.setProperty = /**
* set property value for using as an binding variable
* @param {?} property
* @param {?} value
* @return {?}
*/
function (property, value) {
var _this = this;
var /** @type {?} */ self = this, /** @type {?} */ autosave = false;
if (this.__isAutosave && this[property] !== value) {
autosave = true;
}
self.__editedObservableObservers.forEach(function (callback) {
if (callback.property == property && _this[property] !== value) {
var /** @type {?} */ lastValue = null;
try {
lastValue = objectHash.sha1(value);
}
catch (/** @type {?} */ e) {
lastValue = value;
}
if (lastValue !== callback.lastValue) {
callback.callback(value);
callback.lastValue = lastValue;
}
}
});
this[property] = value;
this.__edited[property] = value;
var /** @type {?} */ event = { property: property, value: value, model: this };
if (this.__editedObserver) {
this.__editedObserver.next(event);
}
this.executeConditionValidatorCircular(property);
this.executeChangesWithCallback(event);
if (autosave) {
this.save(null).subscribe(function (next) {
}, function (error) {
//
});
}
return this;
};
/**
* return current property value
* @param {?} property
* @param {?=} editing
* @return {?}
*/
PersistableModel.prototype.getPropertyValue = /**
* return current property value
* @param {?} property
* @param {?=} editing
* @return {?}
*/
function (property, editing) {
if (editing) {
return this.__edited[property] !== undefined ? this.__edited[property] : this[property];
}
else {
return this[property];
}
};
/**
* return current property value unhashed
* @param {?} property
* @return {?}
*/
PersistableModel.prototype.get = /**
* return current property value unhashed
* @param {?} property
* @return {?}
*/
function (property) {
if (this.__isLoadedRequested === false && this.getAppsAppModuleProvider()) {
this.__isLoadedRequested = true;
this.getAppsAppModuleProvider().lazyLoad(this);
}
return this.getHashedValue(this[property]);
};
/**
* get properties
* @param {?=} stringify
* @return {?}
*/
PersistableModel.prototype.getProperties = /**
* get properties
* @param {?=} stringify
* @return {?}
*/
function (stringify) {
var /** @type {?} */ properties = {}, /** @type {?} */ self = this;
Object.keys(self).forEach(function (property) {
if (property.substr(0, 1) !== '_' && property.substr(0, 5) !== 'tmp__') {
if (stringify) {
properties[property] = self.__toString(property);
}
else {
properties[property] = self.getPropertyValue(property);
}
}
});
return properties;
};
/**
* get properties keys
* @return {?}
*/
PersistableModel.prototype.getPropertiesKeys = /**
* get properties keys
* @return {?}
*/
function () {
var /** @type {?} */ keys = [], /** @type {?} */ self = this, /** @type {?} */ keysO = {};
Object.keys(self).forEach(function (property) {
if (keysO[property] === undefined && property.substr(0, 1) !== '_' && property.substr(0, 5) !== 'tmp__') {
keysO[property] = true;
keys.push(property);
}
});
if (this.__metadata) {
this.__metadata.forEach(function (metadata) {
if (keysO[metadata.propertyName] == undefined) {
keysO[metadata.propertyName] = true;
keys.push(metadata.propertyName);
}
});
}
return keys;
};
/**
* get properties
* @return {?}
*/
PersistableModel.prototype.convertListPropertiesFromArrayToObject = /**
* get properties
* @return {?}
*/
function () {
var /** @type {?} */ self = this;
self.getPropertiesKeys().forEach(function (property) {
if (self.getMetadataValue(property, 'isList', null, 'usePropertyAsUuid')) {
var /** @type {?} */ tmp_1 = {}, /** @type {?} */ usePropertyAsUuid_1 = self.getMetadataValue(property, 'isList', null, 'usePropertyAsUuid');
if (usePropertyAsUuid_1 && usePropertyAsUuid_1 !== undefined && usePropertyAsUuid_1 !== true && self.getPropertyValue(property) && self.getPropertyValue(property).length) {
self.getPropertyValue(property).forEach(function (val) {
if (val[usePropertyAsUuid_1] !== undefined) {
tmp_1[val[usePropertyAsUuid_1]] = val;
}
});
self[property] = tmp_1;
}
}
});
return this;
};
/**
* add a new list entry
* @param {?} property
* @param {?=} data (json object, persistable model or array of those
* @param {?=} uuid string
* @param {?=} internal boolean
* @return {?}
*/
PersistableModel.prototype.add = /**
* add a new list entry
* @param {?} property
* @param {?=} data (json object, persistable model or array of those
* @param {?=} uuid string
* @param {?=} internal boolean
* @return {?}
*/
function (property, data, uuid, internal) {
var /** @type {?} */ self = this, /** @type {?} */ model = self;
if (data === undefined) {
data = {};
}
if (model.getMetadataValue(property, 'isList')) {
var /** @type {?} */ toAddModels = [];
var /** @type {?} */ toCreateModels = [];
if (data.__isPersistableModel !== undefined) {
toAddModels.push(data);
}
else if (typeof data == 'object' && data.length !== undefined) {
data.forEach(function (d) {
if (d.__isPersistableModel !== undefined) {
toAddModels.push(d);
}
else {
toCreateModels.push(d);
}
});
}
else {
if (typeof data == 'string') {
var /** @type {?} */ d = [];
d.push(data);
toCreateModels.push(d);
}
else {
toCreateModels.push(data);
}
}
toCreateModels.forEach(function (d) {
if (uuid === undefined || uuid === null) {
uuid = d !== undefined ? d[model.getMetadataValue(property, 'isList', null, 'usePropertyAsUuid')] : null;
}
if (uuid === undefined || !uuid) {
uuid = data['uuid'] == undefined ? angular2Uuid.UUID.UUID() : data['uuid'];
}
if (typeof d == 'object' && d.length == 1 && d[0] !== undefined) {
d = d[0];
}
var /** @type {?} */ n = null;
if (model.isInBackendMode()) {
// backend mode
var /** @type {?} */ constructor = typeof model.getMetadataValue(property, 'isList') == 'function' ? model.getMetadataValue(property, 'isList') : global[model.getMetadataValue(property, 'isList')];
n = new constructor();
if (uuid !== undefined) {
n.setUuid(uuid);
}
else {
n.setUuid(angular2Uuid.UUID.UUID());
}
if (d !== undefined) {
n.loadJson(d);
}
toAddModels.push(n);
}
else {
n = model.createNewLazyLoadedPersistableModel(model.getAppsAppModuleProvider(), model.getMetadataValue(property, 'isList'), uuid, d);
var /** @type {?} */ usePropertyAsUuid = model.getMetadataValue(property, 'isList', null, 'usePropertyAsUuid');
if (usePropertyAsUuid) {
n.watch(usePropertyAsUuid, function (uuid) {
if (uuid && typeof uuid == 'string' && uuid.length) {
n.setUuid(uuid);
model.refreshListArray(property);
}
});
}
n.setParent(self);
if (self.__isAutosave) {
n.autosave();
}
// force conditions to be calculated initially
// force conditions to be calculated initially
if (!n.isInBackendMode()) {
Object.keys(n.__conditionActionIfMatchesAction).forEach(function (property) {
n.getProperty(property).subscribe(function (value) {
// skip
});
});
Object.keys(n.__conditionActionIfMatchesRemovedProperties).forEach(function (property) {
n.getProperty(property).subscribe(function (value) {
// skip
});
});
}
toAddModels.push(n);
}
});
toAddModels.forEach(function (d) {
model[property].push(d);
});
if (!this.isInBackendMode() && this.__isAutosave) {
this.save().subscribe();
}
model.refreshListArray(property, model[property]);
}
return this;
};
/**
* remove a new list entry
* @param {?} property
* @param {?=} uuidOrObject string or array set of string or PersistableModel or array set of PersistableModel
* @return {?} this
*/
PersistableModel.prototype.remove = /**
* remove a new list entry
* @param {?} property
* @param {?=} uuidOrObject string or array set of string or PersistableModel or array set of PersistableModel
* @return {?} this
*/
function (property, uuidOrObject) {
if (this.getMetadataValue(property, 'isList')) {
var /** @type {?} */ toRemoveUuids = {};
var /** @type {?} */ afterRemovedValue = [];
if (typeof uuidOrObject === 'string') {
toRemoveUuids[uuidOrObject] = true;
}
else {
if (uuidOrObject.__isPersistableModel) {
toRemoveUuids[uuidOrObject.getUuid()] = true;
}
else {
uuidOrObject.forEach(function (o) {
if (uuidOrObject.__isPersistableModel) {
toRemoveUuids[o.getUuid()] = true;
}
else {
toRemoveUuids[o] = true;
}
});
}
}
this.getPropertyValue(property).forEach(function (m) {
if (m.getUuid().length === 0 || toRemoveUuids[m.getUuid()] === undefined) {
afterRemovedValue.push(m);
}
});
this.setProperty(property, this.transformTypeFromMetadata(property, afterRemovedValue));
}
else {
this.setProperty(property, this.transformTypeFromMetadata(property, null));
}
if (this.__isAutosave) {
this.save().subscribe();
}
return this;
};
/**
* clear list entry
* @param {?} property
* @return {?} this
*/
PersistableModel.prototype.clear = /**
* clear list entry
* @param {?} property
* @return {?} this
*/
function (property) {
this[property] = this.transformTypeFromMetadata(property, '');
this.setProperty(property, this[property]);
this.emit();
if (this.__isAutosave) {
this.save().subscribe();
}
return this;
};
/**
* return string representative from given property value
* @param {?=} property
* @return {?}
*/
PersistableModel.prototype.__toString = /**
* return string representative from given property value
* @param {?=} property
* @return {?}
*/
function (property) {
if (property === undefined) {
return this.serialize();
}
var /** @type {?} */ s = null, /** @type {?} */ self = this;
switch (this.getType(property)) {
case 'text':
s = self.getPropertyValue(property);
break;
case 'numberplain':
s = self.getPropertyValue(property);
break;
default:
if (typeof self.getPropertyValue(property) !== 'string') {
s = JSON.stringify(self.getPropertyValue(property));
}
else {
s = self.getPropertyValue(property);
}
}
return s;
};
/**
* set persistenceManager
* @param {?} persistenceManager
* @return {?}
*/
PersistableModel.prototype.setPersistenceManager = /**
* set persistenceManager
* @param {?} persistenceManager
* @return {?}
*/
function (persistenceManager) {
if (persistenceManager !== undefined) {
this.__persistenceManager = persistenceManager;
}
if (this.uuid.length == 0) {
this.uuid = angular2Uuid.UUID.UUID();
}
return this;
};
/**
* valid this object
* @param {?=} softcheck
* @return {?}
*/
PersistableModel.prototype.validate = /**
* valid this object
* @param {?=} softcheck
* @return {?}
*/
function (softcheck) {
var /** @type {?} */ self = this;
return new Promise(function (resolve, reject) {
self.removeConditionProperties();
classValidator.validate(self, { skipMissingProperties: true }).then(function (errors) {
// errors is an array of validation errors
if (errors.length > 0) {
if (softcheck) {
resolve(self);
}
else {
reject(errors);
}
self.__validationErrors = {};
errors.forEach(function (error) {
self.__validationErrors[error.property] = error;
});
}
else {
resolve(self);
self.__validationErrors = {};
}
Object.keys(self.__validatorObserver).forEach(function (property) {
if (self.__validationErrors[property] === undefined) {
self.__validatorObserver[property].next(false);
}
else {
self.__validatorObserver[property].next(self.__validationErrors[property]);
}
});
});
});
};
/**
* remove properties with invalid condition validators
* @return {?}
*/
PersistableModel.prototype.removeConditionProperties = /**
* remove properties with invalid condition validators
* @return {?}
*/
function () {
var /** @type {?} */ self = this;
if (self.__conditionActionIfMatchesRemovedProperties) {
Object.keys(self.__conditionActionIfMatchesRemovedProperties).forEach(function (property) {
if (self.__conditionActionIfMatchesRemovedProperties[property]) {
if (self[property] !== undefined) {
self.__temp[property] = self[property];
delete self[property];
}
}
});
}
return this;
};
/**
* get validation observable for given property
* @param {?} property
* @return {?}
*/
PersistableModel.prototype.getValidation = /**
* get validation observable for given property
* @param {?} property
* @return {?}
*/
function (property) {
var /** @type {?} */ self = this;
if (self.__validator[property] === undefined) {
self.__validator[property] = new rxjs.Observable(function (observer) {
self.__validatorObserver[property] = observer;
});
}
return self.__validator[property];
};
/**
* get condition observable for given property
* @param {?} property
* @return {?}
*/
PersistableModel.prototype.getCondition = /**
* get condition observable for given property
* @param {?} property
* @return {?}
*/
function (property) {
var _this = this;
if (this.__conditionActionIfMatches[property] == undefined) {
if (Object.keys(this.__conditionActionIfMatches).length) {
this.registerConditionValidators(true);
}
if (this.__conditionActionIfMatches[property] === undefined) {
this.__conditionActionIfMatches[property] = new rxjs.Observable(function (observer) {
_this.__conditionActionIfMatchesObserver[property] = observer;
});
}
}
return this.__conditionActionIfMatches[property];
};
/**
* is the object/property on editing state
* @param {?=} property
* @return {?}
*/
PersistableModel.prototype.hasChanges = /**
* is the object/property on editing state
* @param {?=} property
* @return {?}
*/
function (property) {
if (property) {
return !(this.__edited[property] === undefined);
}
else {
return (Object.keys(this.__edited).length) ? true : false;
}
};
/**
* load json data
* @param {?} json
* @param {?=} clone boolean
* @return {?}
*/
PersistableModel.prototype.loadJson = /**
* load json data
* @param {?} json
* @param {?=} clone boolean
* @return {?}
*/
function (json, clone) {
var /** @type {?} */ self = this;
json = typeof json == 'string' ? JSON.parse(json) : json;
var /** @type {?} */ model = /** @type {?} */ (classTransformer.plainToClass(/** @type {?} */ (this.constructor), json, { excludePrefixes: ["__"] }));
if (model) {
if (clone === true || json === null) {
return model;
}
else {
model['tmp__hashedValues'] = {};
if (self['tmp__hashedValues'] === undefined) {
self['tmp__hashedValues'] = {};
}
if (json['tmp__hashedValues'] !== undefined && json['tmp__hashedValues'] !== null) {
Object.keys(json['tmp__hashedValues']).forEach(function (key) {
self['tmp__hashedValues'][key] = json['tmp__hashedValues'][key];
model['tmp__hashedValues'][key] = json['tmp__hashedValues'][key];
});
}
Object.keys(json).forEach(function (property) {
if (property.substr(0, 2) !== '__' || property.substr(0, 5) == 'tmp__') {
if ((self.isInBackendMode() || self.__edited[property] === undefined || self.__edited[property] === null)) {
if (property.substr(0, 5) == 'tmp__' || property.substr(0, 1) == '_') {
self[property] = model[property];
}
else {
self[property] = self.transformTypeFromMetadata(property, model[property]);
}
}
}
});
if (self.getParent()) {
// update external changes to parent model
self.getParent().save().subscribe();
}
self.refreshAllListArrays();
self.validate().then(function (success) {
self.emit();
}).catch(function (error) {
Object.keys(error).forEach(function (e) {
self['__validationErrors'][e.property] = true;
});
});
}
}
return self;
};
/**
* transform type from metadata to avoid non matching data types
* @param {?} property
* @param {?} value
* @return {?}
*/
PersistableModel.prototype.transformTypeFromMetadata = /**
* transform type from metadata to avoid non matching data types
* @param {?} property
* @param {?} value
* @return {?}
*/
function (property, value) {
return this.transformTypeFromMetadataExecute(property, value);
};
/**
* transform type from metadata to avoid non matching data types
* @param {?} property
* @param {?} value
* @return {?}
*/
PersistableModel.prototype.transformTypeFromMetadataExecute = /**
* transform type from metadata to avoid non matching data types
* @param {?} property
* @param {?} value
* @return {?}
*/
function (property, value) {
var _this = this;
var /** @type {?} */ self = this;
if (typeof value === 'function') {
return value;
}
if (this.hasMetadata(property, 'isBoolean')) {
return typeof value == 'boolean' ? value : false;
}
if (this.hasMetadata(property, 'isTime')) {
return typeof value == 'string' ? new Date(value) : (value ? value : new Date());
}
if (this.hasMetadata(property, 'isDate')) {
return value ? new Date(value) : (value ? value : new Date());
}
if (this.hasMetadata(property, 'isInt')) {
var /** @type {?} */ v = typeof value == 'number' ? value : parseInt(value);
return isNaN(v) || typeof v !== 'number' ? 0 : v;
}
if (this.hasMetadata(property, 'isNumber')) {
return value === undefined || typeof value !== 'number' ? 0 : value;
}
if (this.hasMetadata(property, 'isCalendar')) {
return value ? new Date(value) : (value ? value : new Date());
}
if (this.hasMetadata(property, 'isBirthDate')) {
return value ? new Date(value) : (value ? value : new Date());
}
if (this.hasMetadata(property, 'isDateRange')) {
return typeof value == 'object' ? value : [];
}
if (this.hasMetadata(property, 'isList')) {
if (value && typeof value.forEach !== 'function') {
var /** @type {?} */ tmp = [];
Object.keys(value).forEach(function (v) {
tmp.push(value[v]);
});
value = tmp;
}
if (!value) {
value = [];
}
if (!this[property]) {
this[property] = [];
}
if (self.isInBackendMode()) {
this[property] = [];
value.forEach(function (itemOriginal) {
var /** @type {?} */ uuid = itemOriginal[self.getMetadataValue(property, 'isList', null, 'usePropertyAsUuid')];
_this.add(property, itemOriginal, uuid, true);
});
}
else {
// merge lists values
var /** @type {?} */ uuidsOld_1 = {}, /** @type {?} */ uuidsNew_1 = {}, /** @type {?} */ uuidToRemove_1 = {};
value.forEach(function (i) {
if (i.uuid !== undefined) {
uuidsNew_1[i.uuid] = true;
}
});
this[property].forEach(function (i) {
if (i.uuid !== undefined) {
uuidsOld_1[i.uuid] = i;
}
if (uuidsNew_1[i.uuid] === undefined) {
uuidToRemove_1[i.uuid] = true;
}
});
// add and update
value.forEach(function (itemOriginal) {
var /** @type {?} */ uuid = itemOriginal[self.getMetadataValue(property, 'isList', null, 'usePropertyAsUuid')];
if (uuid === undefined && itemOriginal.uuid !== undefined) {
uuid = itemOriginal.uuid;
}
if (uuidsOld_1[uuid] !== undefined) {
if (itemOriginal.__isPersistableModel === undefined) {
Object.keys(itemOriginal).forEach(function (property) {
if (property.substr(0, 2) !== '__' || property.substr(0, 5) == 'tmp__') {
//uuidsOld[uuid][property] = uuidsOld[uuid].transformTypeFromMetadata(property, itemOriginal[property]);
//uuidsOld[uuid][property] = uuidsOld[uuid].transformTypeFromMetadata(property, itemOriginal[property]);
uuidsOld_1[uuid].setProperty(property, uuidsOld_1[uuid].transformTypeFromMetadata(property, itemOriginal[property]));
}
});
}
else {
uuidsOld_1[uuid] = itemOriginal;
}
}
else {
_this.add(property, itemOriginal, uuid, true);
}
});
// remove
this[property].forEach(function (item, i) {
if (uuidToRemove_1[item.uuid] === true) {
_this[property].splice(i, 1);
try {
delete _this[property][item.uuid];
}
catch (/** @type {?} */ e) {
//
}
}
});
}
this.refreshListArray(property);
return this[property];
}
if (this.hasMetadata(property, 'isSelect')) {
if (this.isInBackendMode()) {
var /** @type {?} */ values = typeof value == 'object' ? value : [];
var /** @type {?} */ realValues_1 = [];
if (values && values.length) {
values.forEach(function (val) {
realValues_1.push(self.getHashedValue(val));
});
value = realValues_1;
}
else {
value = values;
}
this.setProperty(property, value);
}
this.executeConditionValidatorCircular(property);
}
return value === null ? '' : value;
};
/**
* Transform all properties
* @param {?=} sync boolean
* @return {?}
*/
PersistableModel.prototype.transformAllProperties = /**
* Transform all properties
* @param {?=} sync boolean
* @return {?}
*/
function (sync) {
var /** @type {?} */ self = this;
if (self.isInBackendMode() || sync === tru