SubsetCreatorController.java

/*
 * Copyright 2018 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.genesys.server.api.v2.impl;

import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import org.genesys.blocks.model.JsonViews;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.v2.facade.SubsetApiService;
import org.genesys.server.api.v2.model.impl.SubsetCreatorDTO;
import org.genesys.server.exception.NotFoundElement;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

@RestController("catalogSubsetCreatorApi2")
@PreAuthorize("isAuthenticated()")
@RequestMapping(SubsetCreatorController.CONTROLLER_URL)
@Api(tags = { "subset" })
public class SubsetCreatorController extends ApiBaseController {

	/** The Constant CONTROLLER_URL. */
	public static final String CONTROLLER_URL = SubsetController.CONTROLLER_URL + "/{uuid}/subsetcreator";

	/** The subset service. */
	@Autowired
	protected SubsetApiService subsetService;

	/**
	 * List subset creators.
	 *
	 * @param uuid the uuid
	 * @return the list of creators
	 */
	@GetMapping(value = "/list")
	public List<SubsetCreatorDTO> listSubsetCreators(@PathVariable("uuid") final UUID uuid) {
		return subsetService.listSubsetCreators(uuid);
	}

	/**
	 * Load by uuid.
	 *
	 * @param uuid the uuid
	 * @return the subset creator
	 * @throws NotFoundElement the not found element
	 */
	@GetMapping(value = "/{creatorUuid}")
	public SubsetCreatorDTO loadByUuid(@PathVariable("creatorUuid") final UUID creatorUuid, @PathVariable UUID uuid) throws NotFoundElement {
		return subsetService.loadSubsetCreator(creatorUuid);
	}

	/**
	 * Creates the subset creator.
	 *
	 * @param uuid the uuid
	 * @param subsetCreator the subset creator
	 * @return the subset creator
	 * @throws NotFoundElement the not found element
	 */
	@PostMapping(value = "/create")
	public SubsetCreatorDTO createSubsetCreator(@PathVariable("uuid") final UUID uuid, @RequestBody final SubsetCreatorDTO subsetCreator) throws NotFoundElement {
		return subsetService.createSubsetCreator(uuid, subsetCreator);
	}

	/**
	 * Delete subset creator.
	 *
	 * @param subsetUuid the subset uuid
	 * @param subsetCreator the subset creator
	 * @return the subset creator
	 * @throws NotFoundElement the not found element
	 */
	// uses request body
	@RequestMapping(value = "/delete", method = { RequestMethod.POST, RequestMethod.DELETE })
	public SubsetCreatorDTO deleteSubsetCreator(@PathVariable("uuid") final UUID subsetUuid, @RequestBody final SubsetCreatorDTO subsetCreator) throws NotFoundElement {
		return subsetService.removeSubsetCreator(subsetUuid, subsetCreator);
	}

	/**
	 * Update subset creator.
	 *
	 * @param subsetUuid the subset uuid
	 * @param subsetCreator the subset creator
	 * @return the subset creator
	 * @throws NotFoundElement the not found element
	 */
	@PostMapping(value = "/update")
	public SubsetCreatorDTO updateSubsetCreator(@PathVariable("uuid") final UUID subsetUuid, @RequestBody final SubsetCreatorDTO subsetCreator) throws NotFoundElement {
		return subsetService.updateSubsetCreator(subsetUuid, subsetCreator);
	}

	/**
	 * Autocomplete.
	 *
	 * @param text the text
	 * @return the list
	 */
	@JsonView(JsonViews.Public.class)
	@GetMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
	public List<SubsetCreatorDTO> autocomplete(@RequestParam("c") final String text, @PathVariable UUID uuid) {
		if (text.length() < 3) {
			return Collections.emptyList();
		}
		return subsetService.autocompleteCreators(text);
	}
}