001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 upgradeToAssetEntryUuidElement(Element rootElement)
041                    throws Exception {
042    
043                    Element assetEntryIdElement = rootElement.element("asset-entry-id");
044    
045                    long assetEntryId = GetterUtil.getLong(assetEntryIdElement.getText());
046    
047                    Connection con = null;
048                    PreparedStatement ps = null;
049                    ResultSet rs = null;
050    
051                    try {
052                            con = DataAccess.getConnection();
053    
054                            ps = con.prepareStatement(
055                                    "select classUuid from AssetEntry where entryId = ?");
056    
057                            ps.setLong(1, assetEntryId);
058    
059                            rs = ps.executeQuery();
060    
061                            if (rs.next()) {
062                                    String classUuid = rs.getString("classUuid");
063    
064                                    Element assetEntryUuidElement = rootElement.addElement(
065                                            "assetEntryUuid");
066    
067                                    assetEntryUuidElement.addText(classUuid);
068    
069                                    rootElement.remove(assetEntryIdElement);
070                            }
071                    }
072                    finally {
073                            DataAccess.cleanUp(con, ps, rs);
074                    }
075            }
076    
077            public static void upgradeToAssetEntryIdElement(Element rootElement) {
078                    Element assetIdElement = rootElement.element("asset-id");
079    
080                    if (Validator.isNotNull(assetIdElement)) {
081                            String assetEntryId = assetIdElement.getText();
082    
083                            Element assetEntryIdElement = rootElement.addElement(
084                                    "assetEntryId");
085    
086                            assetEntryIdElement.addText(assetEntryId);
087    
088                            rootElement.remove(assetIdElement);
089                    }
090            }
091    
092            public static void upgradeToAssetEntryTypeElement(Element rootElement) {
093                    Element assetTypeElement = rootElement.element("asset-type");
094    
095                    if (Validator.isNotNull(assetTypeElement)) {
096                            String assetEntryType = assetTypeElement.getText();
097    
098                            Element assetEntryTypeElement = rootElement.addElement(
099                                    "assetEntryType");
100    
101                            assetEntryTypeElement.addText(assetEntryType);
102    
103                            rootElement.remove(assetTypeElement);
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    }