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