Package org.openbp.server.persistence

Examples of org.openbp.server.persistence.TransactionGuard


   * Any errors will be logged to the message container of this class.
   */
  protected void readModelsFromStore()
  {
    PersistenceContext pc = getPersistenceContextProvider().obtainPersistenceContext();
    TransactionGuard tg = new TransactionGuard(pc);
    try
    {
      readModelsFromDatabase(pc);
      readItemsFromDatabase(pc);
    }
    catch (PersistenceException e)
    {
      tg.doCatch();
      throw new ModelException("DatabaseOperation", "Error loading models from the database: " + e.getMessage(), e);
    }
    finally
    {
      tg.doFinally();
    }
  }
View Full Code Here


   * @throws PersistenceException On error
   */
  public Iterator runQuery(final PersistenceQuery query)
    throws PersistenceException
  {
    TransactionGuard tg = new TransactionGuard(this);
    try
    {
      Class beanClass = determineEntityClass(query.getObjectClass());
      Criteria hc = getHibernateSession().createCriteria(beanClass);
      if (query.getMaxResults() > 0)
      {
        hc.setMaxResults(query.getMaxResults());
      }

      for (Iterator it = query.getOrderings(); it.hasNext();)
      {
        PersistenceOrdering ordering = (PersistenceOrdering) it.next();

        Order ho = ordering.isAscending() ? Order.asc(ordering.getPropertyName()) : Order.desc(ordering.getPropertyName());
        hc.addOrder(ho);
      }

      for (Iterator it = query.getCriterions(); it.hasNext();)
      {
        PersistenceCriterion criterion = (PersistenceCriterion) it.next();

        String property = criterion.getProperty();
        String operator = criterion.getOperator();
        Object value = criterion.getOperand();
        if (PersistenceCriterion.OPERATOR_EQ.equals(operator))
        {
          hc = hc.add(Restrictions.eq(property, value));
        }
        else if (PersistenceCriterion.OPERATOR_EQ_OR_NULL.equals(operator))
        {
          hc = hc.add(Restrictions.disjunction().add(Restrictions.isNull(property)).add(Restrictions.eq(property, value)));
        }
        else if (PersistenceCriterion.OPERATOR_NEQ.equals(operator))
        {
          hc = hc.add(Restrictions.ne(property, value));
        }
        else if (PersistenceCriterion.OPERATOR_GT.equals(operator))
        {
          hc = hc.add(Restrictions.gt(property, value));
        }
        else if (PersistenceCriterion.OPERATOR_GTE.equals(operator))
        {
          hc = hc.add(Restrictions.ge(property, value));
        }
        else if (PersistenceCriterion.OPERATOR_LT.equals(operator))
        {
          hc = hc.add(Restrictions.lt(property, value));
        }
        else if (PersistenceCriterion.OPERATOR_LTE.equals(operator))
        {
          hc = hc.add(Restrictions.le(property, value));
        }
        else if (PersistenceCriterion.OPERATOR_LIKE.equals(operator))
        {
          hc = hc.add(Restrictions.gt(property, value));
        }
        else if (PersistenceCriterion.OPERATOR_NULL.equals(operator))
        {
          hc = hc.add(Restrictions.isNull(property));
        }
        else if (PersistenceCriterion.OPERATOR_ALIAS.equals(operator))
        {
          hc = hc.createAlias(property, (String) value);
        }
      }

      // Run query and wrap result list into a collection that calls onLoad for each element that is being accessed.
      Collection result = hc.list();
      return result.iterator();
    }
    catch (HibernateException e)
    {
      tg.doCatch();
      throw createLoggedException(e);
    }
    finally
    {
      tg.doFinally();
    }
  }
View Full Code Here

   * @throws PersistenceException On error
   */
  public Object saveObject(final Object o)
    throws PersistenceException
  {
    TransactionGuard tg = new TransactionGuard(this);
    try
    {
      getHibernateSession().saveOrUpdate(o);
      return o;
    }
    catch (HibernateException e)
    {
      tg.doCatch();
      throw createLoggedException(e);
    }
    finally
    {
      tg.doFinally();
    }
  }
View Full Code Here

   * @return The new model or null (error messages go to the message container)
   */
  protected Model readModelFromStore(ModelQualifier modelQualifier)
  {
    PersistenceContext pc = getPersistenceContextProvider().obtainPersistenceContext();
    TransactionGuard tg = new TransactionGuard(pc);
    try
    {
      DbModel dbModel = findDbModel(pc, modelQualifier.getModel());

      Model model = dbModelToModel(dbModel);

      // Register the model
      try
      {
        registerModel(model);
      }
      catch (ModelException e)
      {
        getMsgContainer().addMsg(null, "Error registering model $0 in model manager $1", new Object[]
        {
          model.getName(), getClass().getName(), e
        });
        return null;
      }

      PersistenceQuery query = pc.createQuery(DbModelItem.class);
      query.eq("modelName", model.getName());
      for (Iterator it = pc.runQuery(query); it.hasNext();)
      {
        DbModelItem dbModelItem = (DbModelItem) it.next();

        registerDbModelItem(dbModelItem, model);
      }

      return model;
    }
    catch (PersistenceException e)
    {
      tg.doCatch();
      throw new ModelException("DatabaseOperation", "Error loading model from the database: " + e.getMessage(), e);
    }
    finally
    {
      tg.doFinally();
    }
  }
View Full Code Here

   * @throws PersistenceException On error
   */
  public void deleteObject(final Object o)
    throws PersistenceException
  {
    TransactionGuard tg = new TransactionGuard(this);
    try
    {
      getHibernateSession().delete(o);
    }
    catch (HibernateException e)
    {
      tg.doCatch();
      throw createLoggedException(e);
    }
    finally
    {
      tg.doFinally();
    }
  }
View Full Code Here

   * @return The new item or null (error messages go to the message container)
   */
  protected Item readItemFromStore(ModelQualifier itemQualifier)
  {
    PersistenceContext pc = getPersistenceContextProvider().obtainPersistenceContext();
    TransactionGuard tg = new TransactionGuard(pc);
    try
    {
      DbModelItem dbModelItem = findDbModelItem(pc, itemQualifier.getModel(), itemQualifier.getItem(), itemQualifier.getItemType());

      Item item = registerDbModelItem(dbModelItem, null);

      return item;
    }
    catch (PersistenceException e)
    {
      tg.doCatch();
      throw new ModelException("DatabaseOperation", "Error loading models from the database: " + e.getMessage(), e);
    }
    finally
    {
      tg.doFinally();
    }
  }
View Full Code Here

   * @throws PersistenceException On error
   */
  public int executeUpdateOrDelete(String sql)
    throws PersistenceException
  {
    TransactionGuard tg = new TransactionGuard(this);
    try
    {
      SQLQuery query = getHibernateSession().createSQLQuery(sql);
      int count = query.executeUpdate();
      return count;
    }
    catch (HibernateException e)
    {
      tg.doCatch();
      throw createLoggedException(e);
    }
    finally
    {
      tg.doFinally();
    }
  }
View Full Code Here

   * @throws PersistenceException On error
   */
  public Iterator executeSelect(String sql, int maxResults)
    throws PersistenceException
  {
    TransactionGuard tg = new TransactionGuard(this);
    try
    {
      SQLQuery query = getHibernateSession().createSQLQuery(sql);
      if (maxResults > 0)
      {
        query.setMaxResults(maxResults);
      }

      Collection root = query.list();
      return root.iterator();
    }
    catch (HibernateException e)
    {
      tg.doCatch();
      throw createLoggedException(e);
    }
    finally
    {
      tg.doFinally();
    }
  }
View Full Code Here

    throws PersistenceException
  {
    if (obj != null)
    {
      checkObject(obj);
      TransactionGuard tg = new TransactionGuard(this);
      try
      {
        if (obj instanceof PersistentObjectBase)
        {
          ((PersistentObjectBase) obj).flagAsUnloaded();
          // After fetch, retrieve all values from the delegate and store them into the this object's members.
          ((CayenneObjectBase) obj).readPropertiesFromCayenneMap();
        }
        getDataContext().invalidateObjects(Collections.singletonList(obj));
      }
      catch (CayenneRuntimeException e)
      {
        tg.doCatch();

        // We assume that the object doesn't exist any more.
        String msg = LogUtil.error(getClass(), "Persistence error.", e);
        throw new PersistentObjectNotFoundException(msg, e);
      }
      finally
      {
        tg.doFinally();
      }
    }
    return obj;
  }
View Full Code Here

   * @throws PersistenceException On error
   */
  public Iterator runQuery(final PersistenceQuery query)
    throws PersistenceException
  {
    TransactionGuard tg = new TransactionGuard(this);
    try
    {
      Expression exp = null;

      for (Iterator it = query.getCriterions(); it.hasNext();)
      {
        PersistenceCriterion criterion = (PersistenceCriterion) it.next();

        String property = criterion.getProperty();
        String operator = criterion.getOperator();
        Object value = criterion.getOperand();
        if (PersistenceCriterion.OPERATOR_EQ.equals(operator))
        {
          Expression newExp1 = ExpressionFactory.matchExp(property, value);
          Expression newExp2 = ExpressionFactory.matchExp(property, null);
          Expression newExp = newExp1.orExp(newExp2);

          exp = conjugate(newExp, exp);
        }
        else if (PersistenceCriterion.OPERATOR_EQ_OR_NULL.equals(operator))
        {
          Expression newExp = ExpressionFactory.matchExp(property, value);
          exp = conjugate(newExp, exp);
        }
        else if (PersistenceCriterion.OPERATOR_NEQ.equals(operator))
        {
          Expression newExp = ExpressionFactory.noMatchExp(property, value);
          exp = conjugate(newExp, exp);
        }
        else if (PersistenceCriterion.OPERATOR_GT.equals(operator))
        {
          Expression newExp = ExpressionFactory.noMatchExp(property, value);
          exp = conjugate(newExp, exp);
        }
        else if (PersistenceCriterion.OPERATOR_GTE.equals(operator))
        {
          Expression newExp = ExpressionFactory.noMatchExp(property, value);
          exp = conjugate(newExp, exp);
        }
        else if (PersistenceCriterion.OPERATOR_LT.equals(operator))
        {
          Expression newExp = ExpressionFactory.noMatchExp(property, value);
          exp = conjugate(newExp, exp);
        }
        else if (PersistenceCriterion.OPERATOR_LTE.equals(operator))
        {
          Expression newExp = ExpressionFactory.noMatchExp(property, value);
          exp = conjugate(newExp, exp);
        }
        else if (PersistenceCriterion.OPERATOR_LIKE.equals(operator))
        {
          Expression newExp = ExpressionFactory.likeExp(property, value);
          exp = conjugate(newExp, exp);
        }
        else if (PersistenceCriterion.OPERATOR_NULL.equals(operator))
        {
          Expression newExp = ExpressionFactory.matchExp(property, null);
          exp = conjugate(newExp, exp);
        }
        else if (PersistenceCriterion.OPERATOR_ALIAS.equals(operator))
          throw new PersistenceException("'alias' operation not supproted by Cayenne persistence criterion.");
      }

      Class beanClass = determineEntityClass(query.getObjectClass ());
      SelectQuery cq = new SelectQuery(beanClass, exp);

      if (query.getMaxResults() > 0)
      {
        cq.setFetchLimit(query.getMaxResults());
      }

      for (Iterator it = query.getOrderings(); it.hasNext();)
      {
        PersistenceOrdering ordering = (PersistenceOrdering) it.next();

        cq.addOrdering(ordering.getPropertyName(), ordering.isAscending());
      }

      // Run query and wrap result list into a collection that calls onLoad for each element that is being accessed.
      List result = getDataContext().performQuery(cq);
      return new CayenneIterator(result.iterator());
    }
    catch (Exception e)
    {
      tg.doCatch();
      throw createLoggedException(e);
    }
    finally
    {
      tg.doFinally();
    }
  }
View Full Code Here

TOP

Related Classes of org.openbp.server.persistence.TransactionGuard

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.