2021-02-16 - Translations - Functions

From Izara Wiki
Jump to navigation Jump to search

Service - Translations

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} vertexId
 */

HdrApi

Validator: (standard)

handler logic

(standard)

logic

  1. const foundTranslation = null
  2. for each languageCodes
    1. let translatesToEdgeId = #createTranslatesToEdgeId
      1. subjectVertexId
      2. translationLabel
      3. languageCode
    2. let translateVertex = invoke 2021-02-16 - Graph Handler - Functions#GetDirectlyRelatedVertex
      1. edgeId: translatesToEdgeId
    3. if translateVertex != null
      1. foundTranslation = translateVertex.translation
      2. break
  3. if foundTranslation == null (no translations found for array of languageCodes
    1. let defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
      1. subjectVertexId
      2. translationLabel
    2. let translateVertex = invoke 2021-02-16 - Graph Handler - Functions#GetDirectlyRelatedVertex
      1. edgeId: defaultTranslatesToEdgeId
    3. if translateVertex != null
      1. foundTranslation = translateVertex.translation
    4. else
      1. foundTranslation = (or could use a filler string - should never happen)
  4. return foundTranslation

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

  1. create vertex by invoking 2021-02-16 - Graph Handler - Functions#CreateVertex
    1. vertexId: vertexId
    2. vertexLabel: vertexLabel
    3. 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).

  1. create vertex by invoking 2021-02-16 - Graph Handler - Functions#CreateVertex
    1. vertexId: vertexId
    2. vertexLabel: vertexLabel
    3. 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).

  1. const translationVertexId = NPM module - izara-shared#neptuneGraphSharedLib.createVertexId
    1. vertexTag: 'translation'
    2. uniqueId: {random uuid}
  2. const existingVertices = 2021-02-16 - Graph Handler - Functions#GetDirectlyRelatedVertices to find if any existing possiblyTranslatesTo
    1. subjectId: subjectVertexId
    2. edgeLabel: possiblyTranslatesTo
  3. let hasExistingSameLanguage = false
  4. iterate existingVertices
    1. if any match this languageCode and translation can return, no additional work to be done
    2. if any match this languageCode set hasExistingSameLanguage = true
  5. create translation vertex by invoking async 2021-02-16 - Translations - Functions#CreateTranslationVertex
    1. vertexId: translationVertexId
    2. vertexLabel: translationLabel + 'Translation'
    3. translation: translation
  6. create edgeId for possiblyTranslatesTo edge:
    1. const possiblyTranslatesToEdgeId = subjectVertexId + '_Possible_' + translationLabel + '_' + languageCode + '_' + translationVertexId
  7. create possiblyTranslatesTo graph statement by invoking 2021-02-16 - Graph Handler - Functions#CreateStatement
    1. subjectId: subjectVertexId
    2. edgeLabel: 'possiblyTranslatesTo'
    3. edgeId: possiblyTranslatesToEdgeId
    4. objectId: translationVertexId
    5. edgeProperties:
      1. languageCode: languageCode
  8. if hasExistingSameLanguage == false
    1. const translatesToEdgeId = #createTranslatesToEdgeId
      1. subjectVertexId
      2. translationLabel
      3. languageCode
    2. create translatesTo graph statement by invoking async 2021-02-16 - Graph Handler - Functions#CreateStatement
      1. subjectId: subjectVertexId
      2. edgeLabel: 'translatesTo'
      3. edgeId: translatesToEdgeId
      4. objectId: translationVertexId
      5. edgeProperties:
        1. languageCode: languageCode
  9. if existingVertices.length == 0 (no existing possible translations)
    1. const defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
      1. subjectVertexId
      2. translationLabel
    2. create defaultTranslatesTo graph statement by invoking async 2021-02-16 - Graph Handler - Functions#CreateStatement
      1. subjectId: subjectVertexId
      2. edgeLabel: 'defaultTranslatesTo'
      3. edgeId: defaultTranslatesToEdgeId
      4. objectId: translationVertexId
  10. const userVertexId = NPM module - izara-shared#neptuneGraphSharedLib.createVertexId
    1. vertexTag: 'user'
    2. uniqueId: userId
  11. const createdByEdgeId = #createCreatedByEdgeId
    1. translationVertexId
  12. create createdBy graph statement by invoking async 2021-02-16 - Graph Handler - Functions#CreateStatement
    1. subjectId: translationVertexId
    2. edgeLabel: 'createdBy'
    3. edgeId: createdByEdgeId
    4. objectId: userVertexId
    5. edgeProperties:
      1. timestamp: {current timestamp}

Functions

createTranslatesToEdgeId

/**
 * @param {string} subjectVertexId
 * @param {string} translationLabel
 * @param {string} languageCode
 *
 * @returns {string} translatesToEdgeId or null if none found
 */

logic

  1. return subjectVertexId + '_' + translationLabel + '_' + languageCode

createDefaultTranslatesToEdgeId

/**
 * @param {string} subjectVertexId
 * @param {string} translationLabel
 *
 * @returns {string} defaultTranslatesToEdgeId or null if none found
 */

logic

  1. return subjectVertexId + '_Default_' + translationLabel

createCreatedByEdgeId

/**
 * @param {string} translationVertexId
 *
 * @returns {string} createdByEdgeId
 */

logic

  1. return translationVertexId + '_CreatedBy'