File

app/core/services/tcmResolver.service.ts

Index

Properties
Methods

Constructor

constructor(pubService: PublicationService, http: HttpClient)
Parameters :
Name Type Optional
pubService PublicationService No
http HttpClient No

Methods

Public resolveTcmIds
resolveTcmIds(tcms)

Fetches resolved names for a list of tcm ids from REST Service (created by Sandeep Nandy). Keeps cached values of previous searches. If tcm-id exist in cache, we don't include them in the request.

Parameters :
Name Optional Description
tcms No

List of tcmIds that should be resolved

Returns : any

Observable JSON object with tcm-id as key and resolved name as value.

Properties

tcmIdCache
Type : object
Default value : {}
import { Injectable } from '@angular/core';
import { of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { mergeMap } from 'rxjs/operators';
import { PublicationService } from './publication-service/publication.service';

@Injectable()
export class TcmResolverService {

	tcmIdCache = {};

	constructor(
		private pubService: PublicationService,
		private http: HttpClient
	) {}

	/** Fetches resolved names for a list of tcm ids from REST Service (created by Sandeep Nandy).
	 *  Keeps cached values of previous searches. If tcm-id exist in cache, we don't include them
	 * 	in the request.
	 *  @param tcms List of tcmIds that should be resolved
	 *  @return Observable JSON object with tcm-id as key and resolved name as value.
	 */
	public resolveTcmIds(tcms) {
		let tcmString = '';
		const tcmsToFetch = [];
		const tcmHead = `tcm:${this.pubService.getPublicationId()}-`;

		// Look for tcm-ids that are not cached
		tcms.forEach(tcmId => {
			if (!(tcmId in this.tcmIdCache)) {
				tcmsToFetch.push(tcmHead + tcmId);
			}
		});

		// If all tcm-ids are cached, return the cache observable.
		// If not, do a request for the missing tcm-ids and add them to the cache.
		if (tcmsToFetch.length === 0) {
			return of(this.tcmIdCache);
		} else {
			// Parse string with all tcm-ids to fetch
			tcmsToFetch.forEach((tcm, index) => {
				tcmString += tcm;
				if (index < tcms.length - 1) { tcmString += ';'; } // Add ; between tcm-ids, except last one
			});

			let hostName = window.location.hostname;
			let protocol = window.location.protocol;

			// For testing in local environment
			if (hostName === 'localhost' || hostName === '127.0.0.1') {
				hostName = 'www-qa.skf.com';
				protocol = 'http:';
			}

			return this.http.get(`${protocol}//${hostName}/ajax/getKeywordName.json?tcmIds=${tcmString}`).pipe(
				mergeMap(res => {
					Object.keys(res).forEach(tcm => {
						const tcmId = tcm.substring(tcmHead.length);	// Remove tcmHead
						this.tcmIdCache[tcmId] = res[tcm];	// Add to cache
					});

					return of(this.tcmIdCache);
				})
			);
		}
	}
}

result-matching ""

    No results matching ""