<%#
 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.mapper;

import <%=packageName%>.domain.*;
import <%=packageName%>.service.dto.<%= asDto(entityClass) %>;

import org.mapstruct.*;

/**
 * Mapper for the entity {@link <%= asEntity(entityClass) %>} and its DTO {@link <%= asDto(entityClass) %>}.
 */
@Mapper(componentModel = "spring", uses = {<% var existingMappings = [];
  for (idx in relationships) {
    if ((relationships[idx].relationshipType === 'many-to-many' && relationships[idx].ownerSide === true)|| relationships[idx].relationshipType === 'many-to-one' ||(relationships[idx].relationshipType === 'one-to-one' && relationships[idx].ownerSide === true)){
      // if the entity is mapped twice, we should implement the mapping once
      if (!existingMappings.includes(relationships[idx].otherEntityNameCapitalized) && asEntity(relationships[idx].otherEntityNameCapitalized) !== asEntity(entityClass)) {
          existingMappings.push(relationships[idx].otherEntityNameCapitalized);
      } } } %><%= existingMappings.map(otherEntityNameCapitalized => otherEntityNameCapitalized + 'Mapper.class').join(', ') %>})
public interface <%= entityClass %>Mapper extends EntityMapper<<%= asDto(entityClass) %>, <%= asEntity(entityClass) %>> {

<%_
// entity -> DTO mapping
var renMapAnotEnt = false; //Render Mapping Annotation during Entity to DTO conversion?
for (idx in relationships) {
    const relationshipType = relationships[idx].relationshipType;
    const relationshipName = relationships[idx].relationshipName;
    const ownerSide = relationships[idx].ownerSide;
    if (relationshipType === 'many-to-one' || (relationshipType === 'one-to-one' && ownerSide === true)) {
        renMapAnotEnt = true;
_%>
    @Mapping(source = "<%= relationshipName %>.id", target = "<%= relationships[idx].relationshipFieldName %>Id")
            <%_ if (relationships[idx].otherEntityFieldCapitalized !='Id' && relationships[idx].otherEntityFieldCapitalized !== '') { _%>
    @Mapping(source = "<%= relationshipName %>.<%= relationships[idx].otherEntityField %>", target = "<%= relationships[idx].relationshipFieldName %><%= relationships[idx].otherEntityFieldCapitalized %>")
            <%_ } _%>
        <%_ } _%>
    <%_ } _%>
    <%_ if (renMapAnotEnt === true) { _%>
    <%= asDto(entityClass) %> toDto(<%= asEntity(entityClass) %> <%= asEntity(entityInstance) %>);
    <%_ } _%>

<%_
// DTO -> entity mapping
var renMapAnotDto = false;  //Render Mapping Annotation during DTO to Entity conversion?
// var hasOAuthUser = false; // if OAuthUser, use a String id in fromId() method
for (idx in relationships) {
    const relationshipType = relationships[idx].relationshipType;
    const relationshipName = relationships[idx].relationshipName;
    // hasOAuthUser = (relationshipName === 'user' && authenticationType === 'oauth2');
    const relationshipNamePlural = relationships[idx].relationshipNamePlural;
    const ownerSide = relationships[idx].ownerSide;
    if (relationshipType === 'many-to-one' || (relationshipType === 'one-to-one' && ownerSide === true)) {
        renMapAnotDto = true;
_%>
    @Mapping(source = "<%= relationshipName %>Id", target = "<%= relationshipName %>")
            <%_ } else if (relationshipType === 'many-to-many' && ownerSide === false) { renMapAnotDto = true; _%>
    @Mapping(target = "<%= relationshipNamePlural %>", ignore = true)
            <%_ } else if (relationshipType === 'one-to-many') { renMapAnotDto = true; _%>
    @Mapping(target = "<%= relationshipNamePlural %>", ignore = true)
            <%_ } else if (relationshipType === 'one-to-one' && ownerSide === false) { renMapAnotDto = true; _%>
    @Mapping(target = "<%= relationshipName %>", ignore = true)
            <%_ } _%>
        <%_ } _%>
    <%_ if (renMapAnotDto === true) { _%>
    <%= asEntity(entityClass) %> toEntity(<%= asDto(entityClass)%> <%= asDto(entityInstance) %>);
    <%_ } _%>
    <%_ if (databaseType === 'sql' || databaseType === 'mongodb') { _%>

    default <%= asEntity(entityClass) %> fromId(<%= pkType %> id) {
        if (id == null) {
            return null;
        }
        <%= asEntity(entityClass) %> <%= asEntity(entityInstance) %> = new <%= asEntity(entityClass) %>();
        <%= asEntity(entityInstance) %>.setId(id);
        return <%= asEntity(entityInstance) %>;
    }
<%_ } _%>
}
