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.v7_0_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.upgrade.v7_0_0.util.DDMContentTable;
023    import com.liferay.portal.upgrade.v7_0_0.util.DDMStructureTable;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
025    
026    import java.sql.Connection;
027    import java.sql.PreparedStatement;
028    import java.sql.ResultSet;
029    import java.sql.SQLException;
030    import java.sql.Timestamp;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Marcellus Tavares
035     */
036    public class UpgradeDynamicDataMapping extends UpgradeProcess {
037    
038            protected void addStructureVersion(
039                            long structureVersionId, long groupId, long companyId, long userId,
040                            String userName, Timestamp createDate, long structureId,
041                            String name, String description, String definition,
042                            String storageType, int type)
043                    throws Exception {
044    
045                    Connection con = null;
046                    PreparedStatement ps = null;
047    
048                    try {
049                            con = DataAccess.getUpgradeOptimizedConnection();
050    
051                            StringBundler sb = new StringBundler(6);
052    
053                            sb.append("insert into DDMStructureVersion (structureVersionId, ");
054                            sb.append("groupId, companyId, userId, userName, createDate, ");
055                            sb.append("structureId, version, name, description, definition, ");
056                            sb.append("storageType, type_) values (?, ?, ?, ?, ?, ?, ?, ?, ");
057                            sb.append("?, ?, ?, ?, ?)");
058    
059                            String sql = sb.toString();
060    
061                            ps = con.prepareStatement(sql);
062    
063                            ps.setLong(1, structureVersionId);
064                            ps.setLong(2, groupId);
065                            ps.setLong(3, companyId);
066                            ps.setLong(4, userId);
067                            ps.setString(5, userName);
068                            ps.setTimestamp(6, createDate);
069                            ps.setLong(7, structureId);
070                            ps.setString(8, DDMStructureConstants.VERSION_DEFAULT);
071                            ps.setString(9, name);
072                            ps.setString(10, description);
073                            ps.setString(11, definition);
074                            ps.setString(12, storageType);
075                            ps.setInt(13, type);
076    
077                            ps.executeUpdate();
078                    }
079                    catch (Exception e) {
080                            _log.error(
081                                    "Unable to upgrade dynamic data mapping structure version " +
082                                            "with structure ID " + structureId);
083    
084                            throw e;
085                    }
086                    finally {
087                            DataAccess.cleanUp(con, ps);
088                    }
089            }
090    
091            protected void addStructureVersions() throws Exception {
092                    Connection con = null;
093                    PreparedStatement ps = null;
094                    ResultSet rs = null;
095    
096                    try {
097                            con = DataAccess.getUpgradeOptimizedConnection();
098    
099                            ps = con.prepareStatement("select * from DDMStructure");
100    
101                            rs = ps.executeQuery();
102    
103                            while (rs.next()) {
104                                    long structureId = rs.getLong("structureId");
105                                    long groupId = rs.getLong("groupId");
106                                    long companyId = rs.getLong("companyId");
107                                    long userId = rs.getLong("userId");
108                                    String userName = rs.getString("userName");
109                                    Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
110                                    String name = rs.getString("name");
111                                    String description = rs.getString("description");
112                                    String definition = rs.getString("definition");
113                                    String storageType = rs.getString("storageType");
114                                    int type = rs.getInt("type_");
115    
116                                    addStructureVersion(
117                                            increment(), groupId, companyId, userId, userName,
118                                            modifiedDate, structureId, name, description, definition,
119                                            storageType, type);
120                            }
121                    }
122                    finally {
123                            DataAccess.cleanUp(con, ps, rs);
124                    }
125            }
126    
127            protected void addTemplateVersion(
128                            long templateVersionId, long groupId, long companyId, long userId,
129                            String userName, Timestamp createDate, long templateId, String name,
130                            String description, String language, String script)
131                    throws Exception {
132    
133                    Connection con = null;
134                    PreparedStatement ps = null;
135    
136                    try {
137                            con = DataAccess.getUpgradeOptimizedConnection();
138    
139                            StringBundler sb = new StringBundler(4);
140    
141                            sb.append("insert into DDMTemplateVersion (templateVersionId, ");
142                            sb.append("groupId, companyId, userId, userName, createDate, ");
143                            sb.append("templateId, version, name, description, language, ");
144                            sb.append("script) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
145    
146                            String sql = sb.toString();
147    
148                            ps = con.prepareStatement(sql);
149    
150                            ps.setLong(1, templateVersionId);
151                            ps.setLong(2, groupId);
152                            ps.setLong(3, companyId);
153                            ps.setLong(4, userId);
154                            ps.setString(5, userName);
155                            ps.setTimestamp(6, createDate);
156                            ps.setLong(7, templateId);
157                            ps.setString(8, DDMStructureConstants.VERSION_DEFAULT);
158                            ps.setString(9, name);
159                            ps.setString(10, description);
160                            ps.setString(11, language);
161                            ps.setString(12, script);
162    
163                            ps.executeUpdate();
164                    }
165                    catch (Exception e) {
166                            _log.error(
167                                    "Unable to upgrade dynamic data mapping template version " +
168                                            "with template ID " + templateId);
169    
170                            throw e;
171                    }
172                    finally {
173                            DataAccess.cleanUp(con, ps);
174                    }
175            }
176    
177            protected void addTemplateVersions() throws Exception {
178                    Connection con = null;
179                    PreparedStatement ps = null;
180                    ResultSet rs = null;
181    
182                    try {
183                            con = DataAccess.getUpgradeOptimizedConnection();
184    
185                            ps = con.prepareStatement("select * from DDMTemplate");
186    
187                            rs = ps.executeQuery();
188    
189                            while (rs.next()) {
190                                    long templateId = rs.getLong("templateId");
191                                    long groupId = rs.getLong("groupId");
192                                    long companyId = rs.getLong("companyId");
193                                    long userId = rs.getLong("userId");
194                                    String userName = rs.getString("userName");
195                                    Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
196                                    String name = rs.getString("name");
197                                    String description = rs.getString("description");
198                                    String language = rs.getString("language");
199                                    String script = rs.getString("script");
200    
201                                    addTemplateVersion(
202                                            increment(), groupId, companyId, userId, userName,
203                                            modifiedDate, templateId, name, description, language,
204                                            script);
205                            }
206                    }
207                    finally {
208                            DataAccess.cleanUp(con, ps, rs);
209                    }
210            }
211    
212            @Override
213            protected void doUpgrade() throws Exception {
214                    try {
215                            runSQL("alter_column_name DDMContent xml data_ TEXT null");
216                    }
217                    catch (SQLException sqle) {
218                            upgradeTable(
219                                    DDMContentTable.TABLE_NAME, DDMContentTable.TABLE_COLUMNS,
220                                    DDMContentTable.TABLE_SQL_CREATE,
221                                    DDMContentTable.TABLE_SQL_ADD_INDEXES);
222                    }
223    
224                    try {
225                            runSQL("alter_column_name DDMStructure xsd definition TEXT null");
226                    }
227                    catch (SQLException sqle) {
228                            upgradeTable(
229                                    DDMStructureTable.TABLE_NAME, DDMStructureTable.TABLE_COLUMNS,
230                                    DDMStructureTable.TABLE_SQL_CREATE,
231                                    DDMStructureTable.TABLE_SQL_ADD_INDEXES);
232                    }
233    
234                    addStructureVersions();
235                    addTemplateVersions();
236            }
237    
238            private static final Log _log = LogFactoryUtil.getLog(
239                    UpgradeDynamicDataMapping.class);
240    
241    }