MethodController.java
/*
* Copyright 2026 Global Crop Diversity Trust
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root folder or http://www.apache.org/licenses/LICENSE-2.0
*/
package org.gringlobal.api.v2.impl;
import java.io.IOException;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.ApiBaseController;
import org.gringlobal.api.FilteredPage;
import org.gringlobal.api.Pagination;
import org.gringlobal.api.model.MethodAttachDTO;
import org.gringlobal.api.model.MethodDTO;
import org.gringlobal.api.v2.CRUDController;
import org.gringlobal.api.v2.FilteredCRUDController;
import org.gringlobal.api.v2.facade.MethodApiService;
import org.gringlobal.api.v2.facade.MethodAttachmentApiService;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Method;
import org.gringlobal.model.MethodAttach;
import org.gringlobal.model.QMethod;
import org.gringlobal.service.filter.MethodFilter;
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.DeleteMapping;
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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.querydsl.core.types.OrderSpecifier;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
@RestController("methodApi2")
@RequestMapping(MethodController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Method")
public class MethodController extends FilteredCRUDController<MethodDTO, Method, MethodApiService, MethodFilter> {
/** The Constant API_URL. */
public static final String API_URL = ApiBaseController.APIv2_BASE + "/method";
@Autowired
private MethodAttachmentApiService methodAttachmentApiService;
@Override
protected OrderSpecifier<?>[] defaultSort() {
return new OrderSpecifier[] { QMethod.method.id.asc() };
}
@Override
@Operation(operationId = "createMethod", description = "Create a Method", summary = "Add")
public MethodDTO create(@RequestBody MethodDTO entity) {
return super.create(entity);
}
@Override
@Operation(operationId = "updateMethod", description = "Update a Method", summary = "Update")
public MethodDTO update(@RequestBody MethodDTO entity) {
return super.update(entity);
}
@Override
@Operation(operationId = "listMethods", description = "List Methods", summary = "List")
public FilteredPage<MethodDTO, MethodFilter> list(@ParameterObject final Pagination page, @RequestBody(required = false) final MethodFilter filter) throws SearchException, IOException {
return super.list(page, filter);
}
@Override
@Operation(operationId = "filterMethods", description = "Filter Methods", summary = "Filter")
public FilteredPage<MethodDTO, MethodFilter> filter(@RequestParam(name = "f", required = false) final String filterCode, @ParameterObject final Pagination page,
@RequestBody(required = false) final MethodFilter filter) throws IOException, SearchException {
return super.filter(filterCode, page, filter);
}
@PostMapping(value = "/attach/{methodId}/list", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "listAttachments", description = "List method attachmetns", summary = "List attachments")
public Page<MethodAttachDTO> listAttachments(@PathVariable(name = "methodId") final Long methodId, @ParameterObject final Pagination page) {
return methodAttachmentApiService.list(methodId, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
}
@PostMapping(value = "/attach/{methodId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "uploadFile", description = "Attach method file", summary = "Attach file")
public MethodAttachDTO uploadFile(@PathVariable(name = "methodId") final Long methodId, @RequestPart(name = "file") final MultipartFile file,
@RequestPart(name = "metadata") final MethodAttachmentApiService.MethodAttachmentRequestDTO metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {
return methodAttachmentApiService.uploadFile(methodId, file, metadata);
}
@DeleteMapping(value = "/attach/{methodId}/{attachmentId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "removeFile", description = "Remove attached file", summary = "Remove file")
public MethodAttachDTO removeFile(@PathVariable(name = "methodId") final Long methodId, @PathVariable(name = "attachmentId") final Long attachmentId) {
return methodAttachmentApiService.removeFile(methodId, attachmentId);
}
@RestController("methodAttachApi2")
@RequestMapping(MethodAttachController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Method")
public static class MethodAttachController extends CRUDController<MethodAttachDTO, MethodAttach, MethodAttachmentApiService> {
/** The Constant API_URL. */
public static final String API_URL = MethodController.API_URL + "/attach/meta";
@Override
@Operation(operationId = "createMethodAttach", description = "Create MethodAttach", summary = "Create")
public MethodAttachDTO create(@RequestBody MethodAttachDTO entity) {
// Throws UnsupportedOperationException
return super.create(entity);
}
@Override
@Operation(operationId = "updateMethodAttach", description = "Update an existing record", summary = "Update")
public MethodAttachDTO update(@RequestBody MethodAttachDTO entity) {
return super.update(entity);
}
@Override
@Operation(operationId = "getMethodAttach", description = "Get record by ID", summary = "Get")
public MethodAttachDTO get(@PathVariable long id) {
return super.get(id);
}
@Override
@Operation(operationId = "deleteMethodAttach", description = "Delete existing record by ID", summary = "Delete")
public MethodAttachDTO remove(@PathVariable long id) {
return super.remove(id);
}
}
}