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

A workable ParameterCategory subsystem is now available, including a new

method, ParameterCategory findParameterCategoryByName(String name); in
the ParameterCategoryRepository interface, and a REST API call to access
the subsystem, in TraitsController.
parent 828f034e
...@@ -21,4 +21,6 @@ import org.springframework.data.jpa.repository.JpaRepository; ...@@ -21,4 +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);
} }
...@@ -52,7 +52,7 @@ public interface TraitService { ...@@ -52,7 +52,7 @@ public interface TraitService {
* @return * @return
*/ */
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
Parameter addParameter(Crop crop, String title); Parameter addParameter(Crop crop, String category, String title);
/** /**
* Add new method * Add new method
......
...@@ -137,6 +137,9 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -137,6 +137,9 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Autowired @Autowired
private MethodRepository methodRepository; private MethodRepository methodRepository;
@Autowired
private ParameterCategoryRepository parameterCategoryRepository;
@Autowired @Autowired
private ParameterRepository parameterRepository; private ParameterRepository parameterRepository;
...@@ -161,9 +164,6 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -161,9 +164,6 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Autowired @Autowired
private AccessionAliasRepository accessionAliasRepository; private AccessionAliasRepository accessionAliasRepository;
@Autowired
private ParameterCategoryRepository parameterCategoryRepository;
@Override @Override
public long countByInstitute(FaoInstitute institute) { public long countByInstitute(FaoInstitute institute) {
return accessionRepository.countByInstitute(institute); return accessionRepository.countByInstitute(institute);
...@@ -548,9 +548,29 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -548,9 +548,29 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override @Override
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false) @Transactional(readOnly = false)
public Parameter addParameter(Crop crop, String title) { public ParameterCategory addCategory(String name) {
ParameterCategory category = new ParameterCategory();
category.setName(htmlSanitizer.sanitize(name));
parameterCategoryRepository.save(category);
return category;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false)
public Parameter addParameter(Crop crop, String category, String title) {
Parameter parameter = new Parameter(); Parameter parameter = new Parameter();
parameter.setCrop(crop); parameter.setCrop(crop);
ParameterCategory parameterCategory =
parameterCategoryRepository.findParameterCategoryByName(htmlSanitizer.sanitize(category));
parameter.setCategory(parameterCategory);
parameter.setTitle(htmlSanitizer.sanitize(title)); parameter.setTitle(htmlSanitizer.sanitize(title));
parameterRepository.save(parameter); parameterRepository.save(parameter);
......
...@@ -30,6 +30,7 @@ import net.sf.oval.constraint.NotNull; ...@@ -30,6 +30,7 @@ import net.sf.oval.constraint.NotNull;
import org.genesys2.server.exception.AuthorizationException; import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.model.genesys.Method; import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.Parameter; import org.genesys2.server.model.genesys.Parameter;
import org.genesys2.server.model.genesys.ParameterCategory;
import org.genesys2.server.model.impl.Crop; import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.service.CropService; import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GenesysService; import org.genesys2.server.service.GenesysService;
...@@ -100,53 +101,46 @@ public class TraitsController extends RestController { ...@@ -100,53 +101,46 @@ public class TraitsController extends RestController {
return OAuth2Cleanup.clean(methods); return OAuth2Cleanup.clean(methods);
} }
/** /**
* Add a crop * Create a new category (ParameterCategory) for a property of a descriptor
* *
* @return * @return
* @throws ValidationException * @throws ValidationException
*/ */
@RequestMapping(value = "/methods", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE }) @RequestMapping(value = "/category", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody public @ResponseBody
Object createMethod(@RequestBody MethodJson methodJson) throws ValidationException { Object createCategory( @RequestBody CategoryJson categoryJson ) throws ValidationException {
LOG.info("Creating method:");
LOG.info("Creating trait property category:");
Validator validator = new Validator(); Validator validator = new Validator();
List<ConstraintViolation> violations = validator.validate(methodJson); List<ConstraintViolation> violations = validator.validate(categoryJson);
if (violations.size() > 0) { if (violations.size() > 0) {
// TODO We could do better messages on validation error // TODO We could do better messages on validation error
throw new ModelValidationException("Validation problem", violations); throw new ModelValidationException("Validation problem", violations);
} }
Method method = traitService.addMethod(methodJson.description, methodJson.unit, methodJson.fieldName, methodJson.fieldType, methodJson.fieldSize,
methodJson.options, methodJson.range, null); ParameterCategory category = traitService.addCategory(categoryJson.name);
return method;
return category;
} }
public static class MethodJson { public static class CategoryJson {
@NotNull
@NotBlank //@NotNull
@MinLength(10) //@NotBlank
public String description; //public String uri;
@NotBlank
public String unit;
@NotNull @NotNull
@NotBlank @NotBlank
@MatchPattern(pattern = { "[a-zA-Z0-9]+" }) public String name;
public String fieldName;
public int fieldType;
public Integer fieldSize;
@NotBlank
public String options;
@NotBlank
public String range;
} }
/** /**
* Add a crop * Add a property by taxon ("crop")
* *
* @return * @return
* @throws ValidationException * @throws ValidationException
...@@ -169,7 +163,7 @@ public class TraitsController extends RestController { ...@@ -169,7 +163,7 @@ public class TraitsController extends RestController {
Crop crop = cropService.getCrop(taxon) ; Crop crop = cropService.getCrop(taxon) ;
Parameter property = traitService.addParameter(crop, propertyJson.title); Parameter property = traitService.addParameter(crop, propertyJson.category, propertyJson.title);
return property; return property;
} }
...@@ -180,9 +174,60 @@ public class TraitsController extends RestController { ...@@ -180,9 +174,60 @@ public class TraitsController extends RestController {
//@NotBlank //@NotBlank
//public String uri; //public String uri;
@NotNull
@NotBlank
public String category;
@NotNull @NotNull
@NotBlank @NotBlank
public String title; public String title;
} }
/**
* Add a crop
*
* @return
* @throws ValidationException
*/
@RequestMapping(value = "/methods", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object createMethod(@RequestBody MethodJson methodJson) throws ValidationException {
LOG.info("Creating method:");
Validator validator = new Validator();
List<ConstraintViolation> violations = validator.validate(methodJson);
if (violations.size() > 0) {
// TODO We could do better messages on validation error
throw new ModelValidationException("Validation problem", violations);
}
Method method = traitService.addMethod(methodJson.description, methodJson.unit, methodJson.fieldName, methodJson.fieldType, methodJson.fieldSize,
methodJson.options, methodJson.range, null);
return method;
}
public static class MethodJson {
@NotNull
@NotBlank
@MinLength(10)
public String description;
@NotBlank
public String unit;
@NotNull
@NotBlank
@MatchPattern(pattern = { "[a-zA-Z0-9]+" })
public String fieldName;
public int fieldType;
public Integer fieldSize;
@NotBlank
public String options;
@NotBlank
public String range;
}
} }
\ No newline at end of file
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