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.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutFriendlyURL;
025    import com.liferay.portal.model.LayoutPrototype;
026    import com.liferay.portal.service.LayoutFriendlyURLLocalServiceUtil;
027    import com.liferay.portal.service.LayoutLocalServiceUtil;
028    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
029    
030    import java.sql.Connection;
031    import java.sql.PreparedStatement;
032    import java.sql.ResultSet;
033    
034    import java.util.List;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Kenneth Chang
039     */
040    public class VerifyLayout extends VerifyProcess {
041    
042            protected void deleteOrphanedLayouts() throws Exception {
043                    Connection con = null;
044                    PreparedStatement ps = null;
045                    ResultSet rs = null;
046    
047                    try {
048                            con = DataAccess.getUpgradeOptimizedConnection();
049    
050                            ps = con.prepareStatement(
051                                    "select plid from Layout where layoutPrototypeUuid != ''");
052    
053                            rs = ps.executeQuery();
054    
055                            while (rs.next()) {
056                                    long plid = rs.getLong("plid");
057    
058                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
059    
060                                    LayoutPrototype layoutPrototype =
061                                            LayoutPrototypeLocalServiceUtil.
062                                                    fetchLayoutPrototypeByUuidAndCompanyId(
063                                                            layout.getLayoutPrototypeUuid(),
064                                                            layout.getCompanyId());
065    
066                                    if (layoutPrototype == null) {
067                                            String name = layout.getName(LocaleUtil.getDefault());
068    
069                                            if (layout.isLayoutPrototypeLinkEnabled()) {
070                                                    if (_log.isInfoEnabled()) {
071                                                            _log.info(
072                                                                    "Deleting layout \"" + name + "\" because it " +
073                                                                            "is propagated from a deleted layout " +
074                                                                                    "template");
075                                                    }
076    
077                                                    LayoutLocalServiceUtil.deleteLayout(layout);
078                                            }
079                                            else {
080                                                    if (_log.isInfoEnabled()) {
081                                                            _log.info(
082                                                                    "Removing reference to deleted layout " +
083                                                                            "template from layout \"" + name + "\"");
084                                                    }
085    
086                                                    removeLayoutPrototypeUuid(plid);
087                                            }
088                                    }
089                            }
090                    }
091                    finally {
092                            DataAccess.cleanUp(con, ps, rs);
093                    }
094            }
095    
096            @Override
097            protected void doVerify() throws Exception {
098                    deleteOrphanedLayouts();
099                    verifyFriendlyURL();
100                    verifyUuid();
101            }
102    
103            protected void removeLayoutPrototypeUuid(long plid) throws Exception {
104                    Connection con = null;
105                    PreparedStatement ps = null;
106    
107                    try {
108                            con = DataAccess.getUpgradeOptimizedConnection();
109    
110                            ps = con.prepareStatement(
111                                    "update layout set layoutPrototypeUuid = '' where plid = " +
112                                            plid);
113    
114                            ps.executeUpdate();
115                    }
116                    finally {
117                            DataAccess.cleanUp(con, ps);
118                    }
119            }
120    
121            protected void verifyFriendlyURL() throws Exception {
122                    List<Layout> layouts =
123                            LayoutLocalServiceUtil.getNullFriendlyURLLayouts();
124    
125                    for (Layout layout : layouts) {
126                            List<LayoutFriendlyURL> layoutFriendlyURLs =
127                                    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(
128                                            layout.getPlid());
129    
130                            for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
131                                    String friendlyURL = StringPool.SLASH + layout.getLayoutId();
132    
133                                    LayoutLocalServiceUtil.updateFriendlyURL(
134                                            layout.getPlid(), friendlyURL,
135                                            layoutFriendlyURL.getLanguageId());
136                            }
137                    }
138            }
139    
140            protected void verifyUuid() throws Exception {
141                    verifyUuid("AssetEntry");
142                    verifyUuid("JournalArticle");
143    
144                    StringBundler sb = new StringBundler(3);
145    
146                    sb.append("update Layout set uuid_ = sourcePrototypeLayoutUuid where ");
147                    sb.append("sourcePrototypeLayoutUuid != '' and ");
148                    sb.append("uuid_ != sourcePrototypeLayoutUuid");
149    
150                    runSQL(sb.toString());
151            }
152    
153            protected void verifyUuid(String tableName) throws Exception {
154                    StringBundler sb = new StringBundler(12);
155    
156                    sb.append("update ");
157                    sb.append(tableName);
158                    sb.append(" set layoutUuid = (select distinct ");
159                    sb.append("sourcePrototypeLayoutUuid from Layout where ");
160                    sb.append("Layout.uuid_ = ");
161                    sb.append(tableName);
162                    sb.append(".layoutUuid) where exists (select 1 from Layout where ");
163                    sb.append("Layout.uuid_ = ");
164                    sb.append(tableName);
165                    sb.append(".layoutUuid and Layout.uuid_ != ");
166                    sb.append("Layout.sourcePrototypeLayoutUuid and ");
167                    sb.append("Layout.sourcePrototypeLayoutUuid != '')");
168    
169                    runSQL(sb.toString());
170            }
171    
172            private static Log _log = LogFactoryUtil.getLog(VerifyLayout.class);
173    
174    }