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.gringlobal.api.v2.facade.impl;
import java.time.Instant;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.genesys.blocks.model.EmptyModel;
import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.MultiOp;
import org.gringlobal.api.v2.facade.APIServiceFacade;
import org.gringlobal.api.v2.mapper.MapstructMapper;
import org.gringlobal.model.DateVersionEntityId;
import org.gringlobal.service.CRUDService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
public abstract class APIServiceFacadeImpl<S extends CRUDService2<T>, DTO, T extends EmptyModel> implements APIServiceFacade<DTO, T> {
@Autowired
protected S service;
@Autowired
protected MapstructMapper mapper;
/** 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 String getServiceEntityName() {
return service.getServiceEntityName();
}
@Override
public T create(DTO source) {
return service.createFast(convert(source));
}
@Override
public MultiOp<T> create(List<DTO> inserts) {
return service.create(mapper.map(inserts, this::convert));
}
@Override
public DTO get(long id) {
return convert(service.get(id));
}
@Override
public DTO get(T source) {
return convert(service.get(source));
}
@Override
public DTO get(DateVersionEntityId.EntityIdAndModifiedDate id) {
return convert(service.get(id));
}
@Override
public DTO get(long id, Instant modifiedDate) {
return convert(service.get(id, modifiedDate));
}
@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 T update(DTO updated) {
return service.updateFast(convert(updated), service.get(convert(updated)));
}
@Override
public MultiOp<T> update(List<DTO> updates) {
return service.updateFast(mapper.map(updates, this::convert));
}
@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<T> upsertMany(List<DTO> updates) {
return MultiOp.upsert(mapper.map(updates, this::convert), service::createFast, service::get, service::updateFast);
}
@Override
public final Page<DTO> map(Page<T> source) {
return new PageImpl<DTO>(get(source.getContent()), source.getPageable(), source.getTotalElements());
}
@Override
public final <F extends EmptyModelFilter<F, T>> FilteredPage<DTO, F> map(FilteredPage<T, F> source) {
return new FilteredPage<DTO, F>(source.filterCode, source.filter, map(source.page));
}
@Override
public ResponseEntity<StreamingResponseBody> generateReport(String reportTemplate, Set<Long> entityIds, Locale locale,
HttpServletResponse response) throws Exception {
return service.generateReport(reportTemplate, entityIds, locale, response);
}
}