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:
-
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.
-
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, thisdeleteArticle
inJournalArticleLocalServiceUtil
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)
-
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 eachJournalArticle
, which can have multiple versions. Here are steps for identifying the Journal Article versions to delete:- Get all the
JournalArticleResource
objects.
List<JournalArticleResource> journalArticleResources = JournalArticleLocalServiceUtil.getJournalArticleResources(start, end);
-
Get each Journal Article version’s workflow status via the
JournalArticle
object associated with eachJournalArticleResource
. 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 = DynamicQueryFactoryUtil.forClass(JournalArticle.class) .setProjection(ProjectionFactoryUtil.projectionList() .add(ProjectionFactoryUtil.property("id")) .add(ProjectionFactoryUtil.property("version")) .add(ProjectionFactoryUtil.property("status"))) .add(PropertyFactoryUtil.forName("groupId") .eq(journalArticeResource.getGroupId())) .add(PropertyFactoryUtil.forName("articleId") .eq(journalArticeResource.getArticleId())) .addOrder(OrderFactoryUtil.asc("version")); List<Object[]> result = JournalArticleLocalServiceUtil.dynamicQuery(dq); // See the next step for the sample code that goes here }
- 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). TheMIN_NUMBER_FIRST_VERSIONS_KEPT
andMIN_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 < MIN_NUMBER_FIRST_VERSIONS_KEPT) { continue; } if (i >= (result.size() - MIN_NUMBER_LATEST_VERSIONS_KEPT)) { continue; } journalArticlesVersionsToDelete.add(version); } } // See the next step for the sample code that goes here
- Get all the
-
Lastly, delete each Journal Article matching the versions you aggregated.
for (double version : journalArticlesVersionsToDelete) { { JournalArticleLocalServiceUtil.deleteArticle(journalArticeResource.getGroupId(), journalArticeResource.getArticleId(), 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.