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