Example usage for org.hibernate SessionFactory getDefinedFilterNames

List of usage examples for org.hibernate SessionFactory getDefinedFilterNames

Introduction

In this page you can find the example usage for org.hibernate SessionFactory getDefinedFilterNames.

Prototype

Set getDefinedFilterNames();

Source Link

Document

Obtain a set of the names of all filters defined on this SessionFactory.

Usage

From source file:gov.nih.nci.security.authorization.instancelevel.InstanceLevelSecurityHelper.java

License:BSD License

/**
 * This method initializes the filter that are already added to the Sessionfactory.
 * <br>//from w ww. java2s.  com
 * This method also initializes the defined filters configured in HBM/Classes/Packages based on the definedFilterNamesMap. 
 * If definedFilterNamesMap contains 'ALL' as the Filter Name (key) then all defined filters are enabled.
 * <br>
 * This method first obtains the list of all the defined filters from the SessionFactory in the passes Session object. 
 * It then just iterates through the filter list and sets the group names and the application name parameter. 
 * 
 * @param groupNames The names of the groups which are invoking the query
 * @param session The Hibernate Session initialized to execute this query
 * @param authorizationManager The CSM AuthorizationManager instance for this application
 * @param definedFilterNamesMap - Map of defined Filter Names and string value ( enable / disable ) to indicate that the filter should be enabled of disabled.<br>.
 *                            
 */
public static void initializeFiltersForGroups(String[] groupNames, Session session,
        AuthorizationManager authorizationManager, Map<String, String> definedFilterNamesMap) {

    List<String> sessionGroupFilterNamesList = new ArrayList<String>();
    List<String> sessionDefinedFilterNamesList = new ArrayList<String>();
    boolean enableAllDefinedFilterNames = false;

    Set definedFilterNames = null;
    if (definedFilterNamesMap != null && !definedFilterNamesMap.isEmpty()) {
        definedFilterNames = definedFilterNamesMap.keySet();
        if (definedFilterNames.contains("ALL"))
            enableAllDefinedFilterNames = true;
    }

    SessionFactory sessionFactory = session.getSessionFactory();
    Set sessionFilterNamesSet = sessionFactory.getDefinedFilterNames();

    Iterator sessionFilterNamesSetIterator = sessionFilterNamesSet.iterator();
    while (sessionFilterNamesSetIterator.hasNext()) {
        String filterName = (String) sessionFilterNamesSetIterator.next();

        if (null != definedFilterNames) {
            if (enableAllDefinedFilterNames) {
                sessionDefinedFilterNamesList.add(filterName);
            } else {
                if (definedFilterNames.contains(filterName)) {
                    String value = (String) definedFilterNamesMap.get(filterName);
                    if (Constants.ENABLE.equalsIgnoreCase(value)) {
                        sessionDefinedFilterNamesList.add(filterName);
                    }
                }
            }
        }
        FilterDefinition filterDefinition = sessionFactory.getFilterDefinition(filterName);
        if (filterDefinition != null) {

            Set<String> parameterNamesSet = filterDefinition.getParameterNames();
            if (parameterNamesSet != null && parameterNamesSet.contains("GROUP_NAMES")) {
                sessionGroupFilterNamesList.add(filterName);
                // remove this filter name from sessionDefinedFilterNamesList if it exists in there.
                if (sessionDefinedFilterNamesList.contains(filterName))
                    sessionDefinedFilterNamesList.remove(filterName);
            }
        }
    }

    //Enable the User Filters from CSM database for the application
    Iterator sessionGroupFilterNamesListIterator = sessionGroupFilterNamesList.iterator();
    while (sessionGroupFilterNamesListIterator.hasNext()) {
        String filterName = (String) sessionGroupFilterNamesListIterator.next();
        Filter filter = session.enableFilter(filterName);
        filter.setParameterList("GROUP_NAMES", groupNames);
        filter.setParameter("APPLICATION_ID", authorizationManager.getApplicationContext().getApplicationId());
    }
    //Enable the Defined Filters available in HBM/Classes.
    Iterator sessionDefinedFilterNamesListIterator = sessionDefinedFilterNamesList.iterator();
    while (sessionDefinedFilterNamesListIterator.hasNext()) {
        String filterName = (String) sessionDefinedFilterNamesListIterator.next();
        Filter filter = session.enableFilter(filterName);
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil.java

License:Apache License

@SuppressWarnings("rawtypes")
public static void enableDynamicFilterEnablerIfPresent(SessionFactory sessionFactory, Session session) {
    if (sessionFactory != null && session != null) {
        final Set definedFilterNames = sessionFactory.getDefinedFilterNames();
        if (definedFilterNames != null && definedFilterNames.contains(DYNAMIC_FILTER_ENABLER))
            session.enableFilter(DYNAMIC_FILTER_ENABLER); // work around for HHH-2624
    }//from w ww  .ja va2s  . co  m
}