File

projects/wms-framework/src/lib/dataService/entityXmlSerializer.ts

Description

Utility class that provides serialization to entity objects as a ATOM xml format

Index

Methods

Methods

Private getEntry
getEntry()
Returns : any
Private serialize
serialize(value: any)
Parameters :
Name Type Optional
value any No
Returns : any[]
Public toXml
toXml(entity: any, entityFullName: string)

Converts the given entity to a ATOM xml format.

Parameters :
Name Type Optional Description
entity any No

entity to be generated as xml

entityFullName string No

the entity full name (required in ATOM format)

Returns : string

the xml representation of the given entity

import { json2xml } from '../utils/XmlUtils';

/**
 * Utility class that provides serialization to entity objects as a ATOM xml format
 *
 * @export
 * @class EntityXmlSerializer
 */
export class EntityXmlSerializer {
  /**
   * Converts the given entity to a ATOM xml format.
   * @param entity entity to be generated as xml
   * @param entityFullName  the entity full name (required in ATOM format)
   * @returns the xml representation of the given entity
   */
  public toXml(entity: any, entityFullName: string): string {
    let newEntry = this.getEntry();
    for (var prop in entity) {
      var propElement = {};
      let valueSerialization = this.serialize(entity[prop]);
      // filtering out the unknown properties and the internal state property
      /* istanbul ignore else */
      if (
        prop !== 'dataServiceState' &&
        prop !== 'dataServiceId' &&
        (valueSerialization[0] || valueSerialization[1])
      ) {
        let propFinalName = prop;
        if (prop.startsWith('_')) {
          propFinalName = propFinalName.substr(1);
        }

        let propValue: any;
        if (entity[prop] !== undefined) {
          propValue = {
            _: valueSerialization[1],
            $: { 'm:type': valueSerialization[0] },
          };
        } else {
          propValue = {
            $: { 'm:type': valueSerialization[0], 'm:null': true },
          };
        }
        newEntry.entry.content['m:properties'][`d:${propFinalName}`] =
          propValue;
      }
    }
    //
    newEntry.entry.category.$.term = entityFullName;

    return json2xml(newEntry);
  }

  private serialize(value: any): any[] {
    switch (typeof value) {
      case 'string':
        return ['Edm.String', value];
      case 'number':
        return ['Edm.Int32', value];
      case 'boolean':
        return ['Edm.Boolean', value];
      default:
        if (value === undefined) {
          return ['Edm.String', 'null'];
        } else if (value instanceof Date) {
          return ['Edm.DateTime', value.toISOString()];
        } else {
          return [undefined, undefined];
        }
    }
  }

  private getEntry(): any {
    return {
      entry: {
        $: {
          'xmlns:d': 'http://schemas.microsoft.com/ado/2007/08/dataservices',
          'xmlns:m':
            'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata',
          xmlns: 'http://www.w3.org/2005/Atom',
        },
        category: {
          $: {
            scheme:
              'http://schemas.microsoft.com/ado/2007/08/dataservices/scheme',
            term: 'Mercer.DataQuality.SchemeProfile.Category',
          },
        },
        title: {},
        author: {
          name: {},
        },
        updated: new Date().toISOString(),
        id: {},
        content: {
          $: { type: 'application/xml' },
          'm:properties': {},
        },
      },
    };
  }
}

result-matching ""

    No results matching ""