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_1_0;
016    
017    import com.liferay.expando.kernel.model.ExpandoColumnConstants;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LoggingTimer;
021    import com.liferay.portal.kernel.util.UnicodeProperties;
022    
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    
026    /**
027     * @author Terry Jia
028     * @author Brian Wing Shun Chan
029     */
030    public class UpgradeExpando extends UpgradeProcess {
031    
032            @Override
033            protected void doUpgrade() throws Exception {
034                    updateColumnTypeSettingsIndexable();
035                    updateColumnTypeSettingsSelection();
036            }
037    
038            protected void updateColumnTypeSettings(long columnId, String typeSettings)
039                    throws Exception {
040    
041                    try (PreparedStatement ps = connection.prepareStatement(
042                                    "update ExpandoColumn set typeSettings = ? where columnId = " +
043                                            "?")) {
044    
045                            ps.setString(1, typeSettings);
046                            ps.setLong(2, columnId);
047    
048                            ps.executeUpdate();
049                    }
050            }
051    
052            protected void updateColumnTypeSettingsIndexable() throws Exception {
053                    try (LoggingTimer loggingTimer = new LoggingTimer();
054                            PreparedStatement ps = connection.prepareStatement(
055                                    "select columnId, type_, typeSettings from ExpandoColumn " +
056                                            "where typeSettings like '%indexable%'");
057                            ResultSet rs = ps.executeQuery()) {
058    
059                            while (rs.next()) {
060                                    long columnId = rs.getLong("columnId");
061                                    int type = rs.getInt("type_");
062                                    String typeSettings = rs.getString("typeSettings");
063    
064                                    UnicodeProperties typeSettingsProperties =
065                                            new UnicodeProperties(true);
066    
067                                    typeSettingsProperties.load(typeSettings);
068    
069                                    boolean indexable = GetterUtil.getBoolean(
070                                            typeSettingsProperties.getProperty("indexable"));
071    
072                                    if (indexable) {
073                                            if ((type == ExpandoColumnConstants.STRING) ||
074                                                    (type == ExpandoColumnConstants.STRING_ARRAY)) {
075    
076                                                    typeSettingsProperties.setProperty(
077                                                            ExpandoColumnConstants.INDEX_TYPE,
078                                                            String.valueOf(
079                                                                    ExpandoColumnConstants.INDEX_TYPE_TEXT));
080                                            }
081                                            else {
082                                                    typeSettingsProperties.setProperty(
083                                                            ExpandoColumnConstants.INDEX_TYPE,
084                                                            String.valueOf(
085                                                                    ExpandoColumnConstants.INDEX_TYPE_KEYWORD));
086                                            }
087                                    }
088                                    else {
089                                            typeSettingsProperties.setProperty(
090                                                    ExpandoColumnConstants.INDEX_TYPE,
091                                                    String.valueOf(ExpandoColumnConstants.INDEX_TYPE_NONE));
092                                    }
093    
094                                    typeSettingsProperties.remove("indexable");
095    
096                                    updateColumnTypeSettings(
097                                            columnId, typeSettingsProperties.toString());
098                            }
099                    }
100            }
101    
102            protected void updateColumnTypeSettingsSelection() throws Exception {
103                    try (LoggingTimer loggingTimer = new LoggingTimer();
104                            PreparedStatement ps = connection.prepareStatement(
105                                    "select columnId, typeSettings from ExpandoColumn where " +
106                                            "typeSettings like '%selection%'");
107                            ResultSet rs = ps.executeQuery()) {
108    
109                            while (rs.next()) {
110                                    long columnId = rs.getLong("columnId");
111    
112                                    String typeSettings = rs.getString("typeSettings");
113    
114                                    typeSettings = typeSettings.replace(
115                                            "selection=1", "display-type=selection-list");
116                                    typeSettings = typeSettings.replace(
117                                            "selection=0", "display-type=text-box");
118    
119                                    updateColumnTypeSettings(columnId, typeSettings);
120                            }
121                    }
122            }
123    
124    }