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