Commit eae5a6ea authored by Matija Obreza's avatar Matija Obreza

Merge branch '282-accession-apiv1-filter' into 'master'

Resolve "Accession APIv1 /filter"

Closes #282

See merge request genesys-pgr/genesys-server!159
parents 8038a9bf 691c5437
/*
* 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 java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.springframework.beans.factory.annotation.Autowired;
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.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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import io.swagger.annotations.Api;
/**
* Accession API v1
*/
@RestController("accessionApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(AccessionController.API_BASE)
@Api(tags = { "accession" })
public class AccessionController {
/** The Constant API_BASE. */
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/acn";
@Autowired
private AccessionService accessionService;
/** The short filter service. */
@Autowired
protected ShortFilterService shortFilterService;
/**
* Gets the accession
*
* @param uuid the uuid
* @return the subset
*/
@GetMapping(value = "/{UUID}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Accession getByUuid(@PathVariable("UUID") final UUID uuid) {
return accessionService.getByUuid(uuid);
}
/**
* Gets the accession
*
* @param uuid the uuid
* @return the subset
*/
@GetMapping(value = "/10.{dummy:[0-9]+}/**}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Accession getByDoi(HttpServletRequest request) {
final String fullpath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
final String doi = fullpath.substring(1);
return accessionService.getByDoi(doi);
}
/**
* List accessions by filterCode or filter
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filterCode short filter code
* @param filter the filter
* @return the page
* @throws IOException
*/
@PostMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<Accession> list(@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,
@RequestParam(name = "f", required = false) String filterCode,
@RequestBody(required = false) AccessionFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, AccessionFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
return new FilteredPage<>(filterCode, filter, accessionService.list(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
}
}
......@@ -28,6 +28,7 @@ import org.genesys.blocks.security.model.AclObjectIdentity;
import org.genesys.blocks.security.model.AclSid;
import org.genesys.blocks.security.serialization.SidPermissions;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.service.UserService;
......@@ -55,10 +56,12 @@ import io.swagger.annotations.Api;
* @author Matija Obreza
*/
@RestController("permissionApi1")
@RequestMapping(value = { "/api/v1/permission" })
@RequestMapping(value = PermissionController.API_BASE )
@Api(tags = { "permission" })
public class PermissionController {
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/permission";
private static final Logger LOG = LoggerFactory.getLogger(PermissionController.class);
/** The acl service. */
......
......@@ -22,6 +22,7 @@ import java.util.UUID;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.filter.SubsetFilter;
......@@ -53,7 +54,7 @@ import io.swagger.annotations.Api;
public class SubsetController {
/** The Constant API_BASE. */
public static final String API_BASE = "/api/v1/subset";
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/subset";
@Autowired
private SubsetService subsetService;
......
......@@ -36,6 +36,7 @@ import javax.persistence.Transient;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.IdUUID;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop;
......@@ -45,8 +46,8 @@ import org.genesys2.util.NumberUtils;
import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonView;
@MappedSuperclass
public abstract class AccessionData extends AuditedVersionedModel implements IdUUID, Serializable, SelfCleaning {
......@@ -70,6 +71,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "instituteId")
@JsonView({ JsonViews.Minimal.class })
private FaoInstitute institute;
@Column(name = "acceNumb", nullable = false, length = 128)
......@@ -87,7 +89,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "taxonomyId2")
@JsonIgnoreProperties(value = { "id", "version" })
@JsonView({ JsonViews.Minimal.class })
private Taxonomy2 taxonomy;
// FIXME: Remove?
......
......@@ -27,17 +27,16 @@ import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.model.GlobalVersionedAuditedModel;
import org.genesys2.server.model.impl.CropTaxonomy;
import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Cacheable
@Entity
// Add index on all fields
......@@ -124,8 +123,6 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
return sb.toString();
}
@Transient
public String getTaxonNameHtml() {
final StringBuilder sb = new StringBuilder();
sb.append("<i>").append(genus).append(" ").append(species).append("</i>");
......
......@@ -23,20 +23,27 @@ import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import javax.persistence.*;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.genesys.blocks.model.BasicModel;
import org.genesys2.server.api.serialization.CountrySerializer;
import org.genesys.blocks.model.JsonViews;
import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Cacheable
@Entity
@Table(name = "country")
@JsonSerialize(using = CountrySerializer.class)
public class Country extends BasicModel {
private static final long serialVersionUID = -1688723909298769804L;
......@@ -45,6 +52,7 @@ public class Country extends BasicModel {
private String code3;
@Column(unique = false, length = 2)
@JsonView({ JsonViews.Public.class } )
private String code2;
private boolean current;
......@@ -52,23 +60,31 @@ public class Country extends BasicModel {
private String name;
@Column(length = 3)
@JsonView({ JsonViews.Public.class } )
private String codeNum;
@JsonView({ JsonViews.Public.class } )
private Long refnameId;
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "regionId")
@JsonView({ JsonViews.Public.class } )
private GeoRegion region;
@Column(name = "minLongitude")
@JsonView({ JsonViews.Public.class } )
private Double minLongitude;
@Column(name = "minLatitude")
@JsonView({ JsonViews.Public.class } )
private Double minLatitude;
@Column(name = "maxLongitude")
@JsonView({ JsonViews.Public.class } )
private Double maxLongitude;
@Column(name = "maxLatitude")
@JsonView({ JsonViews.Public.class } )
private Double maxLatitude;
/**
......@@ -76,17 +92,21 @@ public class Country extends BasicModel {
*/
@Lob
@Type(type = "org.hibernate.type.TextType")
@JsonView({ JsonViews.Public.class })
private String nameL;
@Lob
@Type(type = "org.hibernate.type.TextType")
@JsonView({ JsonViews.Public.class } )
private String wikiLink;
@Transient
@JsonView({ JsonViews.Protected.class })
private JsonNode nameJ;
@ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "replacedBy")
@JsonView({ JsonViews.Public.class })
private Country replacedBy;
public Country() {
......
......@@ -35,19 +35,17 @@ import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.EntityId;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys2.server.api.serialization.FaoInstituteSerializer;
import com.fasterxml.jackson.annotation.JsonView;
@Cacheable
@Entity
@Table(name = "faoinstitute", uniqueConstraints = @UniqueConstraint(columnNames = { "code" }), indexes = { @Index(columnList = "code", name = "code_FAOINSTITUTE") })
@JsonSerialize(using = FaoInstituteSerializer.class)
public class FaoInstitute extends BasicModel implements GeoReferencedEntity, AclAwareModel, EntityId {
private static final long serialVersionUID = -8773002513838748431L;
......@@ -61,12 +59,15 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl
private String fullName;
@Column(length = 100)
@JsonView({ JsonViews.Public.class })
private String type;
@Column(length = 300)
@JsonView({ JsonViews.Public.class })
private String url;
@Column(length = 300)
@JsonView({ JsonViews.Public.class })
private String email;
@Column(length = LEN_ACRONYM)
......@@ -76,16 +77,17 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl
private String vCode;
@Column(name = "codeSGSV")
@JsonView({ JsonViews.Public.class })
private String codeSGSV;
private boolean current;
@JsonIgnore
@ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "countryId")
@JsonView({ JsonViews.Minimal.class })
private Country country;
@JsonIgnore
@JsonView({ JsonViews.Protected.class })
@OneToMany(cascade = {})
@JoinColumn(referencedColumnName = "code", name = "instCode")
@MapKey(name = "setting")
......
......@@ -17,9 +17,12 @@ import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.VersionedModel;
import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -38,16 +41,20 @@ public class GeoRegion extends VersionedModel {
@ManyToOne()
@JoinColumn(name = "parentId")
@JsonView({ JsonViews.Public.class })
private GeoRegion parentRegion;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "region")
@JsonView({ JsonViews.Public.class })
private List<Country> countries;
@Lob
@Type(type = "org.hibernate.type.TextType")
@JsonIgnore
private String nameL;
@Transient
@JsonIgnore
private JsonNode nameJ;
public String getIsoCode() {
......
......@@ -33,9 +33,10 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.query.Param;
public interface AccessionRepository extends JpaRepository<Accession, Long>, AccessionRepositoryCustom {
public interface AccessionRepository extends JpaRepository<Accession, Long>, AccessionRepositoryCustom, QueryDslPredicateExecutor<Accession> {
@Query("select a.id from Accession a")
public List<Long> listAccessionsIds(Pageable pageable);
......
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* 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.
......@@ -12,31 +12,40 @@
* 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.service;
package org.genesys2.server.api.serialization;
import java.util.UUID;
import java.io.IOException;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.filter.AccessionFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.genesys2.server.model.impl.FaoInstitute;
public interface AccessionService {
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* List accessions by filter
*
* @param filter the filter
* @param page the page
* @return the page
*/
Page<Accession> list(AccessionFilter filter, Pageable page);
public class FaoInstituteSerializer extends JsonSerializer<FaoInstitute> {
@Override
public void serialize(FaoInstitute institute, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
if (institute == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("instCode", institute.getCode());
jgen.writeObjectField("name", institute.getFullName());
jgen.writeEndObject();
}
}
/**
* Gets accession by uuid
*
* @param uuid the uuid
* @return the accession
*/
Accession getByUuid(UUID uuid);
/**
* Gets accession by doi.
*
* @param doi the doi
* @return the by doi
*/
Accession getByDoi(String doi);
}
/*
* 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.service.filter;
import static org.genesys2.server.model.genesys.QAccessionCollect.accessionCollect;
import java.util.Set;
import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys2.server.model.genesys.AccessionCollect;
import org.genesys2.server.model.genesys.QAccessionCollect;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
/**
* Filters for {@link AccessionCollect}
*/
public class AccessionCollectFilter extends BasicModelFilter {
/** The date. */
public StringFilter date;
/** The numb. */
public StringFilter numb;
/** The mission. */
public Set<String> mission;
/** The site. */
public StringFilter site;
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildQuery() {
return buildQuery(accessionCollect);
}
/**
* Builds the query.
*
* @param collect the country
* @return the predicate
*/
public Predicate buildQuery(QAccessionCollect collect) {
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(collect._super, and);
if (date != null) {
and.and(date.buildQuery(collect.collDate));
}
if (numb != null) {
and.and(numb.buildQuery(collect.collNumb));
}
if (site != null) {
and.and(site.buildQuery(collect.collSite));
}
if (mission != null && !mission.isEmpty()) {
and.and(collect.collMissId.in(mission));
}
return and;
}
}
/*
* 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.service.filter;
import static org.genesys2.server.model.genesys.QAccession.accession;
import java.util.Set;
import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys2.server.model.genesys.Accession;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
/**
* Filters for {@link Accession}
*/
public class AccessionFilter extends UuidModelFilter {
/** The historic. */
/// Include historic
public Boolean historic;
/** The mls status. */
public Boolean mlsStatus;
/** The available. */