@Override
  public void duringFinally(MethodInvocation methodInvocation) {
    SystemEvent systemEvent = findAnnotation(methodInvocation);

    if (systemEvent == _nullSystemEvent) {
      return;
    }

    if (!isValid(methodInvocation, _PHASE_DURING_FINALLY)) {
      return;
    }

    if (systemEvent.action() == SystemEventConstants.ACTION_NONE) {
      return;
    }

    Object[] arguments = methodInvocation.getArguments();

    ClassedModel classedModel = (ClassedModel) arguments[0];

    long classPK = getClassPK(classedModel);

    if (classPK == 0) {
      return;
    }

    SystemEventHierarchyEntryThreadLocal.pop(getClassName(classedModel), classPK);
  }
  @Override
  public Object before(MethodInvocation methodInvocation) throws Throwable {
    SystemEvent systemEvent = findAnnotation(methodInvocation);

    if (systemEvent == _nullSystemEvent) {
      return null;
    }

    if (systemEvent.action() != SystemEventConstants.ACTION_NONE) {
      if (!isValid(methodInvocation, _PHASE_BEFORE)) {
        return null;
      }

      Object[] arguments = methodInvocation.getArguments();

      ClassedModel classedModel = (ClassedModel) arguments[0];

      SystemEventHierarchyEntry systemEventHierarchyEntry =
          SystemEventHierarchyEntryThreadLocal.push(
              getClassName(classedModel), getClassPK(classedModel), systemEvent.action());

      if (systemEventHierarchyEntry != null) {
        systemEventHierarchyEntry.setUuid(getUuid(classedModel));
      }
    }

    return null;
  }
  @Override
  public void deletePortletFolder(long folderId) throws PortalException, SystemException {

    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();

    try {
      DLAppHelperThreadLocal.setEnabled(false);

      SystemEventHierarchyEntryThreadLocal.push(Folder.class);

      DLAppLocalServiceUtil.deleteFolder(folderId);
    } catch (NoSuchRepositoryEntryException nsree) {
      if (_log.isErrorEnabled()) {
        _log.error(nsree, nsree);
      }
    } finally {
      DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);

      SystemEventHierarchyEntryThreadLocal.pop(Folder.class);
    }
  }
  @Override
  public void afterReturning(MethodInvocation methodInvocation, Object result) throws Throwable {

    SystemEvent systemEvent = findAnnotation(methodInvocation);

    if ((systemEvent == _nullSystemEvent) || !systemEvent.send()) {
      return;
    }

    if (!isValid(methodInvocation, _PHASE_AFTER_RETURNING)) {
      return;
    }

    Object[] arguments = methodInvocation.getArguments();

    ClassedModel classedModel = (ClassedModel) arguments[0];

    long groupId = getGroupId(classedModel);

    String className = getClassName(classedModel);

    String referrerClassName = null;

    if (classedModel instanceof TypedModel) {
      TypedModel typedModel = (TypedModel) classedModel;

      referrerClassName = typedModel.getClassName();
    }

    long classPK = getClassPK(classedModel);

    SystemEventHierarchyEntry systemEventHierarchyEntry =
        SystemEventHierarchyEntryThreadLocal.peek();

    if ((systemEventHierarchyEntry != null)
        && systemEventHierarchyEntry.hasTypedModel(className, classPK)) {

      if (groupId > 0) {
        SystemEventLocalServiceUtil.addSystemEvent(
            0,
            groupId,
            systemEventHierarchyEntry.getClassName(),
            classPK,
            systemEventHierarchyEntry.getUuid(),
            referrerClassName,
            systemEvent.type(),
            systemEventHierarchyEntry.getExtraData());
      } else {
        SystemEventLocalServiceUtil.addSystemEvent(
            getCompanyId(classedModel),
            systemEventHierarchyEntry.getClassName(),
            classPK,
            systemEventHierarchyEntry.getUuid(),
            referrerClassName,
            systemEvent.type(),
            systemEventHierarchyEntry.getExtraData());
      }
    } else if (groupId > 0) {
      SystemEventLocalServiceUtil.addSystemEvent(
          0,
          groupId,
          className,
          classPK,
          getUuid(classedModel),
          referrerClassName,
          systemEvent.type(),
          StringPool.BLANK);
    } else {
      SystemEventLocalServiceUtil.addSystemEvent(
          getCompanyId(classedModel),
          className,
          classPK,
          getUuid(classedModel),
          referrerClassName,
          systemEvent.type(),
          StringPool.BLANK);
    }
  }