-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
107 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 43 additions & 1 deletion
44
src/main/java/ca/gc/aafc/seqdb/api/validation/SeqBatchValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,44 @@ | ||
package ca.gc.aafc.seqdb.api.validation;public class SeqBatchValidator { | ||
package ca.gc.aafc.seqdb.api.validation; | ||
|
||
import ca.gc.aafc.seqdb.api.entities.SeqBatch; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.context.i18n.LocaleContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.validation.Errors; | ||
import org.springframework.validation.Validator; | ||
|
||
@Component | ||
public class SeqBatchValidator implements Validator { | ||
|
||
private static final String ERROR_MESSAGE_KEY = "validation.constraint.violation.storageUnitOrType"; | ||
|
||
private final MessageSource messageSource; | ||
|
||
public SeqBatchValidator(MessageSource messageSource) { | ||
this.messageSource = messageSource; | ||
} | ||
|
||
@Override | ||
public boolean supports(Class<?> clazz) { | ||
return SeqBatch.class.isAssignableFrom(clazz); | ||
} | ||
|
||
@Override | ||
public void validate(Object target, Errors errors) { | ||
if (!supports(target.getClass())) { | ||
throw new IllegalArgumentException("SeqBatchValidator not supported for class " + target.getClass()); | ||
} | ||
checkStorageOrTypeNotBoth(errors, (SeqBatch) target); | ||
} | ||
|
||
private void checkStorageOrTypeNotBoth(Errors errors, SeqBatch seqBatch) { | ||
if (seqBatch.getStorageUnit() != null && seqBatch.getStorageUnitType() != null) { | ||
String errorMessage = getMessage(ERROR_MESSAGE_KEY); | ||
errors.rejectValue("storageUnit", ERROR_MESSAGE_KEY, errorMessage); | ||
} | ||
} | ||
|
||
private String getMessage(String key) { | ||
return messageSource.getMessage(key, null, LocaleContextHolder.getLocale()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters