001
014
015 package com.liferay.portal.upgrade.util;
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
038 public class UpgradeAssetPublisherManualEntries
039 extends BaseUpgradePortletPreferences {
040
041 public static void upgradeToAssetEntryIdElement(Element rootElement) {
042 Element assetIdElement = rootElement.element("asset-id");
043
044 if (Validator.isNotNull(assetIdElement)) {
045 String assetEntryId = assetIdElement.getText();
046
047 Element assetEntryIdElement = rootElement.addElement(
048 "assetEntryId");
049
050 assetEntryIdElement.addText(assetEntryId);
051
052 rootElement.remove(assetIdElement);
053 }
054 }
055
056 public static void upgradeToAssetEntryTypeElement(Element rootElement) {
057 Element assetTypeElement = rootElement.element("asset-type");
058
059 if (Validator.isNotNull(assetTypeElement)) {
060 String assetEntryType = assetTypeElement.getText();
061
062 Element assetEntryTypeElement = rootElement.addElement(
063 "assetEntryType");
064
065 assetEntryTypeElement.addText(assetEntryType);
066
067 rootElement.remove(assetTypeElement);
068 }
069 }
070
071 public static void upgradeToAssetEntryUuidElement(Element rootElement)
072 throws Exception {
073
074 Element assetEntryIdElement = rootElement.element("assetEntryId");
075
076 long assetEntryId = GetterUtil.getLong(assetEntryIdElement.getText());
077
078 Connection con = null;
079 PreparedStatement ps = null;
080 ResultSet rs = null;
081
082 try {
083 con = DataAccess.getUpgradeOptimizedConnection();
084
085 ps = con.prepareStatement(
086 "select classUuid from AssetEntry where entryId = ?");
087
088 ps.setLong(1, assetEntryId);
089
090 rs = ps.executeQuery();
091
092 if (rs.next()) {
093 String classUuid = rs.getString("classUuid");
094
095 Element assetEntryUuidElement = rootElement.addElement(
096 "assetEntryUuid");
097
098 assetEntryUuidElement.addText(classUuid);
099
100 rootElement.remove(assetEntryIdElement);
101 }
102 }
103 finally {
104 DataAccess.cleanUp(con, ps, rs);
105 }
106 }
107
108 protected String[] getAssetEntryXmls(String[] manualEntries)
109 throws Exception {
110
111 String[] assetEntryXmls = new String[manualEntries.length];
112
113 for (int i = 0; i < manualEntries.length; i++) {
114 String manualEntry = manualEntries[i];
115
116 Document document = SAXReaderUtil.read(manualEntry);
117
118 Element rootElement = document.getRootElement();
119
120 upgradeToAssetEntryIdElement(rootElement);
121
122 upgradeToAssetEntryUuidElement(rootElement);
123
124 upgradeToAssetEntryTypeElement(rootElement);
125
126 assetEntryXmls[i] = document.formattedString(StringPool.BLANK);
127 }
128
129 return assetEntryXmls;
130 }
131
132 @Override
133 protected String getUpdatePortletPreferencesWhereClause() {
134 StringBundler sb = new StringBundler(5);
135
136 sb.append("(portletId like '101_INSTANCE_%') and ((preferences like ");
137 sb.append("'%<preference><name>selection-style</name><value>manual");
138 sb.append("</value></preference>%') OR (preferences like ");
139 sb.append("'%<preference><name>selectionStyle</name><value>manual");
140 sb.append("</value></preference>%'))");
141
142 return sb.toString();
143 }
144
145 @Override
146 protected String upgradePreferences(
147 long companyId, long ownerId, int ownerType, long plid,
148 String portletId, String xml)
149 throws Exception {
150
151 PortletPreferences portletPreferences =
152 PortletPreferencesFactoryUtil.fromXML(
153 companyId, ownerId, ownerType, plid, portletId, xml);
154
155 String[] assetEntryXmls = portletPreferences.getValues(
156 "asset-entry-xml", new String[0]);
157
158 if (ArrayUtil.isEmpty(assetEntryXmls)) {
159 assetEntryXmls = portletPreferences.getValues(
160 "assetEntryXml", new String[0]);
161 }
162
163 String[] manualEntries = portletPreferences.getValues(
164 "manual-entries", new String[0]);
165
166 if (ArrayUtil.isEmpty(manualEntries)) {
167 manualEntries = portletPreferences.getValues(
168 "manualEntries", new String[0]);
169 }
170
171 if (ArrayUtil.isEmpty(assetEntryXmls) &&
172 ArrayUtil.isNotEmpty(manualEntries)) {
173
174 assetEntryXmls = getAssetEntryXmls(manualEntries);
175
176 portletPreferences.setValues("asset-entry-xml", assetEntryXmls);
177 }
178
179 return PortletPreferencesFactoryUtil.toXML(portletPreferences);
180 }
181
182 }