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