FileDocCategorySizeDatePackage
EntityQueryTestBean.javaAPI DocJBoss 4.2.110159Fri Jul 13 20:53:06 BST 2007org.jboss.ejb3.test.clusteredentity.classloader

EntityQueryTestBean

public class EntityQueryTestBean extends Object implements EntityQueryTest
Comment
author
Brian Stansberry
version
$Revision: 60233 $

Fields Summary
private static final Logger
log
private EntityManager
manager
private String
cacheObjectName
private MyListener
listener
Constructors Summary
public EntityQueryTestBean()


    
         
   
Methods Summary
public voidcleanup()

  
      Query query = manager.createQuery("select account from Account as account");
      List accts = query.getResultList();
      if (accts != null)
      {
         for (Iterator it = accts.iterator(); it.hasNext();)
         {
            try
            {
               Account acct = (Account) it.next();
               log.info("Removing " + acct);
               manager.remove(acct);
            }
            catch (Exception ignored) {}
         }
      }
   
public voidcreateAccount(AccountHolderPK pk, java.lang.Integer id, java.lang.Integer openingBalance, java.lang.String branch)

      Account account = new Account();
      account.setId(id);
      account.setAccountHolder(pk);
      account.setBalance(openingBalance);
      account.setBranch(branch);
      manager.persist(account);
   
public java.lang.StringgetBranch(AccountHolderPK pk, boolean useNamed, boolean useRegion)

      if (useNamed)
         return getBranchViaNamedQuery(pk, useRegion);
      else
         return getBranchViaLocalQuery(pk, useRegion);
   
private java.lang.StringgetBranchViaLocalQuery(AccountHolderPK pk, boolean useRegion)

      Query query = manager.createQuery("select account.branch from Account as account where account.accountHolder = ?1");
      query.setParameter(1, pk);
      if (useRegion)
      {
         query.setHint("org.hibernate.cacheRegion", "AccountRegion");
      }
      query.setHint("org.hibernate.cacheable", Boolean.TRUE);
      return (String) query.getResultList().get(0);
   
private java.lang.StringgetBranchViaNamedQuery(AccountHolderPK pk, boolean useRegion)

      String queryName = useRegion ? "account.branch.namedregion"
                                   : "account.branch.default";
      Query query = manager.createNamedQuery(queryName);
      query.setParameter(1, pk);
      return (String) query.getResultList().get(0);
   
public voidgetCache(boolean optimistic)

      if (optimistic)
         cacheObjectName = "jboss.cache:service=OptimisticEJB3EntityTreeCache";
      else
         cacheObjectName = "jboss.cache:service=EJB3EntityTreeCache";

      try
      {
         //Just to initialise the cache with a listener
         TreeCache cache = getCache();
         listener = new MyListener();
         cache.addTreeCacheListener(listener);         
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
   
private org.jboss.cache.TreeCachegetCache()

      MBeanServer server = MBeanServerLocator.locateJBoss();
      TreeCacheMBean proxy = (TreeCacheMBean)MBeanProxyExt.create(TreeCacheMBean.class, new ObjectName(cacheObjectName), server);
      TreeCache cache = proxy.getInstance();
      
      return cache;
   
public intgetCountForBranch(java.lang.String branch, boolean useNamed, boolean useRegion)

      if (useNamed)
         return getCountForBranchViaNamedQuery(branch, useRegion);
      else
         return getCountForBranchViaLocalQuery(branch, useRegion);
   
private intgetCountForBranchViaLocalQuery(java.lang.String branch, boolean useRegion)

      Query query = manager.createQuery("select account from Account as account where account.branch = ?1");
      query.setParameter(1, branch);
      if (useRegion)
      {
         query.setHint("org.hibernate.cacheRegion", "AccountRegion");
      }
      query.setHint("org.hibernate.cacheable", Boolean.TRUE);
      return query.getResultList().size();
      
   
private intgetCountForBranchViaNamedQuery(java.lang.String branch, boolean useRegion)

      String queryName = useRegion ? "account.bybranch.namedregion"
                                   : "account.bybranch.default";
      Query query = manager.createNamedQuery(queryName);
      query.setParameter(1, branch);
      return query.getResultList().size();      
   
private booleangetSawRegion(java.lang.String regionName, java.util.Set sawEvent)

      boolean saw = false;
      Fqn fqn = Fqn.fromString(regionName);
      for (Iterator it = sawEvent.iterator(); it.hasNext();)
      {
         Fqn modified = (Fqn) it.next();
         if (modified.isChildOf(fqn))
         {
            it.remove();
            saw = true;
         }
      }
   return saw;
      
   
public booleangetSawRegionAccess(java.lang.String regionName)

      return getSawRegion(regionName, listener.accessed);
   
public booleangetSawRegionModification(java.lang.String regionName)

      return getSawRegion(regionName, listener.modified);
   
public intgetTotalBalance(AccountHolderPK pk, boolean useNamed, boolean useRegion)

      if (useNamed)
         return getTotalBalanceViaNamedQuery(pk, useRegion);
      else
         return getTotalBalanceViaLocalQuery(pk, useRegion);
   
private intgetTotalBalanceViaLocalQuery(AccountHolderPK pk, boolean useRegion)

      Query query = manager.createQuery("select account.balance from Account as account where account.accountHolder = ?1");
      query.setParameter(1, pk);
      query.setHint("org.hibernate.cacheable", Boolean.TRUE);
      return totalBalances(query);
   
private intgetTotalBalanceViaNamedQuery(AccountHolderPK pk, boolean useRegion)

      String queryName = useRegion ? "account.totalbalance.namedregion"
                                   : "account.totalbalance.default";
      Query query = manager.createNamedQuery(queryName);
      query.setParameter(1, pk);
      return totalBalances(query);
   
public voidremove()

      cleanup();
      listener.clear();
      try
      {
         getCache().removeTreeCacheListener(listener);
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   
private inttotalBalances(javax.persistence.Query balanceQuery)

      List results = balanceQuery.getResultList();
      int total = 0;
      if (results != null)
      {
         for (Iterator it = results.iterator(); it.hasNext();)
         {            
            total += ((Integer) it.next()).intValue();
            System.out.println("Total = " + total);
         }
      }
      return total;      
   
public voidupdateAccountBalance(java.lang.Integer id, java.lang.Integer newBalance)

      Account account = (Account) manager.find(Account.class, id);
      account.setBalance(newBalance);
   
public voidupdateAccountBranch(java.lang.Integer id, java.lang.String branch)

      Account account = (Account) manager.find(Account.class, id);
      account.setBranch(branch);