Merge pull request #1277 from Alfresco/dev-pionnegru-ACA-2845

[ACA-1920] Create File from template
This commit is contained in:
Denys Vuika
2019-12-13 12:30:17 +00:00
committed by GitHub
12 changed files with 531 additions and 3 deletions

View File

@@ -124,3 +124,4 @@ Below is the list of public actions types you can use in the plugin definitions
| 1.8.0 | VIEW_NODE | NodeId<`string`> , [ViewNodeExtras](../features/file-viewer.md#details)<`any`> | Lightweight preview of a node by id. Can be invoked from extensions. For details also see [File Viewer](../features/file-viewer.md#details) | | 1.8.0 | VIEW_NODE | NodeId<`string`> , [ViewNodeExtras](../features/file-viewer.md#details)<`any`> | Lightweight preview of a node by id. Can be invoked from extensions. For details also see [File Viewer](../features/file-viewer.md#details) |
| 1.8.0 | CLOSE_PREVIEW | n/a | Closes the viewer ( preview of the item ) | | 1.8.0 | CLOSE_PREVIEW | n/a | Closes the viewer ( preview of the item ) |
| 1.9.0 | RESET_SELECTION | n/a | Resets active document list selection | | 1.9.0 | RESET_SELECTION | n/a | Resets active document list selection |
| 2.0.0 | CREATE_FILE_FROM_TEMPLATE | n/a | Invoke a dialog listing `Node Templates` folder. Selected template can be copied in the current folder from where tha action was invoked |

View File

@@ -0,0 +1,36 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2019 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { Action } from '@ngrx/store';
export enum TemplateActionTypes {
CreateFileFromTemplate = 'CREATE_FILE_FROM_TEMPLATE'
}
export class CreateFileFromTemplate implements Action {
readonly type = TemplateActionTypes.CreateFileFromTemplate;
constructor() {}
}

View File

@@ -32,6 +32,7 @@ export * from './actions/snackbar.actions';
export * from './actions/upload.actions'; export * from './actions/upload.actions';
export * from './actions/viewer.actions'; export * from './actions/viewer.actions';
export * from './actions/metadata-aspect.actions'; export * from './actions/metadata-aspect.actions';
export * from './actions/template.actions';
export * from './effects/dialog.effects'; export * from './effects/dialog.effects';
export * from './effects/router.effects'; export * from './effects/router.effects';

View File

@@ -0,0 +1,156 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2019 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { EffectsModule } from '@ngrx/effects';
import { AppStore, SnackbarErrorAction } from '@alfresco/aca-shared/store';
import { TemplateEffects } from '../store/effects/template.effects';
import { AppTestingModule } from '../testing/app-testing.module';
import { Store } from '@ngrx/store';
import { MatDialog } from '@angular/material/dialog';
import { AlfrescoApiService, AlfrescoApiServiceMock } from '@alfresco/adf-core';
import { CreateFileFromTemplateService } from './create-file-from-template.service';
import { of } from 'rxjs';
describe('CreateFileFromTemplateService', () => {
let dialog: MatDialog;
let store: Store<AppStore>;
let alfrescoApiService: AlfrescoApiService;
let createFileFromTemplateService: CreateFileFromTemplateService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [AppTestingModule, EffectsModule.forRoot([TemplateEffects])],
providers: [
CreateFileFromTemplateService,
{ provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock }
]
});
store = TestBed.get(Store);
alfrescoApiService = TestBed.get(AlfrescoApiService);
dialog = TestBed.get(MatDialog);
createFileFromTemplateService = TestBed.get(CreateFileFromTemplateService);
});
it('should open dialog with `Node Templates` folder id as data property', () => {
spyOn(
alfrescoApiService.getInstance().nodes,
'getNodeInfo'
).and.returnValue(of({ id: 'templates-folder-id' }));
spyOn(dialog, 'open');
createFileFromTemplateService.openTemplatesDialog();
expect(dialog.open['calls'].argsFor(0)[1].data).toEqual(
jasmine.objectContaining({ currentFolderId: 'templates-folder-id' })
);
});
it('should remove parents for templates node breadcrumb path', () => {
spyOn(
alfrescoApiService.getInstance().nodes,
'getNodeInfo'
).and.returnValue(
of({
id: 'templates-folder-id',
path: {
elements: [],
name: '/Company Home/Data Dictionary'
}
})
);
spyOn(dialog, 'open');
createFileFromTemplateService.openTemplatesDialog();
const breadcrumb = dialog.open['calls']
.argsFor(0)[1]
.data.breadcrumbTransform({
name: 'Node Templates',
path: {
elements: [{ name: 'Company Home' }, { name: 'Data Dictionary' }],
name: '/Company Home/Data Dictionary'
}
});
expect(breadcrumb.path.elements).toEqual([]);
});
it('should return false if selected node is not a template file', () => {
spyOn(
alfrescoApiService.getInstance().nodes,
'getNodeInfo'
).and.returnValue(of({ id: 'templates-folder-id' }));
spyOn(dialog, 'open');
createFileFromTemplateService.openTemplatesDialog();
const isSelectionValid = dialog.open['calls']
.argsFor(0)[1]
.data.isSelectionValid({
isFile: false
});
expect(isSelectionValid).toBe(false);
});
it('should return true if selected node is a template file', () => {
spyOn(
alfrescoApiService.getInstance().nodes,
'getNodeInfo'
).and.returnValue(of({ id: 'templates-folder-id' }));
spyOn(dialog, 'open');
createFileFromTemplateService.openTemplatesDialog();
const isSelectionValid = dialog.open['calls']
.argsFor(0)[1]
.data.isSelectionValid({
isFile: true
});
expect(isSelectionValid).toBe(true);
});
it('should raise an error when getNodeInfo fails', fakeAsync(() => {
spyOn(
alfrescoApiService.getInstance().nodes,
'getNodeInfo'
).and.returnValue(
Promise.reject({
message: `{ "error": { "statusCode": 404 } } `
})
);
spyOn(store, 'dispatch');
createFileFromTemplateService.openTemplatesDialog();
tick();
expect(store.dispatch).toHaveBeenCalledWith(
new SnackbarErrorAction('APP.MESSAGES.ERRORS.GENERIC')
);
}));
});

View File

@@ -0,0 +1,121 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2019 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { Injectable } from '@angular/core';
import { MatDialog, MatDialogConfig } from '@angular/material';
import {
ContentNodeSelectorComponentData,
ContentNodeSelectorComponent
} from '@alfresco/adf-content-services';
import { Subject, from, of } from 'rxjs';
import { Node } from '@alfresco/js-api';
import { AlfrescoApiService } from '@alfresco/adf-core';
import { switchMap, catchError } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { AppStore, SnackbarErrorAction } from '@alfresco/aca-shared/store';
@Injectable({
providedIn: 'root'
})
export class CreateFileFromTemplateService {
constructor(
private store: Store<AppStore>,
private alfrescoApiService: AlfrescoApiService,
public dialog: MatDialog
) {}
openTemplatesDialog(): Subject<Node[]> {
const select = new Subject<Node[]>();
select.subscribe({
complete: this.close.bind(this)
});
const data: ContentNodeSelectorComponentData = {
title: null,
dropdownHideMyFiles: true,
currentFolderId: null,
dropdownSiteList: null,
breadcrumbTransform: this.transformNode.bind(this),
select,
isSelectionValid: this.isSelectionValid.bind(this)
};
data.select.subscribe({
complete: this.close.bind(this)
});
from(
this.alfrescoApiService.getInstance().nodes.getNodeInfo('-root-', {
relativePath: 'Data Dictionary/Node Templates'
})
)
.pipe(
switchMap(node => {
data.currentFolderId = node.id;
return this.dialog
.open(ContentNodeSelectorComponent, <MatDialogConfig>{
data,
panelClass: [
'adf-content-node-selector-dialog',
'aca-template-node-selector-dialog'
],
width: '630px'
})
.afterClosed();
}),
catchError(error => {
this.store.dispatch(
new SnackbarErrorAction('APP.MESSAGES.ERRORS.GENERIC')
);
return of(error);
})
)
.subscribe({ next: () => select.complete() });
return select;
}
private transformNode(node: Node): Node {
if (node && node.path && node.path && node.path.elements instanceof Array) {
let {
path: { elements: elementsPath = [] }
} = node;
elementsPath = elementsPath.filter(
path => path.name !== 'Company Home' && path.name !== 'Data Dictionary'
);
node.path.elements = elementsPath;
}
return node;
}
private isSelectionValid(node: Node): boolean {
return node.isFile;
}
private close() {
this.dialog.closeAll();
}
}

View File

@@ -39,7 +39,8 @@ import {
SearchEffects, SearchEffects,
LibraryEffects, LibraryEffects,
UploadEffects, UploadEffects,
FavoriteEffects FavoriteEffects,
TemplateEffects
} from './effects'; } from './effects';
import { INITIAL_STATE } from './initial-state'; import { INITIAL_STATE } from './initial-state';
@@ -56,7 +57,8 @@ import { INITIAL_STATE } from './initial-state';
SearchEffects, SearchEffects,
LibraryEffects, LibraryEffects,
UploadEffects, UploadEffects,
FavoriteEffects FavoriteEffects,
TemplateEffects
]), ]),
!environment.production !environment.production
? StoreDevtoolsModule.instrument({ maxAge: 25 }) ? StoreDevtoolsModule.instrument({ maxAge: 25 })

View File

@@ -32,3 +32,4 @@ export * from './effects/search.effects';
export * from './effects/library.effects'; export * from './effects/library.effects';
export * from './effects/upload.effects'; export * from './effects/upload.effects';
export * from './effects/upload.effects'; export * from './effects/upload.effects';
export * from './effects/template.effects';

View File

@@ -0,0 +1,93 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2019 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { AppTestingModule } from '../../testing/app-testing.module';
import { TemplateEffects } from './template.effects';
import { EffectsModule } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import {
CreateFileFromTemplate,
SnackbarErrorAction
} from '@alfresco/aca-shared/store';
import { CreateFileFromTemplateService } from '../../services/create-file-from-template.service';
import { of } from 'rxjs';
import { AlfrescoApiServiceMock, AlfrescoApiService } from '@alfresco/adf-core';
import { ContentManagementService } from '../../services/content-management.service';
describe('TemplateEffects', () => {
let store: Store<any>;
let createFileFromTemplateService: CreateFileFromTemplateService;
let alfrescoApiService: AlfrescoApiService;
let contentManagementService: ContentManagementService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [AppTestingModule, EffectsModule.forRoot([TemplateEffects])],
providers: [
CreateFileFromTemplateService,
{ provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock }
]
});
store = TestBed.get(Store);
createFileFromTemplateService = TestBed.get(CreateFileFromTemplateService);
alfrescoApiService = TestBed.get(AlfrescoApiService);
contentManagementService = TestBed.get(ContentManagementService);
spyOn(contentManagementService.reload, 'next');
spyOn(store, 'select').and.returnValue(of({ id: 'parent-id' }));
spyOn(createFileFromTemplateService, 'openTemplatesDialog').and.returnValue(
of([{ id: 'template-id' }])
);
});
it('should reload content on template copy', fakeAsync(() => {
spyOn(alfrescoApiService.getInstance().nodes, 'copyNode').and.returnValue(
of({})
);
store.dispatch(new CreateFileFromTemplate());
tick();
expect(contentManagementService.reload.next).toHaveBeenCalled();
}));
it('should raise error when copy template fails', fakeAsync(() => {
spyOn(store, 'dispatch').and.callThrough();
spyOn(alfrescoApiService.getInstance().nodes, 'copyNode').and.returnValue(
Promise.reject({
message: `{ "error": { "statusCode": 404 } } `
})
);
store.dispatch(new CreateFileFromTemplate());
tick();
expect(contentManagementService.reload.next).not.toHaveBeenCalled();
expect(store.dispatch['calls'].argsFor(1)[0]).toEqual(
new SnackbarErrorAction('APP.MESSAGES.ERRORS.GENERIC')
);
}));
});

View File

@@ -0,0 +1,87 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2019 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { Effect, Actions, ofType } from '@ngrx/effects';
import { Injectable } from '@angular/core';
import { map, withLatestFrom, switchMap, catchError } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import {
CreateFileFromTemplate,
TemplateActionTypes,
getCurrentFolder,
AppStore,
SnackbarErrorAction
} from '@alfresco/aca-shared/store';
import { CreateFileFromTemplateService } from '../../services/create-file-from-template.service';
import { AlfrescoApiService } from '@alfresco/adf-core';
import { ContentManagementService } from '../../services/content-management.service';
import { from, of } from 'rxjs';
import { NodeEntry } from '@alfresco/js-api';
@Injectable()
export class TemplateEffects {
constructor(
private content: ContentManagementService,
private store: Store<AppStore>,
private apiService: AlfrescoApiService,
private actions$: Actions,
private createFileFromTemplateService: CreateFileFromTemplateService
) {}
@Effect({ dispatch: false })
fileFromTemplate$ = this.actions$.pipe(
ofType<CreateFileFromTemplate>(TemplateActionTypes.CreateFileFromTemplate),
map(() => {
this.createFileFromTemplateService
.openTemplatesDialog()
.pipe(
withLatestFrom(this.store.select(getCurrentFolder)),
switchMap(([[template], parentNode]) => {
return from(
this.apiService
.getInstance()
.nodes.copyNode(template.id, { targetParentId: parentNode.id })
);
}),
catchError(error => {
const { statusCode } = JSON.parse(error.message).error;
if (statusCode !== 409) {
this.store.dispatch(
new SnackbarErrorAction('APP.MESSAGES.ERRORS.GENERIC')
);
}
return of(null);
})
)
.subscribe((node: NodeEntry | null) => {
if (node) {
this.content.reload.next();
}
});
})
);
}

View File

@@ -13,4 +13,15 @@
display: none; display: none;
} }
} }
.aca-template-node-selector-dialog {
adf-content-node-selector-panel {
.adf-content-node-selector-content-input {
display: none;
}
.adf-sites-dropdown {
display: none;
}
}
}
} }

View File

@@ -102,6 +102,24 @@
"actions": { "actions": {
"click": "CREATE_LIBRARY" "click": "CREATE_LIBRARY"
} }
},
{
"id": "app.create.separator.2",
"type": "separator",
"order": 650
},
{
"id": "app.create.fileFromTemplate",
"order": 700,
"icon": "description",
"title": "APP.NEW_MENU.MENU_ITEMS.FILE_TEMPLATE",
"description": "APP.NEW_MENU.MENU_ITEMS.FILE_TEMPLATE",
"actions": {
"click": "CREATE_FILE_FROM_TEMPLATE"
},
"rules": {
"enabled": "app.navigation.folder.canUpload"
}
} }
], ],
"navbar": [ "navbar": [

View File

@@ -57,7 +57,8 @@
"CREATE_FOLDER": "Create Folder", "CREATE_FOLDER": "Create Folder",
"UPLOAD_FILE": "Upload File", "UPLOAD_FILE": "Upload File",
"UPLOAD_FOLDER": "Upload Folder", "UPLOAD_FOLDER": "Upload Folder",
"CREATE_LIBRARY": "Create Library" "CREATE_LIBRARY": "Create Library",
"FILE_TEMPLATE": "Create file from template"
}, },
"TOOLTIPS": { "TOOLTIPS": {
"CREATE_FOLDER": "Create new folder", "CREATE_FOLDER": "Create new folder",