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