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.v6_0_3;
016    
017    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.kernel.xml.SAXReaderUtil;
023    import com.liferay.portal.upgrade.util.UpgradeAssetPublisherManualEntries;
024    import com.liferay.portlet.PortletPreferencesFactoryUtil;
025    
026    import javax.portlet.PortletPreferences;
027    
028    /**
029     * @author Julio Camarero
030     */
031    public class UpgradeAssetPublisher extends BaseUpgradePortletPreferences {
032    
033            protected String[] getAssetEntryXmls(String[] assetEntryXmls)
034                    throws Exception {
035    
036                    String[] newAssetEntryXmls = new String[assetEntryXmls.length];
037    
038                    for (int i = 0; i < assetEntryXmls.length; i++) {
039                            String assetEntryXml = assetEntryXmls[i];
040    
041                            Document document = SAXReaderUtil.read(assetEntryXml);
042    
043                            Element rootElement = document.getRootElement();
044    
045                            UpgradeAssetPublisherManualEntries.upgradeToAssetEntryUuidElement(
046                                    rootElement);
047    
048                            newAssetEntryXmls[i] = document.formattedString(StringPool.BLANK);
049                    }
050    
051                    return newAssetEntryXmls;
052            }
053    
054            @Override
055            protected String[] getPortletIds() {
056                    return new String[] {"101_INSTANCE_%"};
057            }
058    
059            @Override
060            protected String upgradePreferences(
061                            long companyId, long ownerId, int ownerType, long plid,
062                            String portletId, String xml)
063                    throws Exception {
064    
065                    PortletPreferences portletPreferences =
066                            PortletPreferencesFactoryUtil.fromXML(
067                                    companyId, ownerId, ownerType, plid, portletId, xml);
068    
069                    String selectionStyle = portletPreferences.getValue(
070                            "selection-style", null);
071    
072                    if (Validator.isNotNull(selectionStyle) &&
073                            !selectionStyle.equals("dynamic")) {
074    
075                            String[] assetEntryXmls = portletPreferences.getValues(
076                                    "asset-entry-xml", new String[0]);
077    
078                            String[] newAssetEntryXmls = getAssetEntryXmls(assetEntryXmls);
079    
080                            portletPreferences.setValues("asset-entry-xml", newAssetEntryXmls);
081                    }
082    
083                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
084            }
085    
086    }