mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-1881] Consolidate NodeService and NodesApiService (#5591)
* [ADF-1881] Consolidate NodeService and NodesApiService * * docs added
This commit is contained in:
@@ -16,20 +16,20 @@
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { NodeEntry, MinimalNode, NodePaging } from '@alfresco/js-api';
|
||||
import { Observable, from, throwError } from 'rxjs';
|
||||
import { MinimalNode, NodeEntry, NodePaging } from '@alfresco/js-api';
|
||||
import { from, Observable, throwError } from 'rxjs';
|
||||
import { AlfrescoApiService } from './alfresco-api.service';
|
||||
import { UserPreferencesService } from './user-preferences.service';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
import { catchError, map } from 'rxjs/operators';
|
||||
import { NodeMetadata } from '../models/node-metadata.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class NodesApiService {
|
||||
|
||||
constructor(
|
||||
private api: AlfrescoApiService,
|
||||
private preferences: UserPreferencesService) {}
|
||||
constructor(private api: AlfrescoApiService,
|
||||
private preferences: UserPreferencesService) {}
|
||||
|
||||
private get nodesApi() {
|
||||
return this.api.getInstance().core.nodesApi;
|
||||
@@ -50,11 +50,9 @@ export class NodesApiService {
|
||||
include: [ 'path', 'properties', 'allowableOperations', 'permissions' ]
|
||||
};
|
||||
const queryOptions = Object.assign(defaults, options);
|
||||
const promise = this.nodesApi
|
||||
.getNode(nodeId, queryOptions)
|
||||
.then(this.getEntryFromEntity);
|
||||
|
||||
return from(promise).pipe(
|
||||
return from(this.nodesApi.getNode(nodeId, queryOptions)).pipe(
|
||||
map(this.getEntryFromEntity),
|
||||
catchError((err) => throwError(err))
|
||||
);
|
||||
}
|
||||
@@ -72,10 +70,8 @@ export class NodesApiService {
|
||||
include: [ 'path', 'properties', 'allowableOperations', 'permissions' ]
|
||||
};
|
||||
const queryOptions = Object.assign(defaults, options);
|
||||
const promise = this.nodesApi
|
||||
.getNodeChildren(nodeId, queryOptions);
|
||||
|
||||
return from(promise).pipe(
|
||||
return from(this.nodesApi.getNodeChildren(nodeId, queryOptions)).pipe(
|
||||
catchError((err) => throwError(err))
|
||||
);
|
||||
}
|
||||
@@ -88,11 +84,8 @@ export class NodesApiService {
|
||||
* @returns Details of the new node
|
||||
*/
|
||||
createNode(parentNodeId: string, nodeBody: any, options: any = {}): Observable<MinimalNode> {
|
||||
const promise = this.nodesApi
|
||||
.addNode(parentNodeId, nodeBody, options)
|
||||
.then(this.getEntryFromEntity);
|
||||
|
||||
return from(promise).pipe(
|
||||
return from(this.nodesApi.addNode(parentNodeId, nodeBody, options)).pipe(
|
||||
map(this.getEntryFromEntity),
|
||||
catchError((err) => throwError(err))
|
||||
);
|
||||
}
|
||||
@@ -122,11 +115,8 @@ export class NodesApiService {
|
||||
};
|
||||
const queryOptions = Object.assign(defaults, options);
|
||||
|
||||
const promise = this.nodesApi
|
||||
.updateNode(nodeId, nodeBody, queryOptions)
|
||||
.then(this.getEntryFromEntity);
|
||||
|
||||
return from(promise).pipe(
|
||||
return from(this.nodesApi.updateNode(nodeId, nodeBody, queryOptions)).pipe(
|
||||
map(this.getEntryFromEntity),
|
||||
catchError((err) => throwError(err))
|
||||
);
|
||||
}
|
||||
@@ -138,9 +128,7 @@ export class NodesApiService {
|
||||
* @returns Empty result that notifies when the deletion is complete
|
||||
*/
|
||||
deleteNode(nodeId: string, options: any = {}): Observable<any> {
|
||||
const promise = this.nodesApi.deleteNode(nodeId, options);
|
||||
|
||||
return from(promise).pipe(
|
||||
return from(this.nodesApi.deleteNode(nodeId, options)).pipe(
|
||||
catchError((err) => throwError(err))
|
||||
);
|
||||
}
|
||||
@@ -151,12 +139,83 @@ export class NodesApiService {
|
||||
* @returns Details of the restored node
|
||||
*/
|
||||
restoreNode(nodeId: string): Observable<MinimalNode> {
|
||||
const promise = this.nodesApi
|
||||
.restoreNode(nodeId)
|
||||
.then(this.getEntryFromEntity);
|
||||
|
||||
return from(promise).pipe(
|
||||
return from(this.nodesApi.restoreNode(nodeId)).pipe(
|
||||
map(this.getEntryFromEntity),
|
||||
catchError((err) => throwError(err))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the metadata and the nodeType for a nodeId cleaned by the prefix.
|
||||
* @param nodeId ID of the target node
|
||||
* @returns Node metadata
|
||||
*/
|
||||
public getNodeMetadata(nodeId: string): Observable<NodeMetadata> {
|
||||
return from(this.nodesApi.getNode(nodeId))
|
||||
.pipe(map(this.cleanMetadataFromSemicolon));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Node from form metadata.
|
||||
* @param path Path to the node
|
||||
* @param nodeType Node type
|
||||
* @param name Node name
|
||||
* @param nameSpace Namespace for properties
|
||||
* @param data Property data to store in the node under namespace
|
||||
* @returns The created node
|
||||
*/
|
||||
public createNodeMetadata(nodeType: string, nameSpace: any, data: any, path: string, name?: string): Observable<NodeEntry> {
|
||||
const properties = {};
|
||||
for (const key in data) {
|
||||
if (data[key]) {
|
||||
properties[nameSpace + ':' + key] = data[key];
|
||||
}
|
||||
}
|
||||
|
||||
return this.createNodeInsideRoot(name || this.generateUuid(), nodeType, properties, path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Node inside `-root-` folder
|
||||
* @param name Node name
|
||||
* @param nodeType Node type
|
||||
* @param properties Node body properties
|
||||
* @param path Path to the node
|
||||
* @returns The created node
|
||||
*/
|
||||
public createNodeInsideRoot(name: string, nodeType: string, properties: any, path: string): Observable<NodeEntry> {
|
||||
const body = {
|
||||
name: name,
|
||||
nodeType: nodeType,
|
||||
properties: properties,
|
||||
relativePath: path
|
||||
};
|
||||
return from(this.nodesApi.addNode('-root-', body, {}));
|
||||
}
|
||||
|
||||
private generateUuid() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||
const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
||||
private cleanMetadataFromSemicolon(nodeEntry: NodeEntry): NodeMetadata {
|
||||
const metadata = {};
|
||||
|
||||
if (nodeEntry && nodeEntry.entry.properties) {
|
||||
for (const key in nodeEntry.entry.properties) {
|
||||
if (key) {
|
||||
if (key.indexOf(':') !== -1) {
|
||||
metadata [key.split(':')[1]] = nodeEntry.entry.properties[key];
|
||||
} else {
|
||||
metadata [key] = nodeEntry.entry.properties[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new NodeMetadata(metadata, nodeEntry.entry.nodeType);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user