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.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            @Override
033            protected void doVerify() throws Exception {
034                    verifyFriendlyURL();
035                    verifyUuid();
036            }
037    
038            protected void verifyFriendlyURL() throws Exception {
039                    List<Layout> layouts =
040                            LayoutLocalServiceUtil.getNullFriendlyURLLayouts();
041    
042                    for (Layout layout : layouts) {
043                            List<LayoutFriendlyURL> layoutFriendlyURLs =
044                                    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(
045                                            layout.getPlid());
046    
047                            for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
048                                    String friendlyURL = StringPool.SLASH + layout.getLayoutId();
049    
050                                    LayoutLocalServiceUtil.updateFriendlyURL(
051                                            layout.getPlid(), friendlyURL,
052                                            layoutFriendlyURL.getLanguageId());
053                            }
054                    }
055            }
056    
057            protected void verifyUuid() throws Exception {
058                    verifyUuid("AssetEntry");
059                    verifyUuid("JournalArticle");
060    
061                    StringBundler sb = new StringBundler(3);
062    
063                    sb.append("update Layout set uuid_ = sourcePrototypeLayoutUuid where ");
064                    sb.append("sourcePrototypeLayoutUuid != '' and ");
065                    sb.append("uuid_ != sourcePrototypeLayoutUuid");
066    
067                    runSQL(sb.toString());
068            }
069    
070            protected void verifyUuid(String tableName) throws Exception {
071                    StringBundler sb = new StringBundler(12);
072    
073                    sb.append("update ");
074                    sb.append(tableName);
075                    sb.append(" set layoutUuid = (select distinct ");
076                    sb.append("sourcePrototypeLayoutUuid from Layout where ");
077                    sb.append("Layout.uuid_ = ");
078                    sb.append(tableName);
079                    sb.append(".layoutUuid) where exists (select 1 from Layout where ");
080                    sb.append("Layout.uuid_ = ");
081                    sb.append(tableName);
082                    sb.append(".layoutUuid and Layout.uuid_ != ");
083                    sb.append("Layout.sourcePrototypeLayoutUuid and ");
084                    sb.append("Layout.sourcePrototypeLayoutUuid != '')");
085    
086                    runSQL(sb.toString());
087            }
088    
089    }