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