/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2017 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU Affero General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Affero General Public License for more details. You should have received a copy of
 * the GNU Affero General Public License along with this program. If not, see
 * http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org. 
 */

package org.openlmis.fulfillment.domain;


import java.util.List;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "order_file_templates")
@NoArgsConstructor
@AllArgsConstructor
public class OrderFileTemplate extends BaseEntity {

  @Column(nullable = false)
  @Getter
  @Setter
  private String filePrefix;

  @Column(nullable = false)
  @Getter
  @Setter
  private Boolean headerInFile;

  @OneToMany(
      mappedBy = "orderFileTemplate",
      cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE},
      fetch = FetchType.EAGER,
      orphanRemoval = true)
  @OrderBy("position ASC")
  @Getter
  @Setter
  private List<OrderFileColumn> orderFileColumns;

  /**
   * Updates itself using data from {@link OrderFileTemplate.Importer}.
   *
   * @param importer instance of {@link OrderFileTemplate.Importer}
   */
  public void importDto(Importer importer) {
    id = importer.getId();
    filePrefix = importer.getFilePrefix();
    headerInFile = importer.getHeaderInFile();

    orderFileColumns.clear();
    if (importer.getOrderFileColumns() != null) {
      importer.getOrderFileColumns().forEach(
          column -> orderFileColumns.add(OrderFileColumn.newInstance(column)));
    }
  }

  /**
   * Export this object to the specified exporter (DTO).
   *
   * @param exporter exporter to export to
   */
  public void export(Exporter exporter) {
    exporter.setId(id);
    exporter.setFilePrefix(filePrefix);
    exporter.setHeaderInFile(headerInFile);
  }

  public interface Exporter {
    void setId(UUID id);

    void setFilePrefix(String filePrefix);

    void setHeaderInFile(Boolean headerInFile);

  }

  public interface Importer {
    UUID getId();

    String getFilePrefix();

    Boolean getHeaderInFile();

    List<OrderFileColumn.Importer> getOrderFileColumns();

  }
}

@@ -2303,16 +2303,63 @@
  %7B%0A     
+ for (OrderFileColumn.Importer columnImporter :
  importe
@@ -2385,35 +2385,43 @@
 ns()
-.forEach(%0A         
+) %7B%0A        OrderFileColumn
  column 
 -%3E o
@@ -2420,12 +2420,11 @@
 umn 
--%3E o
+= O
 rder
@@ -2437,52 +2437,127 @@
 lumn
-s.add(OrderFileColumn.newInstance(column)));
+.newInstance(columnImporter);%0A        column.setOrderFileTemplate(this);%0A%0A        orderFileColumns.add(column);%0A      %7D
 %0A   

Add order file template's reference to imported columns