Skip to content

Commit

Permalink
Sdtt 326 since the webuniversum generator is oslo specific make it so…
Browse files Browse the repository at this point in the history
… that the filtering is done there rather than in the frontend generators (#65)

* Added `publicationEnvironment` to the `README`

* Added new `applyFiltering` flag to `webuniversum-generator` and let this generator do the filtering/sorting for the frontend apps

* Bumped package versions
  • Loading branch information
KristofVDB1 authored Jul 30, 2024
1 parent b25dff2 commit 306b1f3
Show file tree
Hide file tree
Showing 15 changed files with 487 additions and 367 deletions.
13 changes: 9 additions & 4 deletions packages/oslo-converter-uml-ea/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,28 @@ npm install @oslo-flanders/ea-converter
```

## Global install

To use the service from the command line anywhere, you can install it globally.

```bash
npm install -g @oslo-flanders/ea-converter
```

## API

The service is executed from the CLI and expects the following parameters:
| Parameter | Description | Required | Possible values |
| --------- | --------- | ----------- | --------------- |
| `--umlFile` | The URL or local file path of an EA UML diagram | :heavy_check_mark: ||
| `--diagramName` | The name of the UML diagram within the EAP file | :heavy_check_mark: ||
| `--outputFile` | The name of the RDF output file | No, but if omitted, output is written to process.stdout ||
| `--publicationEnvironment` | The base URI of environment where the document will be published | :heavy_check_mark: | |
| `--versionId` | Version identifier for the document | :heavy_check_mark: ||
| `--outputFile` | The name of the RDF output file | No, but if omitted, output is written to process.stdout ||
| `--outputFormat` | RDF content-type specifiying the output format | :heavy_check_mark: | `application/ld+json` |

## Usage

```bash
oslo-converter-ea --umlFile path/to/uml/diagram.eap --diagramName "diagramName" --versionId "test/1" --outputFile path/to/output.jsonld --outputFormat application/ld+json
oslo-converter-ea --umlFile https://github.com/path/to/uml/diagram.eap --diagramName "My UML diagram" --versionId "test/1" --outputFormat application/ld+json
```
oslo-converter-ea --umlFile path/to/uml/diagram.eap --diagramName "diagramName" --versionId "test/1" --outputFile path/to/output.jsonld --outputFormat application/ld+json --publicationEnvironment https://data.vlaanderen.be
oslo-converter-ea --umlFile https://github.com/path/to/uml/diagram.eap --diagramName "My UML diagram" --versionId "test/1" --outputFormat application/ld+json --publicationEnvironment https://data.vlaanderen.be
```
3 changes: 0 additions & 3 deletions packages/oslo-generator-html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ export * from './lib/HtmlGenerationService';
export * from './lib/HtmlGenerationServiceRunner';
export * from './lib/config/DependencyInjectionConfig';
export * from './lib/config/HtmlGenerationServiceConfiguration';
export * from './lib/utils/utils';
export * from './lib/utils/languageEnum';
export * from './lib/utils/specificationTypeEnum';
export * from './lib/types/Class';
148 changes: 38 additions & 110 deletions packages/oslo-generator-html/lib/HtmlGenerationService.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
import { writeFile, mkdir, readdir, stat, copyFile } from 'fs/promises';
import util from 'util';
import path, { resolve, dirname } from 'path';
import { sortClasses, sortDataTypeProperties } from './utils/utils';
import { IService, Scope } from '@oslo-flanders/core';
import { writeFile, mkdir } from 'fs/promises';
import { resolve, dirname } from 'path';
import { IService } from '@oslo-flanders/core';
import { Logger, ServiceIdentifier, fetchFileOrUrl } from '@oslo-flanders/core';
import { inject, injectable } from 'inversify';
import * as nj from 'nunjucks';
import { HtmlGenerationServiceConfiguration } from './config/HtmlGenerationServiceConfiguration';
import { SpecificationType } from './utils/specificationTypeEnum';
import { Class } from './types/Class';
import { Languages } from './utils/languageEnum';

@injectable()
export class HtmlGenerationService implements IService {
public readonly logger: Logger;
public readonly configuration: HtmlGenerationServiceConfiguration;

isInPackage: (c: Class) => boolean = (c: Class) =>
c?.scope === Scope.InPackage;
isExternal = (c: Class) => c?.scope === Scope.External;
isInPublicationEnvironment = (c: Class) =>
c?.scope === Scope.InPublicationEnvironment;
isScoped = (c: Class) => !!c?.scope;

public constructor(
@inject(ServiceIdentifier.Logger) logger: Logger,
@inject(ServiceIdentifier.Configuration)
Expand All @@ -33,15 +22,21 @@ export class HtmlGenerationService implements IService {
}

public async init(): Promise<void> {
const env = nj.configure(resolve(`${__dirname}/templates`));
env.addGlobal('getAnchorTag', this.getAnchorTag);
// Read any custom templates from the templates directory if they are provided
const dirs: string[] = [resolve(`${__dirname}/templates`)];
if (this.configuration.templates) {
await this.copyDir(
this.configuration.templates,
path.join(__dirname, 'templates'),
);
let templatesPath = this.configuration.templates;

// Ensure the path ends with a trailing slash
if (!templatesPath.endsWith('/')) {
templatesPath += '/';
}

const customTemplatesDir: string = resolve(templatesPath);
dirs.push(customTemplatesDir);
}

const env = nj.configure(dirs);
env.addGlobal('getAnchorTag', this.getAnchorTag);
}

public async run(): Promise<void> {
Expand All @@ -59,45 +54,28 @@ export class HtmlGenerationService implements IService {

let data: any = {};

const languageKey: Languages =
Languages[<keyof typeof Languages>this.configuration.language];

const { classes, dataTypes } = config;

data.entities = this.filterEntities(classes);

data.scopedDataTypes = sortDataTypeProperties(
sortClasses(this.filterClasses(dataTypes, this.isScoped), languageKey),
languageKey,
);

data.inPackageDataTypes = this.filterClasses(dataTypes, this.isInPackage);
data.inPackageClasses = this.filterClasses(classes, this.isInPackage);
data.inPackageMerged = this.mergeAndSortClasses(
data.inPackageDataTypes,
data.inPackageClasses,
languageKey,
);
data.inPackageProperties = sortClasses(
this.filterAndFlattenProperties(data.inPackageMerged, this.isInPackage),
languageKey,
);

data.metadata = metadata;

data.externalClasses = this.filterClasses(classes, this.isExternal);
data.externalDataTypes = this.filterClasses(dataTypes, this.isExternal);
data.externalMerged = this.mergeAndSortClasses(
data.externalDataTypes,
data.externalClasses,
languageKey,
);
data.externalProperties = sortClasses(
this.filterAndFlattenProperties(data.externalMerged, this.isExternal),
languageKey,
);

data.stakeholders = stakeholders;
const {
entities,
inPackageClasses,
inPackageDataTypes,
scopedDataTypes,
externalProperties,
inPackageProperties,
inPackageMerged,
} = config;

data = {
...data,
entities,
inPackageMerged,
inPackageClasses,
scopedDataTypes,
inPackageDataTypes,
inPackageProperties,
externalProperties,
metadata,
stakeholders,
};

const html = nj.render(indexPath, {
specName: this.configuration.specificationName,
Expand All @@ -111,32 +89,6 @@ export class HtmlGenerationService implements IService {
await writeFile(this.configuration.output, html);
}

private filterEntities = (entities: Class[]): Class[] =>
entities.filter(
(c) => this.isInPackage(c) || this.isInPublicationEnvironment(c),
);

private mergeAndSortClasses = (
dataTypes: Class[],
classes: Class[],
language: Languages,
): Class[] => {
const merged = [...dataTypes, ...classes];
return sortClasses(merged, language);
};

private filterAndFlattenProperties = (
entities: Class[],
filter: (entity: Class) => boolean,
): Class[] => {
return entities.flatMap((entity) => entity.properties).filter(filter);
};

private filterClasses = (
classes: Class[],
filter: (c: Class) => boolean,
): Class[] => classes.filter(filter);

private getAnchorTag = (id: string, type: string) => {
let domain: string = '';
if (id && id?.includes('#')) {
Expand All @@ -156,28 +108,4 @@ export class HtmlGenerationService implements IService {
throw error;
}
}

private copyDir = async (srcDir: string, destDir: string): Promise<void> => {
try {
await mkdir(destDir, { recursive: true });
const files = await readdir(srcDir);

for (const file of files) {
const srcFile = path.join(srcDir, file);
const destFile = path.join(destDir, file);
const fileStat = await stat(srcFile);

if (fileStat.isDirectory()) {
await this.copyDir(srcFile, destFile);
} else {
await copyFile(srcFile, destFile);
}
}
} catch (error) {
console.error(
`Error copying directory from ${srcDir} to ${destDir}:`,
error,
);
}
};
}
32 changes: 15 additions & 17 deletions packages/oslo-generator-html/lib/templates/ap2.j2
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,20 @@
<div class="grid">
<div class="col--8-12 col--12-12--s">
<div class="region" id="introduction">
<div class="region" id="introduction">
<div class="introduction typography">
{% block introduction %}
<p>Dit document beschrijft een
<strong>applicatieprofiel</strong>, in dit geval
<strong>
{{ data.metadata.title }}</strong>. <!-- Een applicatieprofiel is een specificatie die bestaande termen hergebruikt van verschillende basis-standaarden en
daar meer specificiteit aan toevoegt. -->
<!-- Daarbij wordt het gebruik van de termen toegelicht voor de interbestuurlijke uitwisseling van overheidsinformatie.
-->
Dit applicatieprofiel beantwoordt de vraag over hoe het corresponderende domeinmodel in de praktijk kan toegepast
worden. Daarbij worden de beperkingen (kardinaliteit, codelijsten) toegelicht en de overeenkomstige (RDF) termen
opgelijst.
</p>
{% endblock %}
</div>
<div class="introduction typography">
{% block introduction %}
<p>Dit document beschrijft een
<strong>applicatieprofiel</strong>, in dit geval
<strong>
{{ data.metadata.title }}</strong>. <!-- Een applicatieprofiel is een specificatie die bestaande termen hergebruikt van verschillende basis-standaarden en
daar meer specificiteit aan toevoegt. -->
<!-- Daarbij wordt het gebruik van de termen toegelicht voor de interbestuurlijke uitwisseling van overheidsinformatie.
-->
Dit applicatieprofiel beantwoordt de vraag over hoe het corresponderende domeinmodel in de praktijk kan toegepast
worden. Daarbij worden de beperkingen (kardinaliteit, codelijsten) toegelicht en de overeenkomstige (RDF) termen
opgelijst.
</p>
{% endblock %}
</div>
</div>
<div class="region region--no-space-top typography" id="summary"> <h2 class="h2" aria-level="1" role="heading" id="h2_sotd">Samenvatting</h2>
Expand Down Expand Up @@ -385,7 +383,7 @@
{% endif %}
</div>
{% endfor %}
{% if data.scipedDataTypes %}
{% if data.scopedDataTypes %}
<div class="region region--no-space-top">
<h2 class="h2">Datatypes</h2>
</div>
Expand Down
14 changes: 14 additions & 0 deletions packages/oslo-generator-html/lib/templates/voc2.j2
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,20 @@
</div>
</div>
{% endif %}
{% if ( ( data.externalProperties | length ) > 0 ) %}
<div class="region region--no-space-top">
<h3 class="h3">Externe terminologie</h3>
<div class="grid">
<p>
|
{% for prop in data.externalProperties %}
<a href="#{{ prop.vocabularyLabel[language] }}" rel="property">{{ prop.vocabularyLabel[language] }}</a>
|
{% endfor %}
</p>
</div>
</div>
{% endif %}
</section>
</div>
<div class="region region--no-space-top">
Expand Down
31 changes: 0 additions & 31 deletions packages/oslo-generator-html/lib/types/Class.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/oslo-generator-html/lib/utils/languageEnum.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/oslo-generator-html/lib/utils/utils.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/oslo-generator-html/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oslo-flanders/html-generator",
"version": "0.0.10-alpha.0",
"version": "0.0.11-alpha.0",
"description": "Generates an HTML file using an OSLO webuniversum config",
"author": "Digitaal Vlaanderen <https://data.vlaanderen.be/id/organisatie/OVO002949>",
"homepage": "https://github.com/informatievlaanderen/OSLO-UML-Transformer/tree/main/packages/oslo-generator-html#readme",
Expand Down
11 changes: 7 additions & 4 deletions packages/oslo-generator-json-webuniversum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ npm install -g @oslo-flanders/json-webuniversum-generator
## API

The service is executed from the CLI and expects the following parameters:
| Parameter | Description | Required | Possible values |
| Parameter | Description | Required | Possible values |
| ---------- | ----------------------------------------------------------- | --------------------------------- | --------------- |
| `--input` | The URL or local file path of an OSLO stakeholders csv file |:heavy_check_mark: | |
| `--output` | Name of the output file | No, default `webuniversum-config.json` | |
| `--input` | The URL or local file path of an OSLO stakeholders csv file |:heavy_check_mark: | |
| `--output` | Name of the output file | No, default `webuniversum-config.json` | |
| `--language` | The language in which the config must be generated | :heavy_check_mark: | |
| `--applyFiltering` | Wether or not to apply filters on the generated output. The filters are used for the OSLO-frontend application(s) | No, default `true` | `true` or `false` |

## Usage

```bash
oslo-webuniversum-json-generator --input report.jsonld --language nl
```
oslo-webuniversum-json-generator --input report.jsonld --language nl --applyFiltering false
```
Loading

0 comments on commit 306b1f3

Please sign in to comment.