/**
   * Adds resources for the model, always creating a resource at the individual scope and only
   * creating resources at the group, group template, and company scope if such resources don't
   * already exist.
   *
   * <ol>
   *   <li>If the service context specifies that default group or default guest permissions are to
   *       be added, then only default permissions are added. See {@link
   *       com.liferay.portal.service.ServiceContext#setAddGroupPermissions( boolean)} and {@link
   *       com.liferay.portal.service.ServiceContext#setAddGuestPermissions( boolean)}.
   *   <li>Else ...
   *       <ol>
   *         <li>If the service context specifies to derive default permissions, then default group
   *             and guest permissions are derived from the model and added. See {@link
   *             com.liferay.portal.service.ServiceContext#setDeriveDefaultPermissions( boolean)}.
   *         <li>Lastly group and guest permissions from the service context are applied. See {@link
   *             com.liferay.portal.service.ServiceContext#setGroupPermissions(String[])} and {@link
   *             com.liferay.portal.service.ServiceContext#setGuestPermissions(String[])}.
   *       </ol>
   * </ol>
   *
   * @param auditedModel the model to associate with the resources
   * @param serviceContext the service context to apply. Can set whether to add the model's default
   *     group and guest permissions, set whether to derive default group and guest permissions from
   *     the model, set group permissions to apply, and set guest permissions to apply.
   * @throws PortalException if no portal actions could be found associated with the model or if a
   *     portal exception occurred
   * @throws SystemException if a system exception occurred
   */
  @Override
  public void addModelResources(AuditedModel auditedModel, ServiceContext serviceContext)
      throws PortalException, SystemException {

    if (serviceContext.isAddGroupPermissions() || serviceContext.isAddGuestPermissions()) {

      addResources(
          auditedModel.getCompanyId(),
          getGroupId(auditedModel),
          auditedModel.getUserId(),
          auditedModel.getModelClassName(),
          String.valueOf(auditedModel.getPrimaryKeyObj()),
          false,
          serviceContext.isAddGroupPermissions(),
          serviceContext.isAddGuestPermissions(),
          getPermissionedModel(auditedModel));
    } else {
      if (serviceContext.isDeriveDefaultPermissions()) {
        serviceContext.deriveDefaultPermissions(
            getGroupId(auditedModel), auditedModel.getModelClassName());
      }

      addModelResources(
          auditedModel.getCompanyId(),
          getGroupId(auditedModel),
          auditedModel.getUserId(),
          auditedModel.getModelClassName(),
          String.valueOf(auditedModel.getPrimaryKeyObj()),
          serviceContext.getGroupPermissions(),
          serviceContext.getGuestPermissions(),
          getPermissionedModel(auditedModel));
    }
  }
  public DLFolder addFolder(
      long userId,
      long groupId,
      long repositoryId,
      boolean mountPoint,
      long parentFolderId,
      String name,
      String description,
      boolean hidden,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Folder

    User user = userPersistence.findByPrimaryKey(userId);
    parentFolderId = getParentFolderId(groupId, parentFolderId);
    Date now = new Date();

    validateFolder(groupId, parentFolderId, name);

    long folderId = counterLocalService.increment();

    DLFolder dlFolder = dlFolderPersistence.create(folderId);

    dlFolder.setUuid(serviceContext.getUuid());
    dlFolder.setGroupId(groupId);
    dlFolder.setCompanyId(user.getCompanyId());
    dlFolder.setUserId(user.getUserId());
    dlFolder.setCreateDate(serviceContext.getCreateDate(now));
    dlFolder.setModifiedDate(serviceContext.getModifiedDate(now));
    dlFolder.setRepositoryId(repositoryId);
    dlFolder.setMountPoint(mountPoint);
    dlFolder.setParentFolderId(parentFolderId);
    dlFolder.setName(name);
    dlFolder.setDescription(description);
    dlFolder.setHidden(hidden);
    dlFolder.setOverrideFileEntryTypes(false);
    dlFolder.setExpandoBridgeAttributes(serviceContext);

    dlFolderPersistence.update(dlFolder);

    // Resources

    if (serviceContext.isAddGroupPermissions() || serviceContext.isAddGuestPermissions()) {

      addFolderResources(
          dlFolder, serviceContext.isAddGroupPermissions(), serviceContext.isAddGuestPermissions());
    } else {
      if (serviceContext.isDeriveDefaultPermissions()) {
        serviceContext.deriveDefaultPermissions(repositoryId, DLFolderConstants.getClassName());
      }

      addFolderResources(
          dlFolder, serviceContext.getGroupPermissions(), serviceContext.getGuestPermissions());
    }

    // Parent folder

    if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
      DLFolder parentDLFolder = dlFolderPersistence.findByPrimaryKey(parentFolderId);

      parentDLFolder.setLastPostDate(now);

      dlFolderPersistence.update(parentDLFolder);
    }

    // App helper

    dlAppHelperLocalService.addFolder(userId, new LiferayFolder(dlFolder), serviceContext);

    return dlFolder;
  }