<%#
 Copyright 2013-2019 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%=packageName%>.service.dto;<%
let importApiModelProperty = false;
let importJsonIgnore = false;
let importJsonIgnoreProperties = false;
let importSet = false;
const uniqueEnums = {};
%><%- include ../../domain/imports -%>
<%_ if (typeof javadoc != 'undefined') { _%>
import io.swagger.annotations.ApiModel;
<%_ } if (importApiModelProperty === true) { _%>
import io.swagger.annotations.ApiModelProperty;
<%_ } _%>
<%_ if (fieldsContainInstant) { _%>
import java.time.Instant;
<%_ } _%>
<%_ if (fieldsContainLocalDate) { _%>
import java.time.LocalDate;
<%_ } _%>
<%_ if (fieldsContainZonedDateTime) { _%>
import java.time.ZonedDateTime;
<%_ } _%>
<%_ if (fieldsContainDuration) { _%>
import java.time.Duration;
<%_ } _%>
<%_ if (validation) { _%>
import javax.validation.constraints.*;
<%_ } _%>
import java.io.Serializable;
<%_ if (fieldsContainBigDecimal) { _%>
import java.math.BigDecimal;
<%_ } _%>
<%_ if (fieldsContainBlob && databaseType === 'cassandra') { _%>
import java.nio.ByteBuffer;
<%_ } _%>
<%_ if (fieldsContainOwnerManyToMany) { _%>
import java.util.HashSet;
import java.util.Set;
<%_ } _%>
import java.util.Objects;
<%_ if (databaseType === 'cassandra') { _%>
import java.util.UUID;
<%_ } _%>
<%_ if (fieldsContainBlob && databaseType === 'sql') { _%>
import javax.persistence.Lob;
<%_ } _%>
<%_ for (idx in fields) { if (fields[idx].fieldIsEnum) { _%>
import <%=packageName%>.domain.enumeration.<%= fields[idx].fieldType %>;
<%_ } } _%>

/**
 * A DTO for the {@link <%=packageName%>.domain.<%= asEntity(entityClass) %>} entity.
 */
<%_ if (typeof javadoc !== 'undefined') { _%>
@ApiModel(description = "<%- formatAsApiDescription(javadoc) %>")
<%_ } _%>
public class <%= asDto(entityClass) %> implements Serializable {

    private <%= pkType %> id;

    <%_ for (idx in fields) {
        if (typeof fields[idx].javadoc !== 'undefined') { _%>
<%- formatAsFieldJavadoc(fields[idx].javadoc) %>
        <%_ }
        let required = false;
        const fieldValidate = fields[idx].fieldValidate;
        const fieldValidateRules = fields[idx].fieldValidateRules;
        const fieldValidateRulesMinlength = fields[idx].fieldValidateRulesMinlength;
        const fieldValidateRulesMaxlength = fields[idx].fieldValidateRulesMaxlength;
        const fieldValidateRulesMinbytes = fields[idx].fieldValidateRulesMinbytes;
        const fieldValidateRulesMaxbytes = fields[idx].fieldValidateRulesMaxbytes;
        const fieldValidateRulesMin = fields[idx].fieldValidateRulesMin;
        const fieldValidateRulesMax = fields[idx].fieldValidateRulesMax;
        const fieldValidateRulesPatternJava = fields[idx].fieldValidateRulesPatternJava;
        const fieldType = fields[idx].fieldType;
        const fieldTypeBlobContent = fields[idx].fieldTypeBlobContent;
        const fieldName = fields[idx].fieldName;_%>
    <%_ if (fieldValidate === true) {
            if (fieldValidateRules.includes('required')) {
                required = true;
            } _%>
    <%- include ../../common/field_validators -%>
    <%_ } _%>
    <%_ if (typeof fields[idx].javadoc != 'undefined') { _%>
    @ApiModelProperty(value = "<%- formatAsApiDescription(fields[idx].javadoc) %>"<% if (required) { %>, required = true<% } %>)
    <%_ } _%>
    <%_ if (fieldType === 'byte[]' && databaseType === 'sql') { _%>
    @Lob
    <%_ } _%>
    <%_ if (fieldTypeBlobContent !== 'text') { _%>
    private <%= fieldType %> <%= fieldName %>;
    <%_ } else { _%>
    private String <%= fieldName %>;
    <%_ } _%>

    <%_ if ((fieldType === 'byte[]' || fieldType === 'ByteBuffer') && fieldTypeBlobContent !== 'text') { _%>
    private String <%= fieldName %>ContentType;
        <%_ } _%>
    <%_ } _%>
    <%_ for (idx in relationships) {
        const otherEntityRelationshipName = relationships[idx].otherEntityRelationshipName;
        const relationshipFieldName = relationships[idx].relationshipFieldName;
        const relationshipFieldNamePlural = relationships[idx].relationshipFieldNamePlural;
        const relationshipType = relationships[idx].relationshipType;
        const otherEntityNameCapitalized = relationships[idx].otherEntityNameCapitalized;
        const otherEntityFieldCapitalized = relationships[idx].otherEntityFieldCapitalized;
        const ownerSide = relationships[idx].ownerSide;
        if (typeof relationships[idx].javadoc != 'undefined') { _%>
<%- formatAsFieldJavadoc(relationships[idx].javadoc) %>
    @ApiModelProperty(value = "<%- formatAsApiDescription(relationships[idx].javadoc) %>")
    <%_ }
        if (relationshipType === 'many-to-many' && ownerSide === true) { _%>

    private Set<<%= asDto(otherEntityNameCapitalized) %>> <%= relationshipFieldNamePlural %> = new HashSet<>();
    <%_ } else if (relationshipType === 'many-to-one' || (relationshipType === 'one-to-one' && ownerSide === true)) { _%>

    private <% if (relationshipFieldName === 'user' && authenticationType === 'oauth2') { %>String<% } else { %><%= pkType %><% } %> <%= relationshipFieldName %>Id;
    <%_ if (otherEntityFieldCapitalized !== 'Id' && otherEntityFieldCapitalized !== '') { _%>

    private String <%= relationshipFieldName %><%= otherEntityFieldCapitalized %>;
    <%_ } } } _%>

    public <%= pkType %> getId() {
        return id;
    }

    public void setId(<%= pkType %> id) {
        this.id = id;
    }
    <%_ for (idx in fields) {
        const fieldType = fields[idx].fieldType;
        const fieldTypeBlobContent = fields[idx].fieldTypeBlobContent;
        const fieldInJavaBeanMethod = fields[idx].fieldInJavaBeanMethod;
        const fieldName = fields[idx].fieldName; _%>
        <%_ if (fieldTypeBlobContent !== 'text') { _%>

    public <%= fieldType %> <% if (fieldType.toLowerCase() === 'boolean') { %>is<% } else { %>get<%_ } _%><%= fieldInJavaBeanMethod %>() {
        <%_ } else { _%>

    public String get<%= fieldInJavaBeanMethod %>() {
        <%_ } _%>
        return <%= fieldName %>;
    }

        <%_ if (fieldTypeBlobContent !== 'text') { _%>
    public void set<%= fieldInJavaBeanMethod %>(<%= fieldType %> <%= fieldName %>) {
        <%_ } else { _%>
    public void set<%= fieldInJavaBeanMethod %>(String <%= fieldName %>) {
        <%_ } _%>
        this.<%= fieldName %> = <%= fieldName %>;
    }
        <%_ if ((fieldType === 'byte[]' || fieldType === 'ByteBuffer') && fieldTypeBlobContent !== 'text') { _%>

    public String get<%= fieldInJavaBeanMethod %>ContentType() {
        return <%= fieldName %>ContentType;
    }

    public void set<%= fieldInJavaBeanMethod %>ContentType(String <%= fieldName %>ContentType) {
        this.<%= fieldName %>ContentType = <%= fieldName %>ContentType;
    }
        <%_ } _%>
    <%_ } _%>
    <%_ for (idx in relationships) {
        relationshipFieldName = relationships[idx].relationshipFieldName,
        relationshipFieldNamePlural = relationships[idx].relationshipFieldNamePlural,
        otherEntityName = relationships[idx].otherEntityName,
        otherEntityNamePlural = relationships[idx].otherEntityNamePlural,
        relationshipType = relationships[idx].relationshipType,
        otherEntityNameCapitalized = relationships[idx].otherEntityNameCapitalized,
        otherEntityFieldCapitalized = relationships[idx].otherEntityFieldCapitalized,
        relationshipNameCapitalized = relationships[idx].relationshipNameCapitalized,
        relationshipNameCapitalizedPlural = relationships[idx].relationshipNameCapitalizedPlural,
        ownerSide = relationships[idx].ownerSide;
        if (relationshipType === 'many-to-many' && ownerSide === true) { _%>

    public Set<<%= asDto(otherEntityNameCapitalized) %>> get<%= relationshipNameCapitalizedPlural %>() {
        return <%= relationshipFieldNamePlural %>;
    }

    public void set<%= relationshipNameCapitalizedPlural %>(Set<<%= asDto(otherEntityNameCapitalized) %>> <%= otherEntityNamePlural %>) {
        this.<%= relationshipFieldNamePlural %> = <%= otherEntityNamePlural %>;
    }
    <%_ } else if (relationshipType === 'many-to-one' || (relationshipType === 'one-to-one' && ownerSide === true)) { _%>

    <%_ if (relationshipNameCapitalized.length > 1) { _%>
    public <% if (relationshipFieldName === 'user' && authenticationType === 'oauth2') { %>String<% } else { %><%= pkType %><% } %> get<%= relationshipNameCapitalized %>Id() {
        return <%= relationshipFieldName %>Id;
    }

    public void set<%= relationshipNameCapitalized %>Id(<% if (relationshipFieldName === 'user' && authenticationType === 'oauth2') { %>String<% } else { %><%= pkType %><% } %> <%= otherEntityName %>Id) {
        this.<%= relationshipFieldName %>Id = <%= otherEntityName %>Id;
    }
    <%_ } else { // special case when the entity name has one character _%>
    public <%= pkType %> get<%= relationshipNameCapitalized.toLowerCase() %>Id() {
        return <%= relationshipFieldName %>Id;
    }

    public void set<%= relationshipNameCapitalized.toLowerCase() %>Id(<%= pkType %> <%= otherEntityName %>Id) {
        this.<%= relationshipFieldName %>Id = <%= otherEntityName %>Id;
    }
    <%_ } _%>
    <%_ if (otherEntityFieldCapitalized !== 'Id' && otherEntityFieldCapitalized !== '') { _%>

    public String get<%= relationshipNameCapitalized %><%= otherEntityFieldCapitalized %>() {
        return <%= relationshipFieldName %><%= otherEntityFieldCapitalized %>;
    }

    public void set<%= relationshipNameCapitalized %><%= otherEntityFieldCapitalized %>(String <%= otherEntityName %><%= otherEntityFieldCapitalized %>) {
        this.<%= relationshipFieldName %><%= otherEntityFieldCapitalized %> = <%= otherEntityName %><%= otherEntityFieldCapitalized %>;
    }
    <%_ } } } _%>

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        <%= asDto(entityClass) %> <%= asDto(entityInstance) %> = (<%= asDto(entityClass) %>) o;
        if (<%= asDto(entityInstance) %>.getId() == null || getId() == null) {
            return false;
        }
        return Objects.equals(getId(), <%= asDto(entityInstance) %>.getId());
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getId());
    }

    @Override
    public String toString() {
        return "<%= asDto(entityClass) %>{" +
            "id=" + getId() +
            <%_ for (idx in fields) {
                const fieldName = fields[idx].fieldName;
                const fieldType = fields[idx].fieldType;
                const isNumeric = ['integer', 'long', 'float', 'double', 'bigdecimal'].includes(fieldType.toLowerCase()); _%>
            ", <%= fieldName %>=<% if (! isNumeric) { %>'<% } %>" + <% if (fieldType.toLowerCase() === 'boolean') { %>is<% } else { %>get<% } %><%= fields[idx].fieldInJavaBeanMethod %>() <% if (! isNumeric) { %>+ "'" <% } %>+
            <%_ } _%>
            <%_ for (idx in relationships) {
                const relationshipFieldName = relationships[idx].relationshipFieldName;
                const relationshipType = relationships[idx].relationshipType;
                const relationshipNameCapitalized = relationships[idx].relationshipNameCapitalized;
                const otherEntityFieldCapitalized = relationships[idx].otherEntityFieldCapitalized;
                const ownerSide = relationships[idx].ownerSide; _%>
                <%_ if (relationshipType === 'many-to-one' || (relationshipType === 'one-to-one' && ownerSide === true)) { _%>
            ", <%= relationshipFieldName %>=<% if (relationshipFieldName === 'user' && authenticationType === 'oauth2') { %>'<% } %>" + get<%= relationshipNameCapitalized %>Id() <% if (relationshipFieldName === 'user' && authenticationType === 'oauth2') { %>+ "'" <% } %>+
                    <%_ if (otherEntityFieldCapitalized !== 'Id' && otherEntityFieldCapitalized !== '') { _%>
            ", <%= relationshipFieldName %>='" + get<%= relationshipNameCapitalized %><%= otherEntityFieldCapitalized %>() + "'" +
            <%_ } } } _%>
            "}";
    }
}
