GenesysImageManagerController.java

/*
 * Copyright 2025 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.integration;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.gringlobal.api.ApiBaseController;
import org.gringlobal.api.Pagination;
import org.gringlobal.api.model.integration.GenesysAttachmentDTO;
import org.gringlobal.api.v2.facade.GenesysImageManagerApiService;
import org.gringlobal.model.integration.GenesysAttachment;
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.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.RestController;

import java.util.Set;

@RestController("genesysImageApi2")
@RequestMapping(GenesysImageManagerController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Genesys")
@Slf4j
public class GenesysImageManagerController extends ApiBaseController {

	/** The Constant API_URL. */
	public static final String API_URL = GenesysController.API_URL + "/image/sync";

	@Autowired
	private GenesysImageManagerApiService service;

	@PostMapping(value = "/start", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "startSync", description = "Start genesys images synchronization process", summary = "Start synchronization")
	public GenesysImageManagerApiService.SyncProcessStatus start(@RequestBody Set<String> instCodes) {
		return service.startSyncProcess(instCodes);
	}

	@GetMapping(value = "/status", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "getStatus", description = "Get genesys images synchronization process status", summary = "Get synchronization status")
	public GenesysImageManagerApiService.SyncProcessStatus getStatus() {
		return service.getSyncProcessStatus();
	}

	@PostMapping(value = "/pause", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "pauseSync", description = "Stop synchronization if running, but keep state", summary = "Pause synchronization")
	public void pauseSyc() {
		service.pauseSyncProcess();
	}

	@PostMapping(value = "/cancel", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "cancelSync", description = "Cancel and reset synchronization", summary = "Cancel and reset synchronization")
	public void cancelSync() {
		service.cancelSyncProcess();
	}

	@PostMapping(value = "/upload/list", produces = MediaType.APPLICATION_JSON_VALUE)
	public Page<GenesysAttachmentDTO> getUploadList(@ParameterObject final Pagination page) {
		return service.getUploadList(page.toPageRequest(100, 20));
	}

	@PostMapping(value = "/download/list", produces = MediaType.APPLICATION_JSON_VALUE)
	public Page<GenesysAttachmentDTO> getDownloadList(@ParameterObject final Pagination page) {
		return service.getDownloadList(page.toPageRequest(100, 20));
	}

	@PostMapping(value = "/uploaded/list", produces = MediaType.APPLICATION_JSON_VALUE)
	public Page<GenesysAttachmentDTO> getUploadedList(@ParameterObject final Pagination page) {
		return service.getUploadedList(page.toPageRequest(100, 20));
	}

	@PostMapping(value = "/upload/start", produces = MediaType.APPLICATION_JSON_VALUE)
	public void syncUploadList() throws Exception {
		service.syncUploadList();
	}

	@PostMapping(value = "/download/start", produces = MediaType.APPLICATION_JSON_VALUE)
	public void syncDownloadList() throws Exception {
		service.syncDownloadList();
	}

	@GetMapping(value = "/{listName}/status", produces = MediaType.APPLICATION_JSON_VALUE)
	public GenesysImageManagerApiService.SyncListProcessStatus getSyncListStatus(@PathVariable("listName") GenesysAttachment.ImageSyncList list) {
		return service.getSyncListStatus(list);
	}

}