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_2_0;
016    
017    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.model.GroupConstants;
021    import com.liferay.portlet.PortletPreferencesFactoryUtil;
022    import com.liferay.portlet.assetpublisher.util.AssetPublisher;
023    import com.liferay.util.RSSUtil;
024    
025    import javax.portlet.PortletPreferences;
026    
027    /**
028     * @author Eduardo Garcia
029     * @author Jorge Ferrer
030     */
031    public class UpgradeAssetPublisher extends BaseUpgradePortletPreferences {
032    
033            @Override
034            protected String[] getPortletIds() {
035                    return new String[] {"101_INSTANCE_%"};
036            }
037    
038            @Override
039            protected String upgradePreferences(
040                            long companyId, long ownerId, int ownerType, long plid,
041                            String portletId, String xml)
042                    throws Exception {
043    
044                    PortletPreferences portletPreferences =
045                            PortletPreferencesFactoryUtil.fromXML(
046                                    companyId, ownerId, ownerType, plid, portletId, xml);
047    
048                    upgradeRss(portletPreferences);
049                    upgradeScopeIds(portletPreferences);
050    
051                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
052            }
053    
054            protected void upgradeRss(PortletPreferences portletPreferences)
055                    throws Exception {
056    
057                    String rssFormat = GetterUtil.getString(
058                            portletPreferences.getValue("rssFormat", null));
059    
060                    if (Validator.isNotNull(rssFormat)) {
061                            portletPreferences.setValue(
062                                    "rssFeedType",
063                                    RSSUtil.getFeedType(
064                                            RSSUtil.getFormatType(rssFormat),
065                                            RSSUtil.getFormatVersion(rssFormat)));
066                    }
067    
068                    portletPreferences.reset("rssFormat");
069            }
070    
071            protected void upgradeScopeIds(PortletPreferences portletPreferences)
072                    throws Exception {
073    
074                    String defaultScope = GetterUtil.getString(
075                            portletPreferences.getValue("defaultScope", null));
076    
077                    if (Validator.isNull(defaultScope)) {
078                            return;
079                    }
080    
081                    if (defaultScope.equals("true")) {
082                            portletPreferences.setValues(
083                                    "scopeIds",
084                                    new String[] {
085                                            AssetPublisher.SCOPE_ID_GROUP_PREFIX +
086                                                    GroupConstants.DEFAULT
087                                    });
088                    }
089                    else if (!defaultScope.equals("false")) {
090                            portletPreferences.setValues(
091                                    "scopeIds", new String[] {defaultScope});
092                    }
093    
094                    portletPreferences.reset("defaultScope");
095            }
096    
097    }