001    /**
002     * Copyright (c) 2000-2010 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            protected void doUpgrade() throws Exception {
040                    Connection con = null;
041                    PreparedStatement ps = null;
042                    ResultSet rs = null;
043    
044                    try {
045                            con = DataAccess.getConnection();
046    
047                            ps = con.prepareStatement(_GET_LAYOUT);
048    
049                            rs = ps.executeQuery();
050    
051                            while (rs.next()) {
052                                    long plid = rs.getLong("plid");
053                                    String typeSettings = rs.getString("typeSettings");
054    
055                                    String newTypeSettings = typeSettings;
056    
057                                    Matcher matcher = _pattern.matcher(typeSettings);
058    
059                                    while (matcher.find()) {
060                                            String nestedColumnIds = matcher.group();
061    
062                                            int underlineCount = StringUtil.count(
063                                                    nestedColumnIds, StringPool.UNDERLINE);
064    
065                                            if (underlineCount == _UNDERLINE_COUNT) {
066                                                    String newNestedColumnIds = nestedColumnIds.replaceAll(
067                                                            _pattern.pattern(), "_$1_$2");
068    
069                                                    newTypeSettings = newTypeSettings.replaceAll(
070                                                            nestedColumnIds, newNestedColumnIds);
071                                            }
072                                    }
073    
074                                    if (!newTypeSettings.equals(typeSettings)) {
075                                            updateTypeSettings(plid, newTypeSettings);
076                                    }
077                            }
078                    }
079                    finally {
080                            DataAccess.cleanUp(con, ps, rs);
081                    }
082            }
083    
084            protected void updateTypeSettings(long plid, String typeSettings)
085                    throws Exception {
086    
087                    Connection con = null;
088                    PreparedStatement ps = null;
089    
090                    try {
091                            con = DataAccess.getConnection();
092    
093                            ps = con.prepareStatement(
094                                    "update Layout set typeSettings = ? where plid = " + plid);
095    
096                            ps.setString(1, typeSettings);
097    
098                            ps.executeUpdate();
099                    }
100                    finally {
101                            DataAccess.cleanUp(con, ps);
102                    }
103            }
104    
105            private static final String _GET_LAYOUT =
106                    "select plid, typeSettings from Layout where typeSettings like " +
107                            "'%nested-column-ids=" + PortletKeys.NESTED_PORTLETS +
108                                    PortletConstants.INSTANCE_SEPARATOR + "%'";
109    
110            private static final int _UNDERLINE_COUNT = StringUtil.count(
111                    PortletConstants.INSTANCE_SEPARATOR, StringPool.UNDERLINE) + 1;
112    
113            private static Pattern _pattern = Pattern.compile(
114                    "(" + PortletKeys.NESTED_PORTLETS +
115                            PortletConstants.INSTANCE_SEPARATOR + "[^_,\\s=]+_)([^_,\\s=]+)");
116    
117    }