Example usage for org.hibernate Criteria createCriteria

List of usage examples for org.hibernate Criteria createCriteria

Introduction

In this page you can find the example usage for org.hibernate Criteria createCriteria.

Prototype

public Criteria createCriteria(String associationPath) throws HibernateException;

Source Link

Document

Create a new Criteria, "rooted" at the associated entity.

Usage

From source file:com.hp.dao.SetLunchDAOImpl.java

@Override
public List<SetLunch> getSetLunchList(String pManagerID, String pStaff, Date pFromDate, Date pToDate) {
    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();
    try {//from   w  w  w.  jav a  2 s  .  co m
        Criteria criteria = session.createCriteria(SetLunch.class);
        Criteria criteriaInner = criteria.createCriteria("staff");

        if (pStaff != null && !pStaff.equals("")) {
            criteriaInner.add(Restrictions.eq("id", pStaff));
        } else if (pManagerID != null && !pManagerID.equals("")) {
            criteriaInner.add(Restrictions.eq("manager", pManagerID));
        }

        if (pFromDate != null) {
            criteria.add(Restrictions.ge("timeAt", pFromDate));
        }

        if (pToDate != null) {
            criteria.add(Restrictions.le("timeAt", pToDate));
        }

        criteria.addOrder(Order.asc("timeAt"));
        System.err.println(criteria.toString());

        return criteria.list();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    finally {
        session.close();
    }

}

From source file:com.hp.dao.TimeKeeperDAOImpl.java

@Override
public List<TimeKeeper> getTimeKeeperList(String pManagerID, String pStaff, Date pFromDate, Date pToDate) {
    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();
    try {//from  w w w . j a va  2  s  . co m
        Criteria criteria = session.createCriteria(TimeKeeper.class);
        Criteria criteriaInner = criteria.createCriteria("staff");

        if (pStaff != null && !pStaff.equals("")) {
            criteriaInner.add(Restrictions.eq("id", pStaff));
        } else if (pManagerID != null && !pManagerID.equals("")) {
            criteriaInner.add(Restrictions.eq("manager", pManagerID));
        }

        if (pFromDate != null) {
            criteria.add(Restrictions.ge("timeAt", pFromDate));
        }

        if (pToDate != null) {
            criteria.add(Restrictions.le("timeAt", pToDate));
        }

        criteria.addOrder(Order.asc("timeAt"));
        System.err.println(criteria.toString());

        return criteria.list();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    finally {
        session.close();
    }

}

From source file:com.hypersocket.local.LocalUserRepositoryImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true)/*from  w ww. j a v  a 2  s  .  c  o  m*/
public Collection<? extends Principal> getGroupsByUser(final LocalUser principal) {
    return list(LocalGroup.class, false, new CriteriaConfiguration() {

        @Override
        public void configure(Criteria criteria) {
            criteria = criteria.createCriteria("users");
            criteria.add(Restrictions.eq("id", principal.getId()));
        }
    });
}

From source file:com.hypersocket.local.LocalUserRepositoryImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true)/*from  www . j a  v a2s.  c  o m*/
public Collection<? extends Principal> getUsersByGroup(final LocalGroup principal) {
    return list(LocalUser.class, false, new CriteriaConfiguration() {

        @Override
        public void configure(Criteria criteria) {
            criteria = criteria.createCriteria("groups");
            criteria.add(Restrictions.eq("id", principal.getId()));
        }
    });
}

From source file:com.hypersocket.permissions.PermissionRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from   w w w. java 2s . c o  m*/
@Transactional(readOnly = true)
public Set<Permission> getPrincipalPermissions(Collection<Principal> principals,
        PermissionType... permissionTypes) {

    if (principals == null) {
        return new HashSet<Permission>();
    }

    Criteria crit = createCriteria(Permission.class)
            .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    List<String> list = new ArrayList<String>();
    for (PermissionType t : permissionTypes) {
        list.add(t.getResourceKey());
    }

    if (list.size() > 0) {
        crit = crit.add(Restrictions.in("resourceKey", list));
    }

    List<Long> ids = new ArrayList<Long>();
    for (Principal p : principals) {
        ids.add(p.getId());
    }

    crit = crit.createCriteria("roles").createCriteria("principals").add(Restrictions.in("id", ids));

    return new HashSet<Permission>(crit.list());

}

From source file:com.hypersocket.permissions.PermissionRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w  w  w .jav a  2s . c  om*/
@Transactional(readOnly = true)
public Set<Role> getRolesForPrincipal(List<Principal> principals) {

    if (principals == null) {
        return new HashSet<Role>();
    }

    Criteria crit = createCriteria(Role.class).setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    List<Long> ids = new ArrayList<Long>();
    for (Principal p : principals) {
        ids.add(p.getId());
    }

    crit = crit.createCriteria("principals").add(Restrictions.in("id", ids));

    Set<Role> roles = new HashSet<Role>(crit.list());
    roles.addAll(getAllUserRoles(principals.get(0).getRealm()));
    return roles;
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from   w  w w  .  ja  va 2 s  .c  om*/
public Collection<T> getAssignedResources(Principal... principals) {

    Criteria criteria = createCriteria(getResourceClass());
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    criteria.add(Restrictions.eq("realm", principals[0].getRealm()));

    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", true));

    Set<T> everyone = new HashSet<T>(criteria.list());

    Set<Long> ids = new HashSet<Long>();
    for (Principal p : principals) {
        ids.add(p.getId());
    }

    criteria = createCriteria(getResourceClass());

    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    criteria.add(Restrictions.eq("realm", principals[0].getRealm()));

    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", false));
    criteria = criteria.createCriteria("principals");
    criteria.add(Restrictions.in("id", ids));

    everyone.addAll((List<T>) criteria.list());
    return everyone;
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*w  w w . ja v  a 2 s .  c o  m*/
public Collection<T> searchAssignedResources(List<Principal> principals, final String searchPattern,
        final int start, final int length, final ColumnSort[] sorting, CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    if (StringUtils.isNotBlank(searchPattern)) {
        criteria.add(Restrictions.ilike("name", searchPattern));
    }

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);
    }

    criteria.add(Restrictions.eq("realm", principals.get(0).getRealm()));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", true));

    Set<T> everyone = new HashSet<T>(criteria.list());

    criteria = createCriteria(getResourceClass());

    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("id"));
    projList.add(Projections.property("name"));

    criteria.setProjection(Projections.distinct(projList));
    criteria.setFirstResult(start);
    criteria.setMaxResults(length);

    if (StringUtils.isNotBlank(searchPattern)) {
        criteria.add(Restrictions.ilike("name", searchPattern));
    }

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);
    }

    for (ColumnSort sort : sorting) {
        criteria.addOrder(sort.getSort() == Sort.ASC ? Order.asc(sort.getColumn().getColumnName())
                : Order.desc(sort.getColumn().getColumnName()));
    }

    criteria.add(Restrictions.eq("realm", principals.get(0).getRealm()));

    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", false));
    criteria = criteria.createCriteria("principals");

    List<Long> ids = new ArrayList<Long>();
    for (Principal p : principals) {
        ids.add(p.getId());
    }
    criteria.add(Restrictions.in("id", ids));

    List<Object[]> results = (List<Object[]>) criteria.list();

    if (results.size() > 0) {
        Long[] entityIds = new Long[results.size()];
        int idx = 0;
        for (Object[] obj : results) {
            entityIds[idx++] = (Long) obj[0];
        }

        criteria = createCriteria(getResourceClass());
        criteria.add(Restrictions.in("id", entityIds));

        everyone.addAll((List<T>) criteria.list());
    }
    return everyone;
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@Override
public boolean hasAssignedEveryoneRole(Realm realm, CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());
    criteria.setProjection(Projections.property("id"));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);//from ww  w  .j  ava 2  s.c o m
    }

    criteria.add(Restrictions.eq("realm", realm));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", true));

    List<?> everyoneRoles = criteria.list();

    return everyoneRoles.size() > 0;
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from   ww w. j  a  va  2  s.c  o m*/
public Collection<Principal> getAssignedPrincipals(Realm realm, CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);
    }

    criteria.add(Restrictions.eq("realm", realm));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", false));

    criteria = criteria.createCriteria("principals");
    criteria.setProjection(Projections.distinct(Projections.property("id")));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);

    List<?> uniquePrincipals = criteria.list();

    if (uniquePrincipals.isEmpty()) {
        return new HashSet<Principal>();
    }
    criteria = createCriteria(Principal.class);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    criteria.add(Restrictions.in("id", uniquePrincipals));
    List<?> res = criteria.list();

    return (Collection<Principal>) res;
}