use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class ImplementationIdValidator method validate.
/*
* @should should fail validation if implementation id is null
* @should should fail validation if description is null
* @should should fail validation if pass phrase is null
* @should should fail if given empty implementationId object
* @should should fail if given a pipe in the implementationId code
*
*/
@Override
public void validate(Object obj, Errors errors) throws APIException {
ImplementationId implId = (ImplementationId) obj;
char[] illegalChars = { '^', '|' };
if (implId == null) {
throw new APIException("ImplementationId.null", (Object[]) null);
} else {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "ImplementationId.name.empty");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "implementationId", "ImplementationId.implementationId.empty");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passphrase", "ImplementationId.passphrase.empty");
if (implId.getImplementationId() != null && StringUtils.containsAny(implId.getImplementationId(), illegalChars)) {
errors.rejectValue("implementationId", "ImplementationId.implementationId.invalidCharacter");
}
}
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class ConceptReferenceTermValidator method validate.
/**
* Checks that a given concept reference term object is valid.
*
* @see org.springframework.validation.Validator#validate(java.lang.Object,
* org.springframework.validation.Errors)
* @should fail if the concept reference term object is null
* @should fail if the name is a white space character
* @should fail if the code is null
* @should fail if the code is an empty string
* @should fail if the code is a white space character
* @should fail if the concept reference term code is a duplicate in its concept source
* @should fail if the concept source is null
* @should pass if all the required fields are set and valid
* @should pass if the duplicate name is for a term from another concept source
* @should pass if the duplicate code is for a term from another concept source
* @should fail if a concept reference term map has no concept map type
* @should fail if termB of a concept reference term map is not set
* @should fail if a term is mapped to itself
* @should fail if a term is mapped multiple times to the same term
* @should pass validation if field lengths are correct
* @should fail validation if field lengths are not correct
*/
@Override
public void validate(Object obj, Errors errors) throws APIException {
if (obj == null || !(obj instanceof ConceptReferenceTerm)) {
throw new IllegalArgumentException("The parameter obj should not be null and must be of type" + ConceptReferenceTerm.class);
}
ConceptReferenceTerm conceptReferenceTerm = (ConceptReferenceTerm) obj;
String code = conceptReferenceTerm.getCode();
boolean hasBlankFields = false;
if (!StringUtils.hasText(code)) {
errors.rejectValue("code", "ConceptReferenceTerm.error.codeRequired", "The code property is required for a concept reference term");
hasBlankFields = true;
}
if (conceptReferenceTerm.getConceptSource() == null) {
errors.rejectValue("conceptSource", "ConceptReferenceTerm.error.sourceRequired", "The conceptSource property is required for a concept reference term");
hasBlankFields = true;
}
if (hasBlankFields) {
return;
}
code = code.trim();
// Ensure that there are no terms with the same code in the same source
ConceptReferenceTerm termWithDuplicateCode = Context.getConceptService().getConceptReferenceTermByCode(code, conceptReferenceTerm.getConceptSource());
if (termWithDuplicateCode != null && !OpenmrsUtil.nullSafeEquals(termWithDuplicateCode.getUuid(), conceptReferenceTerm.getUuid())) {
errors.rejectValue("code", "ConceptReferenceTerm.duplicate.code", "Duplicate concept reference term code in its concept source: " + code);
}
// validate the concept reference term maps
if (CollectionUtils.isNotEmpty(conceptReferenceTerm.getConceptReferenceTermMaps())) {
int index = 0;
Set<String> mappedTermUuids = null;
for (ConceptReferenceTermMap map : conceptReferenceTerm.getConceptReferenceTermMaps()) {
if (map == null) {
throw new APIException("ConceptReferenceTerm.add.null", (Object[]) null);
}
if (map.getConceptMapType() == null) {
errors.rejectValue("conceptReferenceTermMaps[" + index + "].conceptMapType", "ConceptReferenceTerm.error.mapTypeRequired", "Concept Map Type is required");
} else if (map.getTermB() == null) {
errors.rejectValue("conceptReferenceTermMaps[" + index + "].termB", "ConceptReferenceTerm.error.termBRequired", "Mapped Term is required");
} else if (map.getTermB().equals(conceptReferenceTerm)) {
errors.rejectValue("conceptReferenceTermMaps[" + index + "].termB", "ConceptReferenceTerm.map.sameTerm", "Cannot map a concept reference term to itself");
}
// don't proceed to the next map
if (errors.hasErrors()) {
return;
}
if (mappedTermUuids == null) {
mappedTermUuids = new HashSet<>();
}
// if we already have a mapping to this term, reject it this map
if (!mappedTermUuids.add(map.getTermB().getUuid())) {
errors.rejectValue("conceptReferenceTermMaps[" + index + "].termB", "ConceptReferenceTerm.termToTerm.alreadyMapped", "Cannot map a reference term multiple times to the same concept reference term");
}
index++;
}
}
ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "code", "version", "description", "retireReason");
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class DiagnosisValidator method validate.
/**
* @see org.springframework.validation.Validator#validate(java.lang.Object,
* org.springframework.validation.Errors)
* @should fail validation if rank is null or a non-positive integer
* @should fail validation if certainty is null
* @should fail validation if diagnosis is null
* @should fail validation if encounter is null
*/
@Override
public void validate(Object o, Errors errors) {
Diagnosis diagnosis = (Diagnosis) o;
if (diagnosis == null) {
throw new APIException("Diagnosis can't be null");
} else if (diagnosis.getVoided()) {
return;
}
if (diagnosis.getEncounter() == null) {
errors.rejectValue("encounter", "error.null");
}
if (diagnosis.getDiagnosis() == null) {
errors.rejectValue("diagnosis", "error.null");
}
if (diagnosis.getCertainty() == null) {
errors.rejectValue("certainty", "error.null");
}
Integer rank = diagnosis.getRank();
if (rank == null) {
errors.rejectValue("rank", "error.null");
} else if (rank < 0) {
errors.rejectValue("rank", "error.rank.notPositiveInteger");
}
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class LocationServiceImpl method retireLocationTag.
/**
* @see org.openmrs.api.LocationService#retireLocationTag(LocationTag, String)
*/
@Override
public LocationTag retireLocationTag(LocationTag tag, String reason) throws APIException {
if (tag.getRetired()) {
return tag;
} else {
if (reason == null) {
throw new APIException("Location.retired.reason.required", (Object[]) null);
}
tag.setRetired(true);
tag.setRetireReason(reason);
tag.setRetiredBy(Context.getAuthenticatedUser());
tag.setDateRetired(new Date());
return Context.getLocationService().saveLocationTag(tag);
}
}
use of org.openmrs.api.APIException in project openmrs-core by openmrs.
the class LocationServiceImpl method saveLocation.
/**
* @see org.openmrs.api.LocationService#saveLocation(org.openmrs.Location)
*/
@Override
public Location saveLocation(Location location) throws APIException {
if (location.getName() == null) {
throw new APIException("Location.name.required", (Object[]) null);
}
// Check for transient tags. If found, try to match by name and overwrite, otherwise throw exception.
if (location.getTags() != null) {
for (LocationTag tag : location.getTags()) {
// only check transient (aka non-precreated) location tags
if (tag.getLocationTagId() == null) {
if (!StringUtils.hasLength(tag.getName())) {
throw new APIException("Location.tag.name.required", (Object[]) null);
}
LocationTag existing = Context.getLocationService().getLocationTagByName(tag.getName());
if (existing != null) {
location.removeTag(tag);
location.addTag(existing);
} else {
throw new APIException("Location.cannot.add.transient.tags", (Object[]) null);
}
}
}
}
CustomDatatypeUtil.saveAttributesIfNecessary(location);
return dao.saveLocation(location);
}
Aggregations