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