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.upgrade.v6_0_0;
016    
017    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portlet.PortletPreferencesFactoryUtil;
027    
028    import javax.portlet.PortletPreferences;
029    
030    /**
031     * @author Julio Camarero
032     * @author Douglas Wong
033     */
034    public class UpgradeAssetPublisher extends BaseUpgradePortletPreferences {
035    
036            protected String[] getAssetEntryXmls(String[] manualEntries)
037                    throws Exception {
038    
039                    String[] assetEntryXmls = new String[manualEntries.length];
040    
041                    for (int i = 0; i < manualEntries.length; i++) {
042                            String manualEntry = manualEntries[i];
043    
044                            Document document = SAXReaderUtil.read(manualEntry);
045    
046                            Element rootElement = document.getRootElement();
047    
048                            upgradeToAssetEntryIdElement(rootElement);
049    
050                            upgradeToAssetEntryTypeElement(rootElement);
051    
052                            assetEntryXmls[i] = document.formattedString(StringPool.BLANK);
053                    }
054    
055                    return assetEntryXmls;
056            }
057    
058            @Override
059            protected String[] getPortletIds() {
060                    return new String[] {"101_INSTANCE_%"};
061            }
062    
063            @Override
064            protected String getUpdatePortletPreferencesWhereClause() {
065                    StringBundler sb = new StringBundler(5);
066    
067                    sb.append("(portletId like '101_INSTANCE_%') and ((preferences like ");
068                    sb.append("'%<preference><name>selection-style</name><value>manual");
069                    sb.append("</value></preference>%') OR (preferences like ");
070                    sb.append("'%<preference><name>selectionStyle</name><value>manual");
071                    sb.append("</value></preference>%'))");
072    
073                    return sb.toString();
074            }
075    
076            @Override
077            protected String upgradePreferences(
078                            long companyId, long ownerId, int ownerType, long plid,
079                            String portletId, String xml)
080                    throws Exception {
081    
082                    PortletPreferences portletPreferences =
083                            PortletPreferencesFactoryUtil.fromXML(
084                                    companyId, ownerId, ownerType, plid, portletId, xml);
085    
086                    long layoutId = GetterUtil.getLong(
087                            portletPreferences.getValue("lfr-scope-layout-id", null));
088    
089                    portletPreferences.reset("lfr-scope-layout-id");
090    
091                    if (layoutId != 0) {
092                            portletPreferences.setValues(
093                                    "scope-ids", new String[] {"Layout_" + layoutId});
094    
095                            portletPreferences.setValue(
096                                    "default-scope", Boolean.FALSE.toString());
097                    }
098    
099                    long classNameId = GetterUtil.getLong(
100                            portletPreferences.getValue("class-name-id", null));
101    
102                    portletPreferences.reset("class-name-id");
103    
104                    if (classNameId != 0) {
105                            portletPreferences.setValues(
106                                    "class-name-ids", new String[] {String.valueOf(classNameId)});
107    
108                            portletPreferences.setValue(
109                                    "any-asset-type", Boolean.FALSE.toString());
110                    }
111    
112                    boolean andOperator = GetterUtil.getBoolean(
113                            portletPreferences.getValue("and-operator", null));
114    
115                    portletPreferences.reset("and-operator");
116    
117                    String[] assetTagNames = portletPreferences.getValues("entries", null);
118                    String[] notAssetTagNames = portletPreferences.getValues(
119                            "not-entries", null);
120    
121                    int i = 0;
122    
123                    if (assetTagNames != null) {
124                            portletPreferences.reset("entries");
125    
126                            portletPreferences.setValue(
127                                    "queryContains" + i, Boolean.TRUE.toString());
128                            portletPreferences.setValue(
129                                    "queryAndOperator" + i, String.valueOf(andOperator));
130                            portletPreferences.setValue("queryName" + i, "assetTags");
131                            portletPreferences.setValues("queryValues" + i, assetTagNames);
132    
133                            i++;
134                    }
135    
136                    if (notAssetTagNames != null) {
137                            portletPreferences.reset("not-entries");
138    
139                            portletPreferences.setValue(
140                                    "queryContains" + i, Boolean.FALSE.toString());
141                            portletPreferences.setValue(
142                                    "queryAndOperator" + i, String.valueOf(andOperator));
143                            portletPreferences.setValue("queryName" + i, "assetTags");
144                            portletPreferences.setValues("queryValues" + i, notAssetTagNames);
145    
146                            i++;
147                    }
148    
149                    String selectionStyle = portletPreferences.getValue(
150                            "selection-style", null);
151    
152                    if (Validator.isNotNull(selectionStyle) &&
153                            !selectionStyle.equals("dynamic")) {
154    
155                            String[] manualEntries = portletPreferences.getValues(
156                                    "manual-entries", new String[0]);
157    
158                            String[] assetEntryXmls = getAssetEntryXmls(manualEntries);
159    
160                            portletPreferences.setValues("asset-entry-xml", assetEntryXmls);
161                    }
162    
163                    String[] assetEntryXmls = portletPreferences.getValues(
164                            "asset-entry-xml", new String[0]);
165    
166                    if (ArrayUtil.isEmpty(assetEntryXmls)) {
167                            assetEntryXmls = portletPreferences.getValues(
168                                    "assetEntryXml", new String[0]);
169                    }
170    
171                    String[] manualEntries = portletPreferences.getValues(
172                            "manual-entries", new String[0]);
173    
174                    if (ArrayUtil.isEmpty(manualEntries)) {
175                            manualEntries = portletPreferences.getValues(
176                                    "manualEntries", new String[0]);
177                    }
178    
179                    if (ArrayUtil.isEmpty(assetEntryXmls) &&
180                            ArrayUtil.isNotEmpty(manualEntries)) {
181    
182                            assetEntryXmls = getAssetEntryXmls(manualEntries);
183    
184                            portletPreferences.setValues("asset-entry-xml", assetEntryXmls);
185                    }
186    
187                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
188            }
189    
190            protected void upgradeToAssetEntryIdElement(Element rootElement) {
191                    Element assetIdElement = rootElement.element("asset-id");
192    
193                    if (assetIdElement != null) {
194                            String assetEntryId = assetIdElement.getText();
195    
196                            Element assetEntryIdElement = rootElement.addElement(
197                                    "assetEntryId");
198    
199                            assetEntryIdElement.addText(assetEntryId);
200    
201                            rootElement.remove(assetIdElement);
202                    }
203            }
204    
205            protected void upgradeToAssetEntryTypeElement(Element rootElement) {
206                    Element assetTypeElement = rootElement.element("asset-type");
207    
208                    if (assetTypeElement != null) {
209                            String assetEntryType = assetTypeElement.getText();
210    
211                            Element assetEntryTypeElement = rootElement.addElement(
212                                    "assetEntryType");
213    
214                            assetEntryTypeElement.addText(assetEntryType);
215    
216                            rootElement.remove(assetTypeElement);
217                    }
218            }
219    
220    }