001    /**
002     * Copyright (c) 2000-present 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.GetterUtil;
019    import com.liferay.portal.verify.VerifyUUID;
020    import com.liferay.portal.verify.model.VerifiableUUIDModel;
021    import com.liferay.portlet.PortletPreferencesFactoryUtil;
022    
023    import javax.portlet.PortletPreferences;
024    
025    /**
026     * @author Julio Camarero
027     */
028    public class UpgradeScopes extends BaseUpgradePortletPreferences {
029    
030            @Override
031            protected void doUpgrade() throws Exception {
032                    super.doUpgrade();
033    
034                    VerifyUUID.verify(
035                            new VerifiableUUIDModel() {
036    
037                                    @Override
038                                    public String getPrimaryKeyColumnName() {
039                                            return "plid";
040                                    }
041    
042                                    @Override
043                                    public String getTableName() {
044                                            return "Layout";
045                                    }
046    
047                            });
048            }
049    
050            @Override
051            protected String getUpdatePortletPreferencesWhereClause() {
052                    return "preferences like '%lfr-scope-layout-id%'";
053            }
054    
055            @Override
056            protected String upgradePreferences(
057                            long companyId, long ownerId, int ownerType, long plid,
058                            String portletId, String xml)
059                    throws Exception {
060    
061                    PortletPreferences portletPreferences =
062                            PortletPreferencesFactoryUtil.fromXML(
063                                    companyId, ownerId, ownerType, plid, portletId, xml);
064    
065                    long linkToLayoutId = GetterUtil.getLong(
066                            portletPreferences.getValue("lfr-scope-layout-id", null));
067    
068                    if (linkToLayoutId > 0) {
069                            String uuid = getLayoutUuid(plid, linkToLayoutId);
070    
071                            if (uuid != null) {
072                                    portletPreferences.setValue("lfr-scope-layout-uuid", uuid);
073                            }
074    
075                            portletPreferences.reset("lfr-scope-layout-id");
076                    }
077    
078                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
079            }
080    
081    }