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.dao.jdbc.DataAccess;
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    import com.liferay.portlet.PortletPreferencesFactoryUtil;
026    
027    import java.sql.Connection;
028    import java.sql.PreparedStatement;
029    import java.sql.ResultSet;
030    
031    import javax.portlet.PortletPreferences;
032    
033    /**
034     * @author Julio Camarero
035     */
036    public class UpgradeAssetPublisher extends BaseUpgradePortletPreferences {
037    
038            protected String[] getAssetEntryXmls(String[] assetEntryXmls)
039                    throws Exception {
040    
041                    String[] newAssetEntryXmls = new String[assetEntryXmls.length];
042    
043                    for (int i = 0; i < assetEntryXmls.length; i++) {
044                            String assetEntryXml = assetEntryXmls[i];
045    
046                            Document document = SAXReaderUtil.read(assetEntryXml);
047    
048                            Element rootElement = document.getRootElement();
049    
050                            upgradeToAssetEntryUuidElement(rootElement);
051    
052                            newAssetEntryXmls[i] = document.formattedString(StringPool.BLANK);
053                    }
054    
055                    return newAssetEntryXmls;
056            }
057    
058            @Override
059            protected String[] getPortletIds() {
060                    return new String[] {"101_INSTANCE_%"};
061            }
062    
063            @Override
064            protected String upgradePreferences(
065                            long companyId, long ownerId, int ownerType, long plid,
066                            String portletId, String xml)
067                    throws Exception {
068    
069                    PortletPreferences portletPreferences =
070                            PortletPreferencesFactoryUtil.fromXML(
071                                    companyId, ownerId, ownerType, plid, portletId, xml);
072    
073                    String selectionStyle = portletPreferences.getValue(
074                            "selection-style", null);
075    
076                    if (Validator.isNotNull(selectionStyle) &&
077                            !selectionStyle.equals("dynamic")) {
078    
079                            String[] assetEntryXmls = portletPreferences.getValues(
080                                    "asset-entry-xml", new String[0]);
081    
082                            String[] newAssetEntryXmls = getAssetEntryXmls(assetEntryXmls);
083    
084                            portletPreferences.setValues("asset-entry-xml", newAssetEntryXmls);
085                    }
086    
087                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
088            }
089    
090            protected void upgradeToAssetEntryUuidElement(Element rootElement)
091                    throws Exception {
092    
093                    Element assetEntryIdElement = rootElement.element("assetEntryId");
094    
095                    long assetEntryId = GetterUtil.getLong(assetEntryIdElement.getText());
096    
097                    Connection con = null;
098                    PreparedStatement ps = null;
099                    ResultSet rs = null;
100    
101                    try {
102                            con = DataAccess.getUpgradeOptimizedConnection();
103    
104                            ps = con.prepareStatement(
105                                    "select classUuid from AssetEntry where entryId = ?");
106    
107                            ps.setLong(1, assetEntryId);
108    
109                            rs = ps.executeQuery();
110    
111                            if (rs.next()) {
112                                    String classUuid = rs.getString("classUuid");
113    
114                                    Element assetEntryUuidElement = rootElement.addElement(
115                                            "assetEntryUuid");
116    
117                                    assetEntryUuidElement.addText(classUuid);
118    
119                                    rootElement.remove(assetEntryIdElement);
120                            }
121                    }
122                    finally {
123                            DataAccess.cleanUp(con, ps, rs);
124                    }
125            }
126    
127    }