Commit a7dd0359 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza

Subsets: creators

- fixed save/update, added FK UK
- code cleanup
- add junit tests
Signed-off-by: Matija Obreza's avatarMatija Obreza <matija.obreza@croptrust.org>
parent df9bd5b0
/*
* 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.genesys2.server.api.v1;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.model.impl.SubsetCreator;
import org.genesys2.server.service.SubsetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
/**
* The Class SubsetCreatorController.
*
* @author Andrey Lugovskoy.
*/
@RestController("catalogSubsetCreatorApi0")
@PreAuthorize("isAuthenticated()")
@RequestMapping(SubsetCreatorController.CONTROLLER_URL)
@Api(tags = { "subset" })
public class SubsetCreatorController {
/** The Constant API_BASE. */
public static final String CONTROLLER_URL = SubsetController.API_BASE + "/{UUID}/subsetcreator";
/** The subset service. */
@Autowired
protected SubsetService subsetService;
/**
* List subset creators.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param uuid the uuid
* @return the page
*/
@GetMapping(value = "/list")
public Page<SubsetCreator> listSubsetCreators(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
@RequestParam(name = "l", required = false, defaultValue = "50") final int pageSize,
@RequestParam(name = "d", required = false, defaultValue = "ASC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "id") final String[] sort, @PathVariable("UUID") final UUID uuid) {
return subsetService.listSubsetCreators(uuid, new PageRequest(page, Integer.min(pageSize, 100), direction, sort));
}
/**
* Load by uuid.
*
* @param uuid the uuid
* @return the subset creator
* @throws NotFoundElement the not found element
*/
@GetMapping(value = "/{creatorUuid}")
public SubsetCreator loadByUuid(@PathVariable("creatorUuid") final UUID uuid) throws NotFoundElement {
return subsetService.loadSubsetCreator(uuid);
}
/**
* 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 SubsetCreator createSubsetCreator(@PathVariable("UUID") final UUID uuid, @RequestBody final SubsetCreator subsetCreator) throws NotFoundElement {
final Subset subset = subsetService.get(uuid);
return subsetService.createSubsetCreator(subset, 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 SubsetCreator deleteSubsetCreator(@PathVariable("UUID") final UUID subsetUuid, @RequestBody final SubsetCreator subsetCreator) throws NotFoundElement {
final Subset subset = subsetService.get(subsetUuid);
return subsetService.removeSubsetCreator(subset, 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 SubsetCreator updateSubsetCreator(@PathVariable("UUID") final UUID subsetUuid, @RequestBody final SubsetCreator subsetCreator) throws NotFoundElement {
final Subset subset = subsetService.get(subsetUuid);
return subsetService.updateSubsetCreator(subset, subsetCreator);
}
/**
* Autocomplete.
*
* @param text the text
* @return the list
*/
@JsonView(JsonViews.Public.class)
@GetMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
public List<SubsetCreator> autocomplete(@RequestParam("c") final String text) {
if (text.length() < 3) {
return Collections.emptyList();
}
return subsetService.autocompleteCreators(text);
}
}
......@@ -18,24 +18,13 @@ package org.genesys2.server.model.impl;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.*;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.blocks.model.UuidModel;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.catalog.annotations.PublishValidation;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.AccessionId;
import org.hibernate.annotations.Type;
......@@ -43,6 +32,8 @@ import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
// TODO: Auto-generated Javadoc
/**
......@@ -101,6 +92,13 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
@Enumerated(EnumType.ORDINAL)
private PublishState state = PublishState.DRAFT;
/** The creators. */
@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "subset", orphanRemoval = true, fetch = FetchType.LAZY)
@Field(type = FieldType.Object)
@JsonView({ JsonViews.Public.class })
private List<SubsetCreator> creators;
/**
* Generate UUID if missing.
*/
......@@ -300,4 +298,22 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
public int getAccessionCount() {
return this.accessionCount;
}
/**
* Gets subset creators
*
* @return subset creators
*/
public List<SubsetCreator> getCreators() {
return creators;
}
/**
* Sets subset creators
*
* @param creators new creators
*/
public void setCreators(List<SubsetCreator> creators) {
this.creators = creators;
}
}
/*
* Copyright 2017 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.genesys2.server.model.impl;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.genesys.blocks.model.UuidModel;
import org.genesys.catalog.annotations.PublishValidation;
import org.genesys.catalog.service.PublishValidationInterface;
import javax.persistence.*;
import java.util.Map;
/**
* The Class SubsetCreator.
*
* @author vPavlov.
*/
@Entity
@Table(name="subset_creator")
public class SubsetCreator extends UuidModel implements PublishValidationInterface {
/**
*
*/
private static final long serialVersionUID = -1536755624303L;
/**
* The Enum SubsetCreatorRole.
*/
public enum SubsetCreatorRole {
/** Data manager: Responsible of the planning and execution of the germplasm characterization and evaluation activity which resulted in the subset. Oversees the collection and management of characterization and evaluation data, and has final sign-off on publication. */
MANAGER,
/** Data collector: Records germplasm characterization or evaluation data in the field. */
COLLECTOR,
/** Data digitizer: Digitizes data. */
DIGITIZER,
/** Data curator: Organizes and validates data and metadata in correct format, ensures quality of both. */
CURATOR
}
/** The full name. */
@PublishValidation
@Column(length = 200, nullable = false)
private String fullName;
/** The email. */
@PublishValidation
private String email;
/** The phone number. */
@PublishValidation
private String phoneNumber;
/** The fax. */
@PublishValidation
private String fax;
/** The institutional affiliation. */
@PublishValidation
private String institutionalAffiliation;
/** The institute address. */
@PublishValidation
private String instituteAddress;
/** The role. */
@Enumerated(EnumType.STRING)
@PublishValidation
private SubsetCreatorRole role;
/** The subset. */
@ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.DETACH }, optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "subsetId")
@JsonIgnore
private Subset subset;
/**
* Gets the role.
*
* @return the role
*/
public SubsetCreatorRole getRole() {
return role;
}
/**
* Sets the role.
*
* @param role the new role
*/
public void setRole(final SubsetCreatorRole role) {
this.role = role;
}
/**
* Gets the subset.
*
* @return the subset
*/
public Subset getSubset() {
return subset;
}
/**
* Sets the subset.
*
* @param subset the new subset
*/
public void setSubset(final Subset subset) {
this.subset = subset;
}
/**
* Gets the full name.
*
* @return the full name
*/
public String getFullName() {
return fullName;
}
/**
* Sets the full name.
*
* @param fullName the new full name
*/
public void setFullName(final String fullName) {
this.fullName = fullName;
}
/**
* Gets the email.
*
* @return the email
*/
public String getEmail() {
return email;
}
/**
* Sets the email.
*
* @param email the new email
*/
public void setEmail(final String email) {
this.email = email;
}
/**
* Gets the phone number.
*
* @return the phone number
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* Sets the phone number.
*
* @param phoneNumber the new phone number
*/
public void setPhoneNumber(final String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
* Gets the fax.
*
* @return the fax
*/
public String getFax() {
return fax;
}
/**
* Sets the fax.
*
* @param fax the new fax
*/
public void setFax(final String fax) {
this.fax = fax;
}
/**
* Gets the institutional affiliation.
*
* @return the institutional affiliation
*/
public String getInstitutionalAffiliation() {
return institutionalAffiliation;
}
/**
* Sets the institutional affiliation.
*
* @param institutionalAffiliation the new institutional affiliation
*/
public void setInstitutionalAffiliation(final String institutionalAffiliation) {
this.institutionalAffiliation = institutionalAffiliation;
}
/**
* Gets the institute address.
*
* @return the institute address
*/
public String getInstituteAddress() {
return instituteAddress;
}
/**
* Sets the institute address.
*
* @param instituteAddress the new institute address
*/
public void setInstituteAddress(final String instituteAddress) {
this.instituteAddress = instituteAddress;
}
/*
* (non-Javadoc)
* @see org.genesys.catalog.service.PublishValidationInterface#validation()
*/
@Override
public Map<String, String> validation() {
return PublishValidationInterface.super.validation();
}
}
/*
* Copyright 2017 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.genesys2.server.persistence;
import org.genesys2.server.model.impl.SubsetCreator;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.UUID;
/**
* The Interface SubsetCreatorRepository.
*
* @author vPavlov
*/
@Repository
public interface SubsetCreatorRepository extends JpaRepository<SubsetCreator, Long>, QueryDslPredicateExecutor<SubsetCreator> {
/**
* Find by uuid.
*
* @param uuid the uuid
* @return the subset creator
*/
SubsetCreator findByUuid(UUID uuid);
/**
* List by uuid of subset.
*
* @param page the page
* @param uuid the uuid
* @return the page
*/
@Query("select distinct sc from SubsetCreator sc where sc.subset.uuid = :uuid")
Page<SubsetCreator> listByUUidOfSubset(Pageable page, @Param("uuid") UUID uuid);
}
......@@ -20,8 +20,10 @@ import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.model.impl.SubsetCreator;
import org.genesys2.server.service.filter.SubsetFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
......@@ -144,4 +146,88 @@ public interface SubsetService {
List<Subset> listByAccession(AccessionId accessionId);
/**
* Create SubsetCreator for Subset.
*
* @param Subset the Subset
* @param input SubsetCreator
* @return created SubsetCreator
* @throws NotFoundElement throws if don't match the version
*/
SubsetCreator createSubsetCreator(Subset Subset, SubsetCreator input) throws NotFoundElement;
/**
* Remove SubsetCreator of Subset.
*
* @param Subset the Subset
* @param input SubsetCreator
* @return removed SubsetCreator
* @throws NotFoundElement throws if don't match the version
*/
SubsetCreator removeSubsetCreator(Subset Subset, SubsetCreator input) throws NotFoundElement;
/**
* Removes the Subset creator.
*
* @param Subset the Subset
* @param SubsetCreatorUuid the Subset creator uuid
* @return the Subset creator
* @throws NotFoundElement the not found element
*/
SubsetCreator removeSubsetCreator(Subset Subset, UUID SubsetCreatorUuid) throws NotFoundElement;
/**
* Load list SubsetCreators by Subset UUID.
*
* @param SubsetUuid Subset UUID
* @param page Page
* @return Page with SubsetCreator list
*/
Page<SubsetCreator> listSubsetCreators(UUID SubsetUuid, Pageable page);
/**
* Load Subset by input SubsetCreator.
*
* @param SubsetCreator input SubsetCreator
* @return loaded SubsetCreator
* @throws NotFoundElement throws if don't match the version
*/
SubsetCreator loadSubsetCreator(SubsetCreator SubsetCreator) throws NotFoundElement;
/**
* Load Subset by input SubsetCreator.
*
* @param SubsetCreatorUuid input SubsetCreator UUID
* @return loaded SubsetCreator
* @throws NotFoundElement throws if don't match the version
*/
SubsetCreator loadSubsetCreator(UUID SubsetCreatorUuid) throws NotFoundElement;
/**
* Load list SubsetCreators by Subset UUID.
*
* @param Subset the Subset
* @return List of SubsetCreator
* @throws NotFoundElement throws if don't match the version
*/
List<SubsetCreator> loadSubsetCreators(Subset Subset) throws NotFoundElement;
/**
* Update Subset creator.
*
* @param Subset the Subset
* @param SubsetCreator the Subset creator
* @return the Subset creator
* @throws NotFoundElement the not found element
*/
SubsetCreator updateSubsetCreator(Subset Subset, SubsetCreator SubsetCreator) throws NotFoundElement;
/**
* Autocomplete creators.
*
* @param text the text
* @return list of matched creators
*/
List<SubsetCreator> autocompleteCreators(String text);
}
......@@ -22,6 +22,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import com.querydsl.core.types.Predicate;
import org.genesys.catalog.exceptions.InvalidApiUsageException;