Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lahiponeja/6840fb51bff99dcbb7a948165667000d to your computer and use it in GitHub Desktop.
Save lahiponeja/6840fb51bff99dcbb7a948165667000d to your computer and use it in GitHub Desktop.
Liferay 6.2 script to remove all old document versions except the latest version. This version cleans out all document versions.
import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery
import com.liferay.portal.kernel.exception.PortalException
import com.liferay.portal.kernel.exception.SystemException
import com.liferay.portal.util.PropsValues
import com.liferay.portlet.documentlibrary.model.DLFileEntry
import com.liferay.portlet.documentlibrary.model.DLFileVersion
import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil
import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil
import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryActionableDynamicQuery
import com.liferay.portlet.documentlibrary.service.persistence.DLFileVersionActionableDynamicQuery
// Since we are going to utilize the previewMode variable in our
// ActionableDynamicQuery method, it must be made final
final previewMode = true;
if(previewMode) {
out.println(
"""<div class="portlet-msg-alert">Preview mode is on: switch off the flag and execute this script again to make changes to the database</div>""")
}
def SCRIPT_ID = "Remove-File-Versions-All"
outputFile = new File(
"""${System.getProperty("liferay.home")}/scripting/out-${SCRIPT_ID}.txt""")
outputFile.getParentFile().mkdirs()
def trace(message) {
out.println(message)
outputFile << "${message}\n"
}
ActionableDynamicQuery actionableDynamicQuery =
new DLFileEntryActionableDynamicQuery() {
// This time, we do not need to implement the addCriteria method because we
// want to select every DLFileEntry in the database. We will only
// implement the performAction method.
@Override
protected void performAction(Object object) {
// Since the method uses Object in its method signature, we must
// first cast it to a DLFileEntry
DLFileEntry file = (DLFileEntry)object
List<DLFileVersion> results = file.getFileVersions(-1);
long theFEId = file.getFileEntryId();
DLFileVersion latestversionColl =
DLFileVersionLocalServiceUtil.getLatestFileVersion(
theFEId, true);
String truelatestversion = latestversionColl.getVersion();
for(DLFileVersion fv : results) {
try {
if (fv.getVersion() != truelatestversion) {
trace(
'This version: ' + fv.getVersion() + ' with Title=' +
fv.getTitle() + ' is older than ' + truelatestversion +
' so deleting ->' + fv.getVersion());
if (!previewMode) {
DLAppServiceUtil.deleteFileVersion(
fv.getFileEntryId(), fv.getVersion());
}
}
}
catch (Exception e1) {
println(e1)
e1.printStackTrace(out)
}
}
}
}
// Set the batch size to a smaller interval since DLFileEntries can potentially
// be very large objects
actionableDynamicQuery.setInterval(PropsValues.DL_FILE_INDEXING_INTERVAL);
// Now that we have our ActionableDynamicQuery object, we simply call its
// performActions method!
actionableDynamicQuery.performActions()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment