APIServiceFacadeImpl.java

/*
 * Copyright 2023 Global Crop Diversity Trust
 *
 * 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 org.genesys.server.api.v2.facade.impl;

import org.genesys.blocks.model.EmptyModel;
import org.genesys.server.api.v2.MultiOp;
import org.genesys.server.api.v2.facade.APIServiceFacade;
import org.genesys.server.api.v2.mapper.MapstructMapper;
import org.genesys.server.service.CRUDService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
public abstract class APIServiceFacadeImpl<S extends CRUDService2<T>, DTO, T extends EmptyModel> implements APIServiceFacade<DTO, T> {
	
	@Autowired
	protected S service;
	
	@Autowired
	protected MapstructMapper mapper;

	@PersistenceContext
	private EntityManager entityManager;

	/** Use {@code mapper.map(source)} in implemented methods */
	protected abstract @NotNull @Valid T convert(DTO source);
	/** Use {@code mapper.map(source)} in implemented methods */
	protected abstract @NotNull @Valid DTO convert(T source);

	@Override
	public DTO create(DTO source) {
		var r = service.createFast(convert(source));
		entityManager.flush();
		entityManager.detach(r);
		return convert(service.get(r));
	}

	@Override
	public MultiOp<DTO> create(List<DTO> inserts) {
		var r = service.createFast(mapper.map(inserts, this::convert));
		entityManager.flush();
		r.success.forEach(entityManager::detach);
		return r.map(this::get);
	}

	@Override
	public DTO get(long id) {
		return convert(service.get(id));
	}

	@Override
	public DTO get(T source) {
		return convert(service.get(source));
	}

	@Override
	public List<DTO> get(List<T> source) {
		return mapper.map(service.get(source), this::convert);
	}

	@Override
	public Page<DTO> list(Pageable page) {
		return mapper.map(service.list(page), this::convert);
	}

	@Override
	public DTO update(DTO updated) {
		var update = convert(updated);
		var r = service.updateFast(update, service.get(update));
		entityManager.flush();
		entityManager.detach(r);
		return convert(service.get(r));
	}

	@Override
	public MultiOp<DTO> update(List<DTO> updates) {
		var r = service.updateFast(mapper.map(updates, this::convert));
		entityManager.flush();
		r.success.forEach(entityManager::detach);
		return r.map(this::get);
	}

	@Override
	public DTO remove(DTO entity) {
		return convert(service.remove(convert(entity)));
	}

	@Override
	public MultiOp<DTO> remove(List<DTO> deletes) {
		return mapper.map(service.remove(mapper.map(deletes, this::convert)), this::convert);
	}

	@Override
	public MultiOp<DTO> upsertMany(List<DTO> updates) {
		var r = MultiOp.upsert(mapper.map(updates, this::convert), service::createFast, service::get, service::updateFast);
		entityManager.flush();
		r.success.forEach(entityManager::detach);
		return r.map(this::get);
	}
}