2021-02-16 - Translations - Functions
Revision as of 03:34, 3 March 2021 by Sven the Barbarian (talk | contribs)
Lambda Functions
GetTranslation
/**
* Finds a matching translation from an order list of language preferences, or return the defaultTranslatesTo translation
* @param {string} subjectVertexId
* @param {string} translationLabel
* @param {string[]} languageCodes - is an array of languageCodes, ordered by preference
*
* @returns {string} translation
*/
HdrApi
Validator: (standard)
handler logic
(standard)
logic
- const foundTranslation = null
- for each languageCodes
- let translatesToEdgeId = #createTranslatesToEdgeId
- subjectVertexId
- translationLabel
- languageCode
- let translateVertex = invoke 2021-02-16 - Graph Handler - Functions#GetDirectlyRelatedVertex
- edgeId: translatesToEdgeId
- if translateVertex != null
- foundTranslation = translateVertex.translation
- break
- let translatesToEdgeId = #createTranslatesToEdgeId
- if foundTranslation == null (no translations found for array of languageCodes
- let defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
- subjectVertexId
- translationLabel
- let translateVertex = invoke 2021-02-16 - Graph Handler - Functions#GetDirectlyRelatedVertex
- edgeId: defaultTranslatesToEdgeId
- if translateVertex != null
- foundTranslation = translateVertex.translation
- else
- foundTranslation = (or could use a filler string - should never happen)
- let defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
- return foundTranslation
ListTranslationsOneLanguage
/**
* Finds all possible translations for one subject and language
* @param {string} subjectVertexId
* @param {string[]} languageCode
*
* @returns {object[]} array of translation objects (maybe edges as well)
*/
HdrApi
Validator: (standard)
handler logic
(standard)
logic
- const translations = (sync) 2021-02-16 - Graph Handler - Functions#GetDirectlyRelatedEdgesVertices
- subjectVertexId: subjectVertexId
- edgeLabel: 'possiblyTranslatesTo'
- return translations
CreateSubjectVertex
/**
* Create a new vertex for the subject of translations
* @param {string} vertexId
* @param {string} vertexLabel
* @param {object} [vertexProperties]
*
* @returns {string} vertexId
*/
HdrInv
Validator: (standard)
handler logic
(standard)
logic
- create vertex by invoking 2021-02-16 - Graph Handler - Functions#CreateVertex
- vertexId: vertexId
- vertexLabel: vertexLabel
- vertexProperties: vertexProperties
CreateTranslationVertex
/**
* Create a new vertex for a translation
* @param {string} vertexId
* @param {string} vertexLabel
* @param {string} translation
*
* @returns {string} vertexId
*/
HdrInv
Validator: (standard)
handler logic
(standard)
logic
Create statements in graph database. Expects subject vertex to already exist (does not create subject with label/properties).
- create vertex by invoking 2021-02-16 - Graph Handler - Functions#CreateVertex
- vertexId: vertexId
- vertexLabel: vertexLabel
- vertexProperties: {translation: translation}
CreateTranslation
/**
* Create a new translation for a source object
* @param {string} subjectVertexId
* @param {string} translationLabel
* @param {string} languageCode
* @param {string} translation
* @param {string} userId
*
* @returns {Object[]} ?translation vertex id?
*/
HdrInv
Validator: (standard)
handler logic
(standard)
logic
Main job is to create statements in graph database. Expects subject vertex to already exist (will not create subject with label/properties).
- const translationVertexId = NPM module - izara-shared#neptuneGraphSharedLib.createVertexId
- vertexTag: 'translation'
- uniqueId: {random uuid}
- const existingVertices = 2021-02-16 - Graph Handler - Functions#GetDirectlyRelatedVertices to find if any existing possiblyTranslatesTo
- subjectId: subjectVertexId
- edgeLabel: 'possiblyTranslatesTo'
- let hasExistingSameLanguage = false
- iterate existingVertices
- if any match this languageCode and translation can return, no additional work to be done
- if any match this languageCode set hasExistingSameLanguage = true
- create translation vertex by invoking async 2021-02-16 - Translations - Functions#CreateTranslationVertex
- vertexId: translationVertexId
- vertexLabel: translationLabel + 'Translation'
- translation: translation
- create edgeId for possiblyTranslatesTo edge:
- const possiblyTranslatesToEdgeId = subjectVertexId + '_Possible_' + translationLabel + '_' + languageCode + '_' + translationVertexId
- create possiblyTranslatesTo graph statement by invoking 2021-02-16 - Graph Handler - Functions#CreateStatement
- subjectId: subjectVertexId
- edgeLabel: 'possiblyTranslatesTo'
- edgeId: possiblyTranslatesToEdgeId
- objectId: translationVertexId
- edgeProperties:
- languageCode: languageCode
- if hasExistingSameLanguage == false
- const translatesToEdgeId = #createTranslatesToEdgeId
- subjectVertexId
- translationLabel
- languageCode
- create translatesTo graph statement by invoking async 2021-02-16 - Graph Handler - Functions#CreateStatement
- subjectId: subjectVertexId
- edgeLabel: 'translatesTo'
- edgeId: translatesToEdgeId
- objectId: translationVertexId
- edgeProperties:
- languageCode: languageCode
- const translatesToEdgeId = #createTranslatesToEdgeId
- if existingVertices.length == 0 (no existing possible translations)
- const defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
- subjectVertexId
- translationLabel
- create defaultTranslatesTo graph statement by invoking async 2021-02-16 - Graph Handler - Functions#CreateStatement
- subjectId: subjectVertexId
- edgeLabel: 'defaultTranslatesTo'
- edgeId: defaultTranslatesToEdgeId
- objectId: translationVertexId
- const defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
- const userVertexId = NPM module - izara-shared#neptuneGraphSharedLib.createVertexId
- vertexTag: 'user'
- uniqueId: userId
- const createdByEdgeId = #createCreatedByEdgeId
- translationVertexId
- create createdBy graph statement by invoking async 2021-02-16 - Graph Handler - Functions#CreateStatement
- subjectId: translationVertexId
- edgeLabel: 'createdBy'
- edgeId: createdByEdgeId
- objectId: userVertexId
- edgeProperties:
- timestamp: {current timestamp}
Functions
createTranslatesToEdgeId
/**
* @param {string} subjectVertexId
* @param {string} translationLabel
* @param {string} languageCode
*
* @returns {string} translatesToEdgeId or null if none found
*/
logic
- return subjectVertexId + '_' + translationLabel + '_' + languageCode
createDefaultTranslatesToEdgeId
/**
* @param {string} subjectVertexId
* @param {string} translationLabel
*
* @returns {string} defaultTranslatesToEdgeId or null if none found
*/
logic
- return subjectVertexId + '_Default_' + translationLabel
createCreatedByEdgeId
/**
* @param {string} translationVertexId
*
* @returns {string} createdByEdgeId
*/
logic
- return translationVertexId + '_CreatedBy'