001
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
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 return PortletPreferencesFactoryUtil.toXML(portletPreferences);
093 }
094
095 long dlFileEntryClassNameId = PortalUtil.getClassNameId(
096 DLFileEntry.class.getName());
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
116 portletPreferences.setValues(
117 "classNameIds",
118 classNameIdsList.toArray(new String[classNameIdsList.size()]));
119
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 return PortletPreferencesFactoryUtil.toXML(portletPreferences);
129 }
130
131 }