Browse Source

UI: Add import/export tenant profile

pull/4506/head
Vladyslav_Prykhodko 5 years ago
committed by Andrew Shvayka
parent
commit
5976d51729
  1. 51
      ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts
  2. 48
      ui-ngx/src/app/modules/home/pages/tenant-profile/tenant-profiles-table-config.resolver.ts
  3. 6
      ui-ngx/src/assets/locale/locale.constant-en_US.json

51
ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts

@ -57,6 +57,8 @@ import { RuleChainService } from '@core/http/rule-chain.service';
import { FiltersInfo } from '@shared/models/query/query.models'; import { FiltersInfo } from '@shared/models/query/query.models';
import { DeviceProfileService } from '@core/http/device-profile.service'; import { DeviceProfileService } from '@core/http/device-profile.service';
import { DeviceProfile } from '@shared/models/device.models'; import { DeviceProfile } from '@shared/models/device.models';
import { TenantProfile } from '@shared/models/tenant.model';
import { TenantProfileService } from '@core/http/tenant-profile.service';
// @dynamic // @dynamic
@Injectable() @Injectable()
@ -70,6 +72,7 @@ export class ImportExportService {
private dashboardUtils: DashboardUtilsService, private dashboardUtils: DashboardUtilsService,
private widgetService: WidgetService, private widgetService: WidgetService,
private deviceProfileService: DeviceProfileService, private deviceProfileService: DeviceProfileService,
private tenantProfileService: TenantProfileService,
private entityService: EntityService, private entityService: EntityService,
private ruleChainService: RuleChainService, private ruleChainService: RuleChainService,
private utils: UtilsService, private utils: UtilsService,
@ -434,7 +437,7 @@ export class ImportExportService {
(deviceProfile) => { (deviceProfile) => {
let name = deviceProfile.name; let name = deviceProfile.name;
name = name.toLowerCase().replace(/\W/g, '_'); name = name.toLowerCase().replace(/\W/g, '_');
this.exportToPc(this.prepareDeviceProfileExport(deviceProfile), name); this.exportToPc(this.prepareProfileExport(deviceProfile), name);
}, },
(e) => { (e) => {
this.handleExportError(e, 'device-profile.export-failed-error'); this.handleExportError(e, 'device-profile.export-failed-error');
@ -460,6 +463,37 @@ export class ImportExportService {
); );
} }
public exportTenantProfile(tenantProfileId: string) {
this.tenantProfileService.getTenantProfile(tenantProfileId).subscribe(
(tenantProfile) => {
let name = tenantProfile.name;
name = name.toLowerCase().replace(/\W/g, '_');
this.exportToPc(this.prepareProfileExport(tenantProfile), name);
},
(e) => {
this.handleExportError(e, 'tenant-profile.export-failed-error');
}
);
}
public importTenantProfile(): Observable<TenantProfile> {
return this.openImportDialog('tenant-profile.import', 'tenant-profile.tenant-profile-file').pipe(
mergeMap((tenantProfile: TenantProfile) => {
if (!this.validateImportedTenantProfile(tenantProfile)) {
this.store.dispatch(new ActionNotificationShow(
{message: this.translate.instant('tenant-profile.invalid-tenant-profile-file-error'),
type: 'error'}));
throw new Error('Invalid tenant profile file');
} else {
return this.tenantProfileService.saveTenantProfile(tenantProfile);
}
}),
catchError(() => {
return of(null);
})
);
}
public exportJSZip(data: object, filename: string) { public exportJSZip(data: object, filename: string) {
import('jszip').then((JSZip) => { import('jszip').then((JSZip) => {
const jsZip = new JSZip.default(); const jsZip = new JSZip.default();
@ -517,6 +551,13 @@ export class ImportExportService {
return true; return true;
} }
private validateImportedTenantProfile(tenantProfile: TenantProfile): boolean {
return isDefined(tenantProfile.name)
&& isDefined(tenantProfile.profileData)
&& isDefined(tenantProfile.isolatedTbCore)
&& isDefined(tenantProfile.isolatedTbRuleEngine);
}
private sumObject(obj1: any, obj2: any): any { private sumObject(obj1: any, obj2: any): any {
Object.keys(obj2).map((key) => { Object.keys(obj2).map((key) => {
if (isObject(obj2[key])) { if (isObject(obj2[key])) {
@ -798,10 +839,10 @@ export class ImportExportService {
return dashboard; return dashboard;
} }
private prepareDeviceProfileExport(deviceProfile: DeviceProfile): DeviceProfile { private prepareProfileExport<T extends DeviceProfile|TenantProfile>(profile: T): T {
deviceProfile = this.prepareExport(deviceProfile); profile = this.prepareExport(profile);
deviceProfile.default = false; profile.default = false;
return deviceProfile; return profile;
} }
private prepareExport(data: any): any { private prepareExport(data: any): any {

48
ui-ngx/src/app/modules/home/pages/tenant-profile/tenant-profiles-table-config.resolver.ts

@ -21,7 +21,8 @@ import {
checkBoxCell, checkBoxCell,
DateEntityTableColumn, DateEntityTableColumn,
EntityTableColumn, EntityTableColumn,
EntityTableConfig EntityTableConfig,
HeaderActionDescriptor
} from '@home/models/entity/entities-table-config.models'; } from '@home/models/entity/entities-table-config.models';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { DatePipe } from '@angular/common'; import { DatePipe } from '@angular/common';
@ -31,6 +32,7 @@ import { TenantProfileService } from '@core/http/tenant-profile.service';
import { TenantProfileComponent } from '../../components/profile/tenant-profile.component'; import { TenantProfileComponent } from '../../components/profile/tenant-profile.component';
import { TenantProfileTabsComponent } from './tenant-profile-tabs.component'; import { TenantProfileTabsComponent } from './tenant-profile-tabs.component';
import { DialogService } from '@core/services/dialog.service'; import { DialogService } from '@core/services/dialog.service';
import { ImportExportService } from '@home/components/import-export/import-export.service';
@Injectable() @Injectable()
export class TenantProfilesTableConfigResolver implements Resolve<EntityTableConfig<TenantProfile>> { export class TenantProfilesTableConfigResolver implements Resolve<EntityTableConfig<TenantProfile>> {
@ -38,6 +40,7 @@ export class TenantProfilesTableConfigResolver implements Resolve<EntityTableCon
private readonly config: EntityTableConfig<TenantProfile> = new EntityTableConfig<TenantProfile>(); private readonly config: EntityTableConfig<TenantProfile> = new EntityTableConfig<TenantProfile>();
constructor(private tenantProfileService: TenantProfileService, constructor(private tenantProfileService: TenantProfileService,
private importExport: ImportExportService,
private translate: TranslateService, private translate: TranslateService,
private datePipe: DatePipe, private datePipe: DatePipe,
private dialogService: DialogService) { private dialogService: DialogService) {
@ -59,6 +62,12 @@ export class TenantProfilesTableConfigResolver implements Resolve<EntityTableCon
); );
this.config.cellActionDescriptors.push( this.config.cellActionDescriptors.push(
{
name: this.translate.instant('tenant-profile.export'),
icon: 'file_download',
isEnabled: () => true,
onAction: ($event, entity) => this.exportTenantProfile($event, entity)
},
{ {
name: this.translate.instant('tenant-profile.set-default'), name: this.translate.instant('tenant-profile.set-default'),
icon: 'flag', icon: 'flag',
@ -80,6 +89,7 @@ export class TenantProfilesTableConfigResolver implements Resolve<EntityTableCon
this.config.onEntityAction = action => this.onTenantProfileAction(action); this.config.onEntityAction = action => this.onTenantProfileAction(action);
this.config.deleteEnabled = (tenantProfile) => tenantProfile && !tenantProfile.default; this.config.deleteEnabled = (tenantProfile) => tenantProfile && !tenantProfile.default;
this.config.entitySelectionEnabled = (tenantProfile) => tenantProfile && !tenantProfile.default; this.config.entitySelectionEnabled = (tenantProfile) => tenantProfile && !tenantProfile.default;
this.config.addActionDescriptors = this.configureAddActions();
} }
resolve(): EntityTableConfig<TenantProfile> { resolve(): EntityTableConfig<TenantProfile> {
@ -88,6 +98,25 @@ export class TenantProfilesTableConfigResolver implements Resolve<EntityTableCon
return this.config; return this.config;
} }
configureAddActions(): Array<HeaderActionDescriptor> {
const actions: Array<HeaderActionDescriptor> = [];
actions.push(
{
name: this.translate.instant('tenant-profile.create-tenant-profile'),
icon: 'insert_drive_file',
isEnabled: () => true,
onAction: ($event) => this.config.table.addEntity($event)
},
{
name: this.translate.instant('tenant-profile.import'),
icon: 'file_upload',
isEnabled: () => true,
onAction: ($event) => this.importTenantProfile($event)
}
);
return actions;
}
setDefaultTenantProfile($event: Event, tenantProfile: TenantProfile) { setDefaultTenantProfile($event: Event, tenantProfile: TenantProfile) {
if ($event) { if ($event) {
$event.stopPropagation(); $event.stopPropagation();
@ -110,6 +139,23 @@ export class TenantProfilesTableConfigResolver implements Resolve<EntityTableCon
); );
} }
importTenantProfile($event: Event) {
this.importExport.importTenantProfile().subscribe(
(deviceProfile) => {
if (deviceProfile) {
this.config.table.updateData();
}
}
);
}
exportTenantProfile($event: Event, tenantProfile: TenantProfile) {
if ($event) {
$event.stopPropagation();
}
this.importExport.exportTenantProfile(tenantProfile.id.id);
}
onTenantProfileAction(action: EntityAction<TenantProfile>): boolean { onTenantProfileAction(action: EntityAction<TenantProfile>): boolean {
switch (action.action) { switch (action.action) {
case 'setDefault': case 'setDefault':

6
ui-ngx/src/assets/locale/locale.constant-en_US.json

@ -2440,6 +2440,12 @@
"set-default-tenant-profile-text": "After the confirmation the tenant profile will be marked as default and will be used for new tenants with no profile specified.", "set-default-tenant-profile-text": "After the confirmation the tenant profile will be marked as default and will be used for new tenants with no profile specified.",
"no-tenant-profiles-found": "No tenant profiles found.", "no-tenant-profiles-found": "No tenant profiles found.",
"create-new-tenant-profile": "Create a new one!", "create-new-tenant-profile": "Create a new one!",
"create-tenant-profile": "Create new tenant profile",
"import": "Import tenant profile",
"export": "Export tenant profile",
"export-failed-error": "Unable to export tenant profile: {{error}}",
"tenant-profile-file": "Tenant profile file",
"invalid-tenant-profile-file-error": "Unable to import tenant profile: Invalid tenant profile data structure.",
"maximum-devices": "Maximum number of devices (0 - unlimited)", "maximum-devices": "Maximum number of devices (0 - unlimited)",
"maximum-devices-required": "Maximum number of devices is required.", "maximum-devices-required": "Maximum number of devices is required.",
"maximum-devices-range": "Minimum number of devices can't be negative", "maximum-devices-range": "Minimum number of devices can't be negative",

Loading…
Cancel
Save