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