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.verify;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.model.LayoutFriendlyURL;
021    import com.liferay.portal.service.LayoutFriendlyURLLocalServiceUtil;
022    import com.liferay.portal.service.LayoutLocalServiceUtil;
023    
024    import java.util.List;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Kenneth Chang
029     */
030    public class VerifyLayout extends VerifyProcess {
031    
032            protected void deleteOrphanedLayouts() throws Exception {
033                    runSQL(
034                            "delete from Layout where layoutPrototypeUuid != '' and " +
035                                    "layoutPrototypeUuid not in (select uuid_ from " +
036                                            "LayoutPrototype)");
037            }
038    
039            @Override
040            protected void doVerify() throws Exception {
041                    deleteOrphanedLayouts();
042                    verifyFriendlyURL();
043                    verifyLayoutPrototypeLinkEnabled();
044                    verifyUuid();
045            }
046    
047            protected void verifyFriendlyURL() throws Exception {
048                    List<Layout> layouts =
049                            LayoutLocalServiceUtil.getNullFriendlyURLLayouts();
050    
051                    for (Layout layout : layouts) {
052                            List<LayoutFriendlyURL> layoutFriendlyURLs =
053                                    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(
054                                            layout.getPlid());
055    
056                            for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
057                                    String friendlyURL = StringPool.SLASH + layout.getLayoutId();
058    
059                                    LayoutLocalServiceUtil.updateFriendlyURL(
060                                            layout.getUserId(), layout.getPlid(), friendlyURL,
061                                            layoutFriendlyURL.getLanguageId());
062                            }
063                    }
064            }
065    
066            protected void verifyLayoutPrototypeLinkEnabled() throws Exception {
067                    runSQL(
068                            "update Layout set layoutPrototypeLinkEnabled = [$FALSE$] where " +
069                                    "type_ = 'link_to_layout' and layoutPrototypeLinkEnabled = " +
070                                    "[$TRUE$]");
071            }
072    
073            protected void verifyUuid() throws Exception {
074                    verifyUuid("AssetEntry");
075                    verifyUuid("JournalArticle");
076    
077                    runSQL(
078                            "update Layout set uuid_ = sourcePrototypeLayoutUuid where " +
079                                    "sourcePrototypeLayoutUuid != '' and uuid_ != " +
080                                            "sourcePrototypeLayoutUuid");
081            }
082    
083            protected void verifyUuid(String tableName) throws Exception {
084                    StringBundler sb = new StringBundler(12);
085    
086                    sb.append("update ");
087                    sb.append(tableName);
088                    sb.append(" set layoutUuid = (select distinct ");
089                    sb.append("sourcePrototypeLayoutUuid from Layout where ");
090                    sb.append("Layout.uuid_ = ");
091                    sb.append(tableName);
092                    sb.append(".layoutUuid) where exists (select 1 from Layout where ");
093                    sb.append("Layout.uuid_ = ");
094                    sb.append(tableName);
095                    sb.append(".layoutUuid and Layout.uuid_ != ");
096                    sb.append("Layout.sourcePrototypeLayoutUuid and ");
097                    sb.append("Layout.sourcePrototypeLayoutUuid != '')");
098    
099                    runSQL(sb.toString());
100            }
101    
102    }