/** * prepare query based on criteria * * @param session hib session * @return the query */ private Criteria attachCriteriaInfo(Session session) { Criteria query = null; if (StringUtils.isBlank(this.entityName)) { if (StringUtils.isBlank(this.alias)) { query = session.createCriteria(this.persistentClass); } else { query = session.createCriteria(this.persistentClass, alias); } } else { query = session.createCriteria(this.entityName); } // add criterions if (this.criterions != null) { query.add(this.criterions); } boolean secondLevelCaching = HibUtils.secondLevelCaching( ByCriteriaStatic.this.cacheable, ByCriteriaStatic.this.queryOptions); query.setCacheable(secondLevelCaching); if (secondLevelCaching) { String secondLevelCacheRegion = HibUtils.secondLevelCacheRegion( ByCriteriaStatic.this.cacheRegion, ByCriteriaStatic.this.queryOptions); if (!StringUtils.isBlank(secondLevelCacheRegion)) { query.setCacheRegion(secondLevelCacheRegion); } } QuerySort querySort = this.queryOptions == null ? null : this.queryOptions.getQuerySort(); if (querySort != null) { List<QuerySortField> sorts = querySort.getQuerySortFields(); for (QuerySortField theSort : GrouperUtil.nonNull(sorts)) { Order order = theSort.isAscending() ? Order.asc(theSort.getColumn()) : Order.desc(theSort.getColumn()); query.addOrder(order); } } QueryPaging queryPaging = this.queryOptions == null ? null : this.queryOptions.getQueryPaging(); if (queryPaging != null) { // GRP-1024: sql server problems with paging page number when not initted if (queryPaging.getFirstIndexOnPage() < 0) { query.setFirstResult(0); } else { query.setFirstResult(queryPaging.getFirstIndexOnPage()); } query.setMaxResults(queryPaging.getPageSize()); } return query; }
public static List<Group> findByOrg(Organisation org, Session session) { Criteria crit = session.createCriteria(Group.class); crit.setCacheable(true); crit.add(Restrictions.eq("organisation", org)); crit.addOrder(Order.asc("name")); return DbUtils.toList(crit, Group.class); }
/** * Perform a search over all child and sub orgs within the given organisation * * <p>If a search query is given it will be split by space and the key words searched in the * title, orgid, address, addressLine2 and postcode fields. * * @param q - search query, which is a space seperated list of key words. Optional * @param organisation - search is for orgs inside this * @param orgType - optional, if given results are limited to organisations of this type * @param session * @return */ public static List<Organisation> search( String q, Organisation organisation, OrgType orgType, Session session) { Criteria crit = session.createCriteria(Organisation.class); crit.setCacheable(true); Disjunction notDeleted = Restrictions.disjunction(); notDeleted.add(Restrictions.isNull("deleted")); notDeleted.add(Restrictions.eq("deleted", Boolean.FALSE)); crit.add(notDeleted); if (q != null) { String[] arr = q.split(" "); Conjunction con = Restrictions.conjunction(); for (String queryPart : arr) { Disjunction dis = Restrictions.disjunction(); String s = "%" + queryPart + "%"; dis.add(Restrictions.ilike("title", s)); dis.add(Restrictions.ilike("orgId", s)); dis.add(Restrictions.ilike("address", s)); dis.add(Restrictions.ilike("addressLine2", s)); dis.add(Restrictions.ilike("postcode", s)); con.add(dis); } crit.add(con); } if (orgType != null) { crit.add(Restrictions.eq("orgType", orgType)); } // TODO: add other properties like address Criteria critParentLink = crit.createCriteria("parentOrgLinks"); critParentLink.add(Restrictions.eq("owner", organisation)); crit.addOrder(Order.asc("title")); return DbUtils.toList(crit, Organisation.class); }
public static List<Organisation> findByOrgType(OrgType orgType, Session session) { Criteria crit = session.createCriteria(Organisation.class); crit.setCacheable(true); crit.add(Restrictions.eq("orgType", orgType)); crit.addOrder(Order.asc("title")); return DbUtils.toList(crit, Organisation.class); }
public static Group findByOrgAndName(Organisation org, String name, Session session) { Criteria crit = session.createCriteria(Group.class); crit.setCacheable(true); crit.add(Restrictions.eq("organisation", org)); crit.add(Restrictions.eq("name", name)); return (Group) crit.uniqueResult(); }
public static List<Profile> findByGroup(Group group, Session session) { Criteria crit = session.createCriteria(Profile.class); crit.setCacheable(true); // join to group membership, then subordinate, then restrict on org Criteria critMembership = crit.createCriteria("memberships"); critMembership.add(Restrictions.eq("groupEntity", group)); return DbUtils.toList(crit, Profile.class); }
public GroupMembership getGroupMembership(BaseEntity u, Organisation withinOrg, Session session) { Criteria crit = session.createCriteria(GroupMembership.class); crit.setCacheable(true); crit.add(Restrictions.eq("member", u)); crit.add(Restrictions.eq("groupEntity", this)); crit.add(Restrictions.eq("withinOrg", withinOrg)); return DbUtils.unique(crit); }
/** * Is the given entity a member of this group, regardless of organisation * * @param entity * @return */ public boolean isMember(BaseEntity entity) { Criteria crit = SessionManager.session().createCriteria(GroupMembership.class); crit.setCacheable(true); crit.add(Restrictions.eq("member", entity)); crit.add(Restrictions.eq("groupEntity", this)); boolean b = !DbUtils.toList(crit, GroupMembership.class).isEmpty(); return b; }
public static List<Profile> findByBusinessUnit(Organisation organisation, Session session) { Criteria crit = session.createCriteria(Profile.class); crit.setCacheable(true); // join to group membership, then subordinate, then restrict on org Criteria critMembership = crit.createCriteria("memberships"); critMembership.add(Restrictions.eq("withinOrg", organisation)); return DbUtils.toList(crit, Profile.class); }
/** * Find the given orgId within an administrative org * * @param adminOrg * @param orgId * @param session * @return */ public static Organisation findByOrgId(Organisation adminOrg, String orgId, Session session) { Criteria crit = session.createCriteria(Organisation.class); Criteria critSubOrg = crit.createCriteria("parentOrgLinks"); crit.setCacheable(true); crit.add(Restrictions.eq("orgId", orgId)); critSubOrg.add(Restrictions.eq("owner", adminOrg)); return DbUtils.unique(crit); }
public static Profile find(String name, Session session) { Criteria crit = session.createCriteria(Profile.class); crit.setCacheable(true); crit.add( Restrictions.disjunction() .add(Restrictions.eq("name", name)) .add(Restrictions.eq("email", name))); return DbUtils.unique(crit); }
public static Organisation getOrganisation( Organisation organisation, String name, Session session) { Criteria crit = session.createCriteria(Organisation.class); crit.setCacheable(true); crit.add( Restrictions.and( Restrictions.eq("organisation", organisation), Restrictions.eq("name", name))); Organisation org = (Organisation) crit.uniqueResult(); return org; }
/** * Find a profile by email address, but only looking within the given organisation or subordinate * orgs * * @param email * @param org * @param session * @return */ public static Profile findByEmail(String email, Organisation org, Session session) { Criteria crit = session.createCriteria(Profile.class); crit.setCacheable(true); // join to group membership, then subordinate, then restrict on org Criteria critMembership = crit.createCriteria("memberships"); Criteria critSubordinate = critMembership.createCriteria("subordinates"); crit.add(Restrictions.eq("email", email)); critSubordinate.add(Restrictions.eq("withinOrg", org)); return DbUtils.unique(crit); }
public static Organisation getRootOrg(Session session) { Criteria crit = session.createCriteria(Organisation.class); crit.setCacheable(true); crit.add(Restrictions.isNull("organisation")); List<Organisation> list = DbUtils.toList(crit, Organisation.class); if (!list.isEmpty()) { return list.get(0); } return null; }
protected void setUpCacheOnCriteria(Criteria criteria, SearchTemplate searchTemplate) { if (searchTemplate.isCacheable()) { criteria.setCacheable(true); if (searchTemplate.hasCacheRegion()) { criteria.setCacheRegion(searchTemplate.getCacheRegion()); } else { criteria.setCacheRegion(getCacheRegion()); } } }
/** * Find a user who has a membership in the given organisation * * @param org * @param name * @param session * @return */ public static Profile find(Organisation org, String name, Session session) { Criteria crit = session.createCriteria(Profile.class); crit.setCacheable(true); // join to group membership, then subordinate, then restrict on org Criteria critMembership = crit.createCriteria("memberships"); Criteria critSubordinate = critMembership.createCriteria("subordinates"); crit.add(Restrictions.eq("name", name)); critSubordinate.add(Restrictions.eq("withinOrg", org)); List list = crit.list(); if (list == null || list.isEmpty()) { log.warn("Profile not found: " + name + " in org: " + org.getOrgId()); return null; } else { return (Profile) list.get(0); } }
/** @see org.iesc.flightws.domain.PassengerDao#loadAll(int, int, int) */ public java.util.Collection loadAll( final int transform, final int pageNumber, final int pageSize) { try { final org.hibernate.Criteria criteria = this.getSession(false).createCriteria(org.iesc.flightws.domain.PassengerImpl.class); criteria.setCacheable(true); if (pageNumber > 0 && pageSize > 0) { criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize)); criteria.setMaxResults(pageSize); } final java.util.Collection results = criteria.list(); this.transformEntities(transform, results); return results; } catch (org.hibernate.HibernateException ex) { throw super.convertHibernateAccessException(ex); } }
/** * 根据对象类型和Session对象取得Hibernate的Criteria对象 * * @param clazz * @param session * @return Criteria */ public Criteria getCriteria(Class<?> clazz, Session session) { // CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); Criteria criteria = session.createCriteria(clazz); for (int i = 0; i < criterionList.size(); i++) { Criterion criterion = criterionList.get(i); criteria.add(criterion); // criteriaBuilder. } for (int i = 0; i < orderList.size(); i++) { Order order = orderList.get(i); criteria.addOrder(order); } if (offset > 0) { criteria.setFirstResult(this.offset); } if (pageSize > 0) { criteria.setMaxResults(this.pageSize); } criteria.setCacheable(true); return criteria; }
public static long countChildOrgs(Organisation organisation, Session session) { Criteria crit = session.createCriteria(Organisation.class); crit.setCacheable(true); Disjunction notDeleted = Restrictions.disjunction(); notDeleted.add(Restrictions.isNull("deleted")); notDeleted.add(Restrictions.eq("deleted", Boolean.FALSE)); crit.add(notDeleted); crit.add(Restrictions.ne("id", organisation.getId())); Criteria critParentLink = crit.createCriteria("parentOrgLinks"); critParentLink.add(Restrictions.eq("owner", organisation)); crit.setProjection(Projections.count("id")); Object result = crit.uniqueResult(); if (result == null) { return 0; } else if (result instanceof Integer) { Integer i = (Integer) result; return i.longValue(); } else { return (long) result; } }
public void selectSecurityList() { HibernateSessionProxy session = (HibernateSessionProxy) entityManager.getDelegate(); Criteria crit = session.createCriteria(Security.class); if (getCode() != null && getCode().length() > 0) { crit.add(Restrictions.like("code", getCode() + "%")); } if (getIsin() != null && getIsin().length() > 0) { crit.add(Restrictions.like("isin", getIsin() + "%")); } crit.setProjection( Projections.projectionList() .add(Projections.property("code"), "code") .add(Projections.property("isin"), "isin")); crit.setMaxResults(30); crit.setCacheable(true); securityList = crit.list(); }
/** * @param bowlMatchupId * @return BowlMatchup for the given ID. */ public BowlMatchup getMatchupById(Integer bowlMatchupId) { Criteria criteria = session.createCriteria(BowlMatchup.class); criteria.add(Restrictions.eq("bowlMatchupId", bowlMatchupId)); criteria.setCacheable(false); return (BowlMatchup) criteria.list().get(0); }
/** * Populates criteria arguments for the given target class and arguments map * * @param datastore the GrailsApplication instance * @param targetClass The target class * @param c The criteria instance * @param argMap The arguments map */ @SuppressWarnings("rawtypes") public static void populateArgumentsForCriteria( AbstractHibernateDatastore datastore, Class<?> targetClass, Criteria c, Map argMap, ConversionService conversionService, boolean useDefaultMapping) { Integer maxParam = null; Integer offsetParam = null; if (argMap.containsKey(ARGUMENT_MAX)) { maxParam = conversionService.convert(argMap.get(ARGUMENT_MAX), Integer.class); } if (argMap.containsKey(ARGUMENT_OFFSET)) { offsetParam = conversionService.convert(argMap.get(ARGUMENT_OFFSET), Integer.class); } if (argMap.containsKey(ARGUMENT_FETCH_SIZE)) { c.setFetchSize(conversionService.convert(argMap.get(ARGUMENT_FETCH_SIZE), Integer.class)); } if (argMap.containsKey(ARGUMENT_TIMEOUT)) { c.setTimeout(conversionService.convert(argMap.get(ARGUMENT_TIMEOUT), Integer.class)); } if (argMap.containsKey(ARGUMENT_FLUSH_MODE)) { c.setFlushMode(convertFlushMode(argMap.get(ARGUMENT_FLUSH_MODE))); } if (argMap.containsKey(ARGUMENT_READ_ONLY)) { c.setReadOnly(ClassUtils.getBooleanFromMap(ARGUMENT_READ_ONLY, argMap)); } String orderParam = (String) argMap.get(ARGUMENT_ORDER); Object fetchObj = argMap.get(ARGUMENT_FETCH); if (fetchObj instanceof Map) { Map fetch = (Map) fetchObj; for (Object o : fetch.keySet()) { String associationName = (String) o; c.setFetchMode(associationName, getFetchMode(fetch.get(associationName))); } } final int max = maxParam == null ? -1 : maxParam; final int offset = offsetParam == null ? -1 : offsetParam; if (max > -1) { c.setMaxResults(max); } if (offset > -1) { c.setFirstResult(offset); } if (ClassUtils.getBooleanFromMap(ARGUMENT_LOCK, argMap)) { c.setLockMode(LockMode.PESSIMISTIC_WRITE); c.setCacheable(false); } else { if (argMap.containsKey(ARGUMENT_CACHE)) { c.setCacheable(ClassUtils.getBooleanFromMap(ARGUMENT_CACHE, argMap)); } else { cacheCriteriaByMapping(targetClass, c); } } final Object sortObj = argMap.get(ARGUMENT_SORT); if (sortObj != null) { boolean ignoreCase = true; Object caseArg = argMap.get(ARGUMENT_IGNORE_CASE); if (caseArg instanceof Boolean) { ignoreCase = (Boolean) caseArg; } if (sortObj instanceof Map) { Map sortMap = (Map) sortObj; for (Object sort : sortMap.keySet()) { final String order = ORDER_DESC.equalsIgnoreCase((String) sortMap.get(sort)) ? ORDER_DESC : ORDER_ASC; addOrderPossiblyNested(datastore, c, targetClass, (String) sort, order, ignoreCase); } } else { final String sort = (String) sortObj; final String order = ORDER_DESC.equalsIgnoreCase(orderParam) ? ORDER_DESC : ORDER_ASC; addOrderPossiblyNested(datastore, c, targetClass, sort, order, ignoreCase); } } else if (useDefaultMapping) { Mapping m = getDomainBinder().getMapping(targetClass); if (m != null) { Map sortMap = m.getSort().getNamesAndDirections(); for (Object sort : sortMap.keySet()) { final String order = ORDER_DESC.equalsIgnoreCase((String) sortMap.get(sort)) ? ORDER_DESC : ORDER_ASC; addOrderPossiblyNested(datastore, c, targetClass, (String) sort, order, true); } } } }
public static Profile findByEmail(String email, Session session) { Criteria crit = session.createCriteria(Profile.class); crit.setCacheable(true); crit.add(Restrictions.eq("email", email)); return DbUtils.unique(crit); }
public static List<Profile> findAll(Session session) { Criteria crit = session.createCriteria(Profile.class); crit.setCacheable(true); return DbUtils.toList(crit, Profile.class); }
/** @return all bowl matchups. */ @SuppressWarnings("unchecked") public List<BowlMatchup> getAllBowlMatchups() { Criteria criteria = session.createCriteria(BowlMatchup.class); criteria.setCacheable(false); return criteria.list(); }
public static Organisation findByAdminDomain(String adminDomain, Session session) { Criteria crit = session.createCriteria(Organisation.class); crit.setCacheable(true); crit.add(Restrictions.eq("adminDomain", adminDomain)); return DbUtils.unique(crit); }
public BowlPick getBowlPickById(Integer bowlPickId) { Criteria criteria = session.createCriteria(BowlPick.class); criteria.add(Restrictions.eq("bowlPickId", bowlPickId)); criteria.setCacheable(false); return (BowlPick) criteria.list().get(0); }
/** * Configures the criteria instance to cache based on the configured mapping. * * @param targetClass The target class * @param criteria The criteria */ public static void cacheCriteriaByMapping(Class<?> targetClass, Criteria criteria) { Mapping m = getDomainBinder().getMapping(targetClass); if (m != null && m.getCache() != null && m.getCache().getEnabled()) { criteria.setCacheable(true); } }