Skip to content

Commit

Permalink
chore: use UI-safe Criterion && update UiCriterion
Browse files Browse the repository at this point in the history
  • Loading branch information
SaadEGI committed Sep 20, 2023
1 parent c8f0868 commit d015c9d
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 91 deletions.
2 changes: 1 addition & 1 deletion src/app/core/services/api/contract-offer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ContractOfferService {
...contractOfferDto,
asset: this.assetPropertyMapper.buildAsset({
connectorEndpoint,
properties: contractOfferDto.asset.properties,
asset: contractOfferDto.asset,
}),
};
}
Expand Down
52 changes: 23 additions & 29 deletions src/app/core/services/api/fake-backend/data/test-assets.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
import {AssetDto, AssetEntry, UiAsset} from '@sovity.de/edc-client';
import {AssetProperties} from '../../../asset-properties';
import {AssetDto, UiAsset} from '@sovity.de/edc-client';

export namespace TestAssets {
export const boring: UiAsset = {
properties: {
[AssetProperties.id]: 'test-asset-1',
[AssetProperties.name]: 'Test Asset 1',
[AssetProperties.description]: 'This is a test asset.',
},
assetId: 'test-asset-1',
name: 'Test Asset 1',
description: 'This is a test asset.',
privateProperties: {
'some-private-property': 'abc',
},
};

export const full: UiAsset = {
[AssetProperties.id]: 'urn:artifact:my-test-asset-4',
[AssetProperties.name]: 'Rail Network 2023 NRW - RailDesigner Export',
[AssetProperties.version]: '1.1',
[AssetProperties.originatorOrganization]: 'Deutsche Bahn AG',
[AssetProperties.keywords]: 'db, bahn, rail, Rail-Designer',
[AssetProperties.contentType]: 'application/json',
[AssetProperties.description]:
assetId: 'urn:artifact:my-test-asset-4',
name: 'Rail Network 2023 NRW - RailDesigner Export',
version: '1.1',
creatorOrganizationName: 'Deutsche Bahn AG',
keywords: ['db', 'bahn', 'rail', 'Rail-Designer'],
mediaType: 'application/json',
description:
'Train Network Map released on 10.01.2023, valid until 31.02.2023. \nFile format is xyz as exported by Rail-Designer.',
[AssetProperties.language]: 'https://w3id.org/idsa/code/EN',
[AssetProperties.publisher]: 'https://my.cool-api.gg/about',
[AssetProperties.standardLicense]: 'https://my.cool-api.gg/license',
[AssetProperties.endpointDocumentation]: 'https://my.cool-api.gg/docs',
[AssetProperties.dataCategory]: 'Infrastructure and Logistics',
[AssetProperties.dataSubcategory]:
'General Information About Planning Of Routes',
[AssetProperties.dataModel]: 'my-data-model-001',
[AssetProperties.geoReferenceMethod]: 'my-geo-reference-method',
[AssetProperties.transportMode]: 'Rail',
language: 'https://w3id.org/idsa/code/EN',
publisherHomepage: 'https://my.cool-api.gg/about',
licenseUrl: 'https://my.cool-api.gg/license',
landingPageUrl: 'https://my.cool-api.gg/docs',
dataCategory: 'Infrastructure and Logistics',
dataSubcategory: 'General Information About Planning Of Routes',
dataModel: 'my-data-model-001',
geoReferenceMethod: 'my-geo-reference-method',
transportMode: 'Rail',
additionalProperties: {},
privateProperties: {},
};
Expand All @@ -40,19 +36,17 @@ export namespace TestAssets {
return {
assetId: entry.assetId,
createdAt: new Date(),
properties: entry.properties,
properties: {},
};
}

export function toDummyAsset(entry: UiAsset): UiAsset {
return dummyAsset(entry.assetId[AssetProperties.id]);
return dummyAsset(entry.assetId);
}

export function dummyAsset(assetId: string): UiAsset {
return {
properties: {
[AssetProperties.id]: assetId,
},
assetId,
privateProperties: {},
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/app/core/services/api/fake-backend/data/test-policies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {UiPolicyDto} from '@sovity.de/edc-client';
import {UiPolicy} from '@sovity.de/edc-client';

export namespace TestPolicies {
export const connectorRestricted: UiPolicyDto = {
export const connectorRestricted: UiPolicy = {
policyJsonLd: '{"example-policy-jsonld": true}',
constraints: [
{
Expand All @@ -13,7 +13,7 @@ export namespace TestPolicies {
errors: [],
};

export const warnings: UiPolicyDto = {
export const warnings: UiPolicy = {
policyJsonLd: '{"example-policy-jsonld": true}',
constraints: [
{
Expand All @@ -24,7 +24,7 @@ export namespace TestPolicies {
],
errors: ['$.duties: Duties are currently unsupported.'],
};
export const failedMapping: UiPolicyDto = {
export const failedMapping: UiPolicy = {
policyJsonLd: '{"example-policy-jsonld": true}',
constraints: [],
errors: ['No constraints found!'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
TransferHistoryEntry,
TransferHistoryPage,
} from '@sovity.de/edc-client';
import {AssetProperties} from '../../asset-properties';
import {assets} from './asset-fake-service';
import {TestAssets} from './data/test-assets';

Expand Down Expand Up @@ -67,8 +66,10 @@ export let transferHistoryEntries: TransferHistoryEntry[] = [
contractAgreementId: 'test-asset-4-cd:f52a5d30-6356-4a55-a75a-3c45d7a88c3e',
direction: 'PROVIDING',
counterPartyConnectorEndpoint: 'https://sovity-demo2-edc/api/v1/ids/data',
assetName: TestAssets.full.properties[AssetProperties.name],
assetId: TestAssets.full.properties[AssetProperties.id],
assetName: TestAssets.full.name
? TestAssets.full.name
: TestAssets.full.assetId,
assetId: TestAssets.full.assetId,
},
];

Expand All @@ -89,9 +90,7 @@ export const transferProcessAsset = (transferProcessId: string): AssetDto => {
TestAssets.dummyAsset(assetId),
);

const assetEntry = assets.find(
(it) => it.properties[AssetProperties.id] === assetId,
);
const assetEntry = assets.find((it) => it.assetId === assetId);

return isProviding && assetEntry
? TestAssets.toAssetDto(assetEntry)
Expand Down
155 changes: 110 additions & 45 deletions src/app/core/services/asset-property-mapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Inject, Injectable} from '@angular/core';
import {UiAsset} from '@sovity.de/edc-client';
import {AssetEditorDialogFormValue} from '../../routes/connector-ui/asset-page/asset-create-dialog/asset-editor-dialog-form-model';
import {DataCategorySelectItemService} from '../../routes/connector-ui/asset-page/data-category-select/data-category-select-item.service';
import {DataSubcategorySelectItemService} from '../../routes/connector-ui/asset-page/data-subcategory-select/data-subcategory-select-item.service';
Expand Down Expand Up @@ -27,76 +28,133 @@ export class AssetPropertyMapper {
private activeFeatureSet: ActiveFeatureSet,
) {}

buildAsset(opts: {
connectorEndpoint: string;
properties: Record<string, string | null>;
}): Asset {
const props = opts.properties;
const lookup = <T>(key: string, fn: (id: string) => T) =>
props[key] ? fn(props[key]!) : null;
buildAsset(opts: {connectorEndpoint: string; asset: UiAsset}): Asset {
const asset = opts.asset;

const language = lookup(AssetProperties.language, (id) =>
this.languageSelectItemService.findById(id),
);
const dataCategory = lookup(AssetProperties.dataCategory, (id) =>
this.dataCategorySelectItemService.findById(id),
);
const dataSubcategory = lookup(AssetProperties.dataSubcategory, (id) =>
this.dataSubcategorySelectItemService.findById(id),
);
const transportMode = lookup(AssetProperties.transportMode, (id) =>
this.transportModeSelectItemService.findById(id),
);
const keywords = (props[AssetProperties.keywords] ?? '')
.split(',')
.map((it) => it.trim())
.filter((it) => it);
const language = asset.language
? this.languageSelectItemService.findById(asset.language)
: null;
const dataCategory = asset.dataCategory
? this.dataCategorySelectItemService.findById(asset.dataCategory)
: null;
const dataSubcategory = asset.dataSubcategory
? this.dataSubcategorySelectItemService.findById(asset.dataSubcategory)
: null;
const transportMode = asset.transportMode
? this.transportModeSelectItemService.findById(asset.transportMode)
: null;

const id = props[AssetProperties.id] ?? 'no-id-was-set';
const additionalProperties = this.buildAdditionalProperties(props);
const keywords = asset.keywords ?? [];

const id = asset.assetId ?? 'no-id-was-set';
const additionalProperties = this.buildAdditionalProperties(
asset.additionalProperties,
);

return {
id,
name: props[AssetProperties.name] ?? id,
version: props[AssetProperties.version],
contentType: props[AssetProperties.contentType],
name: asset.name ?? id,
version: asset.version ?? null,
contentType: asset.mediaType ?? null,
originator: opts.connectorEndpoint,
originatorOrganization:
props[AssetProperties.curatorOrganizationName] ??
'Unknown Organization',
asset.creatorOrganizationName ?? 'Unknown Organization',
keywords,
description: props[AssetProperties.description],
description: trimmedOrNull(asset.description),
language,
publisher: props[AssetProperties.publisher],
standardLicense: props[AssetProperties.standardLicense],
endpointDocumentation: props[AssetProperties.endpointDocumentation],
publisher: asset.publisherHomepage ?? null,
standardLicense: asset.licenseUrl ?? null,
endpointDocumentation: asset.landingPageUrl ?? null,
dataCategory,
dataSubcategory,
dataModel: props[AssetProperties.dataModel],
geoReferenceMethod: props[AssetProperties.geoReferenceMethod],
dataModel: asset.dataModel ?? null,
geoReferenceMethod: asset.geoReferenceMethod ?? null,
transportMode,
httpProxyMethod: this._parseBoolean(
httpProxyMethod: this._parseBoolean(asset.httpDatasourceHintsProxyMethod),
httpProxyPath: this._parseBoolean(asset.httpDatasourceHintsProxyPath),
httpProxyQueryParams: this._parseBoolean(
asset.httpDatasourceHintsProxyBody,
),
httpProxyBody: this._parseBoolean(asset.httpDatasourceHintsProxyBody),
additionalProperties,
};
}

buildUiAssetfromprops(props: Record<string, string>): UiAsset {
return {
assetId: props[AssetProperties.id] ?? '',
name: props[AssetProperties.name] ?? '',
version: props[AssetProperties.version] ?? '',
creatorOrganizationName:
props[AssetProperties.curatorOrganizationName] ?? '',
keywords: props[AssetProperties.keywords]
?.split(',')
.map((it) => it.trim()),
mediaType: props[AssetProperties.contentType] ?? '',
description: props[AssetProperties.description] ?? '',
language: props[AssetProperties.language] ?? '',
publisherHomepage: props[AssetProperties.publisher] ?? '',
licenseUrl: props[AssetProperties.standardLicense] ?? '',
landingPageUrl: props[AssetProperties.endpointDocumentation] ?? '',
dataCategory: props[AssetProperties.dataCategory] ?? '',
dataSubcategory: props[AssetProperties.dataSubcategory] ?? '',
dataModel: props[AssetProperties.dataModel] ?? '',
geoReferenceMethod: props[AssetProperties.geoReferenceMethod] ?? '',
transportMode: props[AssetProperties.transportMode] ?? '',

httpDatasourceHintsProxyMethod: this._parseBooleanfromString(
props[AssetProperties.httpProxyMethod],
),
httpProxyPath: this._parseBoolean(props[AssetProperties.httpProxyPath]),
httpProxyQueryParams: this._parseBoolean(
httpDatasourceHintsProxyPath: this._parseBooleanfromString(
props[AssetProperties.httpProxyPath],
),
httpDatasourceHintsProxyQueryParams: this._parseBooleanfromString(
props[AssetProperties.httpProxyQueryParams],
),
httpProxyBody: this._parseBoolean(props[AssetProperties.httpProxyBody]),
additionalProperties,
httpDatasourceHintsProxyBody: this._parseBooleanfromString(
props[AssetProperties.httpProxyBody],
),

additionalProperties: this.buildAdditionalPropertiesforUiAsset(props),
};
}

private buildAdditionalProperties(props: Record<string, string | null>) {
private buildAdditionalProperties(
props: Record<string, string> | undefined,
): Array<{key: string; value: string}> {
if (!props) {
return [];
}

const knownKeys = Object.values(AssetProperties);
return Object.entries(props)
.filter(([k, _]) => !knownKeys.includes(k))
.filter(([k]) => !knownKeys.includes(k))
.map(([key, value]) => ({
key,
value: value ?? '',
}));
}

private buildAdditionalPropertiesforUiAsset(
props: Record<string, string> | undefined,
): {[key: string]: string} {
if (!props) {
return {};
}

const knownKeys = Object.values(AssetProperties);
const filteredEntries = Object.entries(props).filter(
([k]) => !knownKeys.includes(k),
);

const result: {[key: string]: string} = {};
for (const [key, value] of filteredEntries) {
result[key] = value ?? '';
}

return result;
}

buildProperties(
formValue: AssetEditorDialogFormValue,
): Record<string, string> {
Expand Down Expand Up @@ -153,10 +211,17 @@ export class AssetPropertyMapper {
return removeNullValues(props);
}

private _parseBoolean(value: string | null): boolean | null {
if (!value) {
private _parseBoolean(value: boolean | null | undefined): boolean | null {
if (value != true && value != false) {
return null;
}
return value;
}

private _parseBooleanfromString(value: string | null): boolean | undefined {
if (!value) {
return undefined;
}
return value === 'true';
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/core/services/asset-service-mapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class AssetServiceMapped {
assetPage.assets.map((asset) =>
this.assetPropertyMapper.buildAsset({
connectorEndpoint: this.config.connectorEndpoint,
properties: asset.properties,
asset: asset,
}),
),
),
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/services/models/contract-offer-dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {UiAsset} from '@sovity.de/edc-client';
import {Policy} from '../api/legacy-managent-api-client';
import {AssetDto} from './asset-dto';

/**
* Contract Offer (API Model)
Expand All @@ -9,5 +9,5 @@ export interface ContractOfferDto {
policy: Policy;
provider: string;
consumer: string;
asset: AssetDto;
asset: UiAsset;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class BrokerCatalogMapper {
...offer,
asset: this.assetPropertyMapper.buildAsset({
connectorEndpoint: offer.connectorEndpoint,
properties: offer.properties,
asset: this.assetPropertyMapper.buildUiAssetfromprops(offer.properties),
}),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ContractAgreementCardMappedService {
): ContractAgreementCardMapped {
let asset = this.assetPropertyMapper.buildAsset({
connectorEndpoint: contractAgreement.counterPartyAddress,
properties: contractAgreement.asset.properties,
asset: contractAgreement.asset,
});

return {
Expand Down
Loading

0 comments on commit d015c9d

Please sign in to comment.