protected boolean isAddActivityCounter(
      User user,
      User assetEntryUser,
      AssetEntry assetEntry,
      SocialActivityCounterDefinition activityCounterDefinition) {

    if ((user.isDefaultUser() || !user.isActive())
        && (activityCounterDefinition.getOwnerType()
            != SocialActivityCounterConstants.TYPE_ASSET)) {

      return false;
    }

    if ((assetEntryUser.isDefaultUser() || !assetEntryUser.isActive())
        && (activityCounterDefinition.getOwnerType()
            != SocialActivityCounterConstants.TYPE_ACTOR)) {

      return false;
    }

    if (!activityCounterDefinition.isEnabled() || (activityCounterDefinition.getIncrement() == 0)) {

      return false;
    }

    String name = activityCounterDefinition.getName();

    if ((user.getUserId() == assetEntryUser.getUserId())
        && (name.equals(SocialActivityCounterConstants.NAME_CONTRIBUTION)
            || name.equals(SocialActivityCounterConstants.NAME_POPULARITY))) {

      return false;
    }

    if ((activityCounterDefinition.getOwnerType() == SocialActivityCounterConstants.TYPE_ASSET)
        && !assetEntry.isVisible()) {

      return false;
    }

    return true;
  }
  /**
   * Adds or increments activity counters related to an activity. This method is called
   * asynchronously from the social activity service when the user performs an activity defined in
   * </code>liferay-social.xml</code>.
   *
   * <p>This method first calls the activity processor class, if there is one defined for the
   * activity, checks for limits and increments all the counters that belong to the activity.
   * Afterwards, it processes the activity with respect to achievement classes, if any. Lastly it
   * increments the built-in <code>user.activities</code> and <code>asset.activities</code>
   * counters.
   *
   * @param activity the social activity
   * @throws PortalException if an expected group or expected previous activity counters could not
   *     be found
   */
  @Override
  public void addActivityCounters(SocialActivity activity) throws PortalException {

    if (!socialActivitySettingLocalService.isEnabled(
        activity.getGroupId(), activity.getClassNameId())) {

      return;
    }

    if (!socialActivitySettingLocalService.isEnabled(
        activity.getGroupId(), activity.getClassNameId(), activity.getClassPK())) {

      return;
    }

    if ((activity.getType() == SocialActivityConstants.TYPE_MOVE_ATTACHMENT_TO_TRASH)
        || (activity.getType() == SocialActivityConstants.TYPE_MOVE_TO_TRASH)) {

      disableActivityCounters(activity.getClassNameId(), activity.getClassPK());

      return;
    }

    if ((activity.getType() == SocialActivityConstants.TYPE_RESTORE_ATTACHMENT_FROM_TRASH)
        || (activity.getType() == SocialActivityConstants.TYPE_RESTORE_FROM_TRASH)) {

      enableActivityCounters(activity.getClassNameId(), activity.getClassPK());

      return;
    }

    User user = userPersistence.findByPrimaryKey(activity.getUserId());

    SocialActivityDefinition activityDefinition =
        socialActivitySettingLocalService.getActivityDefinition(
            activity.getGroupId(), activity.getClassName(), activity.getType());

    if ((activityDefinition == null) || !activityDefinition.isCountersEnabled()) {

      return;
    }

    SocialActivityProcessor activityProcessor = activityDefinition.getActivityProcessor();

    if (activityProcessor != null) {
      activityProcessor.processActivity(activity);
    }

    AssetEntry assetEntry = activity.getAssetEntry();

    User assetEntryUser = userPersistence.findByPrimaryKey(assetEntry.getUserId());

    List<SocialActivityCounter> activityCounters = new ArrayList<>();

    for (SocialActivityCounterDefinition activityCounterDefinition :
        activityDefinition.getActivityCounterDefinitions()) {

      if (isAddActivityCounter(user, assetEntryUser, assetEntry, activityCounterDefinition)) {

        SocialActivityCounter activityCounter =
            addActivityCounter(activity.getGroupId(), user, activity, activityCounterDefinition);

        activityCounters.add(activityCounter);
      }
    }

    SocialActivityCounter assetActivitiesCounter = null;

    if (!assetEntryUser.isDefaultUser() && assetEntryUser.isActive() && assetEntry.isVisible()) {

      assetActivitiesCounter = addAssetActivitiesCounter(activity);
    }

    SocialActivityCounter userActivitiesCounter = null;

    if (!user.isDefaultUser() && user.isActive()) {
      userActivitiesCounter = addUserActivitiesCounter(activity);
    }

    for (SocialActivityCounter activityCounter : activityCounters) {
      SocialActivityCounterDefinition activityCounterDefinition =
          activityDefinition.getActivityCounterDefinition(activityCounter.getName());

      if (checkActivityLimit(user, activity, activityCounterDefinition)) {
        incrementActivityCounter(activityCounter, activityCounterDefinition);
      }
    }

    if (assetActivitiesCounter != null) {
      incrementActivityCounter(assetActivitiesCounter, _assetActivitiesActivityCounterDefinition);
    }

    if (userActivitiesCounter != null) {
      incrementActivityCounter(userActivitiesCounter, _userActivitiesActivityCounterDefinition);
    }

    for (SocialAchievement achievement : activityDefinition.getAchievements()) {

      achievement.processActivity(activity);
    }
  }
  protected boolean isViewableGroup(
      PermissionChecker permissionChecker,
      Layout layout,
      String controlPanelCategory,
      boolean checkResourcePermission)
      throws PortalException, SystemException {

    Group group = GroupLocalServiceUtil.getGroup(layout.getGroupId());

    // Inactive sites are not viewable

    if (!group.isActive()) {
      return false;
    } else if (group.isStagingGroup()) {
      Group liveGroup = group.getLiveGroup();

      if (!liveGroup.isActive()) {
        return false;
      }
    }

    // User private layouts are only viewable by the user and anyone who can
    // update the user. The user must also be active.

    if (group.isUser()) {
      long groupUserId = group.getClassPK();

      if (groupUserId == permissionChecker.getUserId()) {
        return true;
      }

      User groupUser = UserLocalServiceUtil.getUserById(groupUserId);

      if (!groupUser.isActive()) {
        return false;
      }

      if (layout.isPrivateLayout()) {
        if (GroupPermissionUtil.contains(
                permissionChecker, groupUser.getGroupId(), ActionKeys.MANAGE_LAYOUTS)
            || UserPermissionUtil.contains(
                permissionChecker,
                groupUserId,
                groupUser.getOrganizationIds(),
                ActionKeys.UPDATE)) {

          return true;
        }

        return false;
      }
    }

    // If the current group is staging, only users with editorial rights
    // can access it

    if (group.isStagingGroup()) {
      if (GroupPermissionUtil.contains(
          permissionChecker, group.getGroupId(), ActionKeys.VIEW_STAGING)) {

        return true;
      }

      return false;
    }

    // Control panel layouts are only viewable by authenticated users

    if (group.isControlPanel()) {
      if (!permissionChecker.isSignedIn()) {
        return false;
      }

      if (PortalPermissionUtil.contains(permissionChecker, ActionKeys.VIEW_CONTROL_PANEL)) {

        return true;
      }

      if (Validator.isNotNull(controlPanelCategory)) {
        return true;
      }

      return false;
    }

    // Site layouts are only viewable by users who are members of the site
    // or by users who can update the site

    if (group.isSite()) {
      if (GroupPermissionUtil.contains(
              permissionChecker, group.getGroupId(), ActionKeys.MANAGE_LAYOUTS)
          || GroupPermissionUtil.contains(
              permissionChecker, group.getGroupId(), ActionKeys.UPDATE)) {

        return true;
      }

      if (layout.isPrivateLayout() && !permissionChecker.isGroupMember(group.getGroupId())) {

        return false;
      }
    }

    // Organization site layouts are also viewable by users who belong to
    // the organization or by users who can update organization

    if (group.isCompany()) {
      return false;
    } else if (group.isLayoutPrototype()) {
      if (LayoutPrototypePermissionUtil.contains(
          permissionChecker, group.getClassPK(), ActionKeys.VIEW)) {

        return true;
      }

      return false;
    } else if (group.isLayoutSetPrototype()) {
      if (LayoutSetPrototypePermissionUtil.contains(
          permissionChecker, group.getClassPK(), ActionKeys.VIEW)) {

        return true;
      }

      return false;
    } else if (group.isOrganization()) {
      long organizationId = group.getOrganizationId();

      if (OrganizationLocalServiceUtil.hasUserOrganization(
          permissionChecker.getUserId(), organizationId, false, false)) {

        return true;
      } else if (OrganizationPermissionUtil.contains(
          permissionChecker, organizationId, ActionKeys.UPDATE)) {

        return true;
      }

      if (!PropsValues.ORGANIZATIONS_MEMBERSHIP_STRICT) {
        List<Organization> userOrgs =
            OrganizationLocalServiceUtil.getUserOrganizations(permissionChecker.getUserId());

        for (Organization organization : userOrgs) {
          for (Organization ancestorOrganization : organization.getAncestors()) {

            if (organizationId == ancestorOrganization.getOrganizationId()) {

              return true;
            }
          }
        }
      }
    } else if (group.isUserGroup()) {
      if (UserGroupPermissionUtil.contains(
          permissionChecker, group.getClassPK(), ActionKeys.UPDATE)) {

        return true;
      }
    }

    // Only check the actual Layout if all of the above failed

    if (containsWithoutViewableGroup(
        permissionChecker, layout, controlPanelCategory, ActionKeys.VIEW)) {

      return true;
    }

    // As a last resort, check if any top level pages are viewable by the
    // user

    List<Layout> layouts =
        LayoutLocalServiceUtil.getLayouts(
            layout.getGroupId(),
            layout.isPrivateLayout(),
            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);

    for (Layout curLayout : layouts) {
      if (!curLayout.isHidden()
          && containsWithoutViewableGroup(
              permissionChecker, curLayout, controlPanelCategory, ActionKeys.VIEW)) {

        return true;
      }
    }

    return false;
  }