Search in sources :

Example 21 with AdministrationService

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

the class Context method checkCoreDataset.

/**
 * Runs through the core data (e.g. privileges, roles, and global properties) and adds them if
 * necessary.
 */
public static void checkCoreDataset() {
    // setting core roles
    try {
        Context.addProxyPrivilege(PrivilegeConstants.MANAGE_ROLES);
        Set<String> currentRoleNames = new HashSet<>();
        for (Role role : Context.getUserService().getAllRoles()) {
            currentRoleNames.add(role.getRole().toUpperCase());
        }
        Map<String, String> map = OpenmrsUtil.getCoreRoles();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String roleName = entry.getKey();
            if (!currentRoleNames.contains(roleName.toUpperCase())) {
                Role role = new Role();
                role.setRole(roleName);
                role.setDescription(entry.getValue());
                Context.getUserService().saveRole(role);
            }
        }
    } catch (Exception e) {
        log.error("Error while setting core roles for openmrs system", e);
    } finally {
        Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_ROLES);
    }
    // setting core privileges
    try {
        Context.addProxyPrivilege(PrivilegeConstants.MANAGE_PRIVILEGES);
        Set<String> currentPrivilegeNames = new HashSet<>();
        for (Privilege privilege : Context.getUserService().getAllPrivileges()) {
            currentPrivilegeNames.add(privilege.getPrivilege().toUpperCase());
        }
        Map<String, String> map = OpenmrsUtil.getCorePrivileges();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String privilegeName = entry.getKey();
            if (!currentPrivilegeNames.contains(privilegeName.toUpperCase())) {
                Privilege p = new Privilege();
                p.setPrivilege(privilegeName);
                p.setDescription(entry.getValue());
                Context.getUserService().savePrivilege(p);
            }
        }
    } catch (Exception e) {
        log.error("Error while setting core privileges", e);
    } finally {
        Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_PRIVILEGES);
    }
    // setting core global properties
    try {
        Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
        Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
        Set<String> currentPropNames = new HashSet<>();
        Map<String, GlobalProperty> propsMissingDescription = new HashMap<>();
        Map<String, GlobalProperty> propsMissingDatatype = new HashMap<>();
        for (GlobalProperty prop : Context.getAdministrationService().getAllGlobalProperties()) {
            currentPropNames.add(prop.getProperty().toUpperCase());
            if (prop.getDescription() == null) {
                propsMissingDescription.put(prop.getProperty().toUpperCase(), prop);
            }
            if (prop.getDatatypeClassname() == null) {
                propsMissingDatatype.put(prop.getProperty().toUpperCase(), prop);
            }
        }
        for (GlobalProperty coreProp : OpenmrsConstants.CORE_GLOBAL_PROPERTIES()) {
            String corePropName = coreProp.getProperty().toUpperCase();
            // if the prop doesn't exist, save it
            if (!currentPropNames.contains(corePropName)) {
                Context.getAdministrationService().saveGlobalProperty(coreProp);
                // add to list in case
                currentPropNames.add(corePropName);
            // of duplicates
            } else {
                // if the prop is missing its description, update it
                GlobalProperty propToUpdate = propsMissingDescription.get(corePropName);
                if (propToUpdate != null) {
                    propToUpdate.setDescription(coreProp.getDescription());
                    Context.getAdministrationService().saveGlobalProperty(propToUpdate);
                }
                // set missing datatypes
                propToUpdate = propsMissingDatatype.get(corePropName);
                if (propToUpdate != null && coreProp.getDatatypeClassname() != null) {
                    propToUpdate.setDatatypeClassname(coreProp.getDatatypeClassname());
                    propToUpdate.setDatatypeConfig(coreProp.getDatatypeConfig());
                    propToUpdate.setPreferredHandlerClassname(coreProp.getPreferredHandlerClassname());
                    propToUpdate.setHandlerConfig(coreProp.getHandlerConfig());
                    Context.getAdministrationService().saveGlobalProperty(propToUpdate);
                }
            }
        }
    } catch (Exception e) {
        log.error("Error while setting core global properties", e);
    } finally {
        Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
        Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
    }
    // setting default validation rule
    AdministrationService as = Context.getAdministrationService();
    Boolean disableValidation = Boolean.valueOf(as.getGlobalProperty(OpenmrsConstants.GP_DISABLE_VALIDATION, "false"));
    ValidateUtil.setDisableValidation(disableValidation);
    PersonName.setFormat(Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LAYOUT_NAME_FORMAT));
    Allergen.setOtherNonCodedConceptUuid(Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_ALLERGEN_OTHER_NON_CODED_UUID));
}
Also used : HashMap(java.util.HashMap) DatabaseUpdateException(org.openmrs.util.DatabaseUpdateException) InputRequiredException(org.openmrs.util.InputRequiredException) APIException(org.openmrs.api.APIException) ModuleMustStartException(org.openmrs.module.ModuleMustStartException) MessageException(org.openmrs.notification.MessageException) GlobalProperty(org.openmrs.GlobalProperty) Role(org.openmrs.Role) AdministrationService(org.openmrs.api.AdministrationService) Privilege(org.openmrs.Privilege) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 22 with AdministrationService

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

the class Context method getMailSession.

/**
 * Gets the mail session required by the mail message service. This function forces
 * authentication via the getAdministrationService() method call
 *
 * @return a java mail session
 */
private static Session getMailSession() {
    if (mailSession == null) {
        synchronized (Context.class) {
            if (mailSession == null) {
                AdministrationService adminService = getAdministrationService();
                Properties props = new Properties();
                props.setProperty("mail.transport.protocol", adminService.getGlobalProperty("mail.transport_protocol"));
                props.setProperty("mail.smtp.host", adminService.getGlobalProperty("mail.smtp_host"));
                props.setProperty("mail.smtp.port", adminService.getGlobalProperty("mail.smtp_port"));
                props.setProperty("mail.from", adminService.getGlobalProperty("mail.from"));
                props.setProperty("mail.debug", adminService.getGlobalProperty("mail.debug"));
                props.setProperty("mail.smtp.auth", adminService.getGlobalProperty("mail.smtp_auth"));
                Authenticator auth = new Authenticator() {

                    @Override
                    public PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(getAdministrationService().getGlobalProperty("mail.user"), getAdministrationService().getGlobalProperty("mail.password"));
                    }
                };
                mailSession = Session.getInstance(props, auth);
            }
        }
    }
    return mailSession;
}
Also used : AdministrationService(org.openmrs.api.AdministrationService) Properties(java.util.Properties) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 23 with AdministrationService

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

the class ModuleFactory method getModulesThatShouldStart.

/**
 * Obtain the list of modules that should be started
 *
 * @return list of modules
 */
private static List<Module> getModulesThatShouldStart() {
    List<Module> modules = new ArrayList<>();
    AdministrationService adminService = Context.getAdministrationService();
    for (Module mod : getLoadedModulesCoreFirst()) {
        String key = mod.getModuleId() + ".started";
        String startedProp = adminService.getGlobalProperty(key, null);
        String mandatoryProp = adminService.getGlobalProperty(mod.getModuleId() + ".mandatory", null);
        boolean isCoreToOpenmrs = mod.isCore() && !ModuleUtil.ignoreCoreModules();
        // as this is probably the first time they are loading it
        if (startedProp == null || "true".equals(startedProp) || "true".equalsIgnoreCase(mandatoryProp) || mod.isMandatory() || isCoreToOpenmrs) {
            modules.add(mod);
        }
    }
    return modules;
}
Also used : AdministrationService(org.openmrs.api.AdministrationService) ArrayList(java.util.ArrayList)

Example 24 with AdministrationService

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

the class PatientSearchCriteria method prepareCriterionForIdentifier.

/**
 * Utility method to add identifier expression to an existing criteria
 *
 * @param identifier
 * @param identifierTypes
 * @param matchIdentifierExactly
 * @param includeVoided true/false whether or not to included voided patients
 */
private Criterion prepareCriterionForIdentifier(String identifier, List<PatientIdentifierType> identifierTypes, boolean matchIdentifierExactly, boolean includeVoided) {
    identifier = HibernateUtil.escapeSqlWildcards(identifier, sessionFactory);
    Conjunction conjunction = Restrictions.conjunction();
    if (!includeVoided) {
        conjunction.add(Restrictions.eq("ids.voided", false));
    }
    // do the identifier restriction
    if (identifier != null) {
        // if the user wants an exact search, match on that.
        if (matchIdentifierExactly) {
            SimpleExpression matchIdentifier = Restrictions.eq("ids.identifier", identifier);
            if (Context.getAdministrationService().isDatabaseStringComparisonCaseSensitive()) {
                matchIdentifier.ignoreCase();
            }
            conjunction.add(matchIdentifier);
        } else {
            AdministrationService adminService = Context.getAdministrationService();
            String regex = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_REGEX, "");
            String patternSearch = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SEARCH_PATTERN, "");
            // remove padding from identifier search string
            if (Pattern.matches("^\\^.{1}\\*.*$", regex)) {
                identifier = removePadding(identifier, regex);
            }
            if (org.springframework.util.StringUtils.hasLength(patternSearch)) {
                conjunction.add(splitAndGetSearchPattern(identifier, patternSearch));
            } else // hsql doesn't know how to deal with 'regexp'
            if ("".equals(regex) || HibernateUtil.isHSQLDialect(sessionFactory)) {
                conjunction.add(getCriterionForSimpleSearch(identifier, adminService));
            } else // if the regex is present, search on that
            {
                regex = replaceSearchString(regex, identifier);
                conjunction.add(Restrictions.sqlRestriction("identifier regexp ?", regex, StringType.INSTANCE));
            }
        }
    }
    // do the type restriction
    if (!CollectionUtils.isEmpty(identifierTypes)) {
        criteria.add(Restrictions.in("ids.identifierType", identifierTypes));
    }
    return conjunction;
}
Also used : AdministrationService(org.openmrs.api.AdministrationService) Conjunction(org.hibernate.criterion.Conjunction) SimpleExpression(org.hibernate.criterion.SimpleExpression)

Example 25 with AdministrationService

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

the class PersonSearchCriteria method getAttributeMatchMode.

MatchMode getAttributeMatchMode() {
    AdministrationService adminService = Context.getAdministrationService();
    String matchModeProperty = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PERSON_ATTRIBUTE_SEARCH_MATCH_MODE, "");
    return (matchModeProperty.equals(OpenmrsConstants.GLOBAL_PROPERTY_PERSON_ATTRIBUTE_SEARCH_MATCH_ANYWHERE)) ? MatchMode.ANYWHERE : MatchMode.EXACT;
}
Also used : AdministrationService(org.openmrs.api.AdministrationService)

Aggregations

AdministrationService (org.openmrs.api.AdministrationService)25 GlobalProperty (org.openmrs.GlobalProperty)8 ArrayList (java.util.ArrayList)5 User (org.openmrs.User)4 Test (org.junit.Test)3 IOException (java.io.IOException)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 EncounterType (org.openmrs.EncounterType)2 APIException (org.openmrs.api.APIException)2 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)2 BindException (org.springframework.validation.BindException)2 Errors (org.springframework.validation.Errors)2 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1