CooperatorController.java

/*
 * Copyright 2020 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.impl;

import com.querydsl.core.types.OrderSpecifier;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.genesys.blocks.auditlog.model.filters.AuditLogFilter;
import org.genesys.blocks.model.filters.NumberFilter;
import org.gringlobal.api.ApiBaseController;
import org.gringlobal.api.FilteredPage;
import org.gringlobal.api.Pagination;
import org.gringlobal.api.model.AuditLogDTO;
import org.gringlobal.api.model.CooperatorDTO;
import org.gringlobal.api.v2.FilteredCRUDController;
import org.gringlobal.api.v2.facade.AuditTrailApiService;
import org.gringlobal.api.v2.facade.CooperatorApiService;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.custom.json.IgnoreEntityRefDeserializer;
import org.gringlobal.model.Cooperator;
import org.gringlobal.model.QCooperator;
import org.gringlobal.service.filter.CooperatorFilter;
import org.gringlobal.worker.dupe.DuplicateFinder;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Set;

@RestController("cooperatorApi2")
@RequestMapping(CooperatorController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Cooperator")
@Validated
public class CooperatorController extends FilteredCRUDController<CooperatorDTO, Cooperator, CooperatorApiService, CooperatorFilter> {

	/** The Constant API_URL. */
	public static final String API_URL = ApiBaseController.APIv2_BASE + "/cooperator";

	@Autowired
	private AuditTrailApiService auditTrailApiService;

	@Override
	protected OrderSpecifier<?>[] defaultSort() {
		return new OrderSpecifier[] { QCooperator.cooperator.id.asc() };
	}

	/**
	 * Retrieve a list of audit logs for the specified cooperator
	 *
	 * @param cooperatorId the cooperatorId
	 * @param page the page request
	 * @return the list of all log entries
	 */
	@GetMapping("/auditlog/{id}")
	public Page<AuditLogDTO> cooperatorAuditLogs(@PathVariable(value = "id") final Long cooperatorId, @ParameterObject final Pagination page) {
		var filter = new AuditLogFilter();
		filter
			.entityId(new NumberFilter<Long>().eq(Set.of(serviceFacade.get(cooperatorId).getId())))
			.classname(Cooperator.class.getName());

		return auditTrailApiService.listAuditLogs(filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	/**
	 * Searching for similar cooperators
	 *
	 * @param id the target cooperator ID
	 * @return found similar cooperators
	 */
	@GetMapping(value = "/similar/{id:\\d+}", produces = MediaType.APPLICATION_JSON_VALUE)
	public List<DuplicateFinder.Hit<CooperatorDTO>> getSimilarCooperatorForID(@PathVariable("id") final long id) {
		return serviceFacade.getSimilarCooperatorForID(id);
	}

	/**
	 * Searching for similar cooperators based on sample Cooperator data
	 *
	 * @param source the source to compare
	 * @return found similar cooperators
	 */
	@PostMapping(value = "/similar", produces = MediaType.APPLICATION_JSON_VALUE)
	public List<DuplicateFinder.Hit<CooperatorDTO>> getSimilarCooperatorForUnsaved(@RequestBody(required = true) @IgnoreEntityRefDeserializer final CooperatorDTO source) {
		return serviceFacade.getSimilarCooperatorForUnsaved(source);
	}

	/**
	 * Create a new Cooperator and update the original one
	 *
	 * @param updated the updated cooperator
	 * @return new record
	 */
	@PostMapping(value = "/save-as-new", produces = MediaType.APPLICATION_JSON_VALUE)
	public CooperatorDTO saveAsNew(@RequestBody(required = true) @Valid final CooperatorDTO updated) {
		return serviceFacade.saveAsNew(updated);
	}

	@Override
	public CooperatorDTO create(@RequestBody CooperatorDTO entity) {
		return super.create(entity);
	}

	@Override
	public CooperatorDTO update(@RequestBody CooperatorDTO entity) {
		return super.update(entity);
	}

	@Override
	public FilteredPage<CooperatorDTO, CooperatorFilter> list(@ParameterObject final Pagination page, @RequestBody(required = false) CooperatorFilter filter) throws SearchException, IOException {
		return super.list(page, filter);
	}

	@Override
	public FilteredPage<CooperatorDTO, CooperatorFilter> filter(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
			@RequestBody(required = false) CooperatorFilter filter) throws IOException, SearchException {
		return super.filter(filterCode, page, filter);
	}

}