/** * Check if the item is accessible for the given User and session. It is if there is no lock or * the user owns the lock. * * @param item * @return */ public boolean canAccess(User u, String sessionId, Lockable item) { boolean result = true; StatelessSession s = getSession(); Transaction tx = s.beginTransaction(); try { Lock example = createLock(item); Lock lock = (Lock) s.createCriteria(Lock.class) .add( Example.create(example) .excludeProperty("httpSessionId") .excludeProperty("userLogin") .excludeProperty("name")) .uniqueResult(); tx.commit(); if (lock != null) { if ((lock.getUserLogin().equals(u.getLogin())) && (lock.getHttpSessionId().equals(sessionId))) result = true; else result = false; } } catch (HibernateException he) { log.debug("Lock problem in canAccess"); tx.rollback(); } return result; }
/** * Aquire lock for given item or check that you still have it. This should be done before database * objects are updated, not just before they are made persistent. It should not be done (Cannot be * done) with objects that don't have a dbID yet. * * @param u * @param sessionId * @param item * @return */ public boolean aquireLock(User u, String sessionId, Lockable item) { boolean result = false; StatelessSession s = getSession(); Transaction tx = s.beginTransaction(); if ((item == null) || (item.getDbID() == null)) throw new Error("Unlockable item"); try { Lock lock = createLock(item); lock.setUserLogin(u.getLogin()); lock.setHttpSessionId(sessionId); log.debug("Aquire Lock\n" + lock.toString()); List<Lock> l = s.createQuery("from Lock where objectId = :id and objectType = :type") .setLong("id", lock.getObjectId()) .setString("type", lock.getObjectType()) .list(); if (l.size() == 0) { s.insert(lock); tx.commit(); result = true; } else { Lock lock2 = l.get(0); if (lock2.getUserLogin().equals(lock.getUserLogin()) && lock2.getHttpSessionId().equals(lock.getHttpSessionId())) result = true; else result = false; tx.commit(); } } catch (HibernateException he) { log.debug("lock aquire failed"); StringWriter sw = new StringWriter(); he.printStackTrace(new PrintWriter(sw)); log.error(sw.toString()); tx.rollback(); } return result; }
/** * Releases the lock on the item (only for given user and session). Returns false if the lock * didn't exist (or was owned by somebody else) * * @param u * @param sessionId * @param item * @return */ public boolean releaseLock(User u, String sessionId, Lockable item) { boolean result = false; StatelessSession s = getSession(); Transaction tx = s.beginTransaction(); try { Lock example = createLock(item); example.setHttpSessionId(sessionId); example.setUserLogin(u.getLogin()); Lock lock = (Lock) s.createCriteria(Lock.class) .add(Example.create(example).excludeProperty("name")) .uniqueResult(); s.delete(lock); tx.commit(); result = true; } catch (HibernateException he) { log.debug("lock not released"); tx.rollback(); } return result; }
public List<Lock> findByUser(User u) { StatelessSession s = getSession(); List<Lock> l = s.createQuery("from Lock where userLogin = :login").setString("login", u.getLogin()).list(); return l; }