001
014
015 package com.liferay.portal.upgrade.v6_0_0;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
019 import com.liferay.portal.kernel.util.ArrayUtil;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.kernel.xml.Document;
025 import com.liferay.portal.kernel.xml.Element;
026 import com.liferay.portal.kernel.xml.SAXReaderUtil;
027 import com.liferay.portlet.PortletPreferencesFactoryUtil;
028
029 import java.sql.Connection;
030 import java.sql.PreparedStatement;
031 import java.sql.ResultSet;
032
033 import javax.portlet.PortletPreferences;
034
035
039 public class UpgradeAssetPublisher extends BaseUpgradePortletPreferences {
040
041 protected String[] getAssetEntryXmls(String[] manualEntries)
042 throws Exception {
043
044 String[] assetEntryXmls = new String[manualEntries.length];
045
046 for (int i = 0; i < manualEntries.length; i++) {
047 String manualEntry = manualEntries[i];
048
049 Document document = SAXReaderUtil.read(manualEntry);
050
051 Element rootElement = document.getRootElement();
052
053 upgradeToAssetEntryIdElement(rootElement);
054
055 upgradeToAssetEntryTypeElement(rootElement);
056
057 assetEntryXmls[i] = document.formattedString(StringPool.BLANK);
058 }
059
060 return assetEntryXmls;
061 }
062
063 protected String getJournalArticleResourceUuid(String journalArticleUuid)
064 throws Exception {
065
066 Connection con = null;
067 PreparedStatement ps = null;
068 ResultSet rs = null;
069
070 try {
071 con = DataAccess.getUpgradeOptimizedConnection();
072
073 StringBundler sb = new StringBundler(5);
074
075 sb.append("select JournalArticleResource.uuid_ from ");
076 sb.append("JournalArticleResource inner join JournalArticle on ");
077 sb.append("JournalArticle.resourcePrimKey = ");
078 sb.append("JournalArticleResource.resourcePrimKey where ");
079 sb.append("JournalArticle.uuid_ = ?");
080
081 ps = con.prepareStatement(sb.toString());
082
083 ps.setString(1, journalArticleUuid);
084
085 rs = ps.executeQuery();
086
087 if (rs.next()) {
088 return rs.getString("uuid_");
089 }
090
091 return null;
092 }
093 finally {
094 DataAccess.cleanUp(con, ps, rs);
095 }
096 }
097
098 @Override
099 protected String[] getPortletIds() {
100 return new String[] {"101_INSTANCE_%"};
101 }
102
103 @Override
104 protected String getUpdatePortletPreferencesWhereClause() {
105 StringBundler sb = new StringBundler(5);
106
107 sb.append("(portletId like '101_INSTANCE_%') and ((preferences like ");
108 sb.append("'%<preference><name>selection-style</name><value>manual");
109 sb.append("</value></preference>%') OR (preferences like ");
110 sb.append("'%<preference><name>selectionStyle</name><value>manual");
111 sb.append("</value></preference>%'))");
112
113 return sb.toString();
114 }
115
116 @Override
117 protected String upgradePreferences(
118 long companyId, long ownerId, int ownerType, long plid,
119 String portletId, String xml)
120 throws Exception {
121
122 PortletPreferences portletPreferences =
123 PortletPreferencesFactoryUtil.fromXML(
124 companyId, ownerId, ownerType, plid, portletId, xml);
125
126 long layoutId = GetterUtil.getLong(
127 portletPreferences.getValue("lfr-scope-layout-id", null));
128
129 portletPreferences.reset("lfr-scope-layout-id");
130
131 if (layoutId != 0) {
132 portletPreferences.setValues(
133 "scope-ids", new String[] {"Layout_" + layoutId});
134
135 portletPreferences.setValue(
136 "default-scope", Boolean.FALSE.toString());
137 }
138
139 long classNameId = GetterUtil.getLong(
140 portletPreferences.getValue("class-name-id", null));
141
142 portletPreferences.reset("class-name-id");
143
144 if (classNameId != 0) {
145 portletPreferences.setValues(
146 "class-name-ids", new String[] {String.valueOf(classNameId)});
147
148 portletPreferences.setValue(
149 "any-asset-type", Boolean.FALSE.toString());
150 }
151
152 boolean andOperator = GetterUtil.getBoolean(
153 portletPreferences.getValue("and-operator", null));
154
155 portletPreferences.reset("and-operator");
156
157 String[] assetTagNames = portletPreferences.getValues("entries", null);
158 String[] notAssetTagNames = portletPreferences.getValues(
159 "not-entries", null);
160
161 int i = 0;
162
163 if (assetTagNames != null) {
164 portletPreferences.reset("entries");
165
166 portletPreferences.setValue(
167 "queryContains" + i, Boolean.TRUE.toString());
168 portletPreferences.setValue(
169 "queryAndOperator" + i, String.valueOf(andOperator));
170 portletPreferences.setValue("queryName" + i, "assetTags");
171 portletPreferences.setValues("queryValues" + i, assetTagNames);
172
173 i++;
174 }
175
176 if (notAssetTagNames != null) {
177 portletPreferences.reset("not-entries");
178
179 portletPreferences.setValue(
180 "queryContains" + i, Boolean.FALSE.toString());
181 portletPreferences.setValue(
182 "queryAndOperator" + i, String.valueOf(andOperator));
183 portletPreferences.setValue("queryName" + i, "assetTags");
184 portletPreferences.setValues("queryValues" + i, notAssetTagNames);
185
186 i++;
187 }
188
189 String selectionStyle = portletPreferences.getValue(
190 "selection-style", null);
191
192 if (Validator.isNotNull(selectionStyle) &&
193 !selectionStyle.equals("dynamic")) {
194
195 String[] manualEntries = portletPreferences.getValues(
196 "manual-entries", new String[0]);
197
198 String[] assetEntryXmls = getAssetEntryXmls(manualEntries);
199
200 portletPreferences.setValues("asset-entry-xml", assetEntryXmls);
201 }
202
203 String[] assetEntryXmls = portletPreferences.getValues(
204 "asset-entry-xml", new String[0]);
205
206 if (ArrayUtil.isEmpty(assetEntryXmls)) {
207 assetEntryXmls = portletPreferences.getValues(
208 "assetEntryXml", new String[0]);
209 }
210
211 String[] manualEntries = portletPreferences.getValues(
212 "manual-entries", new String[0]);
213
214 if (ArrayUtil.isEmpty(manualEntries)) {
215 manualEntries = portletPreferences.getValues(
216 "manualEntries", new String[0]);
217 }
218
219 if (ArrayUtil.isEmpty(assetEntryXmls) &&
220 ArrayUtil.isNotEmpty(manualEntries)) {
221
222 assetEntryXmls = getAssetEntryXmls(manualEntries);
223
224 portletPreferences.setValues("asset-entry-xml", assetEntryXmls);
225 }
226
227 if (ArrayUtil.isNotEmpty(assetEntryXmls)) {
228 upgradeUuids(assetEntryXmls);
229
230 portletPreferences.setValues("assetEntryXml", assetEntryXmls);
231 }
232
233 return PortletPreferencesFactoryUtil.toXML(portletPreferences);
234 }
235
236 protected void upgradeToAssetEntryIdElement(Element rootElement) {
237 Element assetIdElement = rootElement.element("asset-id");
238
239 if (assetIdElement != null) {
240 String assetEntryId = assetIdElement.getText();
241
242 Element assetEntryIdElement = rootElement.addElement(
243 "assetEntryId");
244
245 assetEntryIdElement.addText(assetEntryId);
246
247 rootElement.remove(assetIdElement);
248 }
249 }
250
251 protected void upgradeToAssetEntryTypeElement(Element rootElement) {
252 Element assetTypeElement = rootElement.element("asset-type");
253
254 if (assetTypeElement != null) {
255 String assetEntryType = assetTypeElement.getText();
256
257 Element assetEntryTypeElement = rootElement.addElement(
258 "assetEntryType");
259
260 assetEntryTypeElement.addText(assetEntryType);
261
262 rootElement.remove(assetTypeElement);
263 }
264 }
265
266 protected void upgradeUuids(String[] assetEntryXmls) throws Exception {
267 for (int i = 0; i < assetEntryXmls.length; i++) {
268 String assetEntry = assetEntryXmls[i];
269
270 Document document = SAXReaderUtil.read(assetEntry);
271
272 Element rootElement = document.getRootElement();
273
274 Element assetTypeElementUuid = rootElement.element(
275 "asset-entry-uuid");
276
277 String journalArticleResourceUuid = getJournalArticleResourceUuid(
278 assetTypeElementUuid.getStringValue());
279
280 if (journalArticleResourceUuid == null) {
281 continue;
282 }
283
284 rootElement.remove(assetTypeElementUuid);
285
286 assetTypeElementUuid.setText(journalArticleResourceUuid);
287
288 rootElement.add(assetTypeElementUuid);
289
290 document.setRootElement(rootElement);
291
292 assetEntryXmls[i] = document.formattedString(StringPool.BLANK);
293 }
294 }
295
296 }