Beispiel #1
0
  @Override
  public PortletURL getViewContentURL(HttpServletRequest request, String className, long classPK)
      throws PortalException {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    if (!themeDisplay.isSignedIn()
        || !isTrashEnabled(themeDisplay.getScopeGroupId())
        || !PortletPermissionUtil.hasControlPanelAccessPermission(
            themeDisplay.getPermissionChecker(),
            themeDisplay.getScopeGroupId(),
            PortletKeys.TRASH)) {

      return null;
    }

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    if (trashHandler.isInTrashContainer(classPK)) {
      TrashEntry trashEntry = trashHandler.getTrashEntry(classPK);

      className = trashEntry.getClassName();
      classPK = trashEntry.getClassPK();

      trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);
    }

    TrashRenderer trashRenderer = trashHandler.getTrashRenderer(classPK);

    if (trashRenderer == null) {
      return null;
    }

    Layout layout = themeDisplay.getLayout();

    PortletURL portletURL =
        PortalUtil.getControlPanelPortletURL(
            request, PortletKeys.TRASH, layout.getLayoutId(), PortletRequest.RENDER_PHASE);

    portletURL.setParameter("struts_action", "/trash/view_content");
    portletURL.setParameter("redirect", themeDisplay.getURLCurrent());

    TrashEntry trashEntry = TrashEntryLocalServiceUtil.getEntry(className, classPK);

    if (trashEntry.getRootEntry() != null) {
      portletURL.setParameter("className", className);
      portletURL.setParameter("classPK", String.valueOf(classPK));
    } else {
      portletURL.setParameter("trashEntryId", String.valueOf(trashEntry.getEntryId()));
    }

    portletURL.setParameter("type", trashRenderer.getType());
    portletURL.setParameter("showActions", Boolean.FALSE.toString());
    portletURL.setParameter("showAssetMetadata", Boolean.TRUE.toString());
    portletURL.setParameter("showEditURL", Boolean.FALSE.toString());

    return portletURL;
  }
Beispiel #2
0
  public String getViewContentURL(String className, long classPK, ThemeDisplay themeDisplay)
      throws PortalException, SystemException {

    if (!themeDisplay.isSignedIn()
        || !isTrashEnabled(themeDisplay.getScopeGroupId())
        || !PortletPermissionUtil.hasControlPanelAccessPermission(
            themeDisplay.getPermissionChecker(),
            themeDisplay.getScopeGroupId(),
            PortletKeys.TRASH)) {

      return null;
    }

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    if (trashHandler.isInTrashContainer(classPK)) {
      ContainerModel containerModel = trashHandler.getTrashContainer(classPK);

      className = containerModel.getModelClassName();
      classPK = containerModel.getContainerModelId();

      trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);
    }

    TrashRenderer trashRenderer = trashHandler.getTrashRenderer(classPK);

    if (trashRenderer == null) {
      return null;
    }

    String namespace = PortalUtil.getPortletNamespace(PortletKeys.TRASH);

    Map<String, String[]> params = new HashMap<String, String[]>();

    params.put(namespace + "struts_action", new String[] {"/trash/view_content"});
    params.put(namespace + "redirect", new String[] {themeDisplay.getURLCurrent()});

    TrashEntry trashEntry = TrashEntryLocalServiceUtil.getEntry(className, classPK);

    if (trashEntry.getRootEntry() != null) {
      params.put(namespace + "className", new String[] {className});
      params.put(namespace + "classPK", new String[] {String.valueOf(classPK)});
    } else {
      params.put(
          namespace + "trashEntryId", new String[] {String.valueOf(trashEntry.getEntryId())});
    }

    params.put(namespace + "type", new String[] {trashRenderer.getType()});
    params.put(namespace + "showActions", new String[] {Boolean.FALSE.toString()});
    params.put(namespace + "showAssetMetadata", new String[] {Boolean.TRUE.toString()});
    params.put(namespace + "showEditURL", new String[] {Boolean.FALSE.toString()});

    return PortalUtil.getControlPanelFullURL(
        themeDisplay.getScopeGroupId(), PortletKeys.TRASH, params);
  }
Beispiel #3
0
  protected void addBreadcrumbEntries(
      HttpServletRequest request,
      String className,
      long classPK,
      String paramName,
      PortletURL containerModelURL)
      throws PortalException {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    List<ContainerModel> containerModels = trashHandler.getParentContainerModels(classPK);

    Collections.reverse(containerModels);

    containerModelURL.setParameter("struts_action", "/trash/view");

    PortalUtil.addPortletBreadcrumbEntry(
        request,
        LanguageUtil.get(themeDisplay.getLocale(), "recycle-bin"),
        containerModelURL.toString());

    for (ContainerModel containerModel : containerModels) {
      TrashHandler containerModelTrashHandler =
          TrashHandlerRegistryUtil.getTrashHandler(containerModel.getModelClassName());

      if (!containerModelTrashHandler.isInTrash(containerModel.getContainerModelId())) {

        continue;
      }

      containerModelURL.setParameter("struts_action", "/trash/view_content");

      containerModelURL.setParameter(
          paramName, String.valueOf(containerModel.getContainerModelId()));

      String name = containerModel.getContainerModelName();

      if (containerModelTrashHandler.isInTrash(containerModel.getContainerModelId())) {

        name = TrashUtil.getOriginalTitle(name);
      }

      PortalUtil.addPortletBreadcrumbEntry(request, name, containerModelURL.toString());
    }

    TrashRenderer trashRenderer = trashHandler.getTrashRenderer(classPK);

    PortalUtil.addPortletBreadcrumbEntry(
        request, trashRenderer.getTitle(themeDisplay.getLocale()), null);
  }
Beispiel #4
0
  protected String getLink(SocialActivity activity, ServiceContext serviceContext)
      throws Exception {

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(activity.getClassName());

    long classPK = activity.getClassPK();

    if ((trashHandler != null)
        && (trashHandler.isInTrash(classPK) || trashHandler.isInTrashContainer(classPK))) {

      PortletURL portletURL =
          TrashUtil.getViewContentURL(
              serviceContext.getRequest(), activity.getClassName(), classPK);

      if (portletURL == null) {
        return null;
      }

      return portletURL.toString();
    }

    String path = getPath(activity, serviceContext);

    if (Validator.isNull(path)) {
      return null;
    }

    if (!path.startsWith(StringPool.SLASH)) {
      return path;
    }

    return serviceContext.getPortalURL() + serviceContext.getPathMain() + path;
  }
  /**
   * Deletes the trash entries with the matching group ID considering permissions.
   *
   * @param groupId the primary key of the group
   * @throws PortalException if a portal exception occurred
   * @throws SystemException if a system exception occurred
   */
  @Override
  @Transactional(noRollbackFor = {TrashPermissionException.class})
  public void deleteEntries(long groupId) throws PortalException, SystemException {

    boolean throwTrashPermissionException = false;

    List<TrashEntry> entries = trashEntryPersistence.findByGroupId(groupId);

    PermissionChecker permissionChecker = getPermissionChecker();

    for (TrashEntry entry : entries) {
      try {
        TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(entry.getClassName());

        if (!trashHandler.hasTrashPermission(
            permissionChecker, 0, entry.getClassPK(), ActionKeys.VIEW)) {

          continue;
        }

        deleteEntry(entry);
      } catch (TrashPermissionException tpe) {
        throwTrashPermissionException = true;
      } catch (Exception e) {
        _log.error(e, e);
      }
    }

    if (throwTrashPermissionException) {
      throw new TrashPermissionException(TrashPermissionException.EMPTY_TRASH);
    }
  }
Beispiel #6
0
  @Override
  public String getNewName(
      ThemeDisplay themeDisplay, String className, long classPK, String oldName)
      throws PortalException {

    TrashRenderer trashRenderer = null;

    if (Validator.isNotNull(className) && (classPK > 0)) {
      TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

      trashRenderer = trashHandler.getTrashRenderer(classPK);
    }

    Format dateFormatDateTime =
        FastDateFormatFactoryUtil.getDateTime(themeDisplay.getLocale(), themeDisplay.getTimeZone());

    StringBundler sb = new StringBundler(3);

    sb.append(StringPool.OPEN_PARENTHESIS);
    sb.append(
        StringUtil.replace(dateFormatDateTime.format(new Date()), CharPool.SLASH, CharPool.PERIOD));
    sb.append(StringPool.CLOSE_PARENTHESIS);

    if (trashRenderer != null) {
      return trashRenderer.getNewName(oldName, sb.toString());
    } else {
      return getNewName(oldName, sb.toString());
    }
  }
Beispiel #7
0
  protected void addBreadcrumbEntries(
      HttpServletRequest request,
      String className,
      long classPK,
      String paramName,
      PortletURL containerModelURL)
      throws PortalException, SystemException {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    List<ContainerModel> containerModels = trashHandler.getParentContainerModels(classPK);

    Collections.reverse(containerModels);

    for (ContainerModel containerModel : containerModels) {
      containerModelURL.setParameter(
          paramName, String.valueOf(containerModel.getContainerModelId()));

      PortalUtil.addPortletBreadcrumbEntry(
          request, containerModel.getContainerModelName(), containerModelURL.toString());
    }

    TrashRenderer trashRenderer = trashHandler.getTrashRenderer(classPK);

    PortalUtil.addPortletBreadcrumbEntry(
        request, trashRenderer.getTitle(themeDisplay.getLocale()), null);
  }
Beispiel #8
0
  @Override
  public void addContainerModelBreadcrumbEntries(
      HttpServletRequest request, String className, long classPK, PortletURL containerModelURL)
      throws PortalException {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    String rootContainerModelName =
        LanguageUtil.get(themeDisplay.getLocale(), trashHandler.getRootContainerModelName());

    if (classPK == 0) {
      PortalUtil.addPortletBreadcrumbEntry(request, rootContainerModelName, null);

      return;
    }

    containerModelURL.setParameter("containerModelId", "0");

    PortalUtil.addPortletBreadcrumbEntry(
        request, rootContainerModelName, containerModelURL.toString());

    addBreadcrumbEntries(request, className, classPK, "containerModelId", containerModelURL);
  }
  @Test
  public void testArticleImages() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    int initialArticleImagesCount =
        JournalArticleImageLocalServiceUtil.getArticleImagesCount(group.getGroupId());

    Class<?> clazz = getClass();

    ClassLoader classLoader = clazz.getClassLoader();

    String definition =
        StringUtil.read(
            classLoader,
            "com/liferay/journal/dependencies" + "/test-ddm-structure-image-field.xml");

    DDMForm ddmForm = DDMFormXSDDeserializerUtil.deserialize(definition);

    DDMStructure ddmStructure =
        DDMStructureTestUtil.addStructure(
            serviceContext.getScopeGroupId(), JournalArticle.class.getName(), ddmForm);

    DDMTemplate ddmTemplate =
        DDMTemplateTestUtil.addTemplate(
            serviceContext.getScopeGroupId(), ddmStructure.getStructureId());

    String content =
        StringUtil.read(
            classLoader,
            "com/liferay/journal/dependencies" + "/test-journal-content-image-field.xml");

    Map<String, byte[]> images = new HashMap<>();

    images.put(
        "_image_1_0_en_US",
        FileUtil.getBytes(clazz, "/com/liferay/journal/dependencies/liferay.png"));

    baseModel =
        JournalTestUtil.addArticleWithXMLContent(
            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            content,
            ddmStructure.getStructureKey(),
            ddmTemplate.getTemplateKey(),
            images,
            serviceContext);

    Assert.assertEquals(
        initialArticleImagesCount + 1,
        JournalArticleImageLocalServiceUtil.getArticleImagesCount(group.getGroupId()));

    moveBaseModelToTrash((Long) baseModel.getPrimaryKeyObj());

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(getBaseModelClassName());

    trashHandler.deleteTrashEntry(getTrashEntryClassPK(baseModel));

    Assert.assertEquals(
        initialArticleImagesCount,
        JournalArticleImageLocalServiceUtil.getArticleImagesCount(group.getGroupId()));
  }
  protected void addBreadcrumbEntries(
      HttpServletRequest request,
      LiferayPortletResponse liferayPortletResponse,
      String className,
      long classPK,
      String paramName,
      PortletURL containerModelURL,
      boolean checkInTrashContainers)
      throws PortalException, PortletException {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    PortletURL portletURL = PortletURLUtil.clone(containerModelURL, liferayPortletResponse);

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    List<ContainerModel> containerModels = trashHandler.getParentContainerModels(classPK);

    Collections.reverse(containerModels);

    for (ContainerModel containerModel : containerModels) {
      TrashHandler containerModelTrashHandler =
          TrashHandlerRegistryUtil.getTrashHandler(containerModel.getModelClassName());

      if (checkInTrashContainers
          && !containerModelTrashHandler.isInTrash(containerModel.getContainerModelId())) {

        continue;
      }

      portletURL.setParameter(paramName, String.valueOf(containerModel.getContainerModelId()));

      String name = containerModel.getContainerModelName();

      if (containerModelTrashHandler.isInTrash(containerModel.getContainerModelId())) {

        name = TrashUtil.getOriginalTitle(name);
      }

      PortalUtil.addPortletBreadcrumbEntry(request, name, portletURL.toString());
    }

    TrashRenderer trashRenderer = trashHandler.getTrashRenderer(classPK);

    PortalUtil.addPortletBreadcrumbEntry(
        request, trashRenderer.getTitle(themeDisplay.getLocale()), null);
  }
  /**
   * Returns a range of all the trash entries matching the group ID.
   *
   * @param groupId the primary key of the group
   * @param start the lower bound of the range of trash entries to return
   * @param end the upper bound of the range of trash entries to return (not inclusive)
   * @param obc the comparator to order the trash entries (optionally <code>null</code>)
   * @return the range of matching trash entries ordered by comparator <code>obc</code>
   * @throws PrincipalException if a system exception occurred
   * @throws SystemException if a system exception occurred
   */
  @Override
  public TrashEntryList getEntries(long groupId, int start, int end, OrderByComparator obc)
      throws PrincipalException, SystemException {

    TrashEntryList trashEntriesList = new TrashEntryList();

    int entriesCount = trashEntryPersistence.countByGroupId(groupId);

    boolean approximate = entriesCount > PropsValues.TRASH_SEARCH_LIMIT;

    trashEntriesList.setApproximate(approximate);

    List<TrashEntry> entries =
        trashEntryPersistence.findByGroupId(groupId, 0, end + PropsValues.TRASH_SEARCH_LIMIT, obc);

    List<TrashEntry> filteredEntries = new ArrayList<TrashEntry>();

    PermissionChecker permissionChecker = getPermissionChecker();

    for (TrashEntry entry : entries) {
      String className = entry.getClassName();
      long classPK = entry.getClassPK();

      try {
        TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

        if (trashHandler.hasTrashPermission(permissionChecker, 0, classPK, ActionKeys.VIEW)) {

          filteredEntries.add(entry);
        }
      } catch (Exception e) {
        _log.error(e, e);
      }
    }

    int filteredEntriesCount = filteredEntries.size();

    if ((end != QueryUtil.ALL_POS) && (start != QueryUtil.ALL_POS)) {
      if (end > filteredEntriesCount) {
        end = filteredEntriesCount;
      }

      if (start > filteredEntriesCount) {
        start = filteredEntriesCount;
      }

      filteredEntries = filteredEntries.subList(start, end);
    }

    trashEntriesList.setArray(TrashEntrySoap.toSoapModels(filteredEntries));
    trashEntriesList.setCount(filteredEntriesCount);

    return trashEntriesList;
  }
Beispiel #12
0
  @Override
  public boolean isInTrash(String className, long classPK) throws PortalException {

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    if (trashHandler == null) {
      return false;
    }

    return trashHandler.isInTrash(classPK);
  }
  protected void deleteEntry(TrashEntry entry) throws PortalException {
    PermissionChecker permissionChecker = getPermissionChecker();

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(entry.getClassName());

    if (!trashHandler.hasTrashPermission(
        permissionChecker, 0, entry.getClassPK(), ActionKeys.DELETE)) {

      throw new TrashPermissionException(TrashPermissionException.DELETE);
    }

    trashHandler.deleteTrashEntry(entry.getClassPK());
  }
  @Override
  public List<TrashEntry> getEntries(Hits hits) {
    List<TrashEntry> entries = new ArrayList<>();

    for (Document document : hits.getDocs()) {
      String entryClassName = GetterUtil.getString(document.get(Field.ENTRY_CLASS_NAME));
      long classPK = GetterUtil.getLong(document.get(Field.ENTRY_CLASS_PK));

      try {
        TrashEntry entry = TrashEntryLocalServiceUtil.fetchEntry(entryClassName, classPK);

        if (entry == null) {
          String userName = GetterUtil.getString(document.get(Field.REMOVED_BY_USER_NAME));

          Date removedDate = document.getDate(Field.REMOVED_DATE);

          entry = new TrashEntryImpl();

          entry.setUserName(userName);
          entry.setCreateDate(removedDate);

          TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(entryClassName);

          TrashRenderer trashRenderer = trashHandler.getTrashRenderer(classPK);

          entry.setClassName(trashRenderer.getClassName());
          entry.setClassPK(trashRenderer.getClassPK());

          String rootEntryClassName =
              GetterUtil.getString(document.get(Field.ROOT_ENTRY_CLASS_NAME));
          long rootEntryClassPK = GetterUtil.getLong(document.get(Field.ROOT_ENTRY_CLASS_PK));

          TrashEntry rootTrashEntry =
              TrashEntryLocalServiceUtil.fetchEntry(rootEntryClassName, rootEntryClassPK);

          if (rootTrashEntry != null) {
            entry.setRootEntry(rootTrashEntry);
          }
        }

        entries.add(entry);
      } catch (Exception e) {
        if (_log.isWarnEnabled()) {
          _log.warn(
              "Unable to find trash entry for " + entryClassName + " with primary key " + classPK);
        }
      }
    }

    return entries;
  }
  protected void searchRecentEntries() throws Exception {
    long initialSearchGroupEntriesCount = searchGroupEntriesCount(group.getGroupId(), 0);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    BaseModel<?> parentBaseModel1 = getParentBaseModel(group, serviceContext);
    BaseModel<?> parentBaseModel2 = getParentBaseModel(group, serviceContext);

    String name = PrincipalThreadLocal.getName();

    try {
      User user1 = UserTestUtil.addUser(null, 0);

      PrincipalThreadLocal.setName(user1.getUserId());

      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel2, true, RandomTestUtil.randomString(), serviceContext);

      User user2 = UserTestUtil.addUser(null, 0);

      PrincipalThreadLocal.setName(user2.getUserId());

      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel2, true, RandomTestUtil.randomString(), serviceContext);
    } finally {
      PrincipalThreadLocal.setName(name);
    }

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 5, searchGroupEntriesCount(group.getGroupId(), 0));

    moveParentBaseModelToTrash((Long) parentBaseModel2.getPrimaryKeyObj());

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 3, searchGroupEntriesCount(group.getGroupId(), 0));

    TrashHandler parentTrashHandler =
        TrashHandlerRegistryUtil.getTrashHandler(getParentBaseModelClassName());

    parentTrashHandler.restoreTrashEntry(
        TestPropsValues.getUserId(), (Long) parentBaseModel2.getPrimaryKeyObj());

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 5, searchGroupEntriesCount(group.getGroupId(), 0));
  }
Beispiel #16
0
  public boolean isInTrash(String className, long classPK) throws PortalException, SystemException {

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    if (trashHandler == null) {
      return false;
    }

    if (trashHandler.isInTrash(classPK) || trashHandler.isInTrashContainer(classPK)) {

      return true;
    }

    return false;
  }
  @Override
  public TrashEntry getTrashEntry() throws PortalException {
    if (!isInTrash()) {
      return null;
    }

    TrashEntry trashEntry =
        TrashEntryLocalServiceUtil.fetchEntry(getModelClassName(), getTrashEntryClassPK());

    if (trashEntry != null) {
      return trashEntry;
    }

    TrashHandler trashHandler = getTrashHandler();

    if (!Validator.isNull(trashHandler.getContainerModelClassName(getPrimaryKey()))) {
      ContainerModel containerModel = null;

      try {
        containerModel = trashHandler.getParentContainerModel(this);
      } catch (NoSuchModelException nsme) {
        return null;
      }

      while (containerModel != null) {
        if (containerModel instanceof TrashedModel) {
          TrashedModel trashedModel = (TrashedModel) containerModel;

          return trashedModel.getTrashEntry();
        }

        trashHandler =
            TrashHandlerRegistryUtil.getTrashHandler(
                trashHandler.getContainerModelClassName(containerModel.getContainerModelId()));

        if (trashHandler == null) {
          return null;
        }

        containerModel = trashHandler.getContainerModel(containerModel.getParentContainerModelId());
      }
    }

    return null;
  }
  /**
   * Moves the trash entry with the entity class name and primary key, restoring it to a new
   * location identified by the destination container model ID.
   *
   * <p>This method throws a {@link TrashPermissionException} if the user did not have the
   * permission to perform one of the necessary operations. The exception is created with a type
   * specific to the operation:
   *
   * <ul>
   *   <li>{@link TrashPermissionException#MOVE} - if the user did not have permission to move the
   *       trash entry to the new destination
   *   <li>{@link TrashPermissionException#RESTORE} - if the user did not have permission to restore
   *       the trash entry
   * </ul>
   *
   * @param className the class name of the entity
   * @param classPK the primary key of the entity
   * @param destinationContainerModelId the primary key of the new location
   * @param serviceContext the service context to be applied (optionally <code>null</code>)
   * @throws PortalException if a matching trash entry could not be found, if the user did not have
   *     permission to move the trash entry to the new location, if the user did not have permission
   *     to restore the trash entry, if a duplicate trash entry exists at the new location, or if a
   *     portal exception occurred
   */
  @Override
  public void moveEntry(
      String className,
      long classPK,
      long destinationContainerModelId,
      ServiceContext serviceContext)
      throws PortalException {

    PermissionChecker permissionChecker = getPermissionChecker();

    long scopeGroupId = 0;

    if (serviceContext != null) {
      scopeGroupId = serviceContext.getScopeGroupId();
    }

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    destinationContainerModelId =
        trashHandler.getDestinationContainerModelId(classPK, destinationContainerModelId);

    if (!trashHandler.hasTrashPermission(
        permissionChecker, scopeGroupId, destinationContainerModelId, TrashActionKeys.MOVE)) {

      throw new TrashPermissionException(TrashPermissionException.MOVE);
    }

    if (trashHandler.isInTrash(classPK)
        && !trashHandler.hasTrashPermission(
            permissionChecker, 0, classPK, TrashActionKeys.RESTORE)) {

      throw new TrashPermissionException(TrashPermissionException.RESTORE);
    }

    TrashEntry trashEntry = trashHandler.getTrashEntry(classPK);

    if (trashEntry.isTrashEntry(className, classPK)) {
      trashHandler.checkRestorableEntry(trashEntry, destinationContainerModelId, StringPool.BLANK);
    } else {
      trashHandler.checkRestorableEntry(classPK, destinationContainerModelId, StringPool.BLANK);
    }

    trashHandler.moveTrashEntry(getUserId(), classPK, destinationContainerModelId, serviceContext);
  }
  /**
   * Restores the trash entry to its original location. In order to handle a duplicate trash entry
   * already existing at the original location, either pass in the primary key of the existing trash
   * entry's entity to overwrite or pass in a new name to give to the trash entry being restored.
   *
   * <p>This method throws a {@link TrashPermissionException} if the user did not have the
   * permission to perform one of the necessary operations. The exception is created with a type
   * specific to the operation:
   *
   * <ul>
   *   <li>{@link TrashPermissionException#RESTORE} - if the user did not have permission to restore
   *       the trash entry
   *   <li>{@link TrashPermissionException#RESTORE_OVERWRITE} - if the user did not have permission
   *       to delete the existing trash entry
   *   <li>{@link TrashPermissionException#RESTORE_RENAME} - if the user did not have permission to
   *       rename the trash entry
   * </ul>
   *
   * @param entryId the primary key of the trash entry to restore
   * @param overrideClassPK the primary key of the entity to overwrite (optionally <code>0</code>)
   * @param name a new name to give to the trash entry being restored (optionally <code>null</code>)
   * @return the restored trash entry
   * @throws PortalException if a matching trash entry could not be found, if the user did not have
   *     permission to overwrite an existing trash entry, to rename the trash entry being restored,
   *     or to restore the trash entry in general
   */
  @Override
  public TrashEntry restoreEntry(long entryId, long overrideClassPK, String name)
      throws PortalException {

    PermissionChecker permissionChecker = getPermissionChecker();

    TrashEntry entry = trashEntryPersistence.findByPrimaryKey(entryId);

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(entry.getClassName());

    if (!trashHandler.hasTrashPermission(
        permissionChecker, 0, entry.getClassPK(), TrashActionKeys.RESTORE)) {

      throw new TrashPermissionException(TrashPermissionException.RESTORE);
    }

    if (overrideClassPK > 0) {
      if (!trashHandler.hasTrashPermission(
          permissionChecker, 0, overrideClassPK, TrashActionKeys.OVERWRITE)) {

        throw new TrashPermissionException(TrashPermissionException.RESTORE_OVERWRITE);
      }

      trashHandler.deleteTrashEntry(overrideClassPK);

      trashHandler.checkRestorableEntry(entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, null);
    } else if (name != null) {
      if (!trashHandler.hasTrashPermission(
          permissionChecker, 0, entry.getClassPK(), TrashActionKeys.RENAME)) {

        throw new TrashPermissionException(TrashPermissionException.RESTORE_RENAME);
      }

      trashHandler.checkRestorableEntry(entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, name);

      trashHandler.updateTitle(entry.getClassPK(), name);
    }

    trashHandler.restoreTrashEntry(getUserId(), entry.getClassPK());

    return entry;
  }
  @Override
  protected void doRestoreStagedModel(PortletDataContext portletDataContext, FileEntry fileEntry)
      throws Exception {

    long userId = portletDataContext.getUserId(fileEntry.getUserUuid());

    FileEntry existingFileEntry =
        fetchStagedModelByUuidAndGroupId(fileEntry.getUuid(), portletDataContext.getScopeGroupId());

    if ((existingFileEntry == null) || !existingFileEntry.isInTrash()) {
      return;
    }

    TrashHandler trashHandler =
        TrashHandlerRegistryUtil.getTrashHandler(DLFileEntry.class.getName());

    if (trashHandler.isRestorable(existingFileEntry.getFileEntryId())) {
      trashHandler.restoreTrashEntry(userId, existingFileEntry.getFileEntryId());
    }
  }
Beispiel #21
0
  @Override
  public List<TrashRenderer> getTrashContainerModelTrashRenderers(long classPK, int start, int end)
      throws PortalException, SystemException {

    List<TrashRenderer> trashRenderers = new ArrayList<TrashRenderer>();

    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(classPK);

    List<BookmarksFolder> folders =
        BookmarksFolderLocalServiceUtil.getFolders(folder.getGroupId(), classPK, start, end);

    for (BookmarksFolder curFolder : folders) {
      TrashHandler trashHandler =
          TrashHandlerRegistryUtil.getTrashHandler(BookmarksFolder.class.getName());

      TrashRenderer trashRenderer = trashHandler.getTrashRenderer(curFolder.getPrimaryKey());

      trashRenderers.add(trashRenderer);
    }

    return trashRenderers;
  }
  @Override
  public List<TrashRenderer> getTrashContainedModelTrashRenderers(long classPK, int start, int end)
      throws PortalException {

    List<TrashRenderer> trashRenderers = new ArrayList<TrashRenderer>();

    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(classPK);

    List<BookmarksEntry> entries =
        BookmarksEntryLocalServiceUtil.getEntries(
            folder.getGroupId(), classPK, WorkflowConstants.STATUS_IN_TRASH, start, end);

    for (BookmarksEntry entry : entries) {
      TrashHandler trashHandler =
          TrashHandlerRegistryUtil.getTrashHandler(BookmarksEntry.class.getName());

      TrashRenderer trashRenderer = trashHandler.getTrashRenderer(entry.getEntryId());

      trashRenderers.add(trashRenderer);
    }

    return trashRenderers;
  }
  @Override
  public List<TrashRenderer> getTrashContainedModelTrashRenderers(long classPK, int start, int end)
      throws PortalException, SystemException {

    List<TrashRenderer> trashRenderers = new ArrayList<TrashRenderer>();

    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);

    List<JournalArticle> articles =
        JournalArticleLocalServiceUtil.search(
            folder.getGroupId(), classPK, WorkflowConstants.STATUS_IN_TRASH, start, end);

    for (JournalArticle article : articles) {
      TrashHandler trashHandler =
          TrashHandlerRegistryUtil.getTrashHandler(JournalArticle.class.getName());

      TrashRenderer trashRenderer = trashHandler.getTrashRenderer(article.getResourcePrimKey());

      trashRenderers.add(trashRenderer);
    }

    return trashRenderers;
  }
  protected List<TrashEntry> filterEntries(List<TrashEntry> entries) throws PrincipalException {

    List<TrashEntry> filteredEntries = new ArrayList<>();

    PermissionChecker permissionChecker = getPermissionChecker();

    for (TrashEntry entry : entries) {
      String className = entry.getClassName();
      long classPK = entry.getClassPK();

      try {
        TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

        if (trashHandler.hasTrashPermission(permissionChecker, 0, classPK, ActionKeys.VIEW)) {

          filteredEntries.add(entry);
        }
      } catch (Exception e) {
        _log.error(e, e);
      }
    }

    return filteredEntries;
  }
  @Test
  public void testRestorePageWithParentPageInTrash() throws Exception {
    WikiPage[] pages =
        WikiTestUtil.addTrashedPageWithChildPage(group.getGroupId(), _node.getNodeId(), false);

    WikiPage childPage = pages[1];

    WikiPage newParentPage = WikiTestUtil.addPage(group.getGroupId(), _node.getNodeId(), true);

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(getBaseModelClassName());

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    trashHandler.moveEntry(
        TestPropsValues.getUserId(),
        childPage.getResourcePrimKey(),
        newParentPage.getResourcePrimKey(),
        serviceContext);

    childPage = WikiPageLocalServiceUtil.getPage(childPage.getResourcePrimKey());

    Assert.assertTrue(childPage.isApproved());
    Assert.assertEquals(newParentPage.getTitle(), childPage.getParentTitle());
  }
  /**
   * Moves the trash entry with the entity class name and primary key, restoring it to a new
   * location identified by the destination container model ID.
   *
   * <p>This method throws a {@link TrashPermissionException} if the user did not have the
   * permission to perform one of the necessary operations. The exception is created with a type
   * specific to the operation:
   *
   * <ul>
   *   <li>{@link TrashPermissionException#MOVE} - if the user did not have permission to move the
   *       trash entry to the new destination
   *   <li>{@link TrashPermissionException#RESTORE} - if the user did not have permission to restore
   *       the trash entry
   * </ul>
   *
   * @param className the class name of the entity
   * @param classPK the primary key of the entity
   * @param destinationContainerModelId the primary key of the new location
   * @param serviceContext the service context to be applied (optionally <code>null</code>)
   * @throws PortalException if a matching trash entry could not be found, if the user did not have
   *     permission to move the trash entry to the new location, if the user did not have permission
   *     to restore the trash entry, if a duplicate trash entry exists at the new location, or if a
   *     portal exception occurred
   * @throws SystemException if a system exception occurred
   */
  @Override
  public void moveEntry(
      String className,
      long classPK,
      long destinationContainerModelId,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    PermissionChecker permissionChecker = getPermissionChecker();

    TrashEntry entry = trashEntryLocalService.getEntry(className, classPK);

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    if (!trashHandler.hasTrashPermission(
        permissionChecker, entry.getGroupId(), destinationContainerModelId, TrashActionKeys.MOVE)) {

      throw new TrashPermissionException(TrashPermissionException.MOVE);
    }

    if (trashHandler.isInTrash(classPK)
        && !trashHandler.hasTrashPermission(
            permissionChecker, 0, classPK, TrashActionKeys.RESTORE)) {

      throw new TrashPermissionException(TrashPermissionException.RESTORE);
    }

    trashHandler.checkDuplicateTrashEntry(entry, destinationContainerModelId, StringPool.BLANK);

    if (trashHandler.isInTrash(classPK)) {
      trashHandler.moveTrashEntry(
          getUserId(), classPK, destinationContainerModelId, serviceContext);
    } else {
      trashHandler.moveEntry(getUserId(), classPK, destinationContainerModelId, serviceContext);
    }
  }
  protected void restoreTrashEntry(WikiPage page) throws Exception {
    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(getBaseModelClassName());

    trashHandler.restoreTrashEntry(TestPropsValues.getUserId(), getTrashEntryClassPK(page));
  }
 @Override
 public TrashHandler getTrashHandler() {
   return TrashHandlerRegistryUtil.getTrashHandler(getModelClassName());
 }