Commit 0816bf45 authored by Matija Obreza's avatar Matija Obreza

Crop API: delete crop, rebuild crop-tax associations

Removed crop JSON serializers
parent 771ea2a2
......@@ -19,6 +19,8 @@ package org.genesys2.server.model;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* This superclass adds a Resource Description Framework (RDF) Uniform Resource
* Identifier (URI) to records to link them with semantic web linked open
......@@ -45,6 +47,7 @@ public abstract class GlobalVersionedAuditedModel extends VersionedAuditedModel
this.rdfUri = rdfUri;
}
@JsonIgnore
public String getRdfUriId() {
if (this.rdfUri == null) {
return "";
......
......@@ -37,20 +37,18 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.AclAwareModel;
import org.genesys2.server.model.GlobalVersionedAuditedModel;
import org.genesys2.server.servlet.controller.rest.serialization.CropSerializer;
import org.hibernate.annotations.Type;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Entity
@Table(name = "crop")
@Indexed
@JsonSerialize(using = CropSerializer.class)
public class Crop extends GlobalVersionedAuditedModel implements AclAwareModel {
private static final long serialVersionUID = -2686341831839109257L;
......@@ -79,6 +77,7 @@ public class Crop extends GlobalVersionedAuditedModel implements AclAwareModel {
/**
* Rules
*/
@JsonIgnore
@OneToMany(mappedBy = "crop", fetch = FetchType.EAGER, cascade = {}, orphanRemoval = true)
private List<CropRule> cropRules;
......@@ -207,20 +206,22 @@ public class Crop extends GlobalVersionedAuditedModel implements AclAwareModel {
/**
* Method to return the map of available vernacular names of crops
*
*
* @return Map<String,String> with language code as the key and the
* vernacular string as the value.
*/
@JsonIgnore
public Map<String, String> getLocalNameMap() {
return buildVernacularMap("name");
}
/**
* Method to return the map of available vernacular definitions of crops
*
*
* @return Map<String,String> with language code as the key and the
* vernacular string as the value.
*/
@JsonIgnore
public Map<String, String> getLocalDefinitionMap() {
return buildVernacularMap("description");
}
......
......@@ -25,9 +25,8 @@ import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.servlet.controller.rest.serialization.CropRuleSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Crop rules establish which taxonomies are part of a {@link Crop}.
......@@ -36,7 +35,6 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize;
*/
@Entity
@Table(name = "croprule", uniqueConstraints = { @UniqueConstraint(columnNames = { "cropId", "genus", "species", "subtaxa" }) })
@JsonSerialize(using = CropRuleSerializer.class)
public class CropRule extends BusinessModel {
private static final long serialVersionUID = -2336100072991067193L;
......@@ -52,6 +50,7 @@ public class CropRule extends BusinessModel {
@Column(nullable = true, length = 100)
private String subtaxa;
@JsonIgnore
@ManyToOne(cascade = {}, fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "cropId")
private Crop crop;
......
......@@ -74,4 +74,12 @@ public interface CropService {
*/
void setCropRules(Crop crop, List<CropRule> cropRules);
/**
* Remove crop
*
* @param crop
* @return
*/
Crop delete(Crop crop);
}
......@@ -71,6 +71,17 @@ public class CropServiceImpl implements CropService {
return cropRepository.findByShortName(shortName);
}
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Override
@Transactional
public Crop delete(Crop crop) {
cropTaxonomyRepository.clearCrop(crop);
cropRepository.delete(crop);
crop.setId(null);
return crop;
}
@Override
public List<Crop> listCrops() {
return cropRepository.findAll();
......
......@@ -16,16 +16,13 @@
package org.genesys2.server.servlet.controller.rest;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.ValidationException;
import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator;
import net.sf.oval.constraint.NotBlank;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.model.genesys.Parameter;
import org.genesys2.server.model.impl.Crop;
......@@ -84,7 +81,7 @@ public class CropsController extends RestController {
*/
@RequestMapping(value = "", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object createCrop(@RequestBody CropJson cropJson) throws ValidationException {
Object createCrop(@RequestBody Crop cropJson) throws ValidationException {
LOG.info("Creating crop");
final Validator validator = new Validator();
final List<ConstraintViolation> violations = validator.validate(cropJson);
......@@ -93,23 +90,14 @@ public class CropsController extends RestController {
throw new ModelValidationException("Crop does not validate", violations);
}
Crop crop = cropService.getCrop(cropJson.shortName);
Crop crop = cropService.getCrop(cropJson.getShortName());
if (crop == null) {
crop = cropService.addCrop(cropJson.shortName, cropJson.name, cropJson.description, cropJson.i18n);
crop = cropService.addCrop(cropJson.getShortName(), cropJson.getName(), cropJson.getDescription(), cropJson.getI18n());
} else {
crop = cropService.updateCrop(crop, cropJson.name, cropJson.description, cropJson.i18n);
crop = cropService.updateCrop(crop, cropJson.getName(), cropJson.getDescription(), cropJson.getI18n());
}
// TODO Remove
if (cropJson.rules != null)
for (final CropRuleJson ctj : cropJson.rules) {
final CropRule cropRule = cropService.addCropRule(crop, ctj.genus, ctj.species, ctj.included);
if (cropRule != null) {
LOG.info("Added rule: " + cropRule);
}
}
return crop;
}
......@@ -126,6 +114,19 @@ public class CropsController extends RestController {
return cropService.getCrop(shortName);
}
/**
* Delete crop /crops/{shortName}
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "/{shortName}", method = RequestMethod.DELETE, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Crop deleteCrop(@PathVariable("shortName") String shortName) throws AuthorizationException {
LOG.info("Getting crop " + shortName);
return cropService.delete(cropService.getCrop(shortName));
}
/**
* Get crop descriptors /crops/{shortName}/descriptors
*
......@@ -153,36 +154,28 @@ public class CropsController extends RestController {
LOG.info("Getting crop rules " + shortName);
final Crop crop = cropService.getCrop(shortName);
final List<CropRule> cropRules = cropService.getCropRules(crop);
return OAuth2Cleanup.clean(cropRules);
return cropRules;
}
/**
* Get crop taxonomy rules /crops/{shortName}/rules
*
* @return
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "/{shortName}/rules", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
@RequestMapping(value = "/{shortName}/rules", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object updateCropRules(@PathVariable("shortName") String shortName, @RequestBody List<CropRuleJson> rules) throws AuthorizationException {
List<CropRule> updateCropRules(@PathVariable("shortName") String shortName, @RequestBody List<CropRule> rules) throws AuthorizationException {
LOG.info("Updating crop rules for " + shortName);
final Crop crop = cropService.getCrop(shortName);
if (crop == null)
throw new ResourceNotFoundException("No crop " + shortName);
final List<CropRule> cropRules = new ArrayList<CropRule>();
for (CropRuleJson ctj : rules) {
CropRule cr = new CropRule();
cr.setIncluded(ctj.included);
cr.setGenus(StringUtils.defaultIfBlank(ctj.genus, null));
cr.setSpecies(StringUtils.defaultIfBlank(ctj.species, null));
cropRules.add(cr);
}
cropService.setCropRules(crop, cropRules);
cropService.setCropRules(crop, rules);
final List<CropRule> cropRules2 = cropService.getCropRules(crop);
return OAuth2Cleanup.clean(cropRules2);
return cropService.getCropRules(crop);
}
/**
......@@ -200,19 +193,17 @@ public class CropsController extends RestController {
return OAuth2Cleanup.clean(cropTaxa);
}
public static class CropJson {
@NotBlank
public String shortName;
public String name;
public String description;
public String i18n;
public List<CropRuleJson> rules;
/**
* List all crops
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "/rebuild", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
String rebuild() {
cropService.rebuildTaxonomies();
return JSON_OK;
}
public static class CropRuleJson {
public boolean included;
@NotBlank
public String genus;
public String species;
}
}
/**
* 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.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.writeBooleanField("included", cropRule.isIncluded());
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();
}
}
}
/**
* 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.servlet.controller.rest.serialization;
import java.io.IOException;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.impl.Crop;
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 CropSerializer extends JsonSerializer<Crop> {
@Override
public void serialize(Crop crop, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
if (crop == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("shortName", crop.getShortName());
jgen.writeObjectField("name", crop.getName());
if (StringUtils.isNotBlank(crop.getDescription())) {
jgen.writeObjectField("description", crop.getDescription());
}
if (StringUtils.isNotBlank(crop.getI18n())) {
jgen.writeObjectField("i18n", crop.getI18n());
}
final List<CropRule> croprules = crop.getCropRules();
if (croprules != null) {
jgen.writeArrayFieldStart("rules");
for (final CropRule cropRule : croprules) {
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