001
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
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.setLong(3, groupId);
090 ps.setString(4, portletId);
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
101 if (x < 0) {
102 break;
103 }
104
105 int y = preferences.indexOf("</value>", x);
106
107 String lastPublishDateString = preferences.substring(x, y);
108
109 if (Validator.isNotNull(lastPublishDateString)) {
110 return new Date(
111 GetterUtil.getLong(lastPublishDateString));
112 }
113 }
114 }
115
116 return null;
117 }
118 finally {
119 DataAccess.cleanUp(con, ps, rs);
120 }
121 }
122
123 protected List<Long> getStagedGroupIds() throws Exception {
124 Connection con = null;
125 PreparedStatement ps = null;
126 ResultSet rs = null;
127
128 try {
129 con = DataAccess.getUpgradeOptimizedConnection();
130
131 ps = con.prepareStatement(
132 "select groupId from Group_ where typeSettings like " +
133 "'%staged=true%'");
134
135 rs = ps.executeQuery();
136
137 List<Long> stagedGroupIds = new ArrayList<>();
138
139 while (rs.next()) {
140 long stagedGroupId = rs.getLong("groupId");
141
142 stagedGroupIds.add(stagedGroupId);
143 }
144
145 return stagedGroupIds;
146 }
147 finally {
148 DataAccess.cleanUp(con, ps, rs);
149 }
150 }
151
152 protected void updateLastPublishDates(String portletId, String tableName)
153 throws Exception {
154
155 List<Long> stagedGroupIds = getStagedGroupIds();
156
157 for (long stagedGroupId : stagedGroupIds) {
158 Date lastPublishDate = getPortletLastPublishDate(
159 stagedGroupId, portletId);
160
161 if (lastPublishDate == null) {
162 lastPublishDate = getLayoutSetLastPublishDate(stagedGroupId);
163 }
164
165 if (lastPublishDate == null) {
166 continue;
167 }
168
169 updateStagedModelLastPublishDates(
170 stagedGroupId, tableName, lastPublishDate);
171 }
172 }
173
174 protected void updateStagedModelLastPublishDates(
175 long groupId, String tableName, Date lastPublishDate)
176 throws Exception {
177
178 Connection con = null;
179 PreparedStatement ps = null;
180
181 try {
182 con = DataAccess.getUpgradeOptimizedConnection();
183
184 ps = con.prepareStatement(
185 "update " + tableName + " set lastPublishDate = ? where " +
186 "groupId = ?");
187
188 ps.setDate(1, new java.sql.Date(lastPublishDate.getTime()));
189 ps.setLong(2, groupId);
190
191 ps.executeUpdate();
192 }
193 finally {
194 DataAccess.cleanUp(con, ps);
195 }
196 }
197
198 }