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