Skip to content

Instantly share code, notes, and snippets.

@nshaw
Created June 1, 2016 21:10
Show Gist options
  • Save nshaw/6843635a059703b8862d36615c61e1c7 to your computer and use it in GitHub Desktop.
Save nshaw/6843635a059703b8862d36615c61e1c7 to your computer and use it in GitHub Desktop.
import com.liferay.portal.kernel.util.GetterUtil
import com.liferay.portal.kernel.util.Validator
import com.liferay.portal.kernel.xml.Document
import com.liferay.portal.kernel.xml.Element
import com.liferay.portal.kernel.xml.SAXReaderUtil
import com.liferay.portal.model.LayoutSet
import com.liferay.portal.model.Portlet
import com.liferay.portal.model.PortletPreferences
import com.liferay.portal.service.GroupLocalServiceUtil
import com.liferay.portal.service.LayoutSetLocalServiceUtil
import com.liferay.portal.service.PortletLocalServiceUtil
import com.liferay.portal.service.PortletPreferencesLocalServiceUtil
import com.liferay.portal.util.PortalUtil
import com.liferay.portal.util.PortletKeys
try {
String siteName = "Test Staging"
long companyId = PortalUtil.getDefaultCompanyId();
long groupId = GroupLocalServiceUtil.getGroup(companyId, siteName).getGroupId()
println "Site: " + siteName + ", groupId: " + groupId
// Check the timestamps on the layout sets
println "--- Layout Sets ---"
checkLayoutSet(groupId, false)
checkLayoutSet(groupId, true)
// Check the timestamps for top-level portlets, owned by the group itself
println "--- Site Portlets ---"
List<PortletPreferences> portlets =
PortletPreferencesLocalServiceUtil.getPortletPreferences(groupId,
PortletKeys.PREFS_OWNER_TYPE_GROUP, PortletKeys.PREFS_PLID_SHARED);
showPortletTimestamps(portlets)
}
catch (Exception e) {
println "Script failed: " + e
}
def void checkLayoutSet(long groupId, boolean privateLayout) {
LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(groupId, privateLayout)
String label = (privateLayout) ? "Private Pages" : "Public Pages"
String lastPublishDate = layoutSet.getSettingsProperty("last-publish-date")
if (Validator.isNull(lastPublishDate)) {
println label + " has no last publish date."
}
else {
println label + " last published at " + formatDate(lastPublishDate)
}
}
def void showPortletTimestamps(List<PortletPreferences> portlets) {
for (PortletPreferences prefs : portlets) {
String xml = prefs.getPreferences();
// println HtmlUtil.escape(xml)
String portletId = prefs.getPortletId();
String portletName = portletId;
Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
if (portlet != null) {
portletName = portlet.getDisplayName()
}
if (Validator.isNull(xml)) {
println portletName + " (" + portletId + "), no portlet preferences."
continue;
}
try {
Document document = SAXReaderUtil.read(xml);
Element root = document.getRootElement();
com.liferay.portal.kernel.xml.Node node = root.selectSingleNode("/portlet-preferences/preference[name='last-publish-date']/value");
if (node != null) {
String lastPublish = node.getText();
println portletName + " (" + portletId + "), last-publish-date: " + formatDate(lastPublish)
}
}
catch (Exception e) {
println "Unable to check preferences for portlet " + portletId + " - xml: " + xml;
}
}
}
def String formatDate(String timeInMillis) {
long lastPublishDate = GetterUtil.getLong(timeInMillis)
if (lastPublishDate > 0) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(lastPublishDate);
return cal.getTime();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment