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.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.upgrade.v6_1_0.util.GroupTable;
021    
022    import java.sql.Connection;
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    import java.sql.SQLException;
026    
027    /**
028     * @author Hugo Huijser
029     * @author Jorge Ferrer
030     */
031    public class UpgradeGroup extends UpgradeProcess {
032    
033            @Override
034            protected void doUpgrade() throws Exception {
035                    try {
036                            runSQL("alter_column_type Group_ name VARCHAR(150) null");
037                    }
038                    catch (SQLException sqle) {
039                            upgradeTable(
040                                    GroupTable.TABLE_NAME, GroupTable.TABLE_COLUMNS,
041                                    GroupTable.TABLE_SQL_CREATE, GroupTable.TABLE_SQL_ADD_INDEXES);
042                    }
043    
044                    updateName();
045                    updateSite();
046            }
047    
048            protected long getClassNameId(String className) throws Exception {
049                    Connection con = null;
050                    PreparedStatement ps = null;
051                    ResultSet rs = null;
052    
053                    try {
054                            con = DataAccess.getUpgradeOptimizedConnection();
055    
056                            ps = con.prepareStatement(
057                                    "select classNameId from ClassName_ where value = ?");
058    
059                            ps.setString(1, className);
060    
061                            rs = ps.executeQuery();
062    
063                            if (rs.next()) {
064                                    return rs.getLong("classNameId");
065                            }
066    
067                            return 0;
068                    }
069                    finally {
070                            DataAccess.cleanUp(con, ps, rs);
071                    }
072            }
073    
074            protected void updateName() throws Exception {
075                    long organizationClassNameId = getClassNameId(
076                            "com.liferay.portal.model.Organization");
077    
078                    Connection con = null;
079                    PreparedStatement ps = null;
080                    ResultSet rs = null;
081    
082                    try {
083                            con = DataAccess.getUpgradeOptimizedConnection();
084    
085                            StringBundler sb = new StringBundler(4);
086    
087                            sb.append("select Group_.groupId, Group_.classPK, ");
088                            sb.append("Organization_.name from Group_ inner join ");
089                            sb.append("Organization_ on Organization_.organizationId = ");
090                            sb.append("Group_.classPK where classNameId = ?");
091    
092                            String sql = sb.toString();
093    
094                            ps = con.prepareStatement(sql);
095    
096                            ps.setLong(1, organizationClassNameId);
097    
098                            rs = ps.executeQuery();
099    
100                            while (rs.next()) {
101                                    long groupId = rs.getLong("groupId");
102                                    long classPK = rs.getLong("classPK");
103                                    String name = rs.getString("name");
104    
105                                    updateName(groupId, classPK, name);
106                            }
107                    }
108                    finally {
109                            DataAccess.cleanUp(con, ps, rs);
110                    }
111            }
112    
113            protected void updateName(long groupId, long classPK, String name)
114                    throws Exception {
115    
116                    Connection con = null;
117                    PreparedStatement ps = null;
118    
119                    try {
120                            con = DataAccess.getUpgradeOptimizedConnection();
121    
122                            ps = con.prepareStatement(
123                                    "update Group_ set name = ? where groupId = ?");
124    
125                            StringBundler sb = new StringBundler(3);
126    
127                            sb.append(classPK);
128                            sb.append(_ORGANIZATION_NAME_DELIMETER);
129                            sb.append(name);
130    
131                            ps.setString(1, sb.toString());
132                            ps.setLong(2, groupId);
133    
134                            ps.executeUpdate();
135                    }
136                    finally {
137                            DataAccess.cleanUp(con, ps);
138                    }
139            }
140    
141            protected void updateSite() throws Exception {
142                    long groupClassNameId = getClassNameId(
143                            "com.liferay.portal.model.Group");
144    
145                    runSQL(
146                            "update Group_ set site = TRUE where classNameId = " +
147                                    groupClassNameId);
148    
149                    long organizationClassNameId = getClassNameId(
150                            "com.liferay.portal.model.Organization");
151    
152                    Connection con = null;
153                    PreparedStatement ps = null;
154                    ResultSet rs = null;
155    
156                    try {
157                            con = DataAccess.getUpgradeOptimizedConnection();
158    
159                            String sql =
160                                    "select distinct Group_.groupId from Group_ inner join " +
161                                            "Layout on Layout.groupId = Group_.groupId where " +
162                                                    "classNameId = ?";
163    
164                            ps = con.prepareStatement(sql);
165    
166                            ps.setLong(1, organizationClassNameId);
167    
168                            rs = ps.executeQuery();
169    
170                            while (rs.next()) {
171                                    long groupId = rs.getLong("groupId");
172    
173                                    runSQL(
174                                            "update Group_ set site = TRUE where groupId = " + groupId);
175                            }
176                    }
177                    finally {
178                            DataAccess.cleanUp(con, ps, rs);
179                    }
180            }
181    
182            private static final String _ORGANIZATION_NAME_DELIMETER =
183                    " LFR_ORGANIZATION ";
184    
185    }