001    /**
002     * Copyright (c) 2000-2013 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.util;
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.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portlet.PortletPreferencesFactoryUtil;
027    
028    import java.sql.Connection;
029    import java.sql.PreparedStatement;
030    import java.sql.ResultSet;
031    
032    import javax.portlet.PortletPreferences;
033    
034    /**
035     * @author Douglas Wong
036     */
037    public class UpgradeAssetPublisherManualEntries
038            extends BaseUpgradePortletPreferences {
039    
040            public static void upgradeToAssetEntryIdElement(Element rootElement) {
041                    Element assetIdElement = rootElement.element("asset-id");
042    
043                    if (Validator.isNotNull(assetIdElement)) {
044                            String assetEntryId = assetIdElement.getText();
045    
046                            Element assetEntryIdElement = rootElement.addElement(
047                                    "assetEntryId");
048    
049                            assetEntryIdElement.addText(assetEntryId);
050    
051                            rootElement.remove(assetIdElement);
052                    }
053            }
054    
055            public static void upgradeToAssetEntryTypeElement(Element rootElement) {
056                    Element assetTypeElement = rootElement.element("asset-type");
057    
058                    if (Validator.isNotNull(assetTypeElement)) {
059                            String assetEntryType = assetTypeElement.getText();
060    
061                            Element assetEntryTypeElement = rootElement.addElement(
062                                    "assetEntryType");
063    
064                            assetEntryTypeElement.addText(assetEntryType);
065    
066                            rootElement.remove(assetTypeElement);
067                    }
068            }
069    
070            public static void upgradeToAssetEntryUuidElement(Element rootElement)
071                    throws Exception {
072    
073                    Element assetEntryIdElement = rootElement.element("assetEntryId");
074    
075                    long assetEntryId = GetterUtil.getLong(assetEntryIdElement.getText());
076    
077                    Connection con = null;
078                    PreparedStatement ps = null;
079                    ResultSet rs = null;
080    
081                    try {
082                            con = DataAccess.getUpgradeOptimizedConnection();
083    
084                            ps = con.prepareStatement(
085                                    "select classUuid from AssetEntry where entryId = ?");
086    
087                            ps.setLong(1, assetEntryId);
088    
089                            rs = ps.executeQuery();
090    
091                            if (rs.next()) {
092                                    String classUuid = rs.getString("classUuid");
093    
094                                    Element assetEntryUuidElement = rootElement.addElement(
095                                            "assetEntryUuid");
096    
097                                    assetEntryUuidElement.addText(classUuid);
098    
099                                    rootElement.remove(assetEntryIdElement);
100                            }
101                    }
102                    finally {
103                            DataAccess.cleanUp(con, ps, rs);
104                    }
105            }
106    
107            protected String[] getAssetEntryXmls(String[] manualEntries)
108                    throws Exception {
109    
110                    String[] assetEntryXmls = new String[manualEntries.length];
111    
112                    for (int i = 0; i < manualEntries.length; i++) {
113                            String manualEntry = manualEntries[i];
114    
115                            Document document = SAXReaderUtil.read(manualEntry);
116    
117                            Element rootElement = document.getRootElement();
118    
119                            upgradeToAssetEntryIdElement(rootElement);
120    
121                            upgradeToAssetEntryUuidElement(rootElement);
122    
123                            upgradeToAssetEntryTypeElement(rootElement);
124    
125                            assetEntryXmls[i] = document.formattedString(StringPool.BLANK);
126                    }
127    
128                    return assetEntryXmls;
129            }
130    
131            @Override
132            protected String getUpdatePortletPreferencesWhereClause() {
133                    StringBundler sb = new StringBundler(5);
134    
135                    sb.append("(portletId like '101_INSTANCE_%') and ((preferences like ");
136                    sb.append("'%<preference><name>selection-style</name><value>manual");
137                    sb.append("</value></preference>%') OR (preferences like ");
138                    sb.append("'%<preference><name>selectionStyle</name><value>manual");
139                    sb.append("</value></preference>%'))");
140    
141                    return sb.toString();
142            }
143    
144            @Override
145            protected String upgradePreferences(
146                            long companyId, long ownerId, int ownerType, long plid,
147                            String portletId, String xml)
148                    throws Exception {
149    
150                    PortletPreferences portletPreferences =
151                            PortletPreferencesFactoryUtil.fromXML(
152                                    companyId, ownerId, ownerType, plid, portletId, xml);
153    
154                    String[] assetEntryXmls = portletPreferences.getValues(
155                            "asset-entry-xml", new String[0]);
156    
157                    if (Validator.isNull(assetEntryXmls)) {
158                            assetEntryXmls = portletPreferences.getValues(
159                                    "assetEntryXml", new String[0]);
160                    }
161    
162                    String[] manualEntries = portletPreferences.getValues(
163                            "manual-entries", new String[0]);
164    
165                    if (Validator.isNull(manualEntries)) {
166                            manualEntries = portletPreferences.getValues(
167                                    "manualEntries", new String[0]);
168                    }
169    
170                    if (Validator.isNull(assetEntryXmls) &&
171                            Validator.isNotNull(manualEntries)) {
172    
173                            assetEntryXmls = getAssetEntryXmls(manualEntries);
174    
175                            portletPreferences.setValues("asset-entry-xml", assetEntryXmls);
176                    }
177    
178                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
179            }
180    
181    }