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

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

Introduction

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

Prototype

public void updateTitle(long classPK, String title) throws PortalException;

Source Link

Document

Updates the title of the model entity with the primary key.

Usage

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

License:Open Source License

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

    DLFileEntry dlFileEntry = (DLFileEntry) addBaseModelWithWorkflow(serviceContext);

    moveBaseModelToTrash(dlFileEntry.getFileEntryId());

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

    String title = RandomTestUtil.randomString();

    trashHandler.updateTitle(dlFileEntry.getFileEntryId(), title);

    dlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(dlFileEntry.getFileEntryId());

    DLFileVersion dlFileVersion = dlFileEntry.getLatestFileVersion(true);

    Assert.assertEquals(DLUtil.getSanitizedFileName(title, dlFileEntry.getExtension()),
            dlFileEntry.getFileName());//from  w  w  w  .  j a v  a 2 s  .com

    Assert.assertEquals(DLUtil.getSanitizedFileName(title, dlFileVersion.getExtension()),
            dlFileVersion.getFileName());
}

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   w  w w  .  j a va 2s  .  c o  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;
}