Commit f9472926 authored by Richard Bruskiewich's avatar Richard Bruskiewich Committed by Matija Obreza
Browse files

* cleaned out some spurious htmlsanitization

* added "getCategory" calling ParameterCategoryRepository "findByName"
method. Used in TraitsController "createCategory" method to retrieve
existing categories

* added "getParameter" calling ParameterRepository "findByCropAndTitle"
method. Used in TraitsController "createParameter" method to retrieve
existing parameters
parent 22694705
...@@ -21,6 +21,6 @@ import org.springframework.data.jpa.repository.JpaRepository; ...@@ -21,6 +21,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
public interface ParameterCategoryRepository extends JpaRepository<ParameterCategory, Long> { public interface ParameterCategoryRepository extends JpaRepository<ParameterCategory, Long> {
ParameterCategory findParameterCategoryByName(String name); ParameterCategory findByName(String name);
} }
...@@ -33,4 +33,6 @@ public interface ParameterRepository extends JpaRepository<Parameter, Long> { ...@@ -33,4 +33,6 @@ public interface ParameterRepository extends JpaRepository<Parameter, Long> {
Collection<? extends Parameter> findByCropAndCategory(Crop crop, ParameterCategory pc, Sort sort); Collection<? extends Parameter> findByCropAndCategory(Crop crop, ParameterCategory pc, Sort sort);
// List<Parameter> findAllByCategory(Category category); // List<Parameter> findAllByCategory(Category category);
Parameter findByCropAndTitle(Crop crop, String title) ;
} }
...@@ -53,16 +53,36 @@ public interface TraitService { ...@@ -53,16 +53,36 @@ public interface TraitService {
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
ParameterCategory addCategory(String name); ParameterCategory addCategory(String name);
/**
* Get an existing crop descriptor Property Category by name
*
* @param name
* @return
*/
@PreAuthorize("isAuthenticated()")
ParameterCategory getCategory(String name);
/** /**
* Add new crop descriptor Property * Add new crop descriptor Property
* *
* @param crop * @param crop
* @param category
* @param title * @param title
* @return * @return
*/ */
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
Parameter addParameter(Crop crop, String category, String title); Parameter addParameter(Crop crop, String category, String title);
/**
* Get a crop descriptor property by title
*
* @param crop
* @param title
* @return
*/
@PreAuthorize("isAuthenticated()")
Parameter getParameter(Crop crop, String title);
/** /**
* Add new method * Add new method
* *
......
...@@ -552,9 +552,19 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -552,9 +552,19 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
ParameterCategory category = new ParameterCategory(); ParameterCategory category = new ParameterCategory();
category.setName(htmlSanitizer.sanitize(name)); category.setName(name);
parameterCategoryRepository.save(category); parameterCategoryRepository.save(category);
return category;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true)
public ParameterCategory getCategory(String name) {
ParameterCategory category =
parameterCategoryRepository.findByName(name);
return category; return category;
} }
...@@ -568,15 +578,24 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -568,15 +578,24 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
parameter.setCrop(crop); parameter.setCrop(crop);
ParameterCategory parameterCategory = ParameterCategory parameterCategory =
parameterCategoryRepository.findParameterCategoryByName(htmlSanitizer.sanitize(category)); parameterCategoryRepository.findByName(category);
parameter.setCategory(parameterCategory); parameter.setCategory(parameterCategory);
parameter.setTitle(htmlSanitizer.sanitize(title)); parameter.setTitle(title);
parameterRepository.save(parameter); parameterRepository.save(parameter);
return parameter; return parameter;
} }
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true)
public Parameter getParameter(Crop crop, String title) {
Parameter parameter =
parameterRepository.findByCropAndTitle(crop, title) ; // assumes that crop x title is unique(?)
return parameter;
}
@Override @Override
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false) @Transactional(readOnly = false)
......
...@@ -121,7 +121,13 @@ public class TraitsController extends RestController { ...@@ -121,7 +121,13 @@ public class TraitsController extends RestController {
throw new ModelValidationException("Validation problem", violations); throw new ModelValidationException("Validation problem", violations);
} }
ParameterCategory category = traitService.addCategory(categoryJson.name); ParameterCategory category = traitService.getCategory(categoryJson.name);
if(category == null) // create if not available already?
category= traitService.addCategory(categoryJson.name);
else
LOG.warn("Property category "+categoryJson.name+" already exists?");
return category; return category;
} }
...@@ -140,15 +146,15 @@ public class TraitsController extends RestController { ...@@ -140,15 +146,15 @@ public class TraitsController extends RestController {
/** /**
* Add a property by taxon ("crop") * Adds a property by crop
* *
* @return * @return
* @throws ValidationException * @throws ValidationException
*/ */
@RequestMapping(value = "/{taxon}/property", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE }) @RequestMapping(value = "/{crop}/property", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody public @ResponseBody
Object createProperty( Object createProperty(
@PathVariable("taxon") String taxon, @PathVariable("crop") String cropName,
@RequestBody PropertyJson propertyJson @RequestBody PropertyJson propertyJson
) throws ValidationException { ) throws ValidationException {
...@@ -161,13 +167,20 @@ public class TraitsController extends RestController { ...@@ -161,13 +167,20 @@ public class TraitsController extends RestController {
throw new ModelValidationException("Validation problem", violations); throw new ModelValidationException("Validation problem", violations);
} }
Crop crop = cropService.getCrop(taxon) ; Crop crop = cropService.getCrop(cropName) ;
Parameter property = traitService.getParameter(crop, propertyJson.title);
Parameter property = traitService.addParameter(crop, propertyJson.category, propertyJson.title); if(property == null) // create if not available already?
property = traitService.addParameter(crop, propertyJson.category, propertyJson.title);
else
LOG.warn("Property '"+propertyJson.title+"' in category '"+
propertyJson.category+"' for crop '"+
cropName+"' already exists?");
return property; return property;
} }
public static class PropertyJson { public static class PropertyJson {
//@NotNull //@NotNull
...@@ -205,7 +218,11 @@ public class TraitsController extends RestController { ...@@ -205,7 +218,11 @@ public class TraitsController extends RestController {
throw new ModelValidationException("Validation problem", violations); throw new ModelValidationException("Validation problem", violations);
} }
Parameter property = traitService.getTrait(propertyId); Parameter property ;
if(propertyId != 0)
property = traitService.getTrait(propertyId);
else
property = null ;
Method method = traitService.addMethod(methodJson.description, methodJson.unit, methodJson.fieldName, methodJson.fieldType, methodJson.fieldSize, Method method = traitService.addMethod(methodJson.description, methodJson.unit, methodJson.fieldName, methodJson.fieldType, methodJson.fieldSize,
methodJson.options, methodJson.range, property); methodJson.options, methodJson.range, property);
......
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