AccessionRelatedController.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 com.querydsl.core.types.OrderSpecifier;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.CRUDController;
import org.gringlobal.api.v1.FilteredCRUDController;
import org.gringlobal.model.AccessionInvName;
import org.gringlobal.model.AccessionIpr;
import org.gringlobal.model.AccessionPedigree;
import org.gringlobal.model.AccessionQuarantine;
import org.gringlobal.model.AccessionSource;
import org.gringlobal.model.AccessionSourceMap;
import org.gringlobal.model.Citation;
import org.gringlobal.model.QCitation;
import org.gringlobal.service.AccessionInvNameService;
import org.gringlobal.service.AccessionIprService;
import org.gringlobal.service.AccessionPedigreeService;
import org.gringlobal.service.AccessionQuarantineService;
import org.gringlobal.service.AccessionService;
import org.gringlobal.service.AccessionSourceMapService;
import org.gringlobal.service.AccessionSourceService;
import org.gringlobal.service.CitationService;
import org.gringlobal.service.InventoryService;
import org.gringlobal.service.filter.AccessionInvNameFilter;
import org.gringlobal.service.filter.CitationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController("accessionRelatedApi1")
@RequestMapping(AccessionRelatedController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Accession")
public class AccessionRelatedController extends ApiBaseController {

	/** The Constant API_URL. */
	public static final String API_URL = AccessionController.API_URL;

	@RestController("accessionInvNameApi1")
	@RequestMapping(AccessionInvNameController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Accession")
	public static class AccessionInvNameController extends FilteredCRUDController<AccessionInvName, AccessionInvNameService, AccessionInvNameFilter> {
		/** The Constant API_URL. */
		public static final String API_URL = AccessionRelatedController.API_URL + "/name";

		@Autowired
		private InventoryService inventoryService;

		@Override
		@Operation(operationId = "createAccessionInvName", description = "Create AccessionInvName", summary = "Create")
		public AccessionInvName create(@RequestBody AccessionInvName entity) {
			if (entity.getInventory() != null) {
				entity.setInventory(inventoryService.get(entity.getInventory().getId()));
			}
			return super.create(entity);
		}

		@Override
		@Operation(operationId = "updateAccessionInvName", description = "Update an existing record", summary = "Update")
		public AccessionInvName update(@RequestBody AccessionInvName entity) {
			if (entity.getInventory() != null) {
				entity.setInventory(inventoryService.get(entity.getInventory().getId()));
			}
			return super.update(entity);
		}

		@Override
		@Operation(operationId = "getAccessionInvName", description = "Get record by ID", summary = "Get")
		public AccessionInvName get(@PathVariable long id) {
			return super.get(id);
		}

		@Override
		@Operation(operationId = "deleteAccessionInvName", description = "Delete existing record by ID", summary = "Delete")
		public AccessionInvName remove(@PathVariable long id) {
			return super.remove(id);
		}
	}

	@RestController("accessionIprApi1")
	@RequestMapping(AccessionIprController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Accession")
	@Validated
	public static class AccessionIprController extends CRUDController<AccessionIpr, AccessionIprService> {
		/** The Constant API_URL. */
		public static final String API_URL = AccessionRelatedController.API_URL + "/ipr";

		@Autowired
		private AccessionService accessionService;

		@Override
		@Operation(operationId = "createAccessionIpr", description = "Create AccessionIpr", summary = "Create")
		public AccessionIpr create(@RequestBody AccessionIpr entity) {
			if (entity.getAccession() != null && entity.getAccession().getId() != null) {
				entity.setAccession(accessionService.get(entity.getAccession().getId()));
			}
			return super.create(entity);
		}

		@Override
		@Operation(operationId = "updateAccessionIpr", description = "Update an existing record", summary = "Update")
		public AccessionIpr update(@RequestBody AccessionIpr entity) {
			if (entity.getAccession() != null) {
				entity.setAccession(accessionService.get(entity.getAccession().getId()));
			}
			return super.update(entity);
		}

		@Override
		@Operation(operationId = "getAccessionIpr", description = "Get record by ID", summary = "Get")
		public AccessionIpr get(@PathVariable long id) {
			return super.get(id);
		}

		@Override
		@Operation(operationId = "deleteAccessionIpr", description = "Delete existing record by ID", summary = "Delete")
		public AccessionIpr remove(@PathVariable long id) {
			return super.remove(id);
		}
	}

	@RestController("accessionPedigreeApi1")
	@RequestMapping(AccessionPedigreeController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Accession")
	public static class AccessionPedigreeController extends CRUDController<AccessionPedigree, AccessionPedigreeService> {
		/** The Constant API_URL. */
		public static final String API_URL = AccessionRelatedController.API_URL + "/pedigree";

		@Autowired
		private AccessionService accessionService;

		@Override
		@Operation(operationId = "createAccessionPedigree", description = "Create AccessionPedigree", summary = "Create")
		public AccessionPedigree create(@RequestBody AccessionPedigree entity) {
			if (entity.getAccession() != null) {
				entity.setAccession(accessionService.get(entity.getAccession().getId()));
			}
			return super.create(entity);
		}

		@Override
		@Operation(operationId = "updateAccessionPedigree", description = "Update an existing record", summary = "Update")
		public AccessionPedigree update(@RequestBody AccessionPedigree entity) {
			if (entity.getAccession() != null) {
				entity.setAccession(accessionService.get(entity.getAccession().getId()));
			}
			return super.update(entity);
		}

		@Override
		@Operation(operationId = "getAccessionPedigree", description = "Get record by ID", summary = "Get")
		public AccessionPedigree get(@PathVariable long id) {
			return super.get(id);
		}

		@Override
		@Operation(operationId = "deleteAccessionPedigree", description = "Delete existing record by ID", summary = "Delete")
		public AccessionPedigree remove(@PathVariable long id) {
			return super.remove(id);
		}
	}

	@RestController("accessionSourceApi1")
	@RequestMapping(AccessionSourceController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Accession")
	public static class AccessionSourceController extends CRUDController<AccessionSource, AccessionSourceService> {
		/** The Constant API_URL. */
		public static final String API_URL = AccessionRelatedController.API_URL + "/source";

		@Autowired
		private AccessionService accessionService;

		@Override
		@Operation(operationId = "createAccessionSource", description = "Create AccessionSource", summary = "Create")
		public AccessionSource create(@RequestBody AccessionSource entity) {
			if (entity.getAccession() != null) {
				entity.setAccession(accessionService.get(entity.getAccession().getId()));
			}
			return super.create(entity);
		}

		@Override
		@Operation(operationId = "updateAccessionSource", description = "Update an existing record", summary = "Update")
		public AccessionSource update(@RequestBody AccessionSource entity) {
			if (entity.getAccession() != null) {
				entity.setAccession(accessionService.get(entity.getAccession().getId()));
			}
			return super.update(entity);
		}

		@Override
		@Operation(operationId = "getAccessionSource", description = "Get record by ID", summary = "Get")
		public AccessionSource get(@PathVariable long id) {
			return super.get(id);
		}

		@Override
		@Operation(operationId = "deleteAccessionSource", description = "Delete existing record by ID", summary = "Delete")
		public AccessionSource remove(@PathVariable long id) {
			return super.remove(id);
		}
	}

	@RestController("accessionSourceMapApi1")
	@RequestMapping(AccessionSourceMapController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Accession")
	public static class AccessionSourceMapController extends CRUDController<AccessionSourceMap, AccessionSourceMapService> {
		/** The Constant API_URL. */
		public static final String API_URL = AccessionSourceController.API_URL + "/cooperator-map";
	}

	@RestController("accessionQuarantineApi1")
	@RequestMapping(AccessionQuarantineController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Accession")
	public static class AccessionQuarantineController extends CRUDController<AccessionQuarantine, AccessionQuarantineService> {
		/** The Constant API_URL. */
		public static final String API_URL = AccessionRelatedController.API_URL + "/quarantine";

		@Autowired
		private AccessionService accessionService;

		@Override
		@Operation(operationId = "createAccessionQuarantine", description = "Create AccessionQuarantine", summary = "Create")
		public AccessionQuarantine create(@RequestBody AccessionQuarantine entity) {
			if (entity.getAccession() != null) {
				entity.setAccession(accessionService.get(entity.getAccession().getId()));
			}
			return super.create(entity);
		}

		@Override
		@Operation(operationId = "updateAccessionQuarantine", description = "Update an existing record", summary = "Update")
		public AccessionQuarantine update(@RequestBody AccessionQuarantine entity) {
			if (entity.getAccession() != null) {
				entity.setAccession(accessionService.get(entity.getAccession().getId()));
			}
			return super.update(entity);
		}

		@Override
		@Operation(operationId = "getAccessionQuarantine", description = "Get record by ID", summary = "Get")
		public AccessionQuarantine get(@PathVariable long id) {
			return super.get(id);
		}

		@Override
		@Operation(operationId = "deleteAccessionQuarantine", description = "Delete existing record by ID", summary = "Delete")
		public AccessionQuarantine remove(@PathVariable long id) {
			return super.remove(id);
		}
	}

	@RestController("citationApi1")
	@RequestMapping(CitationController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Accession")
	public static class CitationController extends FilteredCRUDController<Citation, CitationService, CitationFilter> {
		/** The Constant API_URL. */
		public static final String API_URL = AccessionRelatedController.API_URL + "/citation";

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

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

	}
}