package net.contargo.iris.terminal.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

import net.contargo.iris.address.dto.GeoLocationDto;
import net.contargo.iris.terminal.Region;
import net.contargo.iris.terminal.Terminal;

import org.hibernate.validator.constraints.NotEmpty;

import java.math.BigInteger;

import javax.validation.constraints.Size;


/**
 * View Bean Class for Terminal.
 *
 * @author  Arnold Franke - franke@synyx.de
 * @author  Oliver Messner - messner@synyx.de
 */
public final class TerminalDto extends GeoLocationDto {

    private static final int MAX_NAME_SIZE = 254;
    private static final String TERMINAL = "TERMINAL";

    @NotEmpty
    @Size(max = MAX_NAME_SIZE)
    private String name;

    private boolean enabled;

    private String uniqueId;
    private String type;
    private Region region;
    private String organisationId;

    public TerminalDto() {

        // Used to create JSON Objects
    }


    public TerminalDto(Terminal terminal) {

        super(terminal);

        if (terminal != null) {
            this.name = terminal.getName();
            this.enabled = terminal.isEnabled();
            this.type = TERMINAL;
            this.organisationId = terminal.getOrganisation();
            this.uniqueId = terminal.getUniqueId() == null ? null : terminal.getUniqueId().toString();

            if (terminal.getRegion() != null) {
                this.region = terminal.getRegion();
            }
        }
    }

    @JsonProperty("uniqueId")
    public String getUniqueId() {

        return uniqueId;
    }


    public void setUniqueId(String uniqueId) {

        this.uniqueId = uniqueId;
    }


    // Setters are needed so this DTO can be used as @ModelAttribute in Spring MVC
    public void setName(String name) {

        this.name = name;
    }


    public void setEnabled(boolean enabled) {

        this.enabled = enabled;
    }


    public boolean isEnabled() {

        return enabled;
    }


    public String getName() {

        return name;
    }


    public Region getRegion() {

        return region;
    }


    public void setRegion(Region region) {

        this.region = region;
    }


    @Override
    public Terminal toEntity() {

        Terminal terminal = new Terminal(super.toEntity());
        terminal.setEnabled(this.enabled);
        terminal.setName(this.name);
        terminal.setUniqueId(this.uniqueId == null ? null : new BigInteger(this.uniqueId));
        terminal.setRegion(this.region);
        terminal.setOrganisation(this.organisationId);

        return terminal;
    }


    @Override
    public String getType() {

        return type;
    }


    public String getOrganisationId() {

        return organisationId;
    }


    public void setOrganisationId(String organisationId) {

        this.organisationId = organisationId;
    }
}

@@ -867,18 +867,16 @@
 nisation
-Id
 ;%0A%0A    p
@@ -1204,18 +1204,16 @@
 nisation
-Id
  = termi
@@ -2545,18 +2545,16 @@
 nisation
-Id
 );%0A%0A    
@@ -2689,18 +2689,16 @@
 nisation
-Id
 () %7B%0A%0A  
@@ -2714,34 +2714,32 @@
 urn organisation
-Id
 ;%0A    %7D%0A%0A%0A    pu
@@ -2763,18 +2763,16 @@
 nisation
-Id
 (String 
@@ -2783,18 +2783,16 @@
 nisation
-Id
 ) %7B%0A%0A   
@@ -2813,18 +2813,16 @@
 nisation
-Id
  = organ
@@ -2828,18 +2828,16 @@
 nisation
-Id
 ;%0A    %7D%0A

Change organisationId to organisation