<%#
 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 <%= entityAbsolutePackage %>.repository.base.rowmapper;<%# modify by begcode %>

<%_ if (anyFieldIsBigDecimal) { _%>
import java.math.BigDecimal;
<%_} if (anyFieldIsInstant) { _%>
import java.time.Instant;
<%_ } if (anyFieldIsLocalDate) { _%>
import java.time.LocalDate;
<%_ } if (anyFieldIsZonedDateTime) { _%>
import java.time.ZonedDateTime;
<%_ } if (anyFieldIsDuration) { _%>
import java.time.Duration;
<%_ } if (anyFieldIsUUID || otherEntityPrimaryKeyTypesIncludesUUID) { _%>
import java.util.UUID;
<%_ } _%>
import java.util.function.BiFunction;

import org.springframework.stereotype.Service;

import <%= entityAbsolutePackage %>.domain.<%= persistClass %>;
<%_ Object.keys(uniqueEnums).forEach(function(element) { _%>
import <%= entityAbsolutePackage %>.domain.enumeration.<%= element %>;
<%_ }); _%>
<%_ if (packageName !== entityAbsolutePackage) { _%>
import <%= packageName %>.repository.rowmapper.ColumnConverter;
<%_ } _%>

import io.r2dbc.spi.Row;

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

    /**
     * Take a {@link Row} and a column prefix, and extract all the fields.
     * @return the {@link <%= persistClass %>} stored in the database.
     */
    @Override
    public <%= persistClass %> apply(Row row, String prefix) {
        <%= persistClass %> entity = new <%= persistClass %>();
<%_ fields.filter(field => !field.transient).forEach(function(field) {
  let fieldType = field.fieldType;
  if (field.blobContentTypeText) {
    fieldType = 'String';
  }
  if (field.fieldTypeBytes && !field.blobContentTypeText) { _%>
        entity.set<%= field.fieldInJavaBeanMethod %>ContentType(converter.fromRow(row, prefix + "_<%= field.fieldNameAsDatabaseColumn %>_content_type", String.class));
  <%_ } _%>
        entity.set<%= field.fieldInJavaBeanMethod %>(converter.fromRow(row, prefix + "_<%= field.fieldNameAsDatabaseColumn %>", <%= fieldType %>.class));
<%_ }); _%>
<%_ reactiveRegularEagerRelations.forEach(function(rel) { _%>
        entity.set<%= rel.relationshipNameCapitalized %>Id(converter.fromRow(row, prefix + "_<%= rel.joinColumnNames[0] %>", <%= rel.otherEntity.primaryKey.type %>.class));
<%_ }); _%>
        return entity;
    }

}
