001    /**
002     * Copyright (c) 2000-2013 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.GetterUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.xml.Document;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.kernel.xml.SAXReaderUtil;
024    import com.liferay.portal.upgrade.util.UpgradeAssetPublisherManualEntries;
025    import com.liferay.portlet.PortletPreferencesFactoryUtil;
026    
027    import javax.portlet.PortletPreferences;
028    
029    /**
030     * @author Julio Camarero
031     * @author Douglas Wong
032     */
033    public class UpgradeAssetPublisher extends BaseUpgradePortletPreferences {
034    
035            protected String[] getAssetEntryXmls(String[] manualEntries)
036                    throws Exception {
037    
038                    String[] assetEntryXmls = new String[manualEntries.length];
039    
040                    for (int i = 0; i < manualEntries.length; i++) {
041                            String manualEntry = manualEntries[i];
042    
043                            Document document = SAXReaderUtil.read(manualEntry);
044    
045                            Element rootElement = document.getRootElement();
046    
047                            UpgradeAssetPublisherManualEntries.upgradeToAssetEntryIdElement(
048                                    rootElement);
049    
050                            UpgradeAssetPublisherManualEntries.upgradeToAssetEntryTypeElement(
051                                    rootElement);
052    
053                            assetEntryXmls[i] = document.formattedString(StringPool.BLANK);
054                    }
055    
056                    return assetEntryXmls;
057            }
058    
059            @Override
060            protected String[] getPortletIds() {
061                    return new String[] {"101_INSTANCE_%"};
062            }
063    
064            @Override
065            protected String upgradePreferences(
066                            long companyId, long ownerId, int ownerType, long plid,
067                            String portletId, String xml)
068                    throws Exception {
069    
070                    PortletPreferences portletPreferences =
071                            PortletPreferencesFactoryUtil.fromXML(
072                                    companyId, ownerId, ownerType, plid, portletId, xml);
073    
074                    long layoutId = GetterUtil.getLong(
075                            portletPreferences.getValue("lfr-scope-layout-id", null));
076    
077                    portletPreferences.reset("lfr-scope-layout-id");
078    
079                    if (layoutId != 0) {
080                            portletPreferences.setValues(
081                                    "scope-ids", new String[] {"Layout_" + layoutId});
082    
083                            portletPreferences.setValue(
084                                    "default-scope", Boolean.FALSE.toString());
085                    }
086    
087                    long classNameId = GetterUtil.getLong(
088                            portletPreferences.getValue("class-name-id", null));
089    
090                    portletPreferences.reset("class-name-id");
091    
092                    if (classNameId != 0) {
093                            portletPreferences.setValues(
094                                    "class-name-ids", new String[] {String.valueOf(classNameId)});
095    
096                            portletPreferences.setValue(
097                                    "any-asset-type", Boolean.FALSE.toString());
098                    }
099    
100                    boolean andOperator = GetterUtil.getBoolean(
101                            portletPreferences.getValue("and-operator", null));
102    
103                    portletPreferences.reset("and-operator");
104    
105                    String[] assetTagNames = portletPreferences.getValues("entries", null);
106                    String[] notAssetTagNames = portletPreferences.getValues(
107                            "not-entries", null);
108    
109                    int i = 0;
110    
111                    if (assetTagNames != null) {
112                            portletPreferences.reset("entries");
113    
114                            portletPreferences.setValue(
115                                    "queryContains" + i, Boolean.TRUE.toString());
116                            portletPreferences.setValue(
117                                    "queryAndOperator" + i, String.valueOf(andOperator));
118                            portletPreferences.setValue("queryName" + i, "assetTags");
119                            portletPreferences.setValues("queryValues" + i, assetTagNames);
120    
121                            i++;
122                    }
123    
124                    if (notAssetTagNames != null) {
125                            portletPreferences.reset("not-entries");
126    
127                            portletPreferences.setValue(
128                                    "queryContains" + i, Boolean.FALSE.toString());
129                            portletPreferences.setValue(
130                                    "queryAndOperator" + i, String.valueOf(andOperator));
131                            portletPreferences.setValue("queryName" + i, "assetTags");
132                            portletPreferences.setValues("queryValues" + i, notAssetTagNames);
133    
134                            i++;
135                    }
136    
137                    String selectionStyle = portletPreferences.getValue(
138                            "selection-style", null);
139    
140                    if (Validator.isNotNull(selectionStyle) &&
141                            !selectionStyle.equals("dynamic")) {
142    
143                            String[] manualEntries = portletPreferences.getValues(
144                                    "manual-entries", new String[0]);
145    
146                            String[] assetEntryXmls = getAssetEntryXmls(manualEntries);
147    
148                            portletPreferences.setValues("asset-entry-xml", assetEntryXmls);
149                    }
150    
151                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
152            }
153    
154    }