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