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.kernel.upgrade;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.model.LayoutConstants;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LoggingTimer;
022    import com.liferay.portal.kernel.util.PortletKeys;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    import java.util.ArrayList;
030    import java.util.Date;
031    import java.util.List;
032    
033    /**
034     * @author Mate Thurzo
035     */
036    public abstract class BaseUpgradeLastPublishDate extends UpgradeProcess {
037    
038            protected void addLastPublishDateColumn(String tableName) throws Exception {
039                    try (LoggingTimer loggingTimer = new LoggingTimer(tableName)) {
040                            if (hasColumn(tableName, "lastPublishDate")) {
041                                    if (_log.isWarnEnabled()) {
042                                            _log.warn(
043                                                    "Table " + tableName +
044                                                            " already has the column lastPublishDate");
045                                    }
046    
047                                    return;
048                            }
049    
050                            runSQL(
051                                    "alter table " + tableName + " add lastPublishDate DATE null");
052                    }
053            }
054    
055            protected Date getLayoutSetLastPublishDate(long groupId) throws Exception {
056                    try (PreparedStatement ps = connection.prepareStatement(
057                                    "select settings_ from LayoutSet where groupId = ?")) {
058    
059                            ps.setLong(1, groupId);
060    
061                            try (ResultSet rs = ps.executeQuery()) {
062                                    while (rs.next()) {
063                                            UnicodeProperties settingsProperties =
064                                                    new UnicodeProperties(true);
065    
066                                            settingsProperties.load(rs.getString("settings_"));
067    
068                                            String lastPublishDateString =
069                                                    settingsProperties.getProperty("last-publish-date");
070    
071                                            if (Validator.isNotNull(lastPublishDateString)) {
072                                                    return new Date(
073                                                            GetterUtil.getLong(lastPublishDateString));
074                                            }
075                                    }
076    
077                                    return null;
078                            }
079                    }
080            }
081    
082            protected Date getPortletLastPublishDate(long groupId, String portletId)
083                    throws Exception {
084    
085                    try (PreparedStatement ps = connection.prepareStatement(
086                                    "select preferences from PortletPreferences where plid = ? " +
087                                            "and ownerType = ? and ownerId = ? and portletId = ?")) {
088    
089                            ps.setLong(1, LayoutConstants.DEFAULT_PLID);
090                            ps.setInt(2, PortletKeys.PREFS_OWNER_TYPE_GROUP);
091                            ps.setLong(3, groupId);
092                            ps.setString(4, portletId);
093    
094                            try (ResultSet rs = ps.executeQuery()) {
095                                    while (rs.next()) {
096                                            String preferences = rs.getString("preferences");
097    
098                                            if (Validator.isNotNull(preferences)) {
099                                                    int x = preferences.lastIndexOf(
100                                                            "last-publish-date</name><value>");
101    
102                                                    if (x < 0) {
103                                                            break;
104                                                    }
105    
106                                                    int y = preferences.indexOf("</value>", x);
107    
108                                                    String lastPublishDateString = preferences.substring(
109                                                            x, y);
110    
111                                                    if (Validator.isNotNull(lastPublishDateString)) {
112                                                            return new Date(
113                                                                    GetterUtil.getLong(lastPublishDateString));
114                                                    }
115                                            }
116                                    }
117    
118                                    return null;
119                            }
120                    }
121            }
122    
123            protected List<Long> getStagedGroupIds() throws Exception {
124                    try (PreparedStatement ps = connection.prepareStatement(
125                                    "select groupId from Group_ where typeSettings like " +
126                                            "'%staged=true%'");
127                            ResultSet rs = ps.executeQuery()) {
128    
129                            List<Long> stagedGroupIds = new ArrayList<>();
130    
131                            while (rs.next()) {
132                                    long stagedGroupId = rs.getLong("groupId");
133    
134                                    stagedGroupIds.add(stagedGroupId);
135                            }
136    
137                            return stagedGroupIds;
138                    }
139            }
140    
141            protected void updateLastPublishDates(String portletId, String tableName)
142                    throws Exception {
143    
144                    try (LoggingTimer loggingTimer = new LoggingTimer(tableName)) {
145                            List<Long> stagedGroupIds = getStagedGroupIds();
146    
147                            for (long stagedGroupId : stagedGroupIds) {
148                                    Date lastPublishDate = getPortletLastPublishDate(
149                                            stagedGroupId, portletId);
150    
151                                    if (lastPublishDate == null) {
152                                            lastPublishDate = getLayoutSetLastPublishDate(
153                                                    stagedGroupId);
154                                    }
155    
156                                    if (lastPublishDate == null) {
157                                            continue;
158                                    }
159    
160                                    updateStagedModelLastPublishDates(
161                                            stagedGroupId, tableName, lastPublishDate);
162                            }
163                    }
164            }
165    
166            protected void updateStagedModelLastPublishDates(
167                            long groupId, String tableName, Date lastPublishDate)
168                    throws Exception {
169    
170                    try (PreparedStatement ps = connection.prepareStatement(
171                                    "update " + tableName + " set lastPublishDate = ? where " +
172                                            "groupId = ?")) {
173    
174                            ps.setDate(1, new java.sql.Date(lastPublishDate.getTime()));
175                            ps.setLong(2, groupId);
176    
177                            ps.executeUpdate();
178                    }
179            }
180    
181            private static final Log _log = LogFactoryUtil.getLog(
182                    BaseUpgradeLastPublishDate.class);
183    
184    }