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_1_0;
016    
017    import com.liferay.document.library.kernel.model.DLFileEntryTypeConstants;
018    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
019    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.ListUtil;
022    import com.liferay.portal.kernel.util.PortalUtil;
023    import com.liferay.portal.util.PropsValues;
024    
025    import java.sql.PreparedStatement;
026    import java.sql.ResultSet;
027    
028    import java.util.List;
029    
030    import javax.portlet.PortletPreferences;
031    
032    /**
033     * @author Juan Fern??ndez
034     * @author Sergio Sanchez
035     * @author Brian Wing Shun Chan
036     */
037    public class UpgradeAssetPublisher extends BaseUpgradePortletPreferences {
038    
039            protected long getIGImageFileEntryType(long companyId) throws Exception {
040                    try (PreparedStatement ps = connection.prepareStatement(
041                                    "select fileEntryTypeId from DLFileEntryType " +
042                                            "where name = ? AND companyId = ?")) {
043    
044                            ps.setString(1, DLFileEntryTypeConstants.NAME_IG_IMAGE);
045                            ps.setLong(2, companyId);
046    
047                            try (ResultSet rs = ps.executeQuery()) {
048                                    if (rs.next()) {
049                                            return rs.getLong("fileEntryTypeId");
050                                    }
051    
052                                    return 0;
053                            }
054                    }
055            }
056    
057            @Override
058            protected String[] getPortletIds() {
059                    return new String[] {"101_INSTANCE_%"};
060            }
061    
062            @Override
063            protected String upgradePreferences(
064                            long companyId, long ownerId, int ownerType, long plid,
065                            String portletId, String xml)
066                    throws Exception {
067    
068                    if (!PropsValues.DL_FILE_ENTRY_TYPE_IG_IMAGE_AUTO_CREATE_ON_UPGRADE) {
069                            return xml;
070                    }
071    
072                    PortletPreferences portletPreferences =
073                            PortletPreferencesFactoryUtil.fromXML(
074                                    companyId, ownerId, ownerType, plid, portletId, xml);
075    
076                    String[] classNameIds = portletPreferences.getValues(
077                            "classNameIds", null);
078    
079                    if (ArrayUtil.isEmpty(classNameIds)) {
080                            return PortletPreferencesFactoryUtil.toXML(portletPreferences);
081                    }
082    
083                    long dlFileEntryClassNameId = PortalUtil.getClassNameId(
084                            "com.liferay.portlet.documentlibrary.model.DLFileEntry");
085                    long igImageClassNameId = PortalUtil.getClassNameId(
086                            "com.liferay.portlet.imagegallery.model.IGImage");
087    
088                    List<String> classNameIdsList = ListUtil.fromArray(classNameIds);
089    
090                    int index = classNameIdsList.indexOf(
091                            String.valueOf(igImageClassNameId));
092    
093                    if (index >= 0) {
094                            classNameIdsList.remove(index);
095    
096                            if (!classNameIdsList.contains(
097                                            String.valueOf(dlFileEntryClassNameId))) {
098    
099                                    classNameIdsList.add(
100                                            index, String.valueOf(dlFileEntryClassNameId));
101                            }
102    
103                            portletPreferences.setValues(
104                                    "classNameIds",
105                                    classNameIdsList.toArray(new String[classNameIdsList.size()]));
106    
107                            if (classNameIdsList.size() == 1) {
108                                    long fileEntryTypeId = getIGImageFileEntryType(companyId);
109    
110                                    portletPreferences.setValue(
111                                            "anyClassTypeDLFileEntryAssetRendererFactory",
112                                            String.valueOf(fileEntryTypeId));
113                                    portletPreferences.setValue(
114                                            "classTypeIds", String.valueOf(fileEntryTypeId));
115                            }
116                    }
117    
118                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
119            }
120    
121    }