package <%=packageName%>.service.mapper;

import <%=packageName%>.domain.*;
import <%=packageName%>.service.dto.AuthorityDTO;
import org.mapstruct.*;

<%_ if (ormTool !== 'mybatis') { _%>
import java.util.Set;
import java.util.LinkedHashSet;
<%_ } else { _%>
import java.util.List;
import java.util.ArrayList;
<%_ } _%>

<%_
const manyListName = ormTool !== 'mybatis' ? 'Set' : 'List';
const manyListType = ormTool !== 'mybatis' ? 'LinkedHashSet' : 'ArrayList';
_%>
/**
 * Mapper for the entity {@link Authority} and its DTO {@link AuthorityDTO}.
 */
@Mapper(componentModel = "spring")
public interface AuthorityMapper extends EntityMapper<AuthorityDTO, Authority> {

    @Mapping(source = "parent", target = "parent", qualifiedByName = "name")
    @Mapping(target = "viewPermissions", ignore = true)
    AuthorityDTO toDto(Authority authority);

    @Named("noChildren")
    @Mapping(source = "parent", target = "parent")
    @Mapping(target = "children", ignore = true)
    @Mapping(target = "viewPermissions", ignore = true)
    AuthorityDTO toNoChildrenDto(Authority authority);

    @Mapping(target = "children", ignore = true)
    @Mapping(target = "removeChildren", ignore = true)
    @Mapping(target = "viewPermissions", ignore = true)
    @Mapping(target = "removeViewPermissions", ignore = true)
    @Mapping(target = "apiPermissions", ignore = true)
    @Mapping(target = "removeApiPermissions", ignore = true)
    Authority toEntity(AuthorityDTO authorityDTO);

    default Authority fromId(<%= databaseTypeSql ? 'Long' : 'String'%> id) {
        if (id == null) {
            return null;
        }
        Authority authority = new Authority();
        authority.setId(id);
        return authority;
    }

    @Named("name")
    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "id", source = "id")
    @Mapping(target = "name", source = "name")
    @Mapping(target = "code", source = "code")
    default AuthorityDTO toDtoName(Authority authority) {
        if (authority == null) {
            return null;
        }
        AuthorityDTO authorityDTO = new AuthorityDTO();
        authorityDTO.setId(authority.getId());
        authorityDTO.setName(authority.getName());
        return authorityDTO;
    }

    @Named("name<%= manyListName %>")
    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "id", source = "id")
    @Mapping(target = "name", source = "name")
    @Mapping(target = "code", source = "code")
    default <%= manyListType %><AuthorityDTO> toDtoName<%= manyListName %>(<%= manyListName %><Authority> authorities) {
        if (authorities == null) {
            return null;
        }
        <%= manyListType %><AuthorityDTO> authority<%= manyListName %> = new <%= manyListType%><>();
        for (Authority authorityEntity : authorities) {
            authority<%= manyListName %>.add(this.toDtoName(authorityEntity));
        }
        return authority<%= manyListName %>;
    }
}
