Commit 2e578d99 authored by Matija Obreza's avatar Matija Obreza
Browse files

JSON serialization for Crop, CropRule

parent 10a127db
...@@ -30,16 +30,19 @@ import javax.persistence.Transient; ...@@ -30,16 +30,19 @@ import javax.persistence.Transient;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.AuditedModel; import org.genesys2.server.model.AuditedModel;
import org.genesys2.server.servlet.controller.rest.serialization.CropSerializer;
import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store; import org.hibernate.search.annotations.Store;
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;
@Entity @Entity
@Table(name = "crop") @Table(name = "crop")
@Indexed @Indexed
@JsonSerialize(using = CropSerializer.class)
public class Crop extends AuditedModel { public class Crop extends AuditedModel {
private static final long serialVersionUID = -2686341831839109257L; private static final long serialVersionUID = -2686341831839109257L;
......
...@@ -25,6 +25,9 @@ import javax.persistence.Table; ...@@ -25,6 +25,9 @@ import javax.persistence.Table;
import javax.persistence.UniqueConstraint; import javax.persistence.UniqueConstraint;
import org.genesys2.server.model.BusinessModel; import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.servlet.controller.rest.serialization.CropRuleSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/** /**
* Crop rules establish which taxonomies are part of a {@link Crop}. * Crop rules establish which taxonomies are part of a {@link Crop}.
...@@ -33,6 +36,7 @@ import org.genesys2.server.model.BusinessModel; ...@@ -33,6 +36,7 @@ import org.genesys2.server.model.BusinessModel;
*/ */
@Entity @Entity
@Table(name = "croprule", uniqueConstraints = { @UniqueConstraint(columnNames = { "cropId", "genus", "species" }) }) @Table(name = "croprule", uniqueConstraints = { @UniqueConstraint(columnNames = { "cropId", "genus", "species" }) })
@JsonSerialize(using = CropRuleSerializer.class)
public class CropRule extends BusinessModel { public class CropRule extends BusinessModel {
private static final long serialVersionUID = -2336100072991067193L; private static final long serialVersionUID = -2336100072991067193L;
......
...@@ -70,7 +70,7 @@ public class CropsController extends RestController { ...@@ -70,7 +70,7 @@ public class CropsController extends RestController {
Object listCrops() { Object listCrops() {
LOG.info("Listing crops"); LOG.info("Listing crops");
List<Crop> crops = cropService.list(LocaleContextHolder.getLocale()); List<Crop> crops = cropService.list(LocaleContextHolder.getLocale());
return OAuth2Cleanup.clean(crops); return crops;
} }
/** /**
...@@ -109,7 +109,7 @@ public class CropsController extends RestController { ...@@ -109,7 +109,7 @@ public class CropsController extends RestController {
public @ResponseBody public @ResponseBody
Object getCrop(@PathVariable("shortName") String shortName) throws AuthorizationException { Object getCrop(@PathVariable("shortName") String shortName) throws AuthorizationException {
LOG.info("Getting crop " + shortName); LOG.info("Getting crop " + shortName);
return OAuth2Cleanup.clean(cropService.getCrop(shortName)); return cropService.getCrop(shortName);
} }
/** /**
......
/**
* Copyright 2013 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.servlet.controller.rest.serialization;
import java.io.IOException;
import org.genesys2.server.model.impl.CropRule;
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 CropRuleSerializer extends JsonSerializer<CropRule> {
@Override
public void serialize(CropRule cropRule, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
if (cropRule == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("id", cropRule.getId());
jgen.writeObjectField("genus", cropRule.getGenus());
if (cropRule.getSpecies() != null)
jgen.writeObjectField("species", cropRule.getSpecies());
if (cropRule.getCrop() != null)
jgen.writeObjectField("_crop", cropRule.getCrop().getShortName());
jgen.writeEndObject();
}
}
}
...@@ -14,23 +14,42 @@ ...@@ -14,23 +14,42 @@
* limitations under the License. * limitations under the License.
**/ **/
package org.genesys2.server.servlet.controller.rest; package org.genesys2.server.servlet.controller.rest.serialization;
import java.io.IOException; import java.io.IOException;
import org.codehaus.jackson.JsonGenerator; import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.genesys2.server.model.impl.Crop; import org.genesys2.server.model.impl.Crop;
import org.springframework.stereotype.Component; import org.genesys2.server.model.impl.CropRule;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
@Component
public class CropSerializer extends JsonSerializer<Crop> { public class CropSerializer extends JsonSerializer<Crop> {
@Override @Override
public void serialize(Crop value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { public void serialize(Crop crop, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
// FIXME Does not do anything! if (crop == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("shortName", crop.getShortName());
if (StringUtils.isNotBlank(crop.getDescription()))
jgen.writeObjectField("description", crop.getDescription());
if (StringUtils.isNotBlank(crop.getI18n()))
jgen.writeObjectField("i18n", crop.getI18n());
jgen.writeArrayFieldStart("rules");
for (CropRule cropRule : crop.getCropRules()) {
jgen.writeObject(cropRule);
}
jgen.writeEndArray();
jgen.writeEndObject();
}
} }
} }
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