@Override
 public void preUpdate(DescriptorEvent event) {
   Object source = event.getSource();
   UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl) event.getSession();
   // preUpdate is also generated for deleted objects that were modified in this UOW.
   // Do not perform preUpdate validation for such objects as preRemove would have already been
   // called.
   if (!unitOfWork.isObjectDeleted(source)) {
     validateOnCallbackEvent(event, "preUpdate", groupPreUpdate);
   }
 }
Ejemplo n.º 2
0
  public void aboutToInsert(DescriptorEvent event) {

    super.aboutToInsert(event);

    if (this.getDelivery() == null) {
      event.updateAttributeWithObject("delivery", false);
    }
    if (this.getExternalCourier() == null) {
      event.updateAttributeWithObject("externalCourier", false);
    }
    if (this.getActive() == null) {
      event.updateAttributeWithObject("active", false);
    }
  }
Ejemplo n.º 3
0
  public void aboutToInsert(DescriptorEvent event) {

    super.aboutToInsert(event);

    if (this.getVirtualEntity() == null) {
      event.updateAttributeWithObject("virtualEntity", false);
    }
    if (this.getLegalEntity() == null) {
      event.updateAttributeWithObject("legalEntity", false);
    }
    if (this.getAccountingEnabled() == null) {
      event.updateAttributeWithObject("accountingEnabled", false);
    }
    if (this.getExternal() == null) {
      event.updateAttributeWithObject("external", false);
    }
    if (this.getInventory() == null) {
      event.updateAttributeWithObject("inventory", false);
    }
    if (this.getCarrier() == null) {
      event.updateAttributeWithObject("carrier", false);
    }
    if (this.getWarehouse() == null) {
      event.updateAttributeWithObject("warehouse", false);
    }
    if (this.getActive() == null) {
      event.updateAttributeWithObject("active", false);
    }
  }
  public void aboutToInsert(DescriptorEvent event) {

    super.aboutToInsert(event);

    if (this.getHasFromInventory() == null) {
      event.updateAttributeWithObject("hasFromInventory", false);
    }
    if (this.getHasToInventory() == null) {
      event.updateAttributeWithObject("hasToInventory", false);
    }
    if (this.getActive() == null) {
      event.updateAttributeWithObject("active", false);
    }
  }
 protected void setup() {
   descriptor = new RelationalDescriptor();
   descriptor.setJavaClass(TargetInvocationWhileEventExecutionTest.class);
   descriptor.addTableName("DummyTable");
   eventManager = descriptor.getEventManager();
   eventManager.setPreUpdateSelector("invalidMethod"); // this method throws an exception
   eventManager.initialize((AbstractSession) getSession());
   event = new DescriptorEvent(new TargetInvocationWhileEventExecutionTest());
   event.setEventCode(DescriptorEventManager.PreUpdateEvent);
   event.setDescriptor(descriptor);
   event.setSession((AbstractSession) getSession());
   expectedException =
       DescriptorException.targetInvocationWhileEventExecution(
           "invalidMethod", descriptor, new Exception());
 }
 /**
  * INTERNAL: Handler for the descriptor post build event. Convert the key and values to their
  * appropriate type.
  */
 public void postBuild(org.eclipse.persistence.descriptors.DescriptorEvent event) {
   setKey(
       event
           .getSession()
           .getDatasourceLogin()
           .getDatasourcePlatform()
           .getConversionManager()
           .convertObject(getKey(), getKeyType()));
   setValue(
       event
           .getSession()
           .getDatasourceLogin()
           .getDatasourcePlatform()
           .getConversionManager()
           .convertObject(getValue(), getValueType()));
 }
  private Validator getValidator(DescriptorEvent event) {
    ClassDescriptor descriptor = event.getDescriptor();
    Validator res = validatorMap.get(descriptor);
    if (res == null) {
      TraversableResolver traversableResolver =
          new AutomaticLifeCycleValidationTraversableResolver(descriptor);
      res = validatorFactory.usingContext().traversableResolver(traversableResolver).getValidator();

      Validator t = validatorMap.put(descriptor, res);
      if (t != null) {
        // Threading collision, use existing
        res = t;
      }
    }

    return res;
  }
 private void validateOnCallbackEvent(
     DescriptorEvent event, String callbackEventName, Class[] validationGroup) {
   Object source = event.getSource();
   boolean shouldValidate = BEAN_VALIDATION_HELPER.isConstrained(source.getClass());
   if (shouldValidate) {
     Set<ConstraintViolation<Object>> constraintViolations =
         getValidator(event).validate(source, validationGroup);
     if (constraintViolations.size() > 0) {
       // There were errors while call to validate above.
       // Throw a ConstrainViolationException as required by the spec.
       // The transaction would be rolled back automatically
       // TODO need to I18N this.
       throw new ConstraintViolationException(
           "Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'"
               + callbackEventName
               + "'. Please refer to embedded ConstraintViolations for details.",
           (Set<ConstraintViolation<?>>)
               (Object) constraintViolations); /* Do not remove the explicit
                       cast. This issue is related to capture#a not being instance of capture#b. */
     }
   }
 }
  /**
   * INTERNAL: Perform the work to delete an object.
   *
   * @return object - the object being deleted.
   */
  public Object executeDatabaseQuery() throws DatabaseException, OptimisticLockException {
    AbstractSession session = getSession();
    CommitManager commitManager = session.getCommitManager();
    Object object = getObject();
    boolean isUnitOfWork = session.isUnitOfWork();
    try {
      // Check if the object has already been committed, then no work is required
      if (commitManager.isProcessedCommit(object)) {
        return object;
      }
      commitManager.markPreModifyCommitInProgress(getObject());
      if (!isUnitOfWork) {
        session.beginTransaction();
      }
      ClassDescriptor descriptor = this.descriptor;
      DescriptorEventManager eventManager = descriptor.getEventManager();
      // PERF: Avoid events if no listeners.
      if (eventManager.hasAnyEventListeners()) {
        // Need to run pre-delete selector if available
        eventManager.executeEvent(new DescriptorEvent(DescriptorEventManager.PreDeleteEvent, this));
      }

      // Verify if deep shallow modify is turned on
      if (shouldCascadeParts()) {
        descriptor.getQueryManager().preDelete(this);
      }

      // CR#2660080 missing aboutToDelete event.
      // PERF: Avoid events if no listeners.
      if (eventManager.hasAnyEventListeners()) {
        DescriptorEvent event =
            new DescriptorEvent(DescriptorEventManager.AboutToDeleteEvent, this);
        event.setRecord(getModifyRow());
        eventManager.executeEvent(event);
      }

      if (QueryMonitor.shouldMonitor()) {
        QueryMonitor.incrementDelete(this);
      }
      int rowCount = 0;
      // If the object was/will be deleted from a cascade delete constraint, ignore it.
      if (isUnitOfWork
          && ((UnitOfWorkImpl) session).hasCascadeDeleteObjects()
          && ((UnitOfWorkImpl) session).getCascadeDeleteObjects().contains(object)) {
        // Cascade delete does not check optimistic lock, assume ok.
        rowCount = 1;
      } else {
        rowCount = getQueryMechanism().deleteObject().intValue();
      }

      if (rowCount < 1) {
        if (session.hasEventManager()) {
          session.getEventManager().noRowsModified(this, object);
        }
      }

      if (descriptor.usesOptimisticLocking()) {
        descriptor.getOptimisticLockingPolicy().validateDelete(rowCount, object, this);
      }

      commitManager.markPostModifyCommitInProgress(getObject());
      // Verify if deep shallow modify is turned on
      if (shouldCascadeParts()) {
        descriptor.getQueryManager().postDelete(this);
      }

      if ((descriptor.getHistoryPolicy() != null)
          && descriptor.getHistoryPolicy().shouldHandleWrites()) {
        descriptor.getHistoryPolicy().postDelete(this);
      }

      // PERF: Avoid events if no listeners.
      if (eventManager.hasAnyEventListeners()) {
        // Need to run post-delete selector if available
        eventManager.executeEvent(
            new DescriptorEvent(DescriptorEventManager.PostDeleteEvent, this));
      }

      if (!isUnitOfWork) {
        session.commitTransaction();
      }
      commitManager.markCommitCompleted(object);

      // CR3510313, avoid removing aggregate collections from cache (maintain cache is false).
      if (shouldMaintainCache()) {
        if (isUnitOfWork) {
          ((UnitOfWorkImpl) session).addObjectDeletedDuringCommit(object, descriptor);
        } else {
          session
              .getIdentityMapAccessorInstance()
              .removeFromIdentityMap(
                  getPrimaryKey(), descriptor.getJavaClass(), descriptor, object);
        }
      }
      return object;

    } catch (RuntimeException exception) {
      if (!isUnitOfWork) {
        session.rollbackTransaction();
      }
      commitManager.markCommitCompleted(object);
      throw exception;
    }
  }
  /**
   * INTERNAL: Perform the work to delete an object.
   *
   * @return object - the object being deleted.
   */
  public Object executeDatabaseQuery() throws DatabaseException, OptimisticLockException {
    AbstractSession session = getSession();
    CommitManager commitManager = session.getCommitManager();
    Object object = getObject();
    boolean isUnitOfWork = session.isUnitOfWork();
    try {
      // Check if the object has already been deleted, then no work is required.
      if (commitManager.isCommitCompletedInPostOrIgnore(object)) {
        return object;
      }
      ClassDescriptor descriptor = this.descriptor;
      // Check whether the object is already being deleted,
      // if it is, then there is a cycle, and the foreign keys must be nulled.
      if (commitManager.isCommitInPreModify(object)) {
        if (!commitManager.isShallowCommitted(object)) {
          getQueryMechanism().updateForeignKeyFieldBeforeDelete();
          if ((descriptor.getHistoryPolicy() != null)
              && descriptor.getHistoryPolicy().shouldHandleWrites()) {
            descriptor.getHistoryPolicy().postUpdate(this);
          }
          commitManager.markShallowCommit(object);
        }
        return object;
      }
      commitManager.markPreModifyCommitInProgress(getObject());
      if (!isUnitOfWork) {
        session.beginTransaction();
      }
      DescriptorEventManager eventManager = descriptor.getEventManager();
      // PERF: Avoid events if no listeners.
      if (eventManager.hasAnyEventListeners()) {
        // Need to run pre-delete selector if available
        eventManager.executeEvent(new DescriptorEvent(DescriptorEventManager.PreDeleteEvent, this));
      }

      // Verify if deep shallow modify is turned on
      if (shouldCascadeParts()) {
        descriptor.getQueryManager().preDelete(this);
      }

      // Check for deletion dependencies.
      if (isUnitOfWork) {
        Set dependencies = ((UnitOfWorkImpl) session).getDeletionDependencies(object);
        if (dependencies != null) {
          for (Object dependency : dependencies) {
            if (!commitManager.isCommitCompletedInPostOrIgnore(dependency)) {
              ClassDescriptor dependencyDescriptor = session.getDescriptor(dependency);
              // PERF: Get the descriptor query, to avoid extra query creation.
              DeleteObjectQuery deleteQuery =
                  dependencyDescriptor.getQueryManager().getDeleteQuery();
              if (deleteQuery == null) {
                deleteQuery = new DeleteObjectQuery();
                deleteQuery.setDescriptor(dependencyDescriptor);
              } else {
                // Ensure original query has been prepared.
                deleteQuery.checkPrepare(session, deleteQuery.getTranslationRow());
                deleteQuery = (DeleteObjectQuery) deleteQuery.clone();
              }
              deleteQuery.setIsExecutionClone(true);
              deleteQuery.setObject(dependency);
              session.executeQuery(deleteQuery);
            }
          }
        }
      }

      // CR#2660080 missing aboutToDelete event.
      // PERF: Avoid events if no listeners.
      if (eventManager.hasAnyEventListeners()) {
        DescriptorEvent event =
            new DescriptorEvent(DescriptorEventManager.AboutToDeleteEvent, this);
        event.setRecord(getModifyRow());
        eventManager.executeEvent(event);
      }

      if (QueryMonitor.shouldMonitor()) {
        QueryMonitor.incrementDelete(this);
      }
      int rowCount = 0;
      // If the object was/will be deleted from a cascade delete constraint, ignore it.
      if (isUnitOfWork
          && ((UnitOfWorkImpl) session).hasCascadeDeleteObjects()
          && ((UnitOfWorkImpl) session).getCascadeDeleteObjects().contains(object)) {
        // Cascade delete does not check optimistic lock, assume ok.
        rowCount = 1;
      } else {
        rowCount = getQueryMechanism().deleteObject().intValue();
      }

      if (rowCount < 1) {
        if (session.hasEventManager()) {
          session.getEventManager().noRowsModified(this, object);
        }
      }

      if (this.usesOptimisticLocking) {
        descriptor.getOptimisticLockingPolicy().validateDelete(rowCount, object, this);
      }

      commitManager.markPostModifyCommitInProgress(getObject());
      // Verify if deep shallow modify is turned on
      if (shouldCascadeParts()) {
        descriptor.getQueryManager().postDelete(this);
      }

      if ((descriptor.getHistoryPolicy() != null)
          && descriptor.getHistoryPolicy().shouldHandleWrites()) {
        descriptor.getHistoryPolicy().postDelete(this);
      }

      // PERF: Avoid events if no listeners.
      if (eventManager.hasAnyEventListeners()) {
        // Need to run post-delete selector if available
        eventManager.executeEvent(
            new DescriptorEvent(DescriptorEventManager.PostDeleteEvent, this));
      }

      if (!isUnitOfWork) {
        session.commitTransaction();
      }
      commitManager.markCommitCompleted(object);

      // CR3510313, avoid removing aggregate collections from cache (maintain cache is false).
      if (shouldMaintainCache()) {
        if (isUnitOfWork) {
          ((UnitOfWorkImpl) session).addObjectDeletedDuringCommit(object, descriptor);
        } else {
          session
              .getIdentityMapAccessorInstance()
              .removeFromIdentityMap(
                  getPrimaryKey(), descriptor.getJavaClass(), descriptor, object);
        }
      }
      return object;

    } catch (RuntimeException exception) {
      if (!isUnitOfWork) {
        session.rollbackTransaction();
      }
      commitManager.markCommitCompleted(object);
      throw exception;
    }
  }