# HG changeset patch # User Raimund Renkert # Date 1370004474 -7200 # Node ID 9e34b7e872f9649b9446e674e6139d496e6fcfb6 # Parent 530a941dbf79c87d270040467db16d2f28cb65ea Documentation and minor code cosmetics/renamings. diff -r 530a941dbf79 -r 9e34b7e872f9 src/main/java/de/intevation/lada/data/LProbeRepository.java --- a/src/main/java/de/intevation/lada/data/LProbeRepository.java Fri May 31 14:08:25 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/LProbeRepository.java Fri May 31 14:47:54 2013 +0200 @@ -90,14 +90,19 @@ return em.createQuery(criteria).getResultList(); } - public LProbe details(String probeId) { - return em.find(LProbe.class, probeId); - } - + /** + * Validate and persist a new LProbe object. + * + * @param probe The new LProbe object + * @return True on success, else returns false. + */ public boolean create(LProbe probe) { + // Make sure that no old errors and warnings remain. setGeneralError(200); setErrors(new HashMap()); setWarnings(new HashMap()); + + // Try to save the new LProbe. try { manager.create(probe); setWarnings(manager.getWarnings()); @@ -120,26 +125,56 @@ return false; } + /** + * Getter for the error code returned by the validator. + * + * @return The error code returned by the validator. + */ public int getGeneralError() { return generalError; } + /** + * Private setter for the general error code. + * + * @param generalError + */ private void setGeneralError(int generalError) { this.generalError = generalError; } + /** + * Getter for all errors occured while validating a LProbe object. + * + * @return Map of field - error code pairs. + */ public Map getErrors() { return errors; } + /** + * Private setter for validation errors. + * + * @param errors + */ private void setErrors(Map errors) { this.errors = errors; } + /** + * Getter for all warnings occured while validating a LProbe object. + * + * @return Map of field - error code pairs. + */ public Map getWarnings() { return warnings; } + /** + * Private setter for validation warnings. + * + * @param warnings + */ private void setWarnings(Map warnings) { this.warnings = warnings; } diff -r 530a941dbf79 -r 9e34b7e872f9 src/main/java/de/intevation/lada/manage/LProbeManager.java --- a/src/main/java/de/intevation/lada/manage/LProbeManager.java Fri May 31 14:08:25 2013 +0200 +++ b/src/main/java/de/intevation/lada/manage/LProbeManager.java Fri May 31 14:47:54 2013 +0200 @@ -16,6 +16,11 @@ import de.intevation.lada.validation.ValidationException; import de.intevation.lada.validation.Validator; +/** + * This Manager provides databse operations for LProbe objects. + * + * @author Raimund Renkert + */ @Stateless public class LProbeManager { @@ -42,6 +47,16 @@ em.remove(probe); } + /** + * Persist a new LProbe object in the database using the LProbeValidator. + * + * @param probe The new LProbe object. + * + * @throws EntityExistsException + * @throws IllegalArgumentException + * @throws TransactionRequiredException + * @throws ValidationException + */ @TransactionAttribute(TransactionAttributeType.REQUIRED) public void create(LProbe probe) throws EntityExistsException, @@ -52,6 +67,11 @@ em.persist(probe); } + /** + * Get the warnings found while validating a LProbe object. + * + * @return Map of warnings containing field - warn code pair. + */ public Map getWarnings() { return validator.getWarnings(); } diff -r 530a941dbf79 -r 9e34b7e872f9 src/main/java/de/intevation/lada/rest/LProbeService.java --- a/src/main/java/de/intevation/lada/rest/LProbeService.java Fri May 31 14:08:25 2013 +0200 +++ b/src/main/java/de/intevation/lada/rest/LProbeService.java Fri May 31 14:47:54 2013 +0200 @@ -49,8 +49,8 @@ @GET @Path("/{id}") @Produces("text/json") - public LProbe details(@PathParam("id") String id) { - return repository.details(id); + public LProbe findById(@PathParam("id") String id) { + return repository.findById(LProbe.class, id); } /** diff -r 530a941dbf79 -r 9e34b7e872f9 src/main/java/de/intevation/lada/validation/LProbeValidator.java --- a/src/main/java/de/intevation/lada/validation/LProbeValidator.java Fri May 31 14:08:25 2013 +0200 +++ b/src/main/java/de/intevation/lada/validation/LProbeValidator.java Fri May 31 14:47:54 2013 +0200 @@ -5,30 +5,64 @@ import javax.inject.Named; +import de.intevation.lada.model.LProbe; + +/** + * Validator for LProbe objects. + * + * @author Raimund Renkert + */ @Named("lprobevalidator") public class LProbeValidator implements Validator { + /** + * Warnings found while validating the LProbe + */ private Map warnings; + /** + * Validate a LProbe object. + * + * @param probe The LProbe object. + */ @Override public void validate(Object probe) throws ValidationException { warnings = new HashMap(); + if (!(probe instanceof LProbe)) { + Map errors = new HashMap(); + errors.put("lprobe", 610); + throw new ValidationException(errors); + } + // Dummy warning. warnings.put("entnahmeort", 612); validateId(probe); } + /** + * Validate the LProbe id. + * + * @param probe The LProbe object. + * @throws ValidationException + */ private void validateId(Object probe) throws ValidationException{ Map errors = new HashMap(); // TODO Implement me! + + // Dummy error. errors.put("probeid", 611); ValidationException ve = new ValidationException(errors); throw ve; } + /** + * Getter for warnings occured while validating the LProbe. + * + * @return Map of field - warning code pairs. + */ @Override public Map getWarnings() { return warnings; diff -r 530a941dbf79 -r 9e34b7e872f9 src/main/java/de/intevation/lada/validation/ValidationException.java --- a/src/main/java/de/intevation/lada/validation/ValidationException.java Fri May 31 14:08:25 2013 +0200 +++ b/src/main/java/de/intevation/lada/validation/ValidationException.java Fri May 31 14:47:54 2013 +0200 @@ -2,20 +2,40 @@ import java.util.Map; - +/** + * Exception used for validation errors. + * + * @author Raimund Renkert + */ public class ValidationException extends Exception { + /** + * Errors saved in this exception. + */ private Map errors; - @SuppressWarnings("unused") + /** + * Do not allow an empty ValidationException object. + */ private ValidationException() { } + /** + * Create a new instance with errors. + * + * @param errors + */ public ValidationException(Map errors) { + this(); this.errors = errors; } + /** + * Getter for the errors. + * + * @return + */ public Map getErrors() { return errors; } diff -r 530a941dbf79 -r 9e34b7e872f9 src/main/java/de/intevation/lada/validation/Validator.java --- a/src/main/java/de/intevation/lada/validation/Validator.java Fri May 31 14:08:25 2013 +0200 +++ b/src/main/java/de/intevation/lada/validation/Validator.java Fri May 31 14:47:54 2013 +0200 @@ -2,7 +2,11 @@ import java.util.Map; - +/** + * Validator interface. Implement this interface for lada object validations. + * + * @author Raimund Renkert + */ public interface Validator { public void validate(Object object) throws ValidationException;