2018-01-24 14:06:29 +00:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { CoreEventsProvider } from './events';
|
|
|
|
import { CoreSitesProvider } from './sites';
|
2019-01-30 09:29:01 +00:00
|
|
|
import { SQLiteDBTableSchema } from '@classes/sqlitedb';
|
2018-01-24 14:06:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Service that provides some features regarding synchronization.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class CoreSyncProvider {
|
|
|
|
|
|
|
|
// Variables for the database.
|
2018-03-08 12:25:20 +00:00
|
|
|
protected SYNC_TABLE = 'sync';
|
2019-01-30 09:29:01 +00:00
|
|
|
protected tableSchema: SQLiteDBTableSchema = {
|
2018-03-08 12:25:20 +00:00
|
|
|
name: this.SYNC_TABLE,
|
2018-01-24 14:06:29 +00:00
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
name: 'component',
|
|
|
|
type: 'TEXT',
|
|
|
|
notNull: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'id',
|
2018-03-08 12:25:20 +00:00
|
|
|
type: 'TEXT',
|
2018-01-24 14:06:29 +00:00
|
|
|
notNull: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'time',
|
|
|
|
type: 'INTEGER'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'warnings',
|
|
|
|
type: 'TEXT'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
primaryKeys: ['component', 'id']
|
|
|
|
};
|
|
|
|
|
|
|
|
// Store blocked sync objects.
|
2018-01-29 09:05:20 +00:00
|
|
|
protected blockedItems: { [siteId: string]: { [blockId: string]: { [operation: string]: boolean } } } = {};
|
2018-01-24 14:06:29 +00:00
|
|
|
|
|
|
|
constructor(eventsProvider: CoreEventsProvider, private sitesProvider: CoreSitesProvider) {
|
|
|
|
this.sitesProvider.createTableFromSchema(this.tableSchema);
|
|
|
|
|
|
|
|
// Unblock all blocks on logout.
|
|
|
|
eventsProvider.on(CoreEventsProvider.LOGOUT, (data) => {
|
|
|
|
this.clearAllBlocks(data.siteId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block a component and ID so it cannot be synchronized.
|
|
|
|
*
|
|
|
|
* @param {string} component Component name.
|
2018-05-14 08:18:21 +00:00
|
|
|
* @param {string | number} id Unique ID per component.
|
2018-01-24 14:06:29 +00:00
|
|
|
* @param {string} [operation] Operation name. If not defined, a default text is used.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
*/
|
2018-05-14 08:18:21 +00:00
|
|
|
blockOperation(component: string, id: string | number, operation?: string, siteId?: string): void {
|
2018-01-24 14:06:29 +00:00
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncBlockId(component, id);
|
|
|
|
|
|
|
|
if (!this.blockedItems[siteId]) {
|
|
|
|
this.blockedItems[siteId] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.blockedItems[siteId][uniqueId]) {
|
|
|
|
this.blockedItems[siteId][uniqueId] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
operation = operation || '-';
|
|
|
|
|
|
|
|
this.blockedItems[siteId][uniqueId][operation] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all blocks for a site or all sites.
|
|
|
|
*
|
|
|
|
* @param {string} [siteId] If set, clear the blocked objects only for this site. Otherwise clear them for all sites.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
clearAllBlocks(siteId?: string): void {
|
2018-01-24 14:06:29 +00:00
|
|
|
if (siteId) {
|
|
|
|
delete this.blockedItems[siteId];
|
|
|
|
} else {
|
|
|
|
this.blockedItems = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all blocks for a certain component.
|
|
|
|
*
|
|
|
|
* @param {string} component Component name.
|
2018-05-14 08:18:21 +00:00
|
|
|
* @param {string | number} id Unique ID per component.
|
2018-01-24 14:06:29 +00:00
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
*/
|
2018-05-14 08:18:21 +00:00
|
|
|
clearBlocks(component: string, id: string | number, siteId?: string): void {
|
2018-01-24 14:06:29 +00:00
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncBlockId(component, id);
|
|
|
|
if (this.blockedItems[siteId]) {
|
|
|
|
delete this.blockedItems[siteId][uniqueId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 12:25:20 +00:00
|
|
|
/**
|
|
|
|
* Returns a sync record.
|
|
|
|
* @param {string} component Component name.
|
|
|
|
* @param {string | number} id Unique ID per component.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @return {Promise<any>} Record if found or reject.
|
|
|
|
*/
|
|
|
|
getSyncRecord(component: string, id: string | number, siteId?: string): Promise<any> {
|
|
|
|
return this.sitesProvider.getSiteDb(siteId).then((db) => {
|
|
|
|
return db.getRecord(this.SYNC_TABLE, { component: component, id: id });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts or Updates info of a sync record.
|
|
|
|
* @param {string} component Component name.
|
|
|
|
* @param {string | number} id Unique ID per component.
|
|
|
|
* @param {any} data Data that updates the record.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @return {Promise<any>} Promise resolved with done.
|
|
|
|
*/
|
|
|
|
insertOrUpdateSyncRecord(component: string, id: string | number, data: any, siteId?: string): Promise<any> {
|
|
|
|
return this.sitesProvider.getSiteDb(siteId).then((db) => {
|
|
|
|
data.component = component;
|
|
|
|
data.id = id;
|
|
|
|
|
2018-03-20 14:44:26 +00:00
|
|
|
return db.insertRecord(this.SYNC_TABLE, data);
|
2018-03-08 12:25:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:06:29 +00:00
|
|
|
/**
|
|
|
|
* Convenience function to create unique identifiers for a component and id.
|
|
|
|
*
|
|
|
|
* @param {string} component Component name.
|
2018-05-14 08:18:21 +00:00
|
|
|
* @param {string | number} id Unique ID per component.
|
2018-01-24 14:06:29 +00:00
|
|
|
* @return {string} Unique sync id.
|
|
|
|
*/
|
2018-05-14 08:18:21 +00:00
|
|
|
protected getUniqueSyncBlockId(component: string, id: string | number): string {
|
2018-01-24 14:06:29 +00:00
|
|
|
return component + '#' + id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a component is blocked.
|
|
|
|
* One block can have different operations. Here we check how many operations are being blocking the object.
|
|
|
|
*
|
|
|
|
* @param {string} component Component name.
|
2018-05-14 08:18:21 +00:00
|
|
|
* @param {string | number} id Unique ID per component.
|
2018-01-24 14:06:29 +00:00
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
* @return {boolean} Whether it's blocked.
|
|
|
|
*/
|
2018-05-14 08:18:21 +00:00
|
|
|
isBlocked(component: string, id: string | number, siteId?: string): boolean {
|
2018-01-24 14:06:29 +00:00
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
|
|
|
|
if (!this.blockedItems[siteId]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncBlockId(component, id);
|
|
|
|
if (!this.blockedItems[siteId][uniqueId]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.keys(this.blockedItems[siteId][uniqueId]).length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unblock an operation on a component and ID.
|
|
|
|
*
|
|
|
|
* @param {string} component Component name.
|
2018-05-14 08:18:21 +00:00
|
|
|
* @param {string | number} id Unique ID per component.
|
2018-01-24 14:06:29 +00:00
|
|
|
* @param {string} [operation] Operation name. If not defined, a default text is used.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
|
|
|
*/
|
2018-05-14 08:18:21 +00:00
|
|
|
unblockOperation(component: string, id: string | number, operation?: string, siteId?: string): void {
|
2018-01-24 14:06:29 +00:00
|
|
|
operation = operation || '-';
|
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncBlockId(component, id);
|
|
|
|
|
|
|
|
if (this.blockedItems[siteId] && this.blockedItems[siteId][uniqueId]) {
|
|
|
|
delete this.blockedItems[siteId][uniqueId][operation];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|