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_2;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.model.PortletConstants;
022    import com.liferay.portal.util.PortletKeys;
023    
024    import java.sql.Connection;
025    import java.sql.PreparedStatement;
026    import java.sql.ResultSet;
027    
028    import java.util.regex.Matcher;
029    import java.util.regex.Pattern;
030    
031    /**
032     * @author Wesley Gong
033     * @author Bijan Vakili
034     * @author Douglas Wong
035     * @author Brian Wing Shun Chan
036     */
037    public class UpgradeNestedPortlets extends UpgradeProcess {
038    
039            @Override
040            protected void doUpgrade() throws Exception {
041                    Connection con = null;
042                    PreparedStatement ps = null;
043                    ResultSet rs = null;
044    
045                    try {
046                            con = DataAccess.getUpgradeOptimizedConnection();
047    
048                            ps = con.prepareStatement(_GET_LAYOUT);
049    
050                            rs = ps.executeQuery();
051    
052                            while (rs.next()) {
053                                    long plid = rs.getLong("plid");
054                                    String typeSettings = rs.getString("typeSettings");
055    
056                                    String newTypeSettings = typeSettings;
057    
058                                    Matcher matcher = _pattern.matcher(typeSettings);
059    
060                                    while (matcher.find()) {
061                                            String nestedColumnIds = matcher.group();
062    
063                                            int underlineCount = StringUtil.count(
064                                                    nestedColumnIds, StringPool.UNDERLINE);
065    
066                                            if (underlineCount == _UNDERLINE_COUNT) {
067                                                    String newNestedColumnIds =
068                                                            "_" + matcher.group(1) + "_" + matcher.group(2);
069    
070                                                    newTypeSettings = StringUtil.replace(
071                                                            newTypeSettings, nestedColumnIds,
072                                                            newNestedColumnIds);
073                                            }
074                                    }
075    
076                                    if (!newTypeSettings.equals(typeSettings)) {
077                                            updateLayout(plid, newTypeSettings);
078                                    }
079                            }
080                    }
081                    finally {
082                            DataAccess.cleanUp(con, ps, rs);
083                    }
084            }
085    
086            protected void updateLayout(long plid, String typeSettings)
087                    throws Exception {
088    
089                    Connection con = null;
090                    PreparedStatement ps = null;
091    
092                    try {
093                            con = DataAccess.getUpgradeOptimizedConnection();
094    
095                            ps = con.prepareStatement(
096                                    "update Layout set typeSettings = ? where plid = " + plid);
097    
098                            ps.setString(1, typeSettings);
099    
100                            ps.executeUpdate();
101                    }
102                    finally {
103                            DataAccess.cleanUp(con, ps);
104                    }
105            }
106    
107            private static final String _GET_LAYOUT =
108                    "select plid, typeSettings from Layout where typeSettings like " +
109                            "'%nested-column-ids=" + PortletKeys.NESTED_PORTLETS +
110                                    PortletConstants.INSTANCE_SEPARATOR + "%'";
111    
112            private static final int _UNDERLINE_COUNT = StringUtil.count(
113                    PortletConstants.INSTANCE_SEPARATOR, StringPool.UNDERLINE) + 1;
114    
115            private static final Pattern _pattern = Pattern.compile(
116                    "(" + PortletKeys.NESTED_PORTLETS +
117                            PortletConstants.INSTANCE_SEPARATOR + "[^_,\\s=]+_)([^_,\\s=]+)");
118    
119    }