List of usage examples for org.hibernate.criterion Projections count
public static CountProjection count(String propertyName)
From source file:net.thackbarth.sparrow.DatabaseCleaner.java
License:Apache License
void removeDoubleFiles(Session session) { ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.groupProperty(PROPERTY_FILE_PATH).as(PROPERTY_FILE_PATH)) .add(Projections.count(PROPERTY_FILE_PATH).as(PROPERTY_AMOUNT)); Criteria doubleCriteria = session.createCriteria(MusicTrack.class).setProjection(projectionList) .setMaxResults(configuration.getBatchSize()).addOrder(Order.desc(PROPERTY_AMOUNT)); boolean uniqueFileFound = false; do {// ww w . j av a2s.com progress.info("Delete double files from database."); List doubleList = doubleCriteria.list(); if (doubleList.isEmpty()) { uniqueFileFound = true; } else { for (Object obj : doubleList) { uniqueFileFound = checkDoubleFiles(session, (Object[]) obj) || uniqueFileFound; } } session.flush(); } while (!uniqueFileFound); }
From source file:org.ambraproject.service.user.UserRegistrationServiceImpl.java
License:Apache License
/** * {@inheritDoc}//w w w. jav a 2 s.com * * @param userProfile * @param password */ @Override @Transactional public Long registerUser(UserProfile userProfile, String password) throws DuplicateUserException { int existingUserCount = DataAccessUtils.intResult(hibernateTemplate.findByCriteria( DetachedCriteria.forClass(UserProfile.class).add(Restrictions.eq("email", userProfile.getEmail())) .setProjection(Projections.count("email")))); if (existingUserCount > 0) { throw new DuplicateUserException(DuplicateUserException.Field.EMAIL); } existingUserCount = DataAccessUtils.intResult(hibernateTemplate.findByCriteria(DetachedCriteria .forClass(UserProfile.class).add(Restrictions.eq("displayName", userProfile.getDisplayName())) .setProjection(Projections.count("displayName")))); if (existingUserCount > 0) { throw new DuplicateUserException(DuplicateUserException.Field.DISPLAY_NAME); } try { log.debug("Registering new user with email: {}", userProfile.getEmail()); userProfile.setPassword(passwordDigestService.generateDigest(password)); Long id = (Long) hibernateTemplate.save(userProfile); ambraMailer.sendVerificationEmail(userProfile.getEmail(), userProfile.getVerificationToken()); return id; } catch (DataIntegrityViolationException e) { throw new IllegalArgumentException("Didn't provide required field for user profile", e); } }
From source file:org.ambraproject.service.user.UserRegistrationServiceImpl.java
License:Apache License
/** * {@inheritDoc}//from ww w . j ava 2 s . c om */ @Override @Transactional(readOnly = true) public boolean validateVerificationToken(String email, String verificationToken) { if (StringUtils.isEmpty(email)) { throw new IllegalArgumentException("Must supply an email"); } if (StringUtils.isEmpty(verificationToken)) { throw new IllegalArgumentException("Must supplay an verificationToken"); } int count = DataAccessUtils.intResult(hibernateTemplate .findByCriteria(DetachedCriteria.forClass(UserProfile.class).add(Restrictions.eq("email", email)) .add(Restrictions.eq("verificationToken", verificationToken)) .setProjection(Projections.count("email")))); if (count > 1) { throw new IllegalStateException( "More than one user with email: " + email + " and verification token: " + verificationToken); } return count == 1; }
From source file:org.ambraproject.service.user.UserRegistrationServiceImpl.java
License:Apache License
/** * {@inheritDoc}/*from ww w. ja va 2s .c o m*/ */ @Override @Transactional public String sendEmailChangeMessage(final String oldEmail, final String newEmail, final String password) throws NoSuchUserException, DuplicateUserException { for (Map.Entry<String, String> argument : new HashMap<String, String>() { { put("old email", oldEmail); put("new email", newEmail); put("password", password); } }.entrySet()) { if (StringUtils.isEmpty(argument.getValue())) { throw new IllegalArgumentException("Must supply a(n) " + argument.getKey()); } } UserProfile profile = (UserProfile) DataAccessUtils.uniqueResult(hibernateTemplate.findByCriteria( DetachedCriteria.forClass(UserProfile.class).add(Restrictions.eq("email", oldEmail)))); if (profile == null) { throw new NoSuchUserException("No user with the email: " + oldEmail); } boolean validPassword = passwordDigestService.verifyPassword(password, profile.getPassword()); if (!validPassword) { throw new SecurityException("Invalid password"); } int existingUserCount = DataAccessUtils.intResult(hibernateTemplate.findByCriteria( DetachedCriteria.forClass(UserProfile.class).add(Restrictions.eq("email", newEmail).ignoreCase()) .setProjection(Projections.count("email")))); if (existingUserCount > 0) { throw new DuplicateUserException(DuplicateUserException.Field.EMAIL); } log.debug("sending email change verification to {}", newEmail); profile.setVerificationToken(TokenGenerator.getUniqueToken()); hibernateTemplate.update(profile); ambraMailer.sendChangeEmailNotice(oldEmail, newEmail, profile.getVerificationToken()); return profile.getVerificationToken(); }
From source file:org.ambraproject.service.user.UserRegistrationServiceImpl.java
License:Apache License
@Override @Transactional/* w ww . j av a 2 s .com*/ public void updateEmailAddress(final String oldEmail, final String newEmail, final String verificationToken) throws NoSuchUserException, VerificationTokenException, DuplicateUserException { for (Map.Entry<String, String> argument : new HashMap<String, String>() { { put("old email", oldEmail); put("new email", newEmail); put("verification token", verificationToken); } }.entrySet()) { if (StringUtils.isEmpty(argument.getValue())) { throw new IllegalArgumentException("Must supply a(n) " + argument.getKey()); } } UserProfile profile = (UserProfile) DataAccessUtils.uniqueResult(hibernateTemplate.findByCriteria( DetachedCriteria.forClass(UserProfile.class).add(Restrictions.eq("email", oldEmail)))); if (profile == null) { throw new NoSuchUserException("No user with the email: " + oldEmail); } else if (!verificationToken.equals(profile.getVerificationToken())) { throw new VerificationTokenException("An invalid verification token was given for this user"); } int existingUserCount = DataAccessUtils.intResult(hibernateTemplate.findByCriteria( DetachedCriteria.forClass(UserProfile.class).add(Restrictions.eq("email", newEmail).ignoreCase()) .setProjection(Projections.count("email")))); if (existingUserCount > 0) { throw new DuplicateUserException(DuplicateUserException.Field.EMAIL); } log.debug("Changing email for {} to {}", oldEmail, newEmail); profile.setEmail(newEmail); hibernateTemplate.update(profile); }
From source file:org.apache.ode.daohib.bpel.ProcessInstanceDaoImpl.java
License:Apache License
public EventsFirstLastCountTuple getEventsFirstLastCount() { entering("ProcessInstanceDaoImpl.getEventsFirstLastCount"); // Using a criteria, find the min,max, and count of event tstamps. Criteria c = getSession().createCriteria(HBpelEvent.class); c.add(Restrictions.eq("instance", _instance)); c.setProjection(Projections.projectionList().add(Projections.min("tstamp")).add(Projections.max("tstamp")) .add(Projections.count("tstamp"))); Object[] ret = (Object[]) c.uniqueResult(); EventsFirstLastCountTuple flc = new EventsFirstLastCountTuple(); flc.first = (Date) ret[0];/*from ww w . j a va2 s .co m*/ flc.last = (Date) ret[1]; flc.count = (Integer) ret[2]; return flc; }
From source file:org.candlepin.gutterball.curator.EventCurator.java
License:Open Source License
public boolean hasEventForMessage(String messageId) { // Do not include UNKNOWN since they were Events that // existed pre-update and there is no way to recover the // message ID. Criteria criteria = currentSession().createCriteria(Event.class) .add(Restrictions.eq("messageId", messageId)).add(Restrictions.ne("messageId", "UNKNOWN")) .setProjection(Projections.count("id")); return ((Long) criteria.uniqueResult()) > 0; }
From source file:org.candlepin.model.ConsumerCurator.java
License:Open Source License
public boolean doesConsumerExist(String uuid) { long result = (Long) createSecureCriteria().add(Restrictions.eq("uuid", uuid)) .setProjection(Projections.count("id")).uniqueResult(); return result != 0; }
From source file:org.candlepin.model.JobCurator.java
License:Open Source License
public long findNumRunningByOwnerAndClass(String ownerKey, Class<? extends KingpinJob> jobClass) { return (Long) this.currentSession().createCriteria(JobStatus.class) .add(Restrictions.ge("updated", getBlockingCutoff())) .add(Restrictions.eq("state", JobState.RUNNING)).add(Restrictions.eq("targetId", ownerKey)) .add(Restrictions.eq("jobClass", jobClass)).setProjection(Projections.count("id")).uniqueResult(); }
From source file:org.candlepin.model.ProductCurator.java
License:Open Source License
public boolean productHasSubscriptions(Product prod) { return ((Long) currentSession().createCriteria(Subscription.class) .createAlias("providedProducts", "providedProd", JoinType.LEFT_OUTER_JOIN) .createAlias("derivedProvidedProducts", "derivedProvidedProd", JoinType.LEFT_OUTER_JOIN) .add(Restrictions.or(Restrictions.eq("product", prod), Restrictions.eq("derivedProduct", prod), Restrictions.eq("providedProd.id", prod.getId()), Restrictions.eq("derivedProvidedProd.id", prod.getId()))) .setProjection(Projections.count("id")).uniqueResult()) > 0; }