if ( getLogger().isDebugEnabled() ) { getLogger().debug( counts ); }    if ( total == 0 ) { object.getDetails().getPermissions().unSet( Flag.LOCKED ); isUnlocked[i] = true; } else { isUnlocked[i] = false; }
final IObject object = (IObject) s.load(orig.getClass(), orig .getId());   if (!object.getDetails().getPermissions().isSet(Flag.LOCKED)) { isUnlocked[i] = true; continue; }     final Class<? extends IObject> klass = Utils.trueClass(object .getClass());  final long id = object.getId().longValue();   String[][] checks = em.getLockChecks(klass);   long total = 0L; Map<String, Long> counts = new HashMap<String, Long>();   for (String[] check : checks) { final String hql = String.format( "select count(*) from %s where %s%s = :id ", check[0], check[1], ".id"); org.hibernate.Query q = s.createQuery(hql); q.setLong("id", id); Long count = (Long) q.iterate().next();  if (count != null && count.longValue() > 0) { total += count.longValue(); counts.put(hql, count); } }   if (getLogger().isDebugEnabled()) { getLogger().debug(counts); }    if (total == 0) { object.getDetails().getPermissions().unSet(Flag.LOCKED); isUnlocked[i] = true; } else { isUnlocked[i] = false; }
    public boolean[] unlock(final IObject...iObjects )    {    	// do nothing if possible    	if ( iObjects == null | iObjects.length < 1 )    		return new boolean[]{};    	    	// create a new session. It's important that we pass in the empty    	// interceptor here, otherwise even root wouldn't be allowed to unlock    	// the instance.    	Session s =     	SessionFactoryUtils.getNewSession( sf, EmptyInterceptor.INSTANCE );    	    	Transaction tx     	= s.beginTransaction();    	    	try     	{    		boolean[] isUnlocked = new boolean[iObjects.length];    		for (int i = 0; i < iObjects.length; i++) {				IObject orig = iObjects[i];					    		// do nothing if possible again.	    		if ( orig == null || orig.getId() == null ) 	    		{	    			isUnlocked[i] = true;	    			continue;	    		}	    			    		// get the original to operate on	    		final IObject object = (IObject) 	    			s.load( orig.getClass(), orig.getId() );	    			    		// if it's not locked, we don't need to look further.	    		if ( ! object.getDetails().getPermissions().isSet(Flag.LOCKED) )	    		{	    			isUnlocked[i] = true;	    			continue;	    		}	    			    		// since it's a managed entity it's class.getName() might contain	    		// some byte-code generation string	    		final Class<? extends IObject> klass = 	    			Utils.trueClass( object.getClass() );	    			    		final long  id    = object.getId().longValue();	    			    		// the values that could possibly link to this instance.	    		String[][] checks = em.getLockChecks( klass );					    		// reporting	    		long total = 0L;	    		Map<String,Long> counts = new HashMap<String,Long>();	    			    		// run the individual queries	    		for (String[] check : checks) {	    			final String hql = String.format(	    					"select count(*) from %s where %s%s = :id ",	    					check[0],check[1],".id");	    			org.hibernate.Query q = s.createQuery(hql);	    			q.setLong("id",id);	    			Long count = (Long) q.iterate().next();		    			if ( count != null && count.longValue() > 0 )	    			{	    				total += count.longValue();	    				counts.put( hql, count );	    			}				}	    			    		// reporting	    		if ( getLogger().isDebugEnabled() )	    		{	    			getLogger().debug( counts );	    		}	    			    		// if there are no links, the we can unlock	    		// the actual unlocking happens on flush below.	    		if ( total == 0 )	    		{	    			object.getDetails().getPermissions().unSet( Flag.LOCKED );	    			isUnlocked[i] = true;	    		} else {	    			isUnlocked[i] = false;	    		}	    					}    		return isUnlocked;	    }     	    	finally	    {			s.flush();			tx.commit();			s.close();	    }    }