<%#
 Copyright 2013-2024 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

      https://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 %>.repository.rowmapper;


import java.time.Instant;
<%_ if (user.primaryKey.hasUUID) { _%>
import java.util.UUID;
<%_ } _%>
import java.util.function.BiFunction;

import org.springframework.stereotype.Service;

import <%= user.entityAbsoluteClass %>;

import io.r2dbc.spi.Row;

/**
 * Converter between {@link Row} to {@link <%= user.persistClass %>}, with proper type conversions.
 */
@Service
public class UserRowMapper implements BiFunction<a-row, String, <%= user.persistClass %>> {
    private final ColumnConverter converter;
    public UserRowMapper(ColumnConverter converter) {
        this.converter = converter;
    }

    /**
     * Take a {@link Row} and a column prefix, and extract all the fields.
     * @return the {@link <%= user.persistClass %>} stored in the database.
     */
    @Override
    public <%= user.persistClass %> apply(Row row, String prefix) {
        <%= user.persistClass %> entity = new <%= user.persistClass %>();
        entity.setId(row.get(prefix + "_id", <%= user.primaryKey.type %>.class));
        entity.setLogin(converter.fromRow(row, prefix + "_login", String.class));
<%_ if (!authenticationTypeOauth2) { _%>
        entity.setPassword(converter.fromRow(row, prefix + "_password", String.class));
<%_ } _%>
        entity.setFirstName(converter.fromRow(row, prefix + "_first_name", String.class));
        entity.setLastName(converter.fromRow(row, prefix + "_last_name", String.class));
        entity.setEmail(converter.fromRow(row, prefix + "_email", String.class));
        entity.setActivated(Boolean.TRUE.equals(converter.fromRow(row, prefix + "_activated", Boolean.class)));
        entity.setLangKey(converter.fromRow(row, prefix + "_lang_key", String.class));
        entity.setImageUrl(converter.fromRow(row, prefix + "_image_url", String.class));
<%_ if (!authenticationTypeOauth2) { _%>
        entity.setActivationKey(converter.fromRow(row, prefix + "_activation_key", String.class));
        entity.setResetKey(converter.fromRow(row, prefix + "_reset_key", String.class));
        entity.setResetDate(converter.fromRow(row, prefix + "_reset_date", Instant.class));
<%_ } _%>
        return entity;
    }

}
