Skip to content

Instantly share code, notes, and snippets.

@datyger
Last active May 4, 2018 15:16
Show Gist options
  • Save datyger/4dcf165ec9586b60224fea3dafece7c4 to your computer and use it in GitHub Desktop.
Save datyger/4dcf165ec9586b60224fea3dafece7c4 to your computer and use it in GitHub Desktop.
Liferay 6.2 script for deleting layouts (pages), based on various columns and query logic. Thanks to Michael Bowerman for the help!
import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery
import com.liferay.portal.kernel.dao.orm.DynamicQuery
import com.liferay.portal.kernel.dao.orm.Property
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil
import com.liferay.portal.kernel.exception.PortalException
import com.liferay.portal.kernel.exception.SystemException
import com.liferay.portal.model.Layout
import com.liferay.portal.service.LayoutLocalServiceUtil
import com.liferay.portal.service.persistence.LayoutActionableDynamicQuery
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil
import com.liferay.portal.kernel.dao.orm.Disjunction
// Since we are going to utilize the previewMode variable in our
// ActionableDynamicQuery method, it must be made final
final boolean 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>""")
}
final def SCRIPT_ID = "DELETE-BLOG-and-FRIENDS-PAGES"
outputFile = new File(
"""${System.getProperty("liferay.home")}/scripting/out-${SCRIPT_ID}.txt""")
outputFile.getParentFile().mkdirs()
def trace(message) {
out.println(message)
outputFile << "${message}\n"
}
try {
final sc = new com.liferay.portal.service.ServiceContext()
ActionableDynamicQuery actionableDynamicQuery =
new LayoutActionableDynamicQuery() {
// The addCriteria method allows us to specify the criteria that an
// object must meet in order to be eligible for processing. In this
// case, we'll specify that the layout must have a friendlyURL of
// "/friends"
@Override
public void addCriteria(DynamicQuery dynamicQuery) {
// Create a Property object, which specifies the name of the column
// that we are filtering by
Property property = PropertyFactoryUtil.forName("friendlyURL");
// Tell our DynamicQuery object that we want to fetch layouts
// for which our property (The "friendlyURL" column) is equal to
// "/friends" OR "/blog"
Disjunction disjunction = RestrictionsFactoryUtil.disjunction();
disjunction.add(property.eq("/friends"));
disjunction.add(property.eq("/blog"));
dynamicQuery.add(disjunction);
}
// The performAction method will be invoked on each object that is
// fetched. In this case, we will use it to delete the layout
@Override
protected void performAction(Object object)
throws PortalException, SystemException {
// Since the method uses Object in its method signature, we must
// first cast it to a Layout
Layout layout = (Layout)object
trace(
"Deleting... layoutPlid=" + layout.getPlid() + " with GroupId="
+ layout.getGroupId() + " with URL of " + layout.getFriendlyURL() )
if (!previewMode) {
LayoutLocalServiceUtil.deleteLayout(layout, true, sc)
}
}
}
// Now that we have our ActionableDynamicQuery object, we simply call its
// performActions method!
actionableDynamicQuery.performActions()
}
catch (Exception e) {
println(e)
e.printStackTrace(out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment