org.openmrs.Obs

Here are the examples of the java api org.openmrs.Obs taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

76 Examples 7

19 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

private void updateStatusIfNecessary(Obs newObs, Obs.Status originalStatus) {
    if (Obs.Status.FINAL.equals(originalStatus)) {
        newObs.setStatus(Obs.Status.AMENDED);
    }
}

18 Source : RequireVoidReasonVoidHandlerTest.java
with Apache License 2.0
from isstac

/**
 * @see RequireVoidReasonVoidHandler#handle(Voidable,User,Date,String)
 */
@Test
public void handle_shouldNotThrowExceptionIfVoidReasonIsNotBlank() {
    Obs o = Context.getObsService().getObs(7);
    Context.getObsService().voidObs(o, "Some Reason");
}

18 Source : RequireVoidReasonVoidHandlerTest.java
with Apache License 2.0
from isstac

/**
 * @see RequireVoidReasonVoidHandler#handle(Voidable,User,Date,String)
 */
@Test(expected = IllegalArgumentException.clreplaced)
public void handle_shouldThrowIllegalArgumentExceptionIfObsVoidReasonIsBlank() {
    Obs o = Context.getObsService().getObs(7);
    Context.getObsService().voidObs(o, "  ");
}

18 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.api.ObsService#getRevisionObs(org.openmrs.Obs)
 */
@Transactional(readOnly = true)
public Obs getRevisionObs(Obs initialObs) {
    return dao.getRevisionObs(initialObs);
}

18 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.api.ObsService#saveObs(org.openmrs.Obs, String)
 */
@Override
public Obs saveObs(Obs obs, String changeMessage) throws APIException {
    if (obs == null) {
        throw new APIException("Obs.error.cannot.be.null", (Object[]) null);
    }
    if (obs.getId() != null && changeMessage == null) {
        throw new APIException("Obs.error.ChangeMessage.required", (Object[]) null);
    }
    handleExistingObsWithComplexConcept(obs);
    ensureRequirePrivilege(obs);
    // Should allow updating a voided Obs, it seems to be pointless to restrict it,
    // otherwise operations like merge patients won't be possible when to moving voided obs
    if (obs.getObsId() == null || obs.getVoided()) {
        return saveNewOrVoidedObs(obs, changeMessage);
    } else if (!obs.isDirty()) {
        setPersonFromEncounter(obs);
        return saveObsNotDirty(obs, changeMessage);
    } else {
        setPersonFromEncounter(obs);
        return saveExistingObs(obs, changeMessage);
    }
}

18 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.api.ObsService#getObs(java.lang.Integer)
 */
@Override
@Transactional(readOnly = true)
public Obs getObs(Integer obsId) throws APIException {
    Obs obs = dao.getObs(obsId);
    if (obs != null && obs.isComplex()) {
        return getHandler(obs).getObs(obs, ComplexObsHandler.RAW_VIEW);
    }
    return obs;
}

18 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.api.ObsService#getComplexObs(Integer, String)
 */
@Override
@Transactional(readOnly = true)
public Obs getComplexObs(Integer obsId, String view) throws APIException {
    Obs obs = dao.getObs(obsId);
    if (obs != null && obs.isComplex()) {
        return getHandler(obs).getObs(obs, view);
    }
    return obs;
}

18 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

private Obs saveNewOrVoidedObs(Obs obs, String changeMessage) {
    Obs ret = dao.saveObs(obs);
    saveObsGroup(ret, changeMessage);
    return ret;
}

18 Source : EncounterServiceImpl.java
with Apache License 2.0
from isstac

/**
 * This method will remove given Collection of obs and their group members from encounter
 *
 * @param obsToRemove Collection of obs that need to be removed recursively
 * @param encounter the encounter from which the obs will be removed
 */
private void removeGivenObsAndTheirGroupMembersFromEncounter(Collection<Obs> obsToRemove, Encounter encounter) {
    for (Obs o : obsToRemove) {
        encounter.removeObs(o);
        Set<Obs> groupMembers = o.getGroupMembers(true);
        if (CollectionUtils.isNotEmpty(groupMembers)) {
            removeGivenObsAndTheirGroupMembersFromEncounter(groupMembers, encounter);
        }
    }
}

18 Source : EncounterServiceImpl.java
with Apache License 2.0
from isstac

/**
 * This method will add given Collection of obs and their group members to encounter
 *
 * @param obsToAdd Collection of obs that need to be added recursively
 * @param encounter the encounter to which the obs will be added
 */
private void addGivenObsAndTheirGroupMembersToEncounter(Collection<Obs> obsToAdd, Encounter encounter) {
    for (Obs o : obsToAdd) {
        encounter.addObs(o);
        Set<Obs> groupMembers = o.getGroupMembers(true);
        if (CollectionUtils.isNotEmpty(groupMembers)) {
            addGivenObsAndTheirGroupMembersToEncounter(groupMembers, encounter);
        }
    }
}

17 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailValidationIfPersonIdIsNull() {
    Obs obs = new Obs();
    obs.setConcept(Context.getConceptService().getConcept(5089));
    obs.setObsDatetime(new Date());
    obs.setValueNumeric(1.0);
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertTrue(errors.hasFieldErrors("person"));
    replacedertFalse(errors.hasFieldErrors("concept"));
    replacedertFalse(errors.hasFieldErrors("obsDatetime"));
    replacedertFalse(errors.hasFieldErrors("valueNumeric"));
}

17 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

/**
 * Internal method to remove ComplexData when an Obs is purged.
 */
protected boolean purgeComplexData(Obs obs) throws APIException {
    if (obs.isComplex()) {
        ComplexObsHandler handler = getHandler(obs);
        if (null != handler) {
            return handler.purgeComplexData(obs);
        }
    }
    return true;
}

17 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.api.ObsService#purgeObs(org.openmrs.Obs)
 */
@Override
public void purgeObs(Obs obs) throws APIException {
    Context.getObsService().purgeObs(obs, false);
}

17 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

/**
 * Voids an Obs If the Obs argument is an obsGroup, all group members will be voided.
 *
 * @see org.openmrs.api.ObsService#voidObs(org.openmrs.Obs, java.lang.String)
 * @param obs the Obs to void
 * @param reason the void reason
 * @throws APIException
 */
@Override
public Obs voidObs(Obs obs, String reason) throws APIException {
    return dao.saveObs(obs);
}

17 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

/**
 * Unvoids an Obs
 * <p>
 * If the Obs argument is an obsGroup, all group members with the same dateVoided will also be
 * unvoided.
 *
 * @see org.openmrs.api.ObsService#unvoidObs(org.openmrs.Obs)
 * @param obs the Obs to unvoid
 * @return the unvoided Obs
 * @throws APIException
 */
@Override
public Obs unvoidObs(Obs obs) throws APIException {
    return Context.getObsService().saveObs(obs, "unvoid obs");
}

17 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

private void handleExistingObsWithComplexConcept(Obs obs) {
    ComplexData complexData = obs.getComplexData();
    Concept concept = obs.getConcept();
    if (null != concept && concept.isComplex() && null != complexData && null != complexData.getData()) {
        // save or update complexData object on this obs
        // this is done before the database save so that the obs.valueComplex
        // can be filled in by the handler.
        ComplexObsHandler handler = getHandler(obs);
        if (null != handler) {
            handler.saveObs(obs);
        } else {
            throw new APIException("unknown.handler", new Object[] { concept });
        }
    }
}

17 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

private void ensureRequirePrivilege(Obs obs) {
    if (obs.getObsId() == null) {
        Context.requirePrivilege(PrivilegeConstants.ADD_OBS);
    } else {
        Context.requirePrivilege(PrivilegeConstants.EDIT_OBS);
    }
}

17 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.api.ObsService#getHandler(org.openmrs.Obs)
 */
@Override
@Transactional(readOnly = true)
public ComplexObsHandler getHandler(Obs obs) throws APIException {
    if (obs.getConcept().isComplex()) {
        // Get the ConceptComplex from the ConceptService then return its
        // handler.
        if (obs.getConcept() == null) {
            throw new APIException("Obs.error.unable.get.handler", new Object[] { obs });
        }
        String handlerString = Context.getConceptService().getConceptComplex(obs.getConcept().getConceptId()).getHandler();
        if (handlerString == null) {
            throw new APIException("Obs.error.unable.get.handler.and.concept", new Object[] { obs, obs.getConcept() });
        }
        return this.getHandler(handlerString);
    }
    return null;
}

17 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

private void setPersonFromEncounter(Obs obs) {
    Encounter encounter = obs.getEncounter();
    if (encounter != null) {
        obs.setPerson(encounter.getPatient());
    }
}

17 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.api.ObsService#getObsByUuid(java.lang.String)
 */
@Override
@Transactional(readOnly = true)
public Obs getObsByUuid(String uuid) throws APIException {
    Obs obsByUuid = dao.getObsByUuid(uuid);
    if (obsByUuid != null && obsByUuid.isComplex()) {
        return getHandler(obsByUuid).getObs(obsByUuid, ComplexObsHandler.RAW_VIEW);
    }
    return obsByUuid;
}

17 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.api.ObsService#purgeObs(org.openmrs.Obs, boolean)
 */
@Override
public void purgeObs(Obs obs, boolean cascade) throws APIException {
    if (!purgeComplexData(obs)) {
        throw new APIException("Obs.error.unable.purge.complex.data", new Object[] { obs });
    }
    if (cascade) {
        throw new APIException("Obs.error.cascading.purge.not.implemented", (Object[]) null);
    // TODO delete any related objects here before deleting the obs
    // obsGroups objects?
    // orders?
    }
    dao.deleteObs(obs);
}

17 Source : HibernateObsDAO.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.api.ObsService#deleteObs(org.openmrs.Obs)
 */
@Override
public void deleteObs(Obs obs) throws DAOException {
    sessionFactory.getCurrentSession().delete(obs);
}

17 Source : HibernateObsDAO.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.api.db.ObsDAO#saveObs(org.openmrs.Obs)
 */
@Override
public Obs saveObs(Obs obs) throws DAOException {
    if (obs.hasGroupMembers() && obs.getObsId() != null) {
        // hibernate has a problem updating child collections
        // if the parent object was already saved so we do it
        // explicitly here
        for (Obs member : obs.getGroupMembers()) {
            if (member.getObsId() == null) {
                saveObs(member);
            }
        }
    }
    sessionFactory.getCurrentSession().saveOrUpdate(obs);
    return obs;
}

16 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailValidationIfConceptIsNull() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setObsDatetime(new Date());
    obs.setValueNumeric(1.0);
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasFieldErrors("person"));
    replacedertTrue(errors.hasFieldErrors("concept"));
    replacedertFalse(errors.hasFieldErrors("obsDatetime"));
    replacedertFalse(errors.hasFieldErrors("valueNumeric"));
}

16 Source : AbstractHandler.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.obs.ComplexObsHandler#purgeComplexData(org.openmrs.Obs)
 */
public boolean purgeComplexData(Obs obs) {
    File file = getComplexDataFile(obs);
    if (!file.exists()) {
        return true;
    } else if (file.delete()) {
        obs.setComplexData(null);
        return true;
    }
    log.warn("Could not delete complex data object for obsId=" + obs.getObsId() + " located at " + file.getAbsolutePath());
    return false;
}

16 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

private void evictObsAndChildren(Obs obs) {
    Context.evictFromSession(obs);
    if (obs.hasGroupMembers()) {
        for (Obs member : obs.getGroupMembers()) {
            evictObsAndChildren(member);
        }
    }
}

16 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

private void saveObsGroup(Obs obs, String changeMessage) {
    if (obs.isObsGrouping()) {
        for (Obs o : obs.getGroupMembers(true)) {
            Context.getObsService().saveObs(o, changeMessage);
        }
    }
}

16 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

private Obs saveExistingObs(Obs obs, String changeMessage) {
    // get a copy of the preplaceded in obs and save it to the
    // database. This allows us to create a new row and new obs_id
    // this method doesn't copy the obs_id
    Obs newObs = Obs.newInstance(obs);
    unsetVoidedAndCreationProperties(newObs, obs);
    Obs.Status originalStatus = dao.getSavedStatus(obs);
    updateStatusIfNecessary(newObs, originalStatus);
    RequiredDataAdvice.recursivelyHandle(SaveHandler.clreplaced, newObs, changeMessage);
    // save the new row to the database with the changes that
    // have been made to it
    dao.saveObs(newObs);
    saveObsGroup(newObs, null);
    voidExistingObs(obs, changeMessage, newObs);
    return newObs;
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailValidationIfValueTextIsGreaterThanTheMaximumLength() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(19));
    obs.setObsDatetime(new Date());
    // Generate 65535+ characters length text.
    StringBuilder valueText = new StringBuilder();
    for (int i = 0; i < 730; i++) {
        valueText.append("This text should not exceed 65535 characters. Below code will generate a text more than 65535");
    }
    obs.setValueText(valueText.toString());
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasFieldErrors("person"));
    replacedertFalse(errors.hasFieldErrors("concept"));
    replacedertFalse(errors.hasFieldErrors("obsDatetime"));
    replacedertTrue(errors.hasFieldErrors("valueText"));
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldRejectAnInvalidConceptAndDrugCombination() {
    Obs obs = new Obs();
    obs.setPerson(new Person(7));
    obs.setObsDatetime(new Date());
    Concept questionConcept = new Concept(100);
    ConceptDatatype dt = new ConceptDatatype(1);
    dt.setUuid(ConceptDatatype.CODED_UUID);
    questionConcept.setDatatype(dt);
    obs.setConcept(questionConcept);
    obs.setValueCoded(new Concept(101));
    Drug drug = new Drug();
    drug.setConcept(new Concept(102));
    obs.setValueDrug(drug);
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertTrue(errors.hasFieldErrors("valueDrug"));
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldPreplacedValidationIfValueTextIsLessThanTheMaximumLength() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(19));
    obs.setObsDatetime(new Date());
    // Generate 2700+ characters length text.
    StringBuilder valueText = new StringBuilder();
    for (int i = 0; i < 30; i++) {
        valueText.append("This text should not exceed 65535 characters. Below code will generate a text Less than 65535");
    }
    obs.setValueText(valueText.toString());
    Errors errors = new BindException(obs, "obs");
    new ObsValidator().validate(obs, errors);
    replacedert.replacedertFalse(errors.hasFieldErrors("person"));
    replacedert.replacedertFalse(errors.hasFieldErrors("concept"));
    replacedert.replacedertFalse(errors.hasFieldErrors("obsDatetime"));
    replacedert.replacedertFalse(errors.hasFieldErrors("valueText"));
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailValidationIfConceptDatatypeIsNumericAndValueNumericIsNull() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(5089));
    obs.setObsDatetime(new Date());
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasFieldErrors("person"));
    replacedertFalse(errors.hasFieldErrors("concept"));
    replacedertFalse(errors.hasFieldErrors("obsDatetime"));
    replacedertTrue(errors.hasFieldErrors("valueNumeric"));
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailValidationIfConceptDatatypeIsCodedAndValueCodedIsNull() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(4));
    obs.setObsDatetime(new Date());
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasFieldErrors("person"));
    replacedertFalse(errors.hasFieldErrors("concept"));
    replacedertFalse(errors.hasFieldErrors("obsDatetime"));
    replacedertTrue(errors.hasFieldErrors("valueCoded"));
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailIfObsHasNoValuesAndNotParent() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(18));
    obs.setObsDatetime(new Date());
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertTrue(errors.getGlobalErrorCount() > 0);
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailValidationIfConceptDatatypeIsTextAndValueTextIsNull() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(19));
    obs.setObsDatetime(new Date());
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertTrue(errors.hasFieldErrors("valueText"));
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailValidationIfConceptDatatypeIsBooleanAndValueBooleanIsNull() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(18));
    obs.setObsDatetime(new Date());
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasFieldErrors("person"));
    replacedertFalse(errors.hasFieldErrors("concept"));
    replacedertFalse(errors.hasFieldErrors("obsDatetime"));
    replacedertTrue(errors.hasFieldErrors("valueBoolean"));
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailValidationIfObsDatetimeIsNull() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(5089));
    obs.setValueNumeric(1.0);
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasFieldErrors("person"));
    replacedertFalse(errors.hasFieldErrors("concept"));
    replacedertTrue(errors.hasFieldErrors("obsDatetime"));
    replacedertFalse(errors.hasFieldErrors("valueNumeric"));
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPreplacedIfAnswerConceptAndConceptOfValueDrugMatch() {
    Obs obs = new Obs();
    obs.setPerson(new Person(7));
    obs.setObsDatetime(new Date());
    Concept questionConcept = new Concept(100);
    ConceptDatatype dt = new ConceptDatatype(1);
    dt.setUuid(ConceptDatatype.CODED_UUID);
    questionConcept.setDatatype(dt);
    obs.setConcept(questionConcept);
    Concept answerConcept = new Concept(101);
    obs.setValueCoded(answerConcept);
    Drug drug = new Drug();
    drug.setConcept(answerConcept);
    obs.setValueDrug(drug);
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasFieldErrors());
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailValidationIfConceptDatatypeIsDateAndValueDatetimeIsNull() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(20));
    obs.setObsDatetime(new Date());
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasFieldErrors("person"));
    replacedertFalse(errors.hasFieldErrors("concept"));
    replacedertFalse(errors.hasFieldErrors("obsDatetime"));
    replacedertTrue(errors.hasFieldErrors("valueDatetime"));
}

15 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldPreplacedValidationIfAllValuesPresent() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(5089));
    obs.setObsDatetime(new Date());
    obs.setValueNumeric(1.0);
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasErrors());
}

15 Source : ObsValidator.java
with Apache License 2.0
from isstac

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if personId is null
 * @should fail validation if obsDatetime is null
 * @should fail validation if concept is null
 * @should fail validation if concept datatype is boolean and valueBoolean is null
 * @should fail validation if concept datatype is coded and valueCoded is null
 * @should fail validation if concept datatype is date and valueDatetime is null
 * @should fail validation if concept datatype is numeric and valueNumeric is null
 * @should fail validation if concept datatype is text and valueText is null
 * @should fail validation if obs ancestors contains obs
 * @should preplaced validation if all values present
 * @should fail validation if the parent obs has values
 * @should reject an invalid concept and drug combination
 * @should preplaced if answer concept and concept of value drug match
 * @should preplaced validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 * @should not validate if obs is voided
 * @should not validate a voided child obs
 * @should fail for a null object
 */
@Override
public void validate(Object obj, Errors errors) {
    Obs obs = (Obs) obj;
    if (obs == null) {
        throw new APIException("Obs can't be null");
    } else if (obs.getVoided()) {
        return;
    }
    List<Obs> ancestors = new ArrayList<>();
    validateHelper(obs, errors, ancestors, true);
    ValidateUtil.validateFieldLengths(errors, obj.getClreplaced(), "accessionNumber", "valueModifier", "valueComplex", "comment", "voidReason");
}

14 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailValidationIfObsAncestorsContainsObs() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    // datatype = N/A
    obs.setConcept(Context.getConceptService().getConcept(3));
    obs.setObsDatetime(new Date());
    Set<Obs> group = new HashSet<>();
    group.add(obs);
    obs.setGroupMembers(group);
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasFieldErrors("person"));
    replacedertFalse(errors.hasFieldErrors("concept"));
    replacedertFalse(errors.hasFieldErrors("obsDatetime"));
    replacedertTrue(errors.hasFieldErrors("groupMembers"));
}

14 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldNotValidateIfObsIsVoided() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(5089));
    obs.setObsDatetime(new Date());
    obs.setValueNumeric(null);
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertTrue(errors.hasFieldErrors("valueNumeric"));
    obs.setVoided(true);
    errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasErrors());
}

14 Source : AbstractHandler.java
with Apache License 2.0
from isstac

/**
 * Convenience method to create and return a file for the stored ComplexData.data Object
 *
 * @param obs
 * @return File object
 */
public static File getComplexDataFile(Obs obs) {
    String[] names = obs.getValueComplex().split("\\|");
    String filename = names.length < 2 ? names[0] : names[names.length - 1];
    File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
    return new File(dir, filename);
}

14 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

private Obs saveObsNotDirty(Obs obs, String changeMessage) {
    if (!obs.isObsGrouping()) {
        return obs;
    }
    ObsService os = Context.getObsService();
    boolean refreshNeeded = false;
    for (Obs o : obs.getGroupMembers(true)) {
        if (o.getId() == null) {
            os.saveObs(o, null);
        } else {
            Obs newObs = os.saveObs(o, changeMessage);
            refreshNeeded = !newObs.equals(o) || refreshNeeded;
        }
    }
    if (refreshNeeded) {
        Context.refreshEnreplacedy(obs);
    }
    return obs;
}

14 Source : ObsServiceImpl.java
with Apache License 2.0
from isstac

private void unsetVoidedAndCreationProperties(Obs newObs, Obs obs) {
    newObs.setVoided(false);
    newObs.setVoidReason(null);
    newObs.setDateVoided(null);
    newObs.setVoidedBy(null);
    newObs.setCreator(null);
    newObs.setDateCreated(null);
    newObs.setPreviousVersion(obs);
}

14 Source : HibernateObsDAO.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.api.db.ObsDAO#getRevisionObs(org.openmrs.Obs)
 */
@Override
public Obs getRevisionObs(Obs initialObs) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Obs.clreplaced, "obs");
    criteria.add(Restrictions.eq("previousVersion", initialObs));
    return (Obs) criteria.uniqueResult();
}

13 Source : ObsValidatorTest.java
with Apache License 2.0
from isstac

/**
 * @see ObsValidator#validate(Object, Errors)
 */
@Test
public void validate_shouldNotValidateAVoidedChildObs() {
    Obs obs = new Obs();
    obs.setPerson(Context.getPersonService().getPerson(2));
    obs.setConcept(Context.getConceptService().getConcept(5089));
    obs.setObsDatetime(new Date());
    Obs validChild = new Obs();
    validChild.setPerson(Context.getPersonService().getPerson(2));
    validChild.setConcept(Context.getConceptService().getConcept(5089));
    validChild.setObsDatetime(new Date());
    validChild.setValueNumeric(80.0);
    obs.addGroupMember(validChild);
    Obs inValidChild = new Obs();
    obs.addGroupMember(inValidChild);
    Errors errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertTrue(errors.hasErrors());
    inValidChild.setVoided(true);
    errors = new BindException(obs, "obs");
    obsValidator.validate(obs, errors);
    replacedertFalse(errors.hasErrors());
}

13 Source : AbstractHandler.java
with Apache License 2.0
from isstac

/**
 * @see org.openmrs.obs.ComplexObsHandler#getObs(Obs, String)
 */
public Obs getObs(Obs obs, String view) {
    File file = BinaryDataHandler.getComplexDataFile(obs);
    log.debug("value complex: " + obs.getValueComplex());
    log.debug("file path: " + file.getAbsolutePath());
    ComplexData complexData = null;
    try {
        complexData = new ComplexData(file.getName(), OpenmrsUtil.getFileAsBytes(file));
    } catch (IOException e) {
        log.error("Trying to read file: " + file.getAbsolutePath(), e);
    }
    String mimeType = OpenmrsUtil.getFileMimeType(file);
    complexData.setMimeType(mimeType);
    obs.setComplexData(complexData);
    return obs;
}

13 Source : AbstractHandler.java
with Apache License 2.0
from isstac

/**
 * Returns a {@link File} for the given obs complex data to be written to. The output file
 * location is determined off of the {@link OpenmrsConstants#GLOBAL_PROPERTY_COMPLEX_OBS_DIR}
 * and the file name is determined off the current obs.getComplexData().getreplacedle().
 *
 * @param obs the Obs with a non-null complex data on it
 * @return File that the complex data should be written to
 */
public File getOutputFileToWrite(Obs obs) throws IOException {
    // Get the replacedle and remove the extension.
    String replacedle = obs.getComplexData().getreplacedle();
    String extension = "." + getExtension(replacedle);
    // If getExtension returns the replacedle, there was no extension
    if (getExtension(replacedle).equals(replacedle)) {
        extension = "";
    }
    File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
    File outputfile;
    // Get the output stream
    if (null == replacedle) {
        String now = longfmt.format(new Date());
        outputfile = new File(dir, now);
    } else {
        replacedle = replacedle.replace(extension, "");
        outputfile = new File(dir, replacedle + extension);
    }
    int i = 0;
    String tmp;
    // If the Obs does not exist, but the File does, append a two-digit
    // count number to the filename and save it.
    while (obs.getObsId() == null && outputfile.exists() && i < 100) {
        // Remove the extension from the filename.
        tmp = String.valueOf(outputfile.getAbsolutePath().replace(extension, ""));
        // Append two-digit count number to the filename.
        String filename = (i < 1) ? tmp + "_" + nf.format(Integer.valueOf(++i)) : tmp.replace(nf.format(Integer.valueOf(i)), nf.format(Integer.valueOf(++i)));
        // Append the extension to the filename
        outputfile = new File(filename + extension);
    }
    return outputfile;
}

See More Examples