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.ListUtil;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portal.util.PropsValues;
022    import com.liferay.portlet.PortletPreferencesFactoryUtil;
023    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
024    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
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 ((classNameIds != null) && (classNameIds.length > 0)) {
092                            long dlFileEntryClassNameId = PortalUtil.getClassNameId(
093                                    DLFileEntry.class.getName());
094                            long igImageClassNameId = PortalUtil.getClassNameId(
095                                    "com.liferay.portlet.imagegallery.model.IGImage");
096    
097                            List<String> classNameIdsList = ListUtil.fromArray(classNameIds);
098    
099                            int index = classNameIdsList.indexOf(
100                                    String.valueOf(igImageClassNameId));
101    
102                            if (index >= 0) {
103                                    classNameIdsList.remove(index);
104    
105                                    if (!classNameIdsList.contains(
106                                                    String.valueOf(dlFileEntryClassNameId))) {
107    
108                                            classNameIdsList.add(
109                                                    index, String.valueOf(dlFileEntryClassNameId));
110                                    }
111                            }
112    
113                            portletPreferences.setValues(
114                                    "classNameIds",
115                                    classNameIdsList.toArray(new String[classNameIdsList.size()]));
116    
117                            long fileEntryTypeId = getIGImageFileEntryType(companyId);
118    
119                            portletPreferences.setValue(
120                                    "anyClassTypeDLFileEntryAssetRendererFactory",
121                                    String.valueOf(fileEntryTypeId));
122                            portletPreferences.setValue(
123                                    "classTypeIds", String.valueOf(fileEntryTypeId));
124    
125                    }
126    
127                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
128            }
129    
130    }