001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v6_0_3;
016    
017    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
018    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.kernel.xml.SAXReaderUtil;
025    
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    import javax.portlet.PortletPreferences;
030    
031    /**
032     * @author Julio Camarero
033     */
034    public class UpgradeAssetPublisher extends BaseUpgradePortletPreferences {
035    
036            protected String[] getAssetEntryXmls(String[] assetEntryXmls)
037                    throws Exception {
038    
039                    String[] newAssetEntryXmls = new String[assetEntryXmls.length];
040    
041                    for (int i = 0; i < assetEntryXmls.length; i++) {
042                            String assetEntryXml = assetEntryXmls[i];
043    
044                            Document document = SAXReaderUtil.read(assetEntryXml);
045    
046                            Element rootElement = document.getRootElement();
047    
048                            upgradeToAssetEntryUuidElement(rootElement);
049    
050                            newAssetEntryXmls[i] = document.formattedString(StringPool.BLANK);
051                    }
052    
053                    return newAssetEntryXmls;
054            }
055    
056            @Override
057            protected String[] getPortletIds() {
058                    return new String[] {"101_INSTANCE_%"};
059            }
060    
061            @Override
062            protected String upgradePreferences(
063                            long companyId, long ownerId, int ownerType, long plid,
064                            String portletId, String xml)
065                    throws Exception {
066    
067                    PortletPreferences portletPreferences =
068                            PortletPreferencesFactoryUtil.fromXML(
069                                    companyId, ownerId, ownerType, plid, portletId, xml);
070    
071                    String selectionStyle = portletPreferences.getValue(
072                            "selection-style", null);
073    
074                    if (Validator.isNotNull(selectionStyle) &&
075                            !selectionStyle.equals("dynamic")) {
076    
077                            String[] assetEntryXmls = portletPreferences.getValues(
078                                    "asset-entry-xml", new String[0]);
079    
080                            String[] newAssetEntryXmls = getAssetEntryXmls(assetEntryXmls);
081    
082                            portletPreferences.setValues("asset-entry-xml", newAssetEntryXmls);
083                    }
084    
085                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
086            }
087    
088            protected void upgradeToAssetEntryUuidElement(Element rootElement)
089                    throws Exception {
090    
091                    Element assetEntryIdElement = rootElement.element("assetEntryId");
092    
093                    long assetEntryId = GetterUtil.getLong(assetEntryIdElement.getText());
094    
095                    try (PreparedStatement ps = connection.prepareStatement(
096                                    "select classUuid from AssetEntry where entryId = ?")) {
097    
098                            ps.setLong(1, assetEntryId);
099    
100                            try (ResultSet rs = ps.executeQuery()) {
101                                    if (rs.next()) {
102                                            String classUuid = rs.getString("classUuid");
103    
104                                            Element assetEntryUuidElement = rootElement.addElement(
105                                                    "assetEntryUuid");
106    
107                                            assetEntryUuidElement.addText(classUuid);
108    
109                                            rootElement.remove(assetEntryIdElement);
110                                    }
111                            }
112                    }
113            }
114    
115    }