// File has been generated automatically by Eclipse Che TypeScript DTO generator

<dtos: { dto |

<! Write TypeScript DTO interface first !>

<! BEGIN declare dto module !>
export module <dto.packageName> {

  <! BEGIN declare interface !>
  export interface <dto.simpleName> {
  <dto.methods : {method |
    <method.name>(<method.parameters :{parameter | <if(!first(parameter))>,<endif><parameter.name>}>): <method.returnType>;
  }>
  toJson() : any;
  \} <! END declare interface !>
\} <! END declare dto module !>


<! Now write TypeScript DTO implementation with suffix Impl !>
<! BEGIN declare dto module !>
export module <dto.packageName> {

  <! BEGIN declare class !>
  export class <dto.simpleName>Impl implements <dto.name> {

    <! BEGIN declare fields !>
    <dto.fieldAttributeModels:{ field |
    <field.name> : <field.simpleType>;
    }>
    <! add custom field for storing the JSON object !>
    __jsonObject : any;

    <! BEGIN constructor !>
    constructor(__jsonObject?: any) {
      this.__jsonObject = __jsonObject;
      <dto.fieldAttributeModels:{ field |
      <if(field.needInitialize)>
      this.<field.name> = new <field.simpleType>();
      <endif>
      if (__jsonObject) {
        if (__jsonObject.<field.name>) {
          <! handle list object !>
          <if(field.list)>
            __jsonObject.<field.name>.forEach((item) => {
            <if(field.listOfDto)>
            this.<field.name>.push(new <field.dtoImpl>(item));
            <else>
            this.<field.name>.push(item);
            <endif>
            \});
          <endif>
          <! handle map object !>
          <if(field.map)>
          let tmp : Array\<any> = Object.keys(__jsonObject.<field.name>);
          tmp.forEach((key) => {
            <if(field.mapOfDto)>
            this.<field.name>.set(key, new <field.dtoImpl>(__jsonObject.<field.name>[key]));
            <else>
            this.<field.name>.set(key, __jsonObject.<field.name>[key]);
            <endif>
           \});
          <endif>
          <if(field.primitive)>
          this.<field.name> = __jsonObject.<field.name>;
          <endif>
          <if(field.enum)>
          this.<field.name> = __jsonObject.<field.name>;
          <endif>
          <if(field.dto)>
          this.<field.name> = new <field.dtoImpl>(__jsonObject.<field.name>);
          <endif>
        \}
      \}
      }>
    \} <! END constructor !>

    <! BEGIN write methods !>
    <dto.methods : {method |
    <! Generate Getter code !>
    <if(method.getter)>
    <method.name>() : <method.returnType> {
      return this.<method.fieldName>;
    \}
    <endif>
    <! Generate setter code !>
    <if(method.setter)>
    <method.name>(<method.argumentName> : <method.fieldType>) : void {
      this.<method.fieldName> = <method.argumentName>;
    \}
    <endif>
    <! Generate with code !>
    <if(method.with)>
    <method.name>(<method.argumentName> : <method.fieldType>) : <method.returnType> {
      this.<method.fieldName> = <method.argumentName>;
      return this;
    \}
    <endif>
  }> <! END write methods !>
      <! Write the toJson() method !>
      toJson() : any {
        let json : any = {\};

            <dto.fieldAttributeModels:{ field |
            if (this.<field.name>) {
              <! handle list object !>
              <if(field.list)>
                let listArray = [];
                this.<field.name>.forEach((item) => {
                <if(field.listOfDto)>
                listArray.push((item as <field.dtoImpl>).toJson());
                <else>
                listArray.push(item);
                <endif>
                json.<field.name> = listArray;
                \});
              <endif>
              <! handle map object !>
              <if(field.map)>
              let tmpMap : any = {\};
              for (const [key, value] of this.<field.name>.entries()) {
                <if(field.mapOfDto)>
                tmpMap[key] = (value as <field.dtoImpl>).toJson();
                <else>
                tmpMap[key] = value;
                <endif>
               \}
              json.<field.name> = tmpMap;
              <endif>
              <if(field.primitive)>
              json.<field.name> = this.<field.name>;
              <endif>
              <if(field.enum)>
              json.<field.name> = this.<field.name>;
              <endif>
              <if(field.dto)>
              json.<field.name> = (this.<field.name> as <field.dtoImpl>).toJson();
              <endif>
            \}
            }>
        return json;
      \}
  \} <! END declare class !>
\} <! END declare dto module !>

}>
