Search in sources :

Example 56 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class ConceptServiceImpl method mapConceptProposalToConcept.

/**
 * @see org.openmrs.api.ConceptService#mapConceptProposalToConcept(ConceptProposal, Concept, Locale)
 */
@Override
public Concept mapConceptProposalToConcept(ConceptProposal cp, Concept mappedConcept, Locale locale) throws APIException {
    if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_REJECT)) {
        cp.rejectConceptProposal();
        Context.getConceptService().saveConceptProposal(cp);
        return null;
    }
    if (mappedConcept == null) {
        throw new APIException("Concept.mapped.illegal", (Object[]) null);
    }
    ConceptName conceptName = null;
    if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_CONCEPT) || !StringUtils.hasText(cp.getFinalText())) {
        cp.setState(OpenmrsConstants.CONCEPT_PROPOSAL_CONCEPT);
        cp.setFinalText("");
    } else if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_SYNONYM)) {
        checkIfLocked();
        String finalText = cp.getFinalText();
        conceptName = new ConceptName(finalText, null);
        conceptName.setConcept(mappedConcept);
        conceptName.setLocale(locale == null ? Context.getLocale() : locale);
        conceptName.setDateCreated(new Date());
        conceptName.setCreator(Context.getAuthenticatedUser());
        // If this is pre 1.9
        if (conceptName.getUuid() == null) {
            conceptName.setUuid(UUID.randomUUID().toString());
        }
        mappedConcept.addName(conceptName);
        mappedConcept.setChangedBy(Context.getAuthenticatedUser());
        mappedConcept.setDateChanged(new Date());
        ValidateUtil.validate(mappedConcept);
        Context.getConceptService().saveConcept(mappedConcept);
    }
    cp.setMappedConcept(mappedConcept);
    if (cp.getObsConcept() != null) {
        Obs ob = new Obs();
        ob.setEncounter(cp.getEncounter());
        ob.setConcept(cp.getObsConcept());
        ob.setValueCoded(cp.getMappedConcept());
        if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_SYNONYM)) {
            ob.setValueCodedName(conceptName);
        }
        ob.setCreator(Context.getAuthenticatedUser());
        ob.setDateCreated(new Date());
        ob.setObsDatetime(cp.getEncounter().getEncounterDatetime());
        ob.setLocation(cp.getEncounter().getLocation());
        ob.setPerson(cp.getEncounter().getPatient());
        if (ob.getUuid() == null) {
            ob.setUuid(UUID.randomUUID().toString());
        }
        Context.getObsService().saveObs(ob, null);
        cp.setObs(ob);
    }
    return mappedConcept;
}
Also used : Obs(org.openmrs.Obs) APIException(org.openmrs.api.APIException) ConceptName(org.openmrs.ConceptName) Date(java.util.Date)

Example 57 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class AdministrationServiceImpl method setImplementationId.

/**
 * @see org.openmrs.api.AdministrationService#setImplementationId(org.openmrs.ImplementationId)
 */
@Override
public void setImplementationId(ImplementationId implementationId) throws APIException {
    if (implementationId == null) {
        return;
    }
    // check the validity of this implementation id with the server
    String description = implementationId.getDescription();
    try {
        // check that source id is valid
        description = checkImplementationIdValidity(implementationId.getImplementationId(), description, implementationId.getPassphrase());
        // save the server's description back to this concept source object
        implementationId.setDescription(description);
        boolean foundMatchingSource = false;
        // loop over the concept sources to make sure one exists for this hl7Code/implementationId
        List<ConceptSource> sources = Context.getConceptService().getAllConceptSources(false);
        if (sources != null) {
            for (ConceptSource source : sources) {
                if (implementationId.getImplementationId().equals(source.getHl7Code())) {
                    foundMatchingSource = true;
                }
            }
        }
        // as a new ConceptSource
        if (!foundMatchingSource) {
            ConceptSource newConceptSource = new ConceptSource();
            newConceptSource.setName(implementationId.getName());
            newConceptSource.setDescription(implementationId.getDescription());
            newConceptSource.setHl7Code(implementationId.getImplementationId());
            if (Context.getAuthenticatedUser() == null) {
                // (hackish)
                // fake the user because no one is logged in
                newConceptSource.setCreator(new User(1));
            }
            Context.getConceptService().saveConceptSource(newConceptSource);
        }
        // serialize and save the ImplementationId to the global properties table
        String value = Context.getSerializationService().getDefaultSerializer().serialize(implementationId);
        Context.getAdministrationService().saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_IMPLEMENTATION_ID, value));
    } catch (APIException e) {
        throw e;
    } catch (Exception e) {
        // pass any other exceptions on up the train
        throw new APIException(e);
    } finally {
    // save an empty concept source to the database when something fails?
    }
}
Also used : User(org.openmrs.User) APIException(org.openmrs.api.APIException) ConceptSource(org.openmrs.ConceptSource) APIException(org.openmrs.api.APIException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) GlobalProperty(org.openmrs.GlobalProperty)

Example 58 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class CohortServiceImpl method notifyPatientUnvoided.

/**
 * @see org.openmrs.api.CohortService#notifyPatientUnvoided(Patient, User, Date)
 */
@Override
public void notifyPatientUnvoided(Patient patient, User originallyVoidedBy, Date originalDateVoided) throws APIException {
    List<CohortMembership> memberships = getCohortMemberships(patient.getPatientId(), null, true);
    List<CohortMembership> toUnvoid = memberships.stream().filter(m -> m.getVoided() && m.getVoidedBy().equals(originallyVoidedBy) && OpenmrsUtil.compare(truncateToSeconds(m.getDateVoided()), truncateToSeconds(originalDateVoided)) == 0).collect(Collectors.toList());
    for (CohortMembership member : toUnvoid) {
        member.setVoided(false);
        member.setDateVoided(null);
        member.setVoidedBy(null);
        member.setVoidReason(null);
        dao.saveCohortMembership(member);
    }
}
Also used : OpenmrsUtil(org.openmrs.util.OpenmrsUtil) Logger(org.slf4j.Logger) APIException(org.openmrs.api.APIException) Date(java.util.Date) Cohort(org.openmrs.Cohort) LoggerFactory(org.slf4j.LoggerFactory) PrivilegeConstants(org.openmrs.util.PrivilegeConstants) Collectors(java.util.stream.Collectors) CohortMembership(org.openmrs.CohortMembership) CohortService(org.openmrs.api.CohortService) List(java.util.List) DateUtil.truncateToSeconds(org.openmrs.util.DateUtil.truncateToSeconds) User(org.openmrs.User) CohortDAO(org.openmrs.api.db.CohortDAO) Patient(org.openmrs.Patient) Context(org.openmrs.api.context.Context) Transactional(org.springframework.transaction.annotation.Transactional) CohortMembership(org.openmrs.CohortMembership)

Example 59 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class ObsServiceImpl method registerHandler.

/**
 * @see org.openmrs.api.ObsService#registerHandler(String, String)
 */
@Override
public void registerHandler(String key, String handlerClass) throws APIException {
    try {
        Class<?> loadedClass = OpenmrsClassLoader.getInstance().loadClass(handlerClass);
        registerHandler(key, (ComplexObsHandler) loadedClass.newInstance());
    } catch (Exception e) {
        throw new APIException("unable.load.and.instantiate.handler", null, e);
    }
}
Also used : APIException(org.openmrs.api.APIException) APIException(org.openmrs.api.APIException)

Example 60 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class UserServiceImpl method saveUserProperties.

@Override
public User saveUserProperties(Map<String, String> properties) {
    User user = Context.getAuthenticatedUser();
    if (user == null) {
        throw new APIException("no.authenticated.user.found", (Object[]) null);
    }
    user.getUserProperties().clear();
    for (Map.Entry<String, String> entry : properties.entrySet()) {
        user.setUserProperty(entry.getKey(), entry.getValue());
    }
    return dao.saveUser(user, null);
}
Also used : User(org.openmrs.User) APIException(org.openmrs.api.APIException) Map(java.util.Map)

Aggregations

APIException (org.openmrs.api.APIException)86 Date (java.util.Date)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 File (java.io.File)9 Obs (org.openmrs.Obs)7 List (java.util.List)6 Map (java.util.Map)6 User (org.openmrs.User)6 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5 Concept (org.openmrs.Concept)5 OpenmrsObject (org.openmrs.OpenmrsObject)5 InputStream (java.io.InputStream)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Order (org.openmrs.Order)4 FileNotFoundException (java.io.FileNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 MessageDigest (java.security.MessageDigest)3 HashMap (java.util.HashMap)3