Skip to content

Instantly share code, notes, and snippets.

@nshaw
Last active June 1, 2016 21:09
Show Gist options
  • Save nshaw/781b7a5aa56d77c2087a to your computer and use it in GitHub Desktop.
Save nshaw/781b7a5aa56d77c2087a to your computer and use it in GitHub Desktop.
show_publish_dates.groovy
import com.liferay.portal.kernel.util.DateUtil
import com.liferay.portal.kernel.util.GetterUtil
import com.liferay.portal.kernel.util.StringPool
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.Layout
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.LayoutLocalServiceUtil
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
import com.liferay.portlet.PortletPreferencesFactoryUtil
try {
// String siteName = "Demo Site"
// boolean resetDMPublishDates = true;
// String checkFriendlyUrl = "/dm-publish"
//Local
String siteName = "Test6 Staging"
boolean resetDMPublishDates = false;
String checkFriendlyUrl = null;
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)
println "--- Pages ---"
if (checkFriendlyUrl != null) {
Layout layout =
LayoutLocalServiceUtil.getFriendlyURLLayout(groupId, true, checkFriendlyUrl);
showPageTimestamps(layout, resetDMPublishDates);
}
else {
// Check the timestamps for top level pages in each layoutSet
println "----- Public Pages -----"
showPagesTimestamps(groupId, false, resetDMPublishDates);
println "----- Private Pages -----"
showPagesTimestamps(groupId, true, resetDMPublishDates);
}
}
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 showPagesTimestamps(long groupId, boolean privateLayout, boolean resetDMPublishDate) {
List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(groupId, privateLayout, 0);
for (Layout layout : layouts) {
showPageTimestamps(layout, resetDMPublishDate);
}
}
def void showPageTimestamps(Layout layout, boolean resetDMPublishDate) {
long plid = layout.getPlid();
println "-- Page:" + plid + ", " + layout.getName(Locale.ENGLISH) + ", " + layout.getFriendlyURL()
portlets =
PortletPreferencesLocalServiceUtil.getPortletPreferences(PortletKeys.PREFS_OWNER_ID_DEFAULT,
PortletKeys.PREFS_OWNER_TYPE_LAYOUT, plid);
showPortletTimestamps(portlets)
checkDMLastPublishDate(layout, resetDMPublishDate)
}
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()
}
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)
}
}
}
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;
}
def checkDMLastPublishDate(Layout layout, boolean doUpdate) {
String portletId = "20"; //Documents and Media
javax.portlet.PortletPreferences jxPreferences = PortletPreferencesFactoryUtil.getPortletSetup(
(Layout) layout, (String) portletId, StringPool.BLANK);
String lastPublishDate = jxPreferences.getValue("last-publish-date", StringPool.BLANK);
// println "Page " + layout.getFriendlyURL() + " currently has DM publishDate: " + formatDate(lastPublishDate)
if (doUpdate) {
Date date = DateUtil.newDate()
println "Setting new DM publish date:" + date
jxPreferences.setValue("last-publish-date", String.valueOf(date.getTime()))
jxPreferences.store()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment