<%#
 Copyright 2013-2025 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.
-%>
<%
const instanceType = restClass;
const instanceName = restInstance;
const returnType = reactive ? 'Mono' : 'Optional'
const mapOrFlatMap = reactive ? 'flatMap' : 'map'
const returnPrefix = (isService) ? 'return' : returnType + '<' + instanceType + '> result =';
const mapper = entityInstance  + 'Mapper';
_%>
<%_ if (viaService) { _%>
  <%_ if (reactive) { _%>
    Mono<<%= instanceType %>> result = <%= entityInstance %>Service.partialUpdate(<%= instanceName %>);
  <%_ } else { _%>
    Optional<<%= instanceType %>> result = <%= entityInstance %>Service.partialUpdate(<%= instanceName %>);
  <%_ } _%>
<%_ } else { %>
<%- returnPrefix %> <%= entityInstance %>Repository.findById(<%= instanceName %>.get<%= primaryKey.nameCapitalized %>())
  <%_ if (dtoMapstruct || fields.filter(field => !field.id && !field.transient).length > 0) { _%>
    .map(existing<%= entityClass %> -> {
  <%_ if (dtoMapstruct) { _%>
       <%= mapper %>.partialUpdate(existing<%= entityClass %>, <%= instanceName %>);
  <%_ } else { _%>
    <%_ for (const field of fields.filter(field => !field.id && !field.transient)) { _%>
    if (<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>() != null) {
        existing<%= entityClass %>.set<%= field.fieldInJavaBeanMethod %>(<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>());
    }
      <%_ if (field.fieldWithContentType) { _%>
    if (<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>ContentType() != null) {
        existing<%= entityClass %>.set<%= field.fieldInJavaBeanMethod %>ContentType(<%= instanceName %>.get<%= field.fieldInJavaBeanMethod %>ContentType());
    }
      <%_ } _%>
    <%_ } %>
  <% } %>
    return existing<%= entityClass %>;
    })
  <%_ } %>
  .<%= mapOrFlatMap %>(<% if (ormTool === 'hibernate') { %><%= entityInstance %>Repository::save<% } else { %>temp<%= entityClass %> -> {<%= entityInstance %>Repository.save(temp<%= entityClass %>);return <% if (dtoMapstruct && !searchEngineElasticsearch) { %><%= mapper %>.toDto(<%= entityInstance %>Repository.selectById(temp<%= entityClass %>.get<%= primaryKey.nameCapitalized%>()))<% } else { %><%= entityInstance %>Repository.selectById(temp<%= entityClass %>.get<%= primaryKey.nameCapitalized%>())<% } %>;}<% } %>)
  <%_ if (searchEngineElasticsearch) { _%>
    .<%= mapOrFlatMap %>(saved<%= entityClass %> -> {
    <%_ if(reactive) { %>
        <%= entityInstance %>SearchRepository.save(saved<%= entityClass %>);
        return Mono.just(saved<%= entityClass %>);
    <%_ } else { %>
        <%= entityInstance %>SearchRepository.index(saved<%= entityClass %>);
        return saved<%= entityClass %>;
    <%_ } %>
      })
  <%_ } _%>
  <%_ if (dtoMapstruct && (ormTool === 'hibernate' || searchEngineElasticsearch && ormTool === 'mybatis' )) { _%>
   .map(<%= mapper %>::toDto)
  <%_ } _%>;
<%_ } %>
