List of usage examples for org.hibernate Query iterate
Iterator<R> iterate();
From source file:de.eod.jliki.users.utils.UserDBHelper.java
License:Open Source License
/** * Adds a new user to the database if it does not exist.<br/> * @param user the user to add//from w ww . j av a2 s . c o m * @return a hash string of the added user if the user did not exist, <code>null</code> if it existed or insertion failed */ public static String addUserToDB(final User user) { final SessionFactory sf = DBSetup.getDbManager().getSessionFactory(); final Session session = sf.openSession(); final Transaction trx = session.beginTransaction(); final Query nameQuery = session .createQuery("select holder from PermissionHolder as holder where holder.name=:name"); nameQuery.setString("name", user.getName()); final Query emailQuery = session.createQuery("select user from User as user where user.email=:email"); emailQuery.setString("email", user.getEmail()); User dbUser = null; String userhash = null; if (!nameQuery.iterate().hasNext() && !emailQuery.iterate().hasNext()) { session.save(user); final Query userQuery = session.createQuery("select user from User as user where user.name=:username"); userQuery.setString("username", user.getName()); final Iterator<?> it = userQuery.iterate(); if (userQuery.iterate().hasNext()) { dbUser = (User) it.next(); userhash = PasswordHashUtility.generateHashForUrl(dbUser.toString()); } } trx.commit(); session.close(); return userhash; }
From source file:de.eod.jliki.users.utils.UserDBHelper.java
License:Open Source License
/** * Tests if a user is in the database.<br/> * @param username the username to check * @return <code>true</code> if the user is in the database *///from w w w . j a v a 2s .c o m public static boolean isUserInDB(final String username) { final SessionFactory sf = DBSetup.getDbManager().getSessionFactory(); final Session session = sf.openSession(); final Transaction trx = session.beginTransaction(); final Query query = session.createQuery("select user from User as user where user.name=:username"); query.setString("username", username); final boolean isUserInDB = query.iterate().hasNext(); trx.commit(); session.close(); return isUserInDB; }
From source file:de.eod.jliki.users.utils.UserDBHelper.java
License:Open Source License
/** * Activates a user with the given hash and logs it in.<br/> * @param username the username to activate * @param userHash the users hash//from w w w . ja va2 s .co m * @param userLogin the login object to log the user in * @return <code>true</code> if activation worked, <code>false</code> if not */ public static boolean activateUser(final String username, final String userHash, final LoginBean userLogin) { final SessionFactory sf = DBSetup.getDbManager().getSessionFactory(); final Session session = sf.openSession(); final Transaction trx = session.beginTransaction(); final Query userQuery = session.createQuery("select user from User as user where user.name = :username"); userQuery.setString("username", username); User dbUser = null; boolean didActivate = false; final Iterator<?> it = userQuery.iterate(); if (it.hasNext()) { dbUser = (User) it.next(); final String dbUserHash = PasswordHashUtility.generateHashForUrl(dbUser.toString()); if (userHash.equals(dbUserHash) && dbUser.getActive() == ActiveState.INACTIVE) { didActivate = true; dbUser.setActive(ActiveState.ACTIVE); } } final Query groupQuery = session .createQuery("select group from usergroup as group where group.name = :groupname"); groupQuery.setString("groupname", "users"); UserGroup users = null; final Iterator<?> grpIt = groupQuery.iterate(); if (grpIt.hasNext() && didActivate && dbUser != null) { users = (UserGroup) grpIt.next(); dbUser.getGroups().add(users); } if (dbUser != null && didActivate) { UserDBHelper.loginUser(dbUser, didActivate, false, userLogin, session); } trx.commit(); session.close(); return didActivate; }
From source file:de.eod.jliki.users.utils.UserDBHelper.java
License:Open Source License
/** * Logs the user in if it exists and password matches.<br/> * @param username the users username//from w w w . j a v a2 s. c o m * @param pass the password * @param rememberMe flag if cookie is to be set to remember the user * @param userLogin the login object * @return true if login succeeded, false if not */ public static boolean loginUser(final String username, final String pass, final boolean rememberMe, final LoginBean userLogin) { final SessionFactory sf = DBSetup.getDbManager().getSessionFactory(); final Session session = sf.openSession(); final Transaction trx = session.beginTransaction(); final Query query = session.createQuery("select user from User as user where user.name = :username"); query.setString("username", username); User dbUser = null; final Iterator<?> it = query.iterate(); if (query.iterate().hasNext()) { dbUser = (User) it.next(); } else { dbUser = new User(); } final boolean passedLogin = PasswordHashUtility.verifyPassword(pass, dbUser.getPassword(), Base64.decodeBase64(dbUser.getSalt())); final boolean didLogin = UserDBHelper.loginUser(dbUser, passedLogin, rememberMe, userLogin, session); trx.commit(); session.close(); return didLogin; }
From source file:de.eod.jliki.users.utils.UserDBHelper.java
License:Open Source License
/** * Tries to log in a user with a cookie id.<br/> * @param cookieId the cookie id/*from w ww . j av a 2s. c o m*/ * @param userLogin the login object * @return true if login succeeded, false if not */ public static boolean loginUserWithCookie(final String cookieId, final LoginBean userLogin) { final SessionFactory sf = DBSetup.getDbManager().getSessionFactory(); final Session session = sf.openSession(); final Transaction trx = session.beginTransaction(); final Query query = session.createQuery("select user from User as user where user.cookieid = :cookieid"); query.setString("cookieid", cookieId); User dbUser = null; boolean passedLogin = false; final Iterator<?> it = query.iterate(); if (query.iterate().hasNext()) { dbUser = (User) it.next(); passedLogin = true; } else { dbUser = new User(); passedLogin = false; } final boolean didLogin = UserDBHelper.loginUser(dbUser, passedLogin, true, userLogin, session); trx.commit(); session.close(); return didLogin; }
From source file:de.eod.jliki.users.utils.UserDBHelper.java
License:Open Source License
/** * Logs out the user.<br/>/*from w ww .ja va 2 s . c o m*/ * @param userLogin the login object */ public static void logoutUser(final LoginBean userLogin) { final SessionFactory sf = DBSetup.getDbManager().getSessionFactory(); final Session session = sf.openSession(); final Transaction trx = session.beginTransaction(); final Query query = session.createQuery("select user from User as user where user.name = :username"); query.setString("username", userLogin.getUserName()); final Iterator<?> it = query.iterate(); if (query.iterate().hasNext()) { final User dbUser = (User) it.next(); dbUser.setCookieid(""); session.update(dbUser); } trx.commit(); session.close(); userLogin.setLoggedIn(false); userLogin.setPassword(null); userLogin.setRememberMe(false); userLogin.setUserName(null); userLogin.clearPermissions(); if (userLogin.isRememberMe()) { final HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance() .getExternalContext().getResponse(); final Cookie cookie = new Cookie("login", ""); cookie.setMaxAge(0); httpServletResponse.addCookie(cookie); } userLogin.setRememberMe(false); }
From source file:de.eod.jliki.users.utils.UserDBHelper.java
License:Open Source License
/** * Sets the given permissions to the group. <code>NOTHING</code>-Permissions will be deleted.<br/> * @param holderName the groups name/*from w w w. j av a 2 s. c om*/ * @param permissions the permissions to add */ public static void setHolderPermissions(final String holderName, final Permission[] permissions) { final SessionFactory sf = DBSetup.getDbManager().getSessionFactory(); final Session session = sf.openSession(); final Transaction trx = session.beginTransaction(); final Query holderQuery = session .createQuery("select holder from PermissionHolder as holder where holder.name = :holdername"); holderQuery.setString("holdername", holderName); PermissionHolder holder = null; final Iterator<?> grpIt = holderQuery.iterate(); if (grpIt.hasNext()) { holder = (PermissionHolder) grpIt.next(); for (final Permission perm : permissions) { UserDBHelper.addPermissionToHolderWithoutDuplicates(session, holder, perm); } } trx.commit(); session.close(); }
From source file:de.eod.jliki.users.utils.UserDBHelper.java
License:Open Source License
/** * Adds a single permission to the holder making sure no duplicates exist.<br/> * @param session the hibernate session for the permission query * @param holder the holder to add the permission to * @param perm the permission to add//from w w w .j a v a 2 s . c o m */ private static void addPermissionToHolderWithoutDuplicates(final Session session, final PermissionHolder holder, final Permission perm) { final Query permQuery = session.createQuery("select permission from Permission as permission where " + "permission.permissionName = :name and permission.category = :category and " + "permission.type = :type"); permQuery.setString("name", perm.getPermissionName()); permQuery.setString("category", perm.getCategory()); permQuery.setInteger("type", perm.getType().ordinal()); Permission dbPerm = null; final Iterator<?> permIt = permQuery.iterate(); if (permIt.hasNext()) { dbPerm = (Permission) permIt.next(); holder.addPermission(dbPerm); } else { holder.addPermission(perm); } }
From source file:de.eod.jliki.users.utils.UserDBScheduler.java
License:Open Source License
/** * @see java.util.TimerTask#run()//w w w. j a va 2 s .c om * {@inheritDoc} */ @Override public final void run() { final SessionFactory sf = DBSetup.getDbManager().getSessionFactory(); final Session session = sf.openSession(); final Transaction trx = session.beginTransaction(); // delete inactive users older than 24 hours final Calendar yesterday = Calendar.getInstance(); yesterday.add(Calendar.DATE, -1); final Query deleteInactive = session .createQuery("delete User as u where u.active=:active " + "and u.registerdate < :yesterday"); deleteInactive.setInteger("active", ActiveState.INACTIVE.ordinal()); deleteInactive.setDate("yesterday", yesterday.getTime()); deleteInactive.executeUpdate(); // delete session id if invalid final int aWeek = -7; final Calendar aWeekAgo = Calendar.getInstance(); aWeekAgo.add(Calendar.DATE, aWeek); final Query deleteCookieId = session .createQuery("from User as u where u.cookieid is not empty and u.lastlogin < :aweekago"); deleteCookieId.setDate("aweekago", aWeekAgo.getTime()); final Iterator<?> it = deleteCookieId.iterate(); while (it.hasNext()) { final User user = (User) it.next(); user.setCookieid(""); session.update(user); } trx.commit(); session.close(); }
From source file:de.innovationgate.webgate.api.jdbc.filehandling.CS3FileAttachmentHandler.java
License:Open Source License
private Iterator<?> fetchFileEntity(String strFile) throws WGAPIException, HibernateException { // We cannot query if the entity is transient if (_doc.getCreated() == null) { return Collections.emptyList().iterator(); }// w ww. j a va 2 s . c o m String hqlQuery = "select cf from " + _entityDescriptor.getHQLFileEntity() + " cf where cf." + _entityDescriptor.getHQLParentProperty() + "=:entity and cf.name=:name"; Query query = _handling.getParent().getSession().createQuery(hqlQuery); query.setParameter("entity", _entity); query.setParameter("name", strFile); Iterator<?> it = query.iterate(); return it; }