List of usage examples for org.hibernate.criterion Projections id
public static IdentifierProjection id()
From source file:org.egov.infra.persistence.validator.CompositeUniqueCheckValidator.java
License:Open Source License
private boolean checkCompositeUniqueKey(final Object arg0, final Number id) throws IllegalAccessException { final Criteria criteria = entityManager.unwrap(Session.class) .createCriteria(unique.isSuperclass() ? arg0.getClass().getSuperclass() : arg0.getClass()) .setReadOnly(true);//from w w w. jav a 2 s . c om final Conjunction conjunction = Restrictions.conjunction(); for (final String fieldName : unique.fields()) { final Object fieldValue = FieldUtils.readField(arg0, fieldName, true); if (unique.checkForNull() && fieldValue == null) conjunction.add(Restrictions.isNull(fieldName)); else if (fieldValue instanceof String) conjunction.add(Restrictions.eq(fieldName, fieldValue).ignoreCase()); else conjunction.add(Restrictions.eq(fieldName, fieldValue)); } if (id != null) conjunction.add(Restrictions.ne(unique.id(), id)); return criteria.add(conjunction).setProjection(Projections.id()).setMaxResults(1).uniqueResult() == null; }
From source file:org.egov.infra.persistence.validator.UniqueCheckValidator.java
License:Open Source License
private boolean checkUnique(final Object arg0, final Number id, final String fieldName) throws IllegalAccessException { final Criteria criteria = entityManager.unwrap(Session.class) .createCriteria(unique.isSuperclass() ? arg0.getClass().getSuperclass() : arg0.getClass()) .setReadOnly(true);//from ww w. j a va2s. com final Object fieldValue = FieldUtils.readField(arg0, fieldName, true); if (fieldValue instanceof String) criteria.add(Restrictions.eq(fieldName, fieldValue).ignoreCase()); else criteria.add(Restrictions.eq(fieldName, fieldValue)); if (id != null) criteria.add(Restrictions.ne(unique.id(), id)); return criteria.setProjection(Projections.id()).setMaxResults(1).uniqueResult() == null; }
From source file:org.egov.infra.persistence.validator.UniqueDateOverlapValidator.java
License:Open Source License
private boolean checkUnique(Object object) throws IllegalAccessException { Number id = (Number) FieldUtils.readField(object, uniqueDateOverlap.id(), true); Criteria uniqueDateOverlapChecker = entityManager.unwrap(Session.class).createCriteria(object.getClass()) .setReadOnly(true);/*w ww.j a v a 2 s . co m*/ Conjunction uniqueCheck = Restrictions.conjunction(); for (String fieldName : uniqueDateOverlap.uniqueFields()) { Object fieldValue = FieldUtils.readField(object, fieldName, true); if (fieldValue instanceof String) uniqueCheck.add(Restrictions.eq(fieldName, fieldValue).ignoreCase()); else uniqueCheck.add(Restrictions.eq(fieldName, fieldValue)); } Date fromDate = startOfDay((Date) FieldUtils.readField(object, uniqueDateOverlap.fromField(), true)); Date toDate = endOfDay((Date) FieldUtils.readField(object, uniqueDateOverlap.toField(), true)); Conjunction checkFromDate = Restrictions.conjunction(); checkFromDate.add(Restrictions.le(uniqueDateOverlap.fromField(), fromDate)); checkFromDate.add(Restrictions.ge(uniqueDateOverlap.toField(), fromDate)); Conjunction checkToDate = Restrictions.conjunction(); checkToDate.add(Restrictions.le(uniqueDateOverlap.fromField(), toDate)); checkToDate.add(Restrictions.ge(uniqueDateOverlap.toField(), toDate)); Conjunction checkFromAndToDate = Restrictions.conjunction(); checkFromAndToDate.add(Restrictions.ge(uniqueDateOverlap.fromField(), fromDate)); checkFromAndToDate.add(Restrictions.le(uniqueDateOverlap.toField(), toDate)); Disjunction dateRangeChecker = Restrictions.disjunction(); dateRangeChecker.add(checkFromDate).add(checkToDate).add(checkFromAndToDate); uniqueCheck.add(dateRangeChecker); if (id != null) uniqueCheck.add(Restrictions.ne(uniqueDateOverlap.id(), id)); return uniqueDateOverlapChecker.add(uniqueCheck).setProjection(Projections.id()).setMaxResults(1) .uniqueResult() == null; }
From source file:org.encuestame.persistence.dao.imp.AccountDaoImp.java
License:Apache License
/** * Get list of id accounts only if are enabled. * @return list of id's./*from ww w. j ava2 s . c o m*/ */ public List<Long> getAccountsEnabled(final Boolean status) { final DetachedCriteria criteria = DetachedCriteria.forClass(Account.class); criteria.add(Restrictions.eq("enabled", status)); criteria.setProjection(Projections.id()); final List<Long> accountsId = (List<Long>) getHibernateTemplate().findByCriteria(criteria); return accountsId; }
From source file:org.encuestame.persistence.dao.imp.PollDao.java
License:Apache License
@SuppressWarnings("unchecked") public List<Poll> getPollByHashTagName(final String tagName, final Integer startResults, final Integer limitResults, final TypeSearchResult filterby, final SearchPeriods searchPeriods) { final DetachedCriteria detached = DetachedCriteria.forClass(Poll.class).createAlias("hashTags", "hashTags") .setProjection(Projections.id()) .add(Subqueries.propertyIn("hashTags.hashTagId", DetachedCriteria.forClass(HashTag.class, "hash").setProjection(Projections.id()) .add(Restrictions.in("hash.hashTag", new String[] { tagName })))); final DetachedCriteria criteria = DetachedCriteria.forClass(Poll.class, "poll"); criteria.add(Subqueries.propertyIn("poll.pollId", detached)); if (filterby.equals(TypeSearchResult.HASHTAG)) { criteria.addOrder(Order.desc("poll.createDate")); } else if (filterby.equals(TypeSearchResult.HASHTAGRATED)) { criteria.addOrder(Order.desc("numbervotes")); }//from w ww . j ava 2 s . c o m criteria.add(Restrictions.eq("publish", Boolean.TRUE)); calculateSearchPeriodsDates(searchPeriods, detached, "createDate"); return (List<Poll>) filterByMaxorStart(criteria, limitResults, startResults); }
From source file:org.encuestame.persistence.dao.imp.PollDao.java
License:Apache License
@SuppressWarnings("unchecked") public List<Poll> getPollsbyHashTagNameAndDateRange(final String tagName, final SearchPeriods period) { final DetachedCriteria detached = DetachedCriteria.forClass(Poll.class).createAlias("hashTags", "hashTags") .setProjection(Projections.id()) .add(Subqueries.propertyIn("hashTags.hashTagId", DetachedCriteria.forClass(HashTag.class, "hash").setProjection(Projections.id()) .add(Restrictions.in("hash.hashTag", new String[] { tagName })))); final DetachedCriteria criteria = DetachedCriteria.forClass(Poll.class, "poll"); criteria.add(Subqueries.propertyIn("poll.pollId", detached)); criteria.addOrder(Order.desc("poll.createDate")); calculateSearchPeriodsDates(period, criteria, "createDate"); criteria.add(Restrictions.eq("publish", Boolean.TRUE)); return (List<Poll>) getHibernateTemplate().findByCriteria(criteria); }
From source file:org.encuestame.persistence.dao.imp.PollDao.java
License:Apache License
@SuppressWarnings("unchecked") public List<Object[]> getPollsRangeStats(final String tagName, final SearchPeriods period) { final DetachedCriteria detached = DetachedCriteria.forClass(Poll.class).createAlias("hashTags", "hashTags") .setProjection(Projections.id()) .add(Subqueries.propertyIn("hashTags.hashTagId", DetachedCriteria.forClass(HashTag.class, "hash").setProjection(Projections.id()) .add(Restrictions.in("hash.hashTag", new String[] { tagName })))); final DetachedCriteria criteria = DetachedCriteria.forClass(Poll.class, "poll"); criteria.add(Subqueries.propertyIn("poll.pollId", detached)); criteria.addOrder(Order.desc("poll.createDate")); criteria.add(Restrictions.eq("publish", Boolean.TRUE)); calculateSearchPeriodsDates(period, criteria, "createDate"); ProjectionList projList = Projections.projectionList(); projList.add(Projections.groupProperty("createDate")); projList.add(Projections.rowCount()); criteria.setProjection(projList);//from w ww . j a v a 2s .c o m return (List<Object[]>) getHibernateTemplate().findByCriteria(criteria); }
From source file:org.encuestame.persistence.dao.imp.SurveyDaoImp.java
License:Apache License
@SuppressWarnings("unchecked") public List<Survey> getSurveysByHashTagName(final String tagName, final Integer startResults, final Integer limitResults, final TypeSearchResult filterby, final SearchPeriods searchPeriods) { final DetachedCriteria detached = DetachedCriteria.forClass(Survey.class) .createAlias("hashTags", "hashTags").setProjection(Projections.id()) .add(Subqueries.propertyIn("hashTags.hashTagId", DetachedCriteria.forClass(HashTag.class, "hash").setProjection(Projections.id()) .add(Restrictions.in("hash.hashTag", new String[] { tagName })))); final DetachedCriteria criteria = DetachedCriteria.forClass(Survey.class, "survey"); criteria.add(Subqueries.propertyIn("survey.sid", detached)); if (filterby.equals(TypeSearchResult.HASHTAG)) { criteria.addOrder(Order.desc("survey.createDate")); } else if (filterby.equals(TypeSearchResult.HASHTAGRATED)) { criteria.addOrder(Order.desc("numbervotes")); }/*ww w .j a va2s .c om*/ calculateSearchPeriodsDates(searchPeriods, detached, "createDate"); return (List<Survey>) filterByMaxorStart(criteria, limitResults, startResults); }
From source file:org.encuestame.persistence.dao.imp.SurveyDaoImp.java
License:Apache License
@SuppressWarnings("unchecked") public List<Survey> getSurveysbyHashTagNameAndDateRange(final String tagName, final SearchPeriods period) { final DetachedCriteria detached = DetachedCriteria.forClass(Survey.class) .createAlias("hashTags", "hashTags").setProjection(Projections.id()) .add(Subqueries.propertyIn("hashTags.hashTagId", DetachedCriteria.forClass(HashTag.class, "hash").setProjection(Projections.id()) .add(Restrictions.in("hash.hashTag", new String[] { tagName })))); final DetachedCriteria criteria = DetachedCriteria.forClass(Survey.class, "survey"); criteria.add(Subqueries.propertyIn("survey.sid", detached)); criteria.addOrder(Order.desc("survey.createDate")); calculateSearchPeriodsDates(period, criteria, "survey.createDate"); return (List<Survey>) getHibernateTemplate().findByCriteria(criteria); }
From source file:org.encuestame.persistence.dao.imp.SurveyDaoImp.java
License:Apache License
@SuppressWarnings("unchecked") public List<Object[]> getSurveysRangeStats(final String tagName, final SearchPeriods period) { final DetachedCriteria detached = DetachedCriteria.forClass(Survey.class) .createAlias("hashTags", "hashTags").setProjection(Projections.id()) .add(Subqueries.propertyIn("hashTags.hashTagId", DetachedCriteria.forClass(HashTag.class, "hash").setProjection(Projections.id()) .add(Restrictions.in("hash.hashTag", new String[] { tagName })))); final DetachedCriteria criteria = DetachedCriteria.forClass(Survey.class, "survey"); criteria.add(Subqueries.propertyIn("survey.sid", detached)); criteria.addOrder(Order.desc("survey.createDate")); ProjectionList projList = Projections.projectionList(); projList.add(Projections.groupProperty("createDate")); projList.add(Projections.rowCount()); criteria.setProjection(projList);//from w w w . jav a 2 s . com return (List<Object[]>) getHibernateTemplate().findByCriteria(criteria); }