Example: Removing Intermediate Journal Article Versions

These instructions and code samples demonstrate removing intermediate Journal Article versions. In the script console, you can remove unneeded object versions by executing Java or Groovy code.

Here are example steps for removing intermediate Journal Article versions:

  1. Decide how many of the latest versions to keep. You must keep the original version and the most recent version, but you may keep older recent versions too. For example, you may want to keep the two latest versions or just the latest.

  2. Find a method for deleting the entity versions. Liferay DXP app APIs and com.lifieray.portal.kernel API are available at https://docs.liferay.com/dxp/portal.

    If it’s a Service Builder entity, examine the delete* methods in the entity’s *LocalServiceUtil class. For example, this deleteArticle in JournalArticleLocalServiceUtil deletes a Journal Article version:

    deleteArticle(long groupId, java.lang.String articleId, double version, 
        java.lang.String articleURL, 
        com.liferay.portal.kernel.service.ServiceContext serviceContext)
  3. Aggregate the entity versions to delete and the information required to delete them. For example, get all the Journal Article versions in range that match your removal criteria and associate their entity IDs and group IDs with them—the deleteArticle method requires the entity ID and group ID.

    The entity object (e.g., JournalArticle) typically has a version field. JournalArticleResource has each Journal Article’s article ID (the entity’s ID) and group ID. JournalArticleResource is our key to getting each JournalArticle, which can have multiple versions. Here are steps for identifying the Journal Article versions to delete:

    1. Get all the JournalArticleResource objects.
    List<JournalArticleResource> journalArticleResources = 
        JournalArticleLocalServiceUtil.getJournalArticleResources(start, end);
    1. Get each Journal Article version’s workflow status via the JournalArticle object associated with each JournalArticleResource. Dynamic Query is an efficient way to get exactly the data you want (and nothing more) from each object.

    for (JournalArticleResource
    	journalArticeResource : journalArticleResources) {
    	List<Double> journalArticlesVersionsToDelete =
    		new ArrayList<Double>();
        DynamicQuery dq =
        List<Object[]> result =
        // See the next step for the sample code that goes here
    1. For each JournalArticleResource (there’s one for each Journal Article entity), build a list of intermediate versions in range of the first or latest versions you want to keep and whose status qualifies them for deletion. For example, you may want to delete intermediate article versions that are approved or expired (i.e., WorkflowConstants.STATUS_APPROVED or WorkflowConstants.STATUS_EXPIRED). The MIN_NUMBER_FIRST_VERSIONS_KEPT and MIN_NUMBER_LATEST_VERSIONS_KEPT variables here mark the minimum and maximum number of first (oldest) and latest (newest) versions to keep.
    List<Double> journalArticlesVersionsToDelete =
    	new ArrayList<Double>();
    for (int i=0; i < result.size(); i++) {
    	long id = (long) result.get(i)[0];
    	double version = (double) result.get(i)[1];
    	int status = (int) result.get(i)[2];
    	if ((status == WorkflowConstants.STATUS_APPROVED) || (status == WorkflowConstants.STATUS_EXPIRED) {
    		if (i >= (result.size() -
    // See the next step for the sample code that goes here
  4. Lastly, delete each Journal Article matching the versions you aggregated.

    for (double version : journalArticlesVersionsToDelete) {
            journalArticlesVersionsToDelete(i), null, null);

You can write similar code to remove intermediate versions of other entities.

After you’ve pruned your database, test it with Liferay DXP.

« Pruning the DatabaseUpgrading Your Test Server and Database »
Was this article helpful?
0 out of 0 found this helpful