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.v1.impl;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.model.filters.AuditLogFilter;
import org.genesys.blocks.auditlog.service.AuditTrailService;
import org.genesys.blocks.model.filters.NumberFilter;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.FilteredCRUDController;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.Pagination;
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.CooperatorService;
import org.gringlobal.service.filter.CooperatorFilter;
import org.gringlobal.worker.dupe.CooperatorDuplicateFinder;
import org.gringlobal.worker.dupe.DuplicateFinder;
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.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 com.querydsl.core.types.OrderSpecifier;

import org.springdoc.api.annotations.ParameterObject;
import io.swagger.v3.oas.annotations.tags.Tag;

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

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

	@Autowired
	private AuditTrailService auditService;

	@Autowired(required = false)
	private CooperatorDuplicateFinder duplicateFinder;

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

	@Override
	protected Class<CooperatorFilter> filterType() {
		return CooperatorFilter.class;
	}

	/**
	 * 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<AuditLog> cooperatorAuditLogs(@PathVariable(value = "id") final Long cooperatorId, @ParameterObject final Pagination page) {
		var filter = new AuditLogFilter();
		filter
			.entityId(new NumberFilter<Long>().eq(Set.of(crudService.get(cooperatorId).getId())))
			.classname(Cooperator.class.getName());

		return auditService.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<Cooperator>> getSimilarCooperatorForID(@PathVariable("id") final long id) {
		Cooperator cooperator = crudService.get(id);
		return duplicateFinder.findSimilar(cooperator);
	}

	/**
	 * 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<Cooperator>> getSimilarCooperatorForUnsaved(@RequestBody(required = true) @IgnoreEntityRefDeserializer final Cooperator source) {
		return duplicateFinder.findSimilar(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 Cooperator saveAsNew(@RequestBody(required = true) final Cooperator updated) {
		return crudService.saveAsNew(updated);
	}

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

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

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

	@Override
	public FilteredPage<Cooperator, 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);
	}

}