Skip to content

Commit

Permalink
feat: adds lang to es mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
f-necas committed Aug 28, 2023
1 parent 30a1e9a commit 7bce7d0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 18 deletions.
10 changes: 6 additions & 4 deletions libs/api/metadata-converter/src/lib/gn4/atomic-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ export const selectFallback = <T, U>(field: T, fallback: U): T | U =>
field === null ? fallback : field

export const selectTranslatedValue = <T>(
source: SourceWithUnknownProps
): T | null => selectField(source, 'default')
source: SourceWithUnknownProps,
lang3: string
): T | null => selectField(source, lang3) || selectField(source, 'default')

export const selectTranslatedField = <T>(
source: SourceWithUnknownProps,
fieldName: string
): T | null => selectTranslatedValue(selectField(source, fieldName))
fieldName: string,
lang3: string
): T | null => selectTranslatedValue(selectField(source, fieldName), lang3)

export const toDate = (field) => new Date(field)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@ import { TestBed } from '@angular/core/testing'
import { ES_LINK_FIXTURES } from '@geonetwork-ui/common/fixtures'
import { Gn4FieldMapper } from './gn4.field.mapper'
import { MetadataUrlService } from './metadata-url.service'
import { TranslateService } from '@ngx-translate/core'

class MetadataUrlServiceMock {
translate = undefined
getUrl = () => 'url'
}

const translateServiceMock = {
currentLang: 'de',
}

describe('Gn4FieldMapper', () => {
let service: Gn4FieldMapper

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: MetadataUrlService, useClass: MetadataUrlServiceMock },
{
provide: TranslateService,
useValue: translateServiceMock,
},
],
})
service = TestBed.inject(Gn4FieldMapper)
Expand Down
39 changes: 27 additions & 12 deletions libs/api/metadata-converter/src/lib/gn4/gn4.field.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
OnlineLinkResource,
} from '@geonetwork-ui/common/domain/record'
import { matchProtocol } from '../common/distribution.mapper'
import { TranslateService } from '@ngx-translate/core'
import { LANG_2_TO_3_MAPPER } from '@geonetwork-ui/util/i18n'

type ESResponseSource = SourceWithUnknownProps

Expand All @@ -37,7 +39,14 @@ type EsFieldMapperFn = (
providedIn: 'root',
})
export class Gn4FieldMapper {
constructor(private metadataUrlService: MetadataUrlService) {}
constructor(
private metadataUrlService: MetadataUrlService,
private translateService: TranslateService
) {}

private lang3 = LANG_2_TO_3_MAPPER[this.translateService.currentLang]
? 'lang' + LANG_2_TO_3_MAPPER[this.translateService.currentLang]
: null

protected fields: Record<string, EsFieldMapperFn> = {
id: (output, source) =>
Expand All @@ -50,21 +59,22 @@ export class Gn4FieldMapper {
resourceTitleObject: (output, source) => ({
...output,
title: selectFallback(
selectTranslatedField(source, 'resourceTitleObject'),
selectTranslatedField(source, 'resourceTitleObject', this.lang3),
'no title'
),
}),
resourceAbstractObject: (output, source) => ({
...output,
abstract: selectFallback(
selectTranslatedField(source, 'resourceAbstractObject'),
selectTranslatedField(source, 'resourceAbstractObject', this.lang3),
'no title'
),
}),
overview: (output, source) => {
const firstOverview = getFirstValue(selectField(source, 'overview'))
const description = selectTranslatedValue<string>(
selectField(firstOverview, 'text')
selectField(firstOverview, 'text'),
this.lang3
)
return {
...output,
Expand Down Expand Up @@ -150,7 +160,7 @@ export class Gn4FieldMapper {
...output,
keywords: getAsArray(
selectField<SourceWithUnknownProps[]>(source, 'tag')
).map((tag) => selectTranslatedValue<string>(tag)),
).map((tag) => selectTranslatedValue<string>(tag, this.lang3)),
}),
inspireTheme: (output, source) => ({
...output,
Expand Down Expand Up @@ -183,14 +193,14 @@ export class Gn4FieldMapper {
).map((license) => {
const link = getAsUrl(selectField(license, 'link'))
return {
text: selectTranslatedValue<string>(license),
text: selectTranslatedValue<string>(license, this.lang3),
...(link ? { link } : {}),
}
}),
}),
lineageObject: (output, source) => ({
...output,
lineage: selectTranslatedField(source, 'lineageObject'),
lineage: selectTranslatedField(source, 'lineageObject', this.lang3),
}),
mainLanguage: (output) => output,
userSavedCount: (output, source) =>
Expand Down Expand Up @@ -243,7 +253,8 @@ export class Gn4FieldMapper {
useLimitations: [
...(output.useLimitations || []),
...selectField<SourceWithUnknownProps[]>(source, fieldName).map(
selectTranslatedValue
(source: SourceWithUnknownProps) =>
selectTranslatedValue(source, this.lang3)
),
],
}
Expand All @@ -252,7 +263,7 @@ export class Gn4FieldMapper {
...(output.accessConstraints || []),
...selectField<SourceWithUnknownProps[]>(source, fieldName).map(
(field) => ({
text: selectTranslatedValue(field),
text: selectTranslatedValue(field, this.lang3),
type: this.getConstraintsType(fieldName),
})
),
Expand Down Expand Up @@ -299,16 +310,20 @@ export class Gn4FieldMapper {
): DatasetDistribution | null => {
const url = getAsUrl(
selectFallback(
selectTranslatedField<string>(sourceLink, 'urlObject'),
selectTranslatedField<string>(sourceLink, 'urlObject', this.lang3),
selectField<string>(sourceLink, 'url')
)
)
const name = selectFallback(
selectTranslatedField<string>(sourceLink, 'nameObject'),
selectTranslatedField<string>(sourceLink, 'nameObject', this.lang3),
selectField<string>(sourceLink, 'name')
)
const description = selectFallback(
selectTranslatedField<string>(sourceLink, 'descriptionObject'),
selectTranslatedField<string>(
sourceLink,
'descriptionObject',
this.lang3
),
selectField<string>(sourceLink, 'description')
)
// no url: fail early
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CatalogRecord,
DatasetRecord,
} from '@geonetwork-ui/common/domain/record'
import { TranslateService } from '@ngx-translate/core'

class MetadataUrlServiceMock {
translate = undefined
Expand All @@ -31,6 +32,10 @@ class OrganisationsServiceMock {
)
}

const translateServiceMock = {
currentLang: 'de',
}

describe('Gn4MetadataMapper', () => {
let service: Gn4MetadataMapper

Expand All @@ -46,6 +51,10 @@ describe('Gn4MetadataMapper', () => {
provide: OrganizationsServiceInterface,
useClass: OrganisationsServiceMock,
},
{
provide: TranslateService,
useValue: translateServiceMock,
},
],
})
})
Expand Down Expand Up @@ -634,7 +643,7 @@ describe('Gn4MetadataMapper', () => {
updatedTimes: 1,
},
useLimitations: [
'Restriction lié à l’exercice du droit moral',
'Einschränkung im Zusammenhang mit der Ausübung moralischer Rechte',
"Restriction légale d'utilisation à préciser",
],
spatialExtents: [],
Expand Down
2 changes: 2 additions & 0 deletions libs/common/fixtures/src/lib/elasticsearch/full-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,8 @@ export const ES_FIXTURE_FULL_RESPONSE = deepFreeze({
{
default: 'Restriction lié à l’exercice du droit moral',
langfre: 'Restriction lié à l’exercice du droit moral',
langger:
'Einschränkung im Zusammenhang mit der Ausübung moralischer Rechte',
},
],
geom: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, DebugElement, Input, NO_ERRORS_SCHEMA } from '@angular/core'
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { By } from '@angular/platform-browser'
import { SearchFacade } from '../state/search.facade'
import { TranslateModule } from '@ngx-translate/core'
import { TranslateModule, TranslateService } from '@ngx-translate/core'
import { of } from 'rxjs'

import { ResultsHitsContainerComponent } from './results-hits.container.component'
Expand All @@ -21,6 +21,10 @@ const searchFacadeMock = {
resultsHits$: of(null),
}

const translateServiceMock = {
currentLang: 'de',
}

describe('ResultsHitsContainerComponent', () => {
let component: ResultsHitsContainerComponent
let fixture: ComponentFixture<ResultsHitsContainerComponent>
Expand All @@ -39,6 +43,10 @@ describe('ResultsHitsContainerComponent', () => {
provide: SearchFacade,
useValue: searchFacadeMock,
},
{
provide: TranslateService,
useValue: translateServiceMock,
},
],
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents()
Expand Down

0 comments on commit 7bce7d0

Please sign in to comment.