Commit 691c5437 authored by Matija Obreza's avatar Matija Obreza

Improved JSON serialization and filtering

- No  custom JsonSerializers, use annotations instead
- Added AccessionFilter#coll <- AccessionCollectFilter
parent ecd9b2f3
/**
* Copyright 2014 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.serialization;
import java.io.IOException;
import org.genesys2.server.model.impl.Country;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class CountrySerializer extends JsonSerializer<Country> {
@Override
public void serialize(Country country, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
if (country == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("code", country.getCode3());
jgen.writeObjectField("name", country.getName());
jgen.writeObjectField("refnameId", country.getRefnameId());
jgen.writeObjectField("current", country.isCurrent());
jgen.writeEndObject();
}
}
}
/**
* Copyright 2014 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.serialization;
import java.io.IOException;
import org.genesys2.server.model.impl.FaoInstitute;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
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();
}
}
}
...@@ -36,6 +36,7 @@ import javax.persistence.Transient; ...@@ -36,6 +36,7 @@ import javax.persistence.Transient;
import org.genesys.blocks.model.AuditedVersionedModel; import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.IdUUID; import org.genesys.blocks.model.IdUUID;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning; import org.genesys.blocks.model.SelfCleaning;
import org.genesys2.server.model.impl.Country; import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop; import org.genesys2.server.model.impl.Crop;
...@@ -45,8 +46,8 @@ import org.genesys2.util.NumberUtils; ...@@ -45,8 +46,8 @@ import org.genesys2.util.NumberUtils;
import org.hibernate.annotations.Type; import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonView;
@MappedSuperclass @MappedSuperclass
public abstract class AccessionData extends AuditedVersionedModel implements IdUUID, Serializable, SelfCleaning { public abstract class AccessionData extends AuditedVersionedModel implements IdUUID, Serializable, SelfCleaning {
...@@ -70,6 +71,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU ...@@ -70,6 +71,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
@ManyToOne(cascade = {}, optional = false) @ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "instituteId") @JoinColumn(name = "instituteId")
@JsonView({ JsonViews.Minimal.class })
private FaoInstitute institute; private FaoInstitute institute;
@Column(name = "acceNumb", nullable = false, length = 128) @Column(name = "acceNumb", nullable = false, length = 128)
...@@ -87,7 +89,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU ...@@ -87,7 +89,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
@ManyToOne(cascade = {}, optional = false) @ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "taxonomyId2") @JoinColumn(name = "taxonomyId2")
@JsonIgnoreProperties(value = { "id", "version" }) @JsonView({ JsonViews.Minimal.class })
private Taxonomy2 taxonomy; private Taxonomy2 taxonomy;
// FIXME: Remove? // FIXME: Remove?
......
...@@ -27,17 +27,16 @@ import javax.persistence.Lob; ...@@ -27,17 +27,16 @@ import javax.persistence.Lob;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.PrePersist; import javax.persistence.PrePersist;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint; import javax.persistence.UniqueConstraint;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.model.GlobalVersionedAuditedModel; import org.genesys2.server.model.GlobalVersionedAuditedModel;
import org.genesys2.server.model.impl.CropTaxonomy; import org.genesys2.server.model.impl.CropTaxonomy;
import org.hibernate.annotations.Type; import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Cacheable @Cacheable
@Entity @Entity
// Add index on all fields // Add index on all fields
......
...@@ -23,20 +23,27 @@ import java.util.Comparator; ...@@ -23,20 +23,27 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; 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.genesys.blocks.model.BasicModel;
import org.genesys2.server.api.serialization.CountrySerializer; import org.genesys.blocks.model.JsonViews;
import org.hibernate.annotations.Type; import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Cacheable @Cacheable
@Entity @Entity
@Table(name = "country") @Table(name = "country")
@JsonSerialize(using = CountrySerializer.class)
public class Country extends BasicModel { public class Country extends BasicModel {
private static final long serialVersionUID = -1688723909298769804L; private static final long serialVersionUID = -1688723909298769804L;
...@@ -45,6 +52,7 @@ public class Country extends BasicModel { ...@@ -45,6 +52,7 @@ public class Country extends BasicModel {
private String code3; private String code3;
@Column(unique = false, length = 2) @Column(unique = false, length = 2)
@JsonView({ JsonViews.Public.class } )
private String code2; private String code2;
private boolean current; private boolean current;
...@@ -52,23 +60,31 @@ public class Country extends BasicModel { ...@@ -52,23 +60,31 @@ public class Country extends BasicModel {
private String name; private String name;
@Column(length = 3) @Column(length = 3)
@JsonView({ JsonViews.Public.class } )
private String codeNum; private String codeNum;
@JsonView({ JsonViews.Public.class } )
private Long refnameId; private Long refnameId;
@ManyToOne(fetch = FetchType.LAZY, optional = true) @ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "regionId") @JoinColumn(name = "regionId")
@JsonView({ JsonViews.Public.class } )
private GeoRegion region; private GeoRegion region;
@Column(name = "minLongitude") @Column(name = "minLongitude")
@JsonView({ JsonViews.Public.class } )
private Double minLongitude; private Double minLongitude;
@Column(name = "minLatitude") @Column(name = "minLatitude")
@JsonView({ JsonViews.Public.class } )
private Double minLatitude; private Double minLatitude;
@Column(name = "maxLongitude") @Column(name = "maxLongitude")
@JsonView({ JsonViews.Public.class } )
private Double maxLongitude; private Double maxLongitude;
@Column(name = "maxLatitude") @Column(name = "maxLatitude")
@JsonView({ JsonViews.Public.class } )
private Double maxLatitude; private Double maxLatitude;
/** /**
...@@ -76,17 +92,21 @@ public class Country extends BasicModel { ...@@ -76,17 +92,21 @@ public class Country extends BasicModel {
*/ */
@Lob @Lob
@Type(type = "org.hibernate.type.TextType") @Type(type = "org.hibernate.type.TextType")
@JsonView({ JsonViews.Public.class })
private String nameL; private String nameL;
@Lob @Lob
@Type(type = "org.hibernate.type.TextType") @Type(type = "org.hibernate.type.TextType")
@JsonView({ JsonViews.Public.class } )
private String wikiLink; private String wikiLink;
@Transient @Transient
@JsonView({ JsonViews.Protected.class })
private JsonNode nameJ; private JsonNode nameJ;
@ManyToOne(cascade = {}, optional = true) @ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "replacedBy") @JoinColumn(name = "replacedBy")
@JsonView({ JsonViews.Public.class })
private Country replacedBy; private Country replacedBy;
public Country() { public Country() {
......
...@@ -35,19 +35,17 @@ import javax.persistence.OneToMany; ...@@ -35,19 +35,17 @@ import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.UniqueConstraint; 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.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.BasicModel; import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.EntityId; import org.genesys.blocks.model.EntityId;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.security.model.AclAwareModel; import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys2.server.api.serialization.FaoInstituteSerializer;
import com.fasterxml.jackson.annotation.JsonView;
@Cacheable @Cacheable
@Entity @Entity
@Table(name = "faoinstitute", uniqueConstraints = @UniqueConstraint(columnNames = { "code" }), indexes = { @Index(columnList = "code", name = "code_FAOINSTITUTE") }) @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 { public class FaoInstitute extends BasicModel implements GeoReferencedEntity, AclAwareModel, EntityId {
private static final long serialVersionUID = -8773002513838748431L; private static final long serialVersionUID = -8773002513838748431L;
...@@ -61,12 +59,15 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl ...@@ -61,12 +59,15 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl
private String fullName; private String fullName;
@Column(length = 100) @Column(length = 100)
@JsonView({ JsonViews.Public.class })
private String type; private String type;
@Column(length = 300) @Column(length = 300)
@JsonView({ JsonViews.Public.class })
private String url; private String url;
@Column(length = 300) @Column(length = 300)
@JsonView({ JsonViews.Public.class })
private String email; private String email;
@Column(length = LEN_ACRONYM) @Column(length = LEN_ACRONYM)
...@@ -76,16 +77,17 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl ...@@ -76,16 +77,17 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl
private String vCode; private String vCode;
@Column(name = "codeSGSV") @Column(name = "codeSGSV")
@JsonView({ JsonViews.Public.class })
private String codeSGSV; private String codeSGSV;
private boolean current; private boolean current;
@JsonIgnore
@ManyToOne(cascade = {}, optional = true) @ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "countryId") @JoinColumn(name = "countryId")
@JsonView({ JsonViews.Minimal.class })
private Country country; private Country country;
@JsonIgnore @JsonView({ JsonViews.Protected.class })
@OneToMany(cascade = {}) @OneToMany(cascade = {})
@JoinColumn(referencedColumnName = "code", name = "instCode") @JoinColumn(referencedColumnName = "code", name = "instCode")
@MapKey(name = "setting") @MapKey(name = "setting")
......
...@@ -17,9 +17,12 @@ import javax.persistence.OneToMany; ...@@ -17,9 +17,12 @@ import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.VersionedModel; import org.genesys.blocks.model.VersionedModel;
import org.hibernate.annotations.Type; 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.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
...@@ -38,16 +41,20 @@ public class GeoRegion extends VersionedModel { ...@@ -38,16 +41,20 @@ public class GeoRegion extends VersionedModel {
@ManyToOne() @ManyToOne()
@JoinColumn(name = "parentId") @JoinColumn(name = "parentId")
@JsonView({ JsonViews.Public.class })
private GeoRegion parentRegion; private GeoRegion parentRegion;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "region") @OneToMany(fetch = FetchType.LAZY, mappedBy = "region")
@JsonView({ JsonViews.Public.class })
private List<Country> countries; private List<Country> countries;
@Lob @Lob
@Type(type = "org.hibernate.type.TextType") @Type(type = "org.hibernate.type.TextType")
@JsonIgnore
private String nameL; private String nameL;
@Transient @Transient
@JsonIgnore
private JsonNode nameJ; private JsonNode nameJ;
public String getIsoCode() { public String getIsoCode() {
......
/*
* 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;
}
}
...@@ -22,25 +22,23 @@ import java.util.Set; ...@@ -22,25 +22,23 @@ import java.util.Set;
import org.genesys.blocks.model.filters.NumberFilter; import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter; import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.UuidModelFilter; import org.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys2.server.model.genesys.Accession;
import com.querydsl.core.BooleanBuilder; import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate; import com.querydsl.core.types.Predicate;
// TODO: Auto-generated Javadoc
/** /**
* The Class SubsetFilter. * Filters for {@link Accession}
*
* @author Maxym Borodenko
*/ */
public class AccessionFilter extends UuidModelFilter { public class AccessionFilter extends UuidModelFilter {
/** The historic. */ /** The historic. */
/// Include historic /// Include historic
public Boolean historic; public Boolean historic;
/** The mls status. */ /** The mls status. */
public Boolean mlsStatus; public Boolean mlsStatus;
/** The available. */ /** The available. */
public Boolean available; public Boolean available;
...@@ -70,9 +68,11 @@ public class AccessionFilter extends UuidModelFilter { ...@@ -70,9 +68,11 @@ public class AccessionFilter extends UuidModelFilter {
/** The origin. */ /** The origin. */
public CountryFilter origin; public CountryFilter origin;
public AccessionGeoFilter geo; public AccessionGeoFilter<