001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(11);
072    
073                    sb.append("update ");
074                    sb.append(tableName);
075                    sb.append(" set layoutUuid = (select sourcePrototypeLayoutUuid from ");
076                    sb.append("Layout where Layout.uuid_ = ");
077                    sb.append(tableName);
078                    sb.append(".layoutUuid) where exists (select 1 from Layout where ");
079                    sb.append("Layout.uuid_ = ");
080                    sb.append(tableName);
081                    sb.append(".layoutUuid and Layout.uuid_ != ");
082                    sb.append("Layout.sourcePrototypeLayoutUuid and ");
083                    sb.append("Layout.sourcePrototypeLayoutUuid != '')");
084    
085                    runSQL(sb.toString());
086            }
087    
088    }