2023-10-17 13:45:41 +00:00
|
|
|
import { BaseCommand, flags } from '@adonisjs/core/build/standalone';
|
2024-01-26 08:39:03 +00:00
|
|
|
// import Logger from '@ioc:Adonis/Core/Logger';
|
2023-10-17 13:45:41 +00:00
|
|
|
import { XMLBuilder } from 'xmlbuilder2/lib/interfaces';
|
|
|
|
import { create } from 'xmlbuilder2';
|
|
|
|
import Dataset from 'App/Models/Dataset';
|
|
|
|
import XmlModel from 'App/Library/XmlModel';
|
|
|
|
import { readFileSync } from 'fs';
|
|
|
|
import { transform } from 'saxon-js';
|
|
|
|
import { Client } from '@opensearch-project/opensearch';
|
2024-01-26 08:39:03 +00:00
|
|
|
import { getDomain } from 'App/Utils/utility-functions';
|
2023-10-17 13:45:41 +00:00
|
|
|
|
2023-10-23 13:27:39 +00:00
|
|
|
const opensearchNode = process.env.OPENSEARCH_HOST || 'localhost';
|
2023-10-31 14:38:43 +00:00
|
|
|
const client = new Client({ node: `http://${opensearchNode}` }); // replace with your OpenSearch endpoint
|
2023-10-17 13:45:41 +00:00
|
|
|
|
|
|
|
export default class IndexDatasets extends BaseCommand {
|
|
|
|
public static commandName = 'index:datasets';
|
|
|
|
public static description = 'Index datasets based on publish_id';
|
|
|
|
|
|
|
|
@flags.number({ alias: 'p' })
|
|
|
|
public publish_id: number;
|
|
|
|
|
|
|
|
public static settings = {
|
|
|
|
/**
|
|
|
|
* Set the following value to true, if you want to load the application
|
|
|
|
* before running the command. Don't forget to call `node ace generate:manifest`
|
|
|
|
* afterwards.
|
|
|
|
*/
|
|
|
|
loadApp: true,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the following value to true, if you want this command to keep running until
|
|
|
|
* you manually decide to exit the process. Don't forget to call
|
|
|
|
* `node ace generate:manifest` afterwards.
|
|
|
|
*/
|
|
|
|
stayAlive: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
this.logger.info('Hello world!');
|
|
|
|
// const { default: Dataset } = await import('App/Models/Dataset');
|
|
|
|
const datasets = await this.getDatasets();
|
|
|
|
const proc = readFileSync('public/assets2/solr.sef.json');
|
|
|
|
const index_name = 'tethys-records';
|
|
|
|
|
|
|
|
for (var dataset of datasets) {
|
|
|
|
// Logger.info(`File publish_id ${dataset.publish_id}`);
|
2024-01-26 08:39:03 +00:00
|
|
|
// const jsonString = await this.getJsonString(dataset, proc);
|
2023-10-17 13:45:41 +00:00
|
|
|
// console.log(jsonString);
|
2024-01-26 08:39:03 +00:00
|
|
|
await this.indexDocument(dataset, index_name, proc);
|
2023-10-17 13:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getDatasets(): Promise<Dataset[]> {
|
|
|
|
const query = Dataset.query().preload('xmlCache').where('server_state', 'published');
|
|
|
|
if (this.publish_id) {
|
|
|
|
query.where('publish_id', this.publish_id);
|
|
|
|
}
|
|
|
|
return await query;
|
|
|
|
}
|
|
|
|
|
2024-01-26 08:39:03 +00:00
|
|
|
private async indexDocument(dataset: Dataset, index_name: string, proc: Buffer): Promise<void> {
|
|
|
|
try {
|
|
|
|
const doc = await this.getJsonString(dataset, proc);
|
|
|
|
|
|
|
|
let document = JSON.parse(doc);
|
|
|
|
await client.index({
|
|
|
|
id: dataset.publish_id?.toString(),
|
|
|
|
index: index_name,
|
|
|
|
body: document,
|
|
|
|
refresh: true,
|
|
|
|
});
|
|
|
|
this.logger.info(`dataset with publish_id ${dataset.publish_id} successfully indexed`);
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.error(`An error occurred while indexing datsaet with publish_id ${dataset.publish_id}.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 13:45:41 +00:00
|
|
|
private async getJsonString(dataset, proc) {
|
|
|
|
let xml = create({ version: '1.0', encoding: 'UTF-8', standalone: true }, '<root></root>');
|
|
|
|
const datasetNode = xml.root().ele('Dataset');
|
|
|
|
await this.createXmlRecord(dataset, datasetNode);
|
|
|
|
const xmlString = xml.end({ prettyPrint: false });
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await transform({
|
|
|
|
stylesheetText: proc,
|
|
|
|
destination: 'serialized',
|
|
|
|
sourceText: xmlString,
|
|
|
|
});
|
|
|
|
return result.principalResult;
|
|
|
|
} catch (error) {
|
2024-01-26 08:39:03 +00:00
|
|
|
this.logger.error(`An error occurred while creating the user, error: ${error.message},`);
|
2023-10-17 13:45:41 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async createXmlRecord(dataset: Dataset, datasetNode: XMLBuilder): Promise<void> {
|
|
|
|
const domNode = await this.getDatasetXmlDomNode(dataset);
|
|
|
|
if (domNode) {
|
2024-01-26 08:39:03 +00:00
|
|
|
dataset.publish_id && this.addLandingPageAttribute(domNode, dataset.publish_id.toString());
|
|
|
|
this.addSpecInformation(domNode, 'data-type:' + dataset.type);
|
2023-10-17 13:45:41 +00:00
|
|
|
datasetNode.import(domNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getDatasetXmlDomNode(dataset: Dataset): Promise<XMLBuilder | null> {
|
|
|
|
const xmlModel = new XmlModel(dataset);
|
|
|
|
// xmlModel.setModel(dataset);
|
|
|
|
xmlModel.excludeEmptyFields();
|
|
|
|
xmlModel.caching = true;
|
|
|
|
// const cache = dataset.xmlCache ? dataset.xmlCache : null;
|
|
|
|
// dataset.load('xmlCache');
|
|
|
|
if (dataset.xmlCache) {
|
|
|
|
xmlModel.xmlCache = dataset.xmlCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return cache.getDomDocument();
|
|
|
|
const domDocument: XMLBuilder | null = await xmlModel.getDomDocument();
|
|
|
|
return domDocument;
|
|
|
|
}
|
2024-01-26 08:39:03 +00:00
|
|
|
|
|
|
|
private addSpecInformation(domNode: XMLBuilder, information: string) {
|
|
|
|
domNode.ele('SetSpec').att('Value', information);
|
|
|
|
}
|
|
|
|
|
|
|
|
private addLandingPageAttribute(domNode: XMLBuilder, dataid: string) {
|
|
|
|
const baseDomain = process.env.OAI_BASE_DOMAIN || 'localhost';
|
|
|
|
const url = 'https://' + getDomain(baseDomain) + '/dataset/' + dataid;
|
|
|
|
// add attribute du dataset xml element
|
|
|
|
domNode.att('landingpage', url);
|
|
|
|
}
|
2023-10-17 13:45:41 +00:00
|
|
|
}
|