From 5995f87ec0460a807c46041f8a52884f0f6460c9 Mon Sep 17 00:00:00 2001
From: Giuseppe Digilio <giuseppe.digilio@4science.it>
Date: Wed, 28 Nov 2018 10:34:31 +0100
Subject: [PATCH] Added getEntryByValue method

---
 src/app/core/integration/authority.service.ts |  3 +-
 .../integration/integration.service.spec.ts   |  6 ++-
 .../core/integration/integration.service.ts   | 40 +++++++++++++++++--
 3 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/src/app/core/integration/authority.service.ts b/src/app/core/integration/authority.service.ts
index 515a0da378..02c1141d2a 100644
--- a/src/app/core/integration/authority.service.ts
+++ b/src/app/core/integration/authority.service.ts
@@ -9,7 +9,8 @@ import { RemoteDataBuildService } from '../cache/builders/remote-data-build.serv
 @Injectable()
 export class AuthorityService extends IntegrationService {
   protected linkPath = 'authorities';
-  protected browseEndpoint = 'entries';
+  protected entriesEndpoint = 'entries';
+  protected entryValueEndpoint = 'entryValues';
 
   constructor(
     protected responseCache: ResponseCacheService,
diff --git a/src/app/core/integration/integration.service.spec.ts b/src/app/core/integration/integration.service.spec.ts
index 745bf8bb8e..b357c2e99e 100644
--- a/src/app/core/integration/integration.service.spec.ts
+++ b/src/app/core/integration/integration.service.spec.ts
@@ -13,11 +13,13 @@ import { RemoteDataBuildService } from '../cache/builders/remote-data-build.serv
 import { getMockRemoteDataBuildService } from '../../shared/mocks/mock-remote-data-build.service';
 
 const LINK_NAME = 'authorities';
-const BROWSE = 'entries';
+const ENTRIES = 'entries';
+const ENTRY_VALUE = 'entryValue';
 
 class TestService extends IntegrationService {
   protected linkPath = LINK_NAME;
-  protected browseEndpoint = BROWSE;
+  protected entriesEndpoint = ENTRIES;
+  protected entryValueEndpoint = ENTRY_VALUE;
 
   constructor(
     protected responseCache: ResponseCacheService,
diff --git a/src/app/core/integration/integration.service.ts b/src/app/core/integration/integration.service.ts
index 8e39f2a82d..e69e488f07 100644
--- a/src/app/core/integration/integration.service.ts
+++ b/src/app/core/integration/integration.service.ts
@@ -16,7 +16,8 @@ export abstract class IntegrationService {
   protected abstract requestService: RequestService;
   protected abstract rdbService: RemoteDataBuildService;
   protected abstract linkPath: string;
-  protected abstract browseEndpoint: string;
+  protected abstract entriesEndpoint: string;
+  protected abstract entryValueEndpoint: string;
   protected abstract halService: HALEndpointService;
 
   protected getData(request: GetRequest): Observable<IntegrationData> {
@@ -37,12 +38,12 @@ export abstract class IntegrationService {
         .distinctUntilChanged());
   }
 
-  protected getIntegrationHref(endpoint, options: IntegrationSearchOptions = new IntegrationSearchOptions()): string {
+  protected getEntriesHref(endpoint, options: IntegrationSearchOptions = new IntegrationSearchOptions()): string {
     let result;
     const args = [];
 
     if (hasValue(options.name)) {
-      result = `${endpoint}/${options.name}/${this.browseEndpoint}`;
+      result = `${endpoint}/${options.name}/${this.entriesEndpoint}`;
     } else {
       result = endpoint;
     }
@@ -78,9 +79,30 @@ export abstract class IntegrationService {
     return result;
   }
 
+  protected getEntryValueHref(endpoint, options: IntegrationSearchOptions = new IntegrationSearchOptions()): string {
+    let result;
+    const args = [];
+
+    if (hasValue(options.name) && hasValue(options.query)) {
+      result = `${endpoint}/${options.name}/${this.entryValueEndpoint}/${options.query}`;
+    } else {
+      result = endpoint;
+    }
+
+    if (hasValue(options.metadata)) {
+      args.push(`metadata=${options.metadata}`);
+    }
+
+    if (isNotEmpty(args)) {
+      result = `${result}?${args.join('&')}`;
+    }
+
+    return result;
+  }
+
   public getEntriesByName(options: IntegrationSearchOptions): Observable<IntegrationData> {
     return this.halService.getEndpoint(this.linkPath)
-      .map((endpoint: string) => this.getIntegrationHref(endpoint, options))
+      .map((endpoint: string) => this.getEntriesHref(endpoint, options))
       .filter((href: string) => isNotEmpty(href))
       .distinctUntilChanged()
       .map((endpointURL: string) => new IntegrationRequest(this.requestService.generateRequestId(), endpointURL))
@@ -89,4 +111,14 @@ export abstract class IntegrationService {
       .distinctUntilChanged();
   }
 
+  public getEntryByValue(options: IntegrationSearchOptions): Observable<IntegrationData> {
+    return this.halService.getEndpoint(this.linkPath)
+      .map((endpoint: string) => this.getEntryValueHref(endpoint, options))
+      .filter((href: string) => isNotEmpty(href))
+      .distinctUntilChanged()
+      .map((endpointURL: string) => new IntegrationRequest(this.requestService.generateRequestId(), endpointURL))
+      .do((request: GetRequest) => this.requestService.configure(request))
+      .flatMap((request: GetRequest) => this.getData(request))
+      .distinctUntilChanged();
+  }
 }
-- 
GitLab