[ADF-983] User Preferences Service (#2044)

* user preferences service

* unit tests and code improvements

* fix tests

* fix tests

* fix tests

* add missing production settings

* readme updates

* integrate with the core module
This commit is contained in:
Denys Vuika
2017-07-05 10:39:50 +01:00
committed by Eugenio Romano
parent ac9b660e83
commit 70a3c863e6
16 changed files with 323 additions and 32 deletions

View File

@@ -4,6 +4,9 @@
"application": { "application": {
"name": "Alfresco" "name": "Alfresco"
}, },
"pagination": {
"size": 25
},
"activiti": { "activiti": {
"rest": { "rest": {
"fields": [ "fields": [

View File

@@ -3,5 +3,24 @@
"bpmHost": "http://{hostname}", "bpmHost": "http://{hostname}",
"application": { "application": {
"name": "Alfresco" "name": "Alfresco"
},
"pagination": {
"size": 25
},
"activiti": {
"rest": {
"fields": [
{
"processId": "0",
"taskId": "7501",
"fieldId": "label10",
"values": [
{ "id": "f1", "name": "Field 1" },
{ "id": "f2", "name": "Field 2" },
{ "id": "f3", "name": "Field 3" }
]
}
]
}
} }
} }

View File

@@ -18,7 +18,7 @@
import { DebugElement } from '@angular/core'; import { DebugElement } from '@angular/core';
import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
import { CoreModule, AlfrescoTranslationService } from 'ng2-alfresco-core'; import { CoreModule, AlfrescoTranslationService, AppConfigModule } from 'ng2-alfresco-core';
import { AnalyticsReportListComponent } from '../components/analytics-report-list.component'; import { AnalyticsReportListComponent } from '../components/analytics-report-list.component';
import { AnalyticsService } from '../services/analytics.service'; import { AnalyticsService } from '../services/analytics.service';
import { ReportParametersModel } from '../models/report.model'; import { ReportParametersModel } from '../models/report.model';
@@ -45,7 +45,10 @@ describe('AnalyticsReportListComponent', () => {
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
CoreModule.forRoot() CoreModule.forRoot(),
AppConfigModule.forRoot('app.config.json', {
bpmHost: 'http://localhost:9876/bpm'
})
], ],
declarations: [ declarations: [
AnalyticsReportListComponent AnalyticsReportListComponent

View File

@@ -20,7 +20,7 @@ import { DebugElement, SimpleChange } from '@angular/core';
import { MdTooltipModule, MdButtonModule, OVERLAY_PROVIDERS } from '@angular/material'; import { MdTooltipModule, MdButtonModule, OVERLAY_PROVIDERS } from '@angular/material';
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
import * as moment from 'moment'; import * as moment from 'moment';
import { CoreModule, AlfrescoTranslationService } from 'ng2-alfresco-core'; import { CoreModule, AlfrescoTranslationService, AppConfigModule } from 'ng2-alfresco-core';
import { AnalyticsReportParametersComponent } from '../components/analytics-report-parameters.component'; import { AnalyticsReportParametersComponent } from '../components/analytics-report-parameters.component';
import { WIDGET_DIRECTIVES } from '../components/widgets/index'; import { WIDGET_DIRECTIVES } from '../components/widgets/index';
@@ -44,6 +44,9 @@ describe('AnalyticsReportParametersComponent', () => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
CoreModule.forRoot(), CoreModule.forRoot(),
AppConfigModule.forRoot('app.config.json', {
bpmHost: 'http://localhost:9876/bpm'
}),
MdTooltipModule, MdTooltipModule,
MdButtonModule MdButtonModule
], ],

View File

@@ -375,6 +375,80 @@ The supported variables are:
| hostname | `location.hostname` | | hostname | `location.hostname` |
| port | `location.port` | | port | `location.port` |
### Unit testing
You can also provide custom values for the entire service.
This might become handy when creating unit tests.
```ts
describe('MyTest', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
CoreModule.forRoot(),
AppConfigModule.forRoot('app.config.json', {
ecmHost: 'http://localhost:9876/ecm'
})
]
});
});
});
```
In the example above custom values are applied on the top of all the values the `AppConfigService` has previously loaded.
If there is an 'app.config.json' file loaded at unit test run time then your custom values will overwrite exiting values with the same keys if present.
## User Preferences Service
The `UserPreferencesService` allows you to store preferences for the components.
The preferences are bound to a particular `prefix` so the application can switch between different profiles on demand.
For example upon login you can set the `prefix` as current username:
```ts
import { UserPreferencesService, AlfrescoAuthenticationService } from 'ng2-alfresco-core';
@Component({...})
class AppComponent {
constructor(private userPreferences: UserPreferencesService,
private authService: AlfrescoAuthenticationService) {
}
onLoggedIn() {
this.userPreferences.setStoragePrefix(
this.authService.getEcmUsername()
);
}
}
```
As soon as you assign the storage prefix all settings that you get or set via the `UserPreferencesService` will be saved to dedicated profile.
You can import the service in your controller an use its APIs like below:
```ts
@Component({...})
class AppComponent {
constructor(userPreferences: UserPreferencesService) {
userPreferences.set('myProperty1', 'value1');
userPreferences.set('myProperty2', 'value2');
console.log(
userPreferences.get('myProperty1')
);
}
}
```
The service also provides quick access to a set of the "known" properties used across ADF components.
Known properties:
- paginationSize (number) - gets or sets the preferred pagination size
## Notification Service ## Notification Service
The Notification Service is implemented on top of the Angular 2 Material Design snackbar. The Notification Service is implemented on top of the Angular 2 Material Design snackbar.

View File

@@ -28,6 +28,7 @@ import { CardViewModule } from './src/components/view/card-view.module';
import { CollapsableModule } from './src/components/collapsable/collapsable.module'; import { CollapsableModule } from './src/components/collapsable/collapsable.module';
import { AdfToolbarComponent } from './src/components/toolbar/toolbar.component'; import { AdfToolbarComponent } from './src/components/toolbar/toolbar.component';
import { AppConfigModule } from './src/services/app-config.service'; import { AppConfigModule } from './src/services/app-config.service';
import { UserPreferencesService } from './src/services/user-preferences.service';
import { import {
AlfrescoAuthenticationService, AlfrescoAuthenticationService,
@@ -72,6 +73,7 @@ export * from './src/events/base.event';
export * from './src/events/base-ui.event'; export * from './src/events/base-ui.event';
export * from './src/events/folder-created.event'; export * from './src/events/folder-created.event';
export * from './src/models/index'; export * from './src/models/index';
export { UserPreferencesService } from './src/services/user-preferences.service';
export * from './src/models/index'; export * from './src/models/index';

View File

@@ -24,17 +24,14 @@ export class AppConfigService {
private config: any = { private config: any = {
'ecmHost': 'http://{hostname}:{port}/ecm', 'ecmHost': 'http://{hostname}:{port}/ecm',
'bpmHost': 'http://{hostname}:{port}/bpm', 'bpmHost': 'http://{hostname}:{port}/bpm'
'application': {
'name': 'Alfresco'
}
}; };
configFile: string = null; configFile: string = null;
constructor(private http: Http) {} constructor(private http: Http) {}
get<T>(key: string): T { get<T>(key: string, defaultValue?: T): T {
let result: any = ObjectUtils.getValue(this.config, key); let result: any = ObjectUtils.getValue(this.config, key);
if (typeof result === 'string') { if (typeof result === 'string') {
const map = new Map<string, string>(); const map = new Map<string, string>();
@@ -42,18 +39,22 @@ export class AppConfigService {
map.set('port', location.port); map.set('port', location.port);
result = this.formatString(result, map); result = this.formatString(result, map);
} }
if (result === undefined) {
return defaultValue;
}
return <T> result; return <T> result;
} }
load(resource: string = 'app.config.json'): Promise<any> { load(resource: string = 'app.config.json', values?: {}): Promise<any> {
this.configFile = resource; this.configFile = resource;
return new Promise((resolve, reject) => { return new Promise(resolve => {
this.config = Object.assign({}, values || {});
this.http.get(resource).subscribe( this.http.get(resource).subscribe(
data => { data => {
this.config = Object.assign({}, this.config, data.json() || {}); this.config = Object.assign({}, this.config, data.json() || {});
resolve(this.config); resolve(this.config);
}, },
(err) => { () => {
const errorMessage = `Error loading ${resource}`; const errorMessage = `Error loading ${resource}`;
console.log(errorMessage); console.log(errorMessage);
resolve(this.config); resolve(this.config);
@@ -74,11 +75,11 @@ export class AppConfigService {
} }
} }
export function InitAppConfigServiceProvider(resource: string): any { export function InitAppConfigServiceProvider(resource: string, values?: {}): any {
return { return {
provide: APP_INITIALIZER, provide: APP_INITIALIZER,
useFactory: (configService: AppConfigService) => { useFactory: (configService: AppConfigService) => {
return () => configService.load(resource); return () => configService.load(resource, values);
}, },
deps: [ deps: [
AppConfigService AppConfigService
@@ -96,12 +97,12 @@ export function InitAppConfigServiceProvider(resource: string): any {
] ]
}) })
export class AppConfigModule { export class AppConfigModule {
static forRoot(resource: string): ModuleWithProviders { static forRoot(resource: string, values?: {}): ModuleWithProviders {
return { return {
ngModule: AppConfigModule, ngModule: AppConfigModule,
providers: [ providers: [
AppConfigService, AppConfigService,
InitAppConfigServiceProvider(resource) InitAppConfigServiceProvider(resource, values)
] ]
}; };
} }

View File

@@ -0,0 +1,95 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* 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 { TestBed, async } from '@angular/core/testing';
import { AppConfigModule } from './app-config.service';
import { StorageService } from './storage.service';
import { UserPreferencesService } from './user-preferences.service';
describe('UserPreferencesService', () => {
const defaultPaginationSize: number = 10;
let preferences: UserPreferencesService;
let storage: StorageService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
AppConfigModule.forRoot('app.config.json', {
'pagination': {
'size': defaultPaginationSize
}
})
],
providers: [
StorageService,
UserPreferencesService
]
}).compileComponents();
}));
beforeEach(() => {
preferences = TestBed.get(UserPreferencesService);
storage = TestBed.get(StorageService);
});
it('should get default pagination from app config', () => {
expect(preferences.paginationSize).toBe(defaultPaginationSize);
});
it('should use [GUEST] as default storage prefix', () => {
expect(preferences.getStoragePrefix()).toBe('GUEST');
});
it('should change storage prefix', () => {
preferences.setStoragePrefix('USER_A');
expect(preferences.getStoragePrefix()).toBe('USER_A');
});
it('should format property key for default prefix', () => {
expect(preferences.getPropertyKey('propertyA')).toBe('GUEST__propertyA');
});
it('should format property key for custom prefix', () => {
preferences.setStoragePrefix('USER_A');
expect(preferences.getPropertyKey('propertyA')).toBe('USER_A__propertyA');
});
it('should save value with default prefix', () => {
preferences.set('propertyA', 'valueA');
const propertyKey = preferences.getPropertyKey('propertyA');
expect(storage.getItem(propertyKey)).toBe('valueA');
});
it('should save value with custom prefix', () => {
preferences.setStoragePrefix('USER_A');
preferences.set('propertyA', 'valueA');
const propertyKey = preferences.getPropertyKey('propertyA');
expect(storage.getItem(propertyKey)).toBe('valueA');
});
it('should store custom pagination settings for default prefix', () => {
preferences.paginationSize = 5;
expect(preferences.paginationSize).toBe(5);
});
it('should return default paginationSize value', () => {
preferences.set('PAGINATION_SIZE', 0);
expect(preferences.paginationSize).toBe(defaultPaginationSize);
});
});

View File

@@ -0,0 +1,71 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* 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 { AppConfigService } from './app-config.service';
import { StorageService } from './storage.service';
@Injectable()
export class UserPreferencesService {
private defaults = {
paginationSize: 25
};
private _storagePrefix: string = 'GUEST';
getStoragePrefix(): string {
return this._storagePrefix;
}
setStoragePrefix(value: string) {
this._storagePrefix = value || 'GUEST';
}
constructor(
appConfig: AppConfigService,
private storage: StorageService) {
this.defaults.paginationSize = appConfig.get('pagination.size', 25);
}
getPropertyKey(property: string): string {
return `${this.getStoragePrefix()}__${property}`;
}
set(property: string, value: any) {
if (!property) { return; }
this.storage.setItem(
this.getPropertyKey(property),
value
);
}
get(property: string): string {
const key = this.getPropertyKey(property);
return this.storage.getItem(key);
}
set paginationSize(value: number) {
this.set('PAGINATION_SIZE', value);
}
get paginationSize(): number {
return Number(this.get('PAGINATION_SIZE')) || this.defaults.paginationSize;
}
}

View File

@@ -43,7 +43,9 @@
"es2015", "es2015",
"dom" "dom"
], ],
"suppressImplicitAnyIndexErrors": true "suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"noUnusedParameters": true
}, },
"exclude": [ "exclude": [
"demo", "demo",

View File

@@ -18,13 +18,13 @@
import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { TagActionsComponent } from '../components/tag-actions.component'; import { TagActionsComponent } from '../components/tag-actions.component';
import { DebugElement } from '@angular/core'; import { DebugElement } from '@angular/core';
import { CoreModule } from 'ng2-alfresco-core'; import { CoreModule, AppConfigModule } from 'ng2-alfresco-core';
import { TagService } from '../services/tag.service'; import { TagService } from '../services/tag.service';
import { MdInputModule } from '@angular/material'; import { MdInputModule } from '@angular/material';
declare let jasmine: any; declare let jasmine: any;
describe('Test ng2-alfresco-tag Tag actions list', () => { describe('TagActionsComponent', () => {
let component: any; let component: any;
let fixture: ComponentFixture<TagActionsComponent>; let fixture: ComponentFixture<TagActionsComponent>;
@@ -35,7 +35,10 @@ describe('Test ng2-alfresco-tag Tag actions list', () => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
MdInputModule, MdInputModule,
CoreModule.forRoot() CoreModule.forRoot(),
AppConfigModule.forRoot('app.config.json', {
ecmHost: 'http://localhost:9876/ecm'
})
], ],
declarations: [ declarations: [
TagActionsComponent TagActionsComponent

View File

@@ -17,14 +17,14 @@
import { DebugElement } from '@angular/core'; import { DebugElement } from '@angular/core';
import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { CoreModule } from 'ng2-alfresco-core'; import { CoreModule, AppConfigModule } from 'ng2-alfresco-core';
import { TagList } from './../components/tag-list.component'; import { TagList } from './../components/tag-list.component';
import { TagService } from '../services/tag.service'; import { TagService } from '../services/tag.service';
import { MdInputModule } from '@angular/material'; import { MdInputModule } from '@angular/material';
declare let jasmine: any; declare let jasmine: any;
describe('Test ng2-alfresco-tag Tag list All ECM', () => { describe('TagList', () => {
let dataTag = { let dataTag = {
'list': { 'list': {
@@ -51,7 +51,10 @@ describe('Test ng2-alfresco-tag Tag list All ECM', () => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
MdInputModule, MdInputModule,
CoreModule.forRoot() CoreModule.forRoot(),
AppConfigModule.forRoot('app.config.json', {
ecmHost: 'http://localhost:9876/ecm'
})
], ],
declarations: [ declarations: [
TagList TagList

View File

@@ -18,13 +18,13 @@
import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { TagNodeList } from '../components/tag-node-list.component'; import { TagNodeList } from '../components/tag-node-list.component';
import { DebugElement } from '@angular/core'; import { DebugElement } from '@angular/core';
import { CoreModule } from 'ng2-alfresco-core'; import { CoreModule, AppConfigModule } from 'ng2-alfresco-core';
import { TagService } from '../services/tag.service'; import { TagService } from '../services/tag.service';
import { MdInputModule } from '@angular/material'; import { MdInputModule } from '@angular/material';
declare let jasmine: any; declare let jasmine: any;
describe('Test ng2-alfresco-tag Tag relative node list', () => { describe('TagNodeList', () => {
let dataTag = { let dataTag = {
'list': { 'list': {
@@ -51,7 +51,10 @@ describe('Test ng2-alfresco-tag Tag relative node list', () => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
MdInputModule, MdInputModule,
CoreModule.forRoot() CoreModule.forRoot(),
AppConfigModule.forRoot('app.config.json', {
ecmHost: 'http://localhost:9876/ecm'
})
], ],
declarations: [ declarations: [
TagNodeList TagNodeList

View File

@@ -16,19 +16,22 @@
*/ */
import { TestBed, async } from '@angular/core/testing'; import { TestBed, async } from '@angular/core/testing';
import { CoreModule } from 'ng2-alfresco-core'; import { CoreModule, AppConfigModule } from 'ng2-alfresco-core';
import { TagService } from '../services/tag.service'; import { TagService } from '../services/tag.service';
declare let jasmine: any; declare let jasmine: any;
describe('Tag service', () => { describe('TagService', () => {
let service: TagService; let service: TagService;
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
CoreModule.forRoot() CoreModule.forRoot(),
AppConfigModule.forRoot('app.config.json', {
ecmHost: 'http://localhost:9876/ecm'
})
], ],
providers: [ providers: [
TagService TagService

View File

@@ -17,7 +17,7 @@
import { EventEmitter } from '@angular/core'; import { EventEmitter } from '@angular/core';
import { TestBed } from '@angular/core/testing'; import { TestBed } from '@angular/core/testing';
import { CoreModule } from 'ng2-alfresco-core'; import { CoreModule, AppConfigModule } from 'ng2-alfresco-core';
import { UploadService } from './upload.service'; import { UploadService } from './upload.service';
import { FileModel, FileUploadOptions } from '../models/file.model'; import { FileModel, FileUploadOptions } from '../models/file.model';
@@ -29,7 +29,10 @@ describe('UploadService', () => {
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
CoreModule.forRoot() CoreModule.forRoot(),
AppConfigModule.forRoot('app.config.json', {
ecmHost: 'http://localhost:9876/ecm'
})
], ],
providers: [ providers: [
UploadService UploadService

View File

@@ -17,13 +17,13 @@
import { DebugElement } from '@angular/core'; import { DebugElement } from '@angular/core';
import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { CoreModule } from 'ng2-alfresco-core'; import { CoreModule, AppConfigModule } from 'ng2-alfresco-core';
import { DataTableModule } from 'ng2-alfresco-datatable'; import { DataTableModule } from 'ng2-alfresco-datatable';
import { WebscriptComponent } from '../src/webscript.component'; import { WebscriptComponent } from '../src/webscript.component';
declare let jasmine: any; declare let jasmine: any;
describe('Test ng2-alfresco-webscript', () => { describe('WebscriptComponent', () => {
let component: WebscriptComponent; let component: WebscriptComponent;
let fixture: ComponentFixture<WebscriptComponent>; let fixture: ComponentFixture<WebscriptComponent>;
@@ -34,6 +34,9 @@ describe('Test ng2-alfresco-webscript', () => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
CoreModule.forRoot(), CoreModule.forRoot(),
AppConfigModule.forRoot('app.config.json', {
ecmHost: 'http://localhost:9876/ecm'
}),
DataTableModule DataTableModule
], ],
declarations: [ declarations: [