Example usage for org.hibernate Session enableFilter

List of usage examples for org.hibernate Session enableFilter

Introduction

In this page you can find the example usage for org.hibernate Session enableFilter.

Prototype

Filter enableFilter(String filterName);

Source Link

Document

Enable the named filter for this current session.

Usage

From source file:onl.netfishers.netshot.RestService.java

License:Open Source License

/**
 * Gets the device configs.//from w  w w  .  j av  a2 s.c  o m
 *
 * @param request the request
 * @param id the id
 * @return the device configs
 * @throws WebApplicationException the web application exception
 */
@GET
@Path("devices/{id}/configs")
@RolesAllowed("readonly")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public List<Config> getDeviceConfigs(@PathParam("id") Long id) throws WebApplicationException {
    logger.debug("REST request, get device {} configs.", id);
    Session session = Database.getSession();
    try {
        session.enableFilter("lightAttributesOnly");
        @SuppressWarnings("unchecked")
        List<Config> deviceConfigs = session
                .createQuery("from Config c left join fetch c.attributes ca where c.device = :device")
                .setLong("device", id).list();
        return deviceConfigs;
    } catch (HibernateException e) {
        logger.error("Unable to fetch the configs.", e);
        throw new NetshotBadRequestException("Unable to fetch the configs",
                NetshotBadRequestException.NETSHOT_DATABASE_ACCESS_ERROR);
    } finally {
        session.close();
    }
}

From source file:org.activityinfo.server.database.hibernate.entity.DomainFilters.java

License:Open Source License

public static void applyDeletedFilter(EntityManager em) {
    org.hibernate.Session session = ((HibernateEntityManager) em).getSession();

    /* Hide entities deleted by users */
    session.enableFilter("hideDeleted");
}

From source file:org.activityinfo.server.database.hibernate.entity.DomainFilters.java

License:Open Source License

public static void applyVisibleFilter(int userId, EntityManager em) {
    /* Hide entities that this user does not have permission to view */
    org.hibernate.Session session = ((HibernateEntityManager) em).getSession();

    Filter filter = session.enableFilter("userVisible");
    filter.setParameter("currentUserId", userId);
}

From source file:org.agnitas.web.StrutsActionBase.java

License:Open Source License

/**
 * Creates Hibernate session using session factory.
 *
 * @param req servlet request object//  ww w  . ja  va 2 s  . c o  m
 * @return new hibernate session
 */
protected Session getHibernateSession(HttpServletRequest req) {
    Session aSession = null;

    if (sf == null) {
        sf = AgnUtils.retrieveSessionFactory(this.getServlet().getServletContext());
    }
    aSession = sf.openSession();
    aSession.enableFilter("companyFilter").setParameter("companyFilterID", new Integer(this.getCompanyID(req)));
    return aSession;
}

From source file:org.archiviststoolkit.hibernate.SessionFactory.java

License:Open Source License

/**
 * Used to create a new session from the preconfigured hibernate factory.
 * If the class parameter is present check to see if any filters should
 * be added/* www. j  av  a 2s . c o m*/
 *
 * @return the session which is opened
 */

public Session openSession(Interceptor interceptor, Class clazz, Boolean alwaysApplyFilters) {

    Session newSession = null;

    try {
        if (newSession == null) {
            if (interceptor == null) {
                newSession = singleton.sessionFactory.openSession();
            } else {
                newSession = singleton.sessionFactory.openSession(interceptor);
            }
        }

    } catch (HibernateException hibernateException) {
        logger.log(Level.SEVERE, "Failed to open a hibernate session", hibernateException);
    }
    if (clazz != null) {
        if (alwaysApplyFilters || (ApplicationFrame.getInstance().getCurrentUser() != null
                && !Users.doesCurrentUserHaveAccess(Users.ACCESS_CLASS_SUPERUSER))) {
            if (clazz == Resources.class || clazz == Accessions.class || clazz == Users.class
                    || clazz == Repositories.class || clazz == Locations.class
                    || clazz == DigitalObjects.class) {
                newSession.enableFilter("repository").setParameter("repositoryId",
                        ApplicationFrame.getInstance().getCurrentUserRepositoryId());
            }
        }

    }
    return (newSession);
}

From source file:org.bonitasoft.engine.persistence.TenantHibernatePersistenceService.java

License:Open Source License

protected void updateTenantFilter(final Session session, final boolean useTenant) throws SPersistenceException {
    if (useTenant) {
        try {/* w w w .  j av  a2s  . co  m*/
            session.enableFilter(TENANT_FILTER).setParameter(TENANT_ID, getTenantId());
        } catch (final STenantIdNotSetException e) {
            throw new SPersistenceException(e);
        }
    } else {
        session.disableFilter(TENANT_FILTER);
    }
}

From source file:org.bonitasoft.engine.test.persistence.repository.TestRepository.java

License:Open Source License

protected Session getSessionWithTenantFilter() {
    final Session session = getSession();
    session.enableFilter("tenantFilter").setParameter("tenantId", PersistentObjectBuilder.DEFAULT_TENANT_ID);
    return session;
}

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
    }// ww w . j a  v a  2 s  .  c  o m
}

From source file:org.eclipse.emf.teneo.hibernate.test.emf.sample.EmployeeAction.java

License:Open Source License

/** Creates an item, an address and links them to a po. */
@Override//from  www . j  av  a 2 s  . com
public void doAction(TestStore store) {
    final EmployeeFactory f = EmployeeFactory.eINSTANCE;

    {
        final Employee e1 = f.createEmployee();
        e1.setName("e1");
        e1.setAge(29);
        e1.setSalary(100);
        e1.setHireDate(new Date(System.currentTimeMillis() + 100000));

        final Employee e2 = f.createEmployee();
        e2.setName("e2");
        e2.setAge(39);
        e2.setSalary(1000);
        final long ONE_DAY = 3600 * 1000 * 24;
        e2.setHireDate(new Date(System.currentTimeMillis() - ONE_DAY - ONE_DAY));

        final Employee e3 = f.createEmployee();
        e3.setName("e3");
        e3.setAge(49);
        e3.setSalary(10000);
        e3.setHireDate(new Date(System.currentTimeMillis() + 100000));

        final Employee e4 = f.createEmployee();
        e4.setName("e4");
        e4.setAge(59);
        e4.setSalary(100000);
        e4.setHireDate(new Date(System.currentTimeMillis() + 100000));

        final Department d = f.createDepartment();
        d.setName("d1");
        d.getEmployees().add(e1);
        d.getEmployees().add(e2);
        d.getEmployees().add(e3);
        d.getEmployees().add(e4);

        store.beginTransaction();
        store.store(d);
        store.commitTransaction();
    }

    {
        final Session s = ((HibernateTestStore) store).getSession();
        store.beginTransaction();
        List<?> es = store.getObjects(Employee.class);
        checkList(es, new String[] { "e1", "e2", "e3", "e4" });

        s.enableFilter("ageRange").setParameter("fromAge", 35).setParameter("toAge", 55);
        es = store.getObjects(Employee.class);
        checkList(es, new String[] { "e2", "e3" });

        s.enableFilter("salaryHigherThan").setParameter("salary", 5000);
        es = store.getObjects(Employee.class);
        checkList(es, new String[] { "e3" });

        s.disableFilter("salaryHigherThan");
        s.enableFilter("ageRange").setParameter("fromAge", 25).setParameter("toAge", 55);
        es = store.getObjects(Employee.class);
        checkList(es, new String[] { "e1", "e2", "e3" });

        s.enableFilter("salaryHigherThan").setParameter("salary", 500);
        es = store.getObjects(Employee.class);
        checkList(es, new String[] { "e2", "e3" });

        final Date dt = new Date();
        s.enableFilter("hireDate").setParameter("date", dt);
        es = store.getObjects(Employee.class);
        checkList(es, new String[] { "e3" });

        s.disableFilter("salaryHigherThan");
        s.disableFilter("ageRange");
        s.disableFilter("hireDate");

        store.commitTransaction();
    }
    {
        Session s = ((HibernateTestStore) store).getSession();
        store.beginTransaction();
        Department d = store.getObject(Department.class);
        List<?> es = d.getEmployees();
        checkList(es, new String[] { "e1", "e2", "e3", "e4" });
        store.commitTransaction();

        store.beginTransaction();
        s = ((HibernateTestStore) store).getSession();
        s.enableFilter("ageRange").setParameter("fromAge", 35).setParameter("toAge", 55);
        d = store.getObject(Department.class);
        es = d.getEmployees();
        checkList(es, new String[] { "e2", "e3" });
        store.commitTransaction();

        store.beginTransaction();
        s = ((HibernateTestStore) store).getSession();
        s.enableFilter("ageRange").setParameter("fromAge", 35).setParameter("toAge", 55);
        s.enableFilter("salaryHigherThan").setParameter("salary", 5000);
        d = store.getObject(Department.class);
        es = d.getEmployees();
        checkList(es, new String[] { "e3" });
        store.commitTransaction();

        store.beginTransaction();
        s = ((HibernateTestStore) store).getSession();
        s.enableFilter("ageRange").setParameter("fromAge", 25).setParameter("toAge", 55);
        d = store.getObject(Department.class);
        es = d.getEmployees();
        checkList(es, new String[] { "e1", "e2", "e3" });
        store.commitTransaction();

        store.beginTransaction();
        s = ((HibernateTestStore) store).getSession();
        s.enableFilter("ageRange").setParameter("fromAge", 25).setParameter("toAge", 55);
        s.enableFilter("salaryHigherThan").setParameter("salary", 500);
        d = store.getObject(Department.class);
        es = d.getEmployees();
        checkList(es, new String[] { "e2", "e3" });
        store.commitTransaction();
    }
}

From source file:org.ednovo.gooru.infrastructure.persistence.hibernate.HibernateDaoSupport.java

License:Open Source License

public Session getSession() {

    Session currentSession = null;
    try {/*from  www  . ja  v a 2 s .  c  om*/
        currentSession = getSessionFactory().getCurrentSession();
    } catch (Exception e) {
        currentSession = getSessionFactory().openSession();
    }
    currentSession.enableFilter("customFieldFilter").setParameterList("customFieldFilterParam",
            Constants.customFieldsKey);
    return currentSession;
}