Untitled
unknown
plain_text
a year ago
5.0 kB
2
Indexable
Never
package com.ocp.kb.navigation.template.loader.upgrade.v1_1_0; import com.liferay.portal.kernel.upgrade.UpgradeProcess; import com.liferay.portal.kernel.xml.*; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import java.util.Objects; public class UpdateSiteNavigationMenuTemplateKeys extends UpgradeProcess { public static String SITE_NAVIGATION_MENU_PATTERN = "com_liferay_site_navigation_menu_web_portlet_SiteNavigationMenuPortlet%"; public static final String NAV_PILLS = "ddmTemplate_NAVIGATION_PILLS"; public static final String NAV_PILLS_STACKED = "ddmTemplate_NAVIGATION_PILLS_STACKED"; public static final String NAV_PILLS_JUSTIFIED = "ddmTemplate_NAVIGATION_PILLS_JUSTIFIED"; public static final String SPLIT_BUTTON_DROPDOWNS_FTL = "ddmTemplate_SPLIT-BUTTON-DROPDOWNS-FTL"; // @Override // protected String upgradePreferences(long companyId, long ownerId, int ownerType, long plid, String portletId, // String xml) throws Exception { // PortletPreferences portletPreferences = portletPreferencesFactory.fromXML(companyId, ownerId, ownerType, plid, // portletId, xml); // // String displayStyle = portletPreferences.getValue("displayStyle", StringPool.BLANK); // // if (!displayStyle.isBlank()) { // portletPreferences.setValue("displayStyle", getNewTemplateName(displayStyle)); // } // // return portletPreferencesFactory.toXML(portletPreferences); // } @Override protected void doUpgrade() throws Exception { Map<String, String> portletsToPreferencesForUpdate = getPortletsToPreferencesForUpdate(); updatePortletTemplateKeys(portletsToPreferencesForUpdate); } protected void updatePortletTemplateKeys(Map<String, String> portletsToPreferencesForUpdate) throws SQLException { try (PreparedStatement ps = connection.prepareStatement( "update PortletPreferences set preferences = ? where porletid = ?")) { // TODO: Дописать обновление через PreparedStatement, проверить, отдать в ревью. } } protected Map<String, String> getPortletsToPreferencesForUpdate() throws SQLException, DocumentException { Map<String, String> portletsToNewPreferences = new HashMap<>(); try (PreparedStatement ps = connection.prepareStatement( "select portletid, preferences " + "from PortletPreferences " + "where portletId like ? and preferences like ?")) { ps.setString(1, SITE_NAVIGATION_MENU_PATTERN); ps.setString(2, "%ddmTemplate_%"); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { String portletId = rs.getString("portletid"); String preferences = rs.getString("preferences"); String newPreferences = changeOldTemplateKeys(preferences); if (!preferences.equals(newPreferences)) { portletsToNewPreferences.put(portletId, newPreferences); } } } } return portletsToNewPreferences; } protected String changeOldTemplateKeys(String oldPreferences) throws DocumentException { Document document = SAXReaderUtil.read(oldPreferences); Element portletPreferences = document.getRootElement(); portletPreferences.elements("preference") .stream() .filter(e -> e.element("name").getStringValue().equals("displayStyle")) .forEach(e -> { Element displayStyleValue = e.element("value"); displayStyleValue.setText(getNewTemplateName(displayStyleValue.getText())); }); Document newPrefsDoc = portletPreferences.getDocument(); String newPrefsXml = newPrefsDoc.asXML(); int newLineIndex = newPrefsXml.indexOf('\n'); return (newLineIndex > 0) ? newPrefsXml.substring(newLineIndex + 1) : newPrefsXml; } protected String getNewTemplateName(String oldTemplateName) { String newTemplateName = oldTemplateName; switch (oldTemplateName) { case NAV_PILLS: case SPLIT_BUTTON_DROPDOWNS_FTL: newTemplateName = "ddmTemplate_NAV-PILLS-FTL"; break; case NAV_PILLS_STACKED: newTemplateName = "ddmTemplate_NAV-PILLS-STACKED-FTL"; break; case NAV_PILLS_JUSTIFIED: newTemplateName = "ddmTemplate_NAV-PILLS-JUSTIFIED-FTL"; break; } return newTemplateName; } }