DiversityTreeCreatorController.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.genesys.server.api.v1;

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

import org.genesys.blocks.model.JsonViews;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.Pagination;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.model.impl.DiversityTree;
import org.genesys.server.model.impl.DiversityTreeCreator;
import org.genesys.server.service.DiversityTreeService;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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.fasterxml.jackson.annotation.JsonView;

import io.swagger.annotations.Api;

/**
 * The Class DiversityTreeCreatorController.
 *
 * @author Maxym Borodenko
 */
@RestController("diversityTreeCreatorApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(DiversityTreeCreatorController.CONTROLLER_URL)
@Api(tags = { "diversityTree" })
public class DiversityTreeCreatorController extends ApiBaseController {

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

	/** The diversity tree service. */
	@Autowired
	private DiversityTreeService treeService;

	/**
	 * List of creators.
	 *
	 * @param page the page
	 * @param uuid the uuid
	 * @return the page
	 */
	@GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
	public Page<DiversityTreeCreator> list(@PathVariable("uuid") final UUID uuid, @ParameterObject final Pagination page) {
		return treeService.listCreators(uuid, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "id"));
	}

	/**
	 * Load by uuid.
	 *
	 * @param uuid the uuid
	 * @return the creator
	 * @throws NotFoundElement the not found element
	 */
	@GetMapping(value = "/{creatorUuid}", produces = MediaType.APPLICATION_JSON_VALUE)
	public DiversityTreeCreator loadByUuid(@PathVariable("creatorUuid") final UUID creatorUuid, @PathVariable UUID uuid) throws NotFoundElement {
		return treeService.loadCreator(creatorUuid);
	}

	/**
	 * Create a tree creator.
	 *
	 * @param uuid the uuid
	 * @param creator the creator
	 * @return saved creator
	 * @throws NotFoundElement the not found element
	 */
	@PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
	public DiversityTreeCreator create(@PathVariable("uuid") final UUID uuid, @RequestBody final DiversityTreeCreator creator) throws NotFoundElement {
		final DiversityTree tree = treeService.get(uuid);
		return treeService.createCreator(tree, creator);
	}

	/**
	 * Update creator.
	 *
	 * @param treeUuid the tree uuid
	 * @param creator the tree creator
	 * @return the creator
	 * @throws NotFoundElement the not found element
	 */
	@PutMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
	public DiversityTreeCreator update(@PathVariable("uuid") final UUID treeUuid, @RequestBody final DiversityTreeCreator creator) throws NotFoundElement {
		final DiversityTree tree = treeService.get(treeUuid);
		return treeService.updateCreator(tree, creator);
	}

	/**
	 * Delete creator.
	 *
	 * @param treeUuid the tree uuid
	 * @param creatorUuid the creator uuid
	 * @param version the creator version
	 * @return the tree creator
	 * @throws NotFoundElement the not found element
	 */
	@DeleteMapping(value = "/{creatorUuid},{version}", produces = MediaType.APPLICATION_JSON_VALUE)
	public DiversityTreeCreator remove(@PathVariable("uuid") final UUID treeUuid, @PathVariable("creatorUuid") final UUID creatorUuid,
			@PathVariable("version") final int version) throws NotFoundElement {

		final DiversityTree tree = treeService.get(treeUuid);
		return treeService.removeCreator(tree, treeService.loadCreator(creatorUuid, version));
	}

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

		return treeService.autocompleteCreators(term);
	}
}