Example usage for com.liferay.portal.kernel.trash TrashHandler deleteTrashEntry

List of usage examples for com.liferay.portal.kernel.trash TrashHandler deleteTrashEntry

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.trash TrashHandler deleteTrashEntry.

Prototype

public void deleteTrashEntry(long classPK) throws PortalException;

Source Link

Document

Deletes the model entity with the primary key.

Usage

From source file:com.liferay.document.library.file.rank.trash.test.DLFileRankTrashHandlerTest.java

License:Open Source License

protected void trashDLFileRank() throws Exception {
    Group group = GroupTestUtil.addGroup();

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

    Folder folder = addFolder(group, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, serviceContext);

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

    FileEntry fileEntry = null;//from   w w  w  .  ja v a  2  s. c  o  m

    try {
        boolean workflowEnabled = WorkflowThreadLocal.isEnabled();

        try {
            WorkflowThreadLocal.setEnabled(true);

            fileEntry = addFileEntry(folder, serviceContext);
        } finally {
            WorkflowThreadLocal.setEnabled(workflowEnabled);
        }

        DLAppLocalServiceUtil.addFileRank(group.getGroupId(), TestPropsValues.getCompanyId(),
                TestPropsValues.getUserId(), fileEntry.getFileEntryId(), serviceContext);

        Assert.assertEquals(1, getActiveDLFileRanksCount(group.getGroupId(), fileEntry.getFileEntryId()));

        DLTrashServiceUtil.moveFileEntryToTrash(fileEntry.getFileEntryId());

        Assert.assertEquals(0, getActiveDLFileRanksCount(group.getGroupId(), fileEntry.getFileEntryId()));

        trashHandler.restoreTrashEntry(TestPropsValues.getUserId(), fileEntry.getFileEntryId());

        Assert.assertEquals(1, getActiveDLFileRanksCount(group.getGroupId(), fileEntry.getFileEntryId()));
    } finally {
        trashHandler.deleteTrashEntry(fileEntry.getFileEntryId());
    }
}

From source file:com.liferay.journal.trash.test.JournalArticleTrashHandlerTest.java

License:Open Source License

@Test
public void testArticleImages() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(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 = _ddmFormXSDDeserializer.deserialize(definition);

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

    DDMTemplate ddmTemplate = DDMTemplateTestUtil.addTemplate(serviceContext.getScopeGroupId(),
            ddmStructure.getStructureId(), PortalUtil.getClassNameId(JournalArticle.class));

    InputStream inputStream = classLoader.getResourceAsStream("/com/liferay/journal/dependencies/liferay.png");

    FileEntry tempFileEntry = TempFileEntryUtil.addTempFileEntry(group.getGroupId(),
            TestPropsValues.getUserId(), JournalArticle.class.getName(), "liferay.png", inputStream,
            ContentTypes.IMAGE_PNG);/*from  w  w w.  ja va2  s  .c o m*/

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

    Document document = SAXReaderUtil.read(content);

    Element dynamicContent = (Element) document.selectSingleNode("//dynamic-content");

    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

    jsonObject.put("groupId", group.getGroupId());
    jsonObject.put("name", "liferay.png");
    jsonObject.put("tempFile", Boolean.TRUE.toString());
    jsonObject.put("title", "liferay.png");
    jsonObject.put("type", "journal");
    jsonObject.put("uuid", tempFileEntry.getUuid());

    dynamicContent.setText(jsonObject.toString());

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

    JournalArticle article = (JournalArticle) baseModel;

    long folderId = article.getImagesFolderId();

    Assert.assertEquals(1, PortletFileRepositoryUtil.getPortletFileEntriesCount(group.getGroupId(), folderId));

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

    Assert.assertEquals(0, PortletFileRepositoryUtil.getPortletFileEntriesCount(group.getGroupId(), folderId,
            WorkflowConstants.STATUS_APPROVED));
    Assert.assertEquals(1, PortletFileRepositoryUtil.getPortletFileEntriesCount(group.getGroupId(), folderId,
            WorkflowConstants.STATUS_IN_TRASH));

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

    trashHandler.deleteTrashEntry(getTrashEntryClassPK(baseModel));

    Assert.assertEquals(0, PortletFileRepositoryUtil.getPortletFileEntriesCount(group.getGroupId(), folderId));
}

From source file:com.liferay.trash.service.impl.TrashEntryLocalServiceImpl.java

License:Open Source License

@Override
public void checkEntries() throws PortalException {
    ActionableDynamicQuery actionableDynamicQuery = trashEntryLocalService.getActionableDynamicQuery();

    actionableDynamicQuery.setPerformActionMethod(new ActionableDynamicQuery.PerformActionMethod<TrashEntry>() {

        @Override//  w w w  . ja v  a 2s .c o  m
        public void performAction(TrashEntry trashEntry) throws PortalException {

            Date createDate = trashEntry.getCreateDate();

            Group group = groupLocalService.fetchGroup(trashEntry.getGroupId());

            if (group == null) {
                return;
            }

            Date date = getMaxAge(group);

            if (createDate.before(date) || !TrashUtil.isTrashEnabled(group)) {

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

                if (trashHandler != null) {
                    try {
                        trashHandler.deleteTrashEntry(trashEntry.getClassPK());
                    } catch (Exception e) {
                        _log.error(e, e);
                    }
                }
            }
        }

    });
    actionableDynamicQuery.setTransactionConfig(DefaultActionableDynamicQuery.REQUIRES_NEW_TRANSACTION_CONFIG);

    actionableDynamicQuery.performActions();
}

From source file:com.liferay.trash.service.impl.TrashEntryLocalServiceImpl.java

License:Open Source License

@Override
public void deleteEntries(long groupId, boolean deleteTrashedModels) {
    List<TrashEntry> entries = getEntries(groupId);

    for (TrashEntry entry : entries) {
        deleteEntry(entry);//from ww w .ja v  a2s.c o m

        if (deleteTrashedModels) {
            TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(entry.getClassName());

            if (trashHandler != null) {
                try {
                    trashHandler.deleteTrashEntry(entry.getClassPK());
                } catch (Exception e) {
                    _log.error(e, e);
                }
            }
        }
    }
}

From source file:com.liferay.trash.service.impl.TrashEntryServiceImpl.java

License:Open Source License

/**
 * 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>/*from   ww  w  .ja va  2s  .  co  m*/
 * 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:
 * </p>
 *
 * <ul>
 * <li>
 * {@link TrashPermissionException#RESTORE} - if the user did not have
 * permission to restore the trash entry
 * </li>
 * <li>
 * {@link TrashPermissionException#RESTORE_OVERWRITE} - if the user did not
 * have permission to delete the existing trash entry
 * </li>
 * <li>
 * {@link TrashPermissionException#RESTORE_RENAME} - if the user did not
 * have permission to rename the trash entry
 * </li>
 * </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
 */
@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(
                ModelAdapterUtil.adapt(com.liferay.trash.kernel.model.TrashEntry.class, 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(
                ModelAdapterUtil.adapt(com.liferay.trash.kernel.model.TrashEntry.class, entry),
                TrashEntryConstants.DEFAULT_CONTAINER_ID, name);

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

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

    return entry;
}

From source file:com.liferay.trash.service.impl.TrashEntryServiceImpl.java

License:Open Source License

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);
    }/*from  w w  w .j  a v a2 s  .  com*/

    trashHandler.deleteTrashEntry(entry.getClassPK());
}

From source file:com.liferay.trash.test.util.BaseTrashHandlerTestCase.java

License:Open Source License

@Test
public void testDeleteTrashVersions() throws Exception {
    Assume.assumeTrue(this instanceof WhenHasParent);
    Assume.assumeTrue(this instanceof WhenIsUpdatableBaseModel);

    WhenIsUpdatableBaseModel whenIsUpdatableBaseModel = (WhenIsUpdatableBaseModel) this;

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

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

    int initialTrashVersionsCount = TrashVersionLocalServiceUtil.getTrashVersionsCount();

    baseModel = addBaseModel(parentBaseModel, serviceContext);

    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);

    baseModel = whenIsUpdatableBaseModel.updateBaseModel((Long) baseModel.getPrimaryKeyObj(), serviceContext);

    WhenHasParent whenHasParent = (WhenHasParent) this;

    whenHasParent.moveParentBaseModelToTrash((Long) parentBaseModel.getPrimaryKeyObj());

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

    trashHandler.deleteTrashEntry(getTrashEntryClassPK(baseModel));

    Assert.assertEquals(initialTrashVersionsCount, TrashVersionLocalServiceUtil.getTrashVersionsCount());
}

From source file:com.liferay.trash.test.util.BaseTrashHandlerTestCase.java

License:Open Source License

@Test
public void testTrashAndDeleteWithApprovedStatus() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

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

    int initialBaseModelsCount = getNotInTrashBaseModelsCount(parentBaseModel);

    baseModel = addBaseModel(parentBaseModel, serviceContext);

    baseModel = getBaseModel((Long) baseModel.getPrimaryKeyObj());

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

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

    trashHandler.deleteTrashEntry(getTrashEntryClassPK(baseModel));

    Assert.assertEquals(initialBaseModelsCount, getNotInTrashBaseModelsCount(parentBaseModel));

    Assert.assertEquals(1, getDeletionSystemEventCount(trashHandler, -1));
}

From source file:com.liferay.trash.test.util.BaseTrashHandlerTestCase.java

License:Open Source License

@Test
public void testTrashAndDeleteWithApprovedStatusIndexable() throws Exception {

    Assume.assumeTrue(this instanceof WhenIsIndexableBaseModel);
    Assume.assumeFalse(this instanceof WhenParentModelIsSameType);

    WhenIsIndexableBaseModel whenIsIndexableBaseModel = (WhenIsIndexableBaseModel) this;

    int initialBaseModelsSearchCount = whenIsIndexableBaseModel.searchBaseModelsCount(getBaseModelClass(),
            group.getGroupId());/*from   w w  w . j av  a  2 s . c o  m*/

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

    int initialTrashEntriesSearchCount = whenIsIndexableBaseModel
            .searchTrashEntriesCount(whenIsIndexableBaseModel.getSearchKeywords(), serviceContext);

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

    baseModel = addBaseModel(parentBaseModel, serviceContext);

    baseModel = getBaseModel((Long) baseModel.getPrimaryKeyObj());

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

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

    trashHandler.deleteTrashEntry(getTrashEntryClassPK(baseModel));

    Assert.assertEquals(initialBaseModelsSearchCount,
            whenIsIndexableBaseModel.searchBaseModelsCount(getBaseModelClass(), group.getGroupId()));
    Assert.assertEquals(initialTrashEntriesSearchCount, whenIsIndexableBaseModel
            .searchTrashEntriesCount(whenIsIndexableBaseModel.getSearchKeywords(), serviceContext));
}

From source file:com.liferay.trash.test.util.BaseTrashHandlerTestCase.java

License:Open Source License

@Test
public void testTrashAndDeleteWithApprovedStatusIndexableAndEqualsParentAndBaseModelClass() throws Exception {

    Assume.assumeTrue(this instanceof WhenIsIndexableBaseModel);
    Assume.assumeTrue(this instanceof WhenParentModelIsSameType);

    WhenIsIndexableBaseModel whenIsIndexableBaseModel = (WhenIsIndexableBaseModel) this;

    int initialBaseModelsSearchCount = whenIsIndexableBaseModel.searchBaseModelsCount(getBaseModelClass(),
            group.getGroupId());//ww w  .  j a  va 2 s.c o m

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

    int initialTrashEntriesSearchCount = whenIsIndexableBaseModel
            .searchTrashEntriesCount(whenIsIndexableBaseModel.getSearchKeywords(), serviceContext);

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

    baseModel = addBaseModel(parentBaseModel, serviceContext);

    baseModel = getBaseModel((Long) baseModel.getPrimaryKeyObj());

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

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

    trashHandler.deleteTrashEntry(getTrashEntryClassPK(baseModel));

    Assert.assertEquals(initialBaseModelsSearchCount + 1,
            whenIsIndexableBaseModel.searchBaseModelsCount(getBaseModelClass(), group.getGroupId()));
    Assert.assertEquals(initialTrashEntriesSearchCount, whenIsIndexableBaseModel
            .searchTrashEntriesCount(whenIsIndexableBaseModel.getSearchKeywords(), serviceContext));
}