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.upgrade.v5_2_6;
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 = nestedColumnIds.replaceAll(
068                                                            _pattern.pattern(), "_$1_$2");
069    
070                                                    newTypeSettings = newTypeSettings.replaceAll(
071                                                            nestedColumnIds, newNestedColumnIds);
072                                            }
073                                    }
074    
075                                    if (!newTypeSettings.equals(typeSettings)) {
076                                            updateTypeSettings(plid, newTypeSettings);
077                                    }
078                            }
079                    }
080                    finally {
081                            DataAccess.cleanUp(con, ps, rs);
082                    }
083            }
084    
085            protected void updateTypeSettings(long plid, String typeSettings)
086                    throws Exception {
087    
088                    Connection con = null;
089                    PreparedStatement ps = null;
090    
091                    try {
092                            con = DataAccess.getUpgradeOptimizedConnection();
093    
094                            ps = con.prepareStatement(
095                                    "update Layout set typeSettings = ? where plid = " + plid);
096    
097                            ps.setString(1, typeSettings);
098    
099                            ps.executeUpdate();
100                    }
101                    finally {
102                            DataAccess.cleanUp(con, ps);
103                    }
104            }
105    
106            private static final String _GET_LAYOUT =
107                    "select plid, typeSettings from Layout where typeSettings like " +
108                            "'%nested-column-ids=" + PortletKeys.NESTED_PORTLETS +
109                                    PortletConstants.INSTANCE_SEPARATOR + "%'";
110    
111            private static final int _UNDERLINE_COUNT = StringUtil.count(
112                    PortletConstants.INSTANCE_SEPARATOR, StringPool.UNDERLINE) + 1;
113    
114            private static Pattern _pattern = Pattern.compile(
115                    "(" + PortletKeys.NESTED_PORTLETS +
116                            PortletConstants.INSTANCE_SEPARATOR + "[^_,\\s=]+_)([^_,\\s=]+)");
117    
118    }