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;
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
......
......@@ -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() {
......
/*
* 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,15 +22,13 @@ 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;
// TODO: Auto-generated Javadoc
/**
* The Class SubsetFilter.
*
* @author Maxym Borodenko
* Filters for {@link Accession}
*/
public class AccessionFilter extends UuidModelFilter {
......@@ -73,6 +71,8 @@ public class AccessionFilter extends UuidModelFilter {
public AccessionGeoFilter geo;
public AccessionCollectFilter coll;
/**
* Builds the query.
*
......@@ -112,15 +112,18 @@ public class AccessionFilter extends UuidModelFilter {
if (origin != null) {
and.and(origin.buildQuery(accession.countryOfOrigin));
}
if (geo != null) {
and.and(geo.buildQuery(accession.accessionId.geo));
}
if (mlsStatus != null) {
and.and(accession.mlsStatus.eq(mlsStatus));
}
if (available != null) {
and.and(accession.available.eq(available));
}
if (geo != null) {
and.and(geo.buildQuery(accession.accessionId.geo));
}
if (coll != null) {
and.and(coll.buildQuery(accession.accessionId.coll));
}
return and;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment