1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.util;
24  
25  import com.liferay.portal.dao.orm.hibernate.BooleanType;
26  import com.liferay.portal.dao.orm.hibernate.DoubleType;
27  import com.liferay.portal.dao.orm.hibernate.FloatType;
28  import com.liferay.portal.dao.orm.hibernate.IntegerType;
29  import com.liferay.portal.dao.orm.hibernate.LongType;
30  import com.liferay.portal.dao.orm.hibernate.ShortType;
31  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
32  import com.liferay.portal.kernel.log.Log;
33  import com.liferay.portal.kernel.log.LogFactoryUtil;
34  import com.liferay.portal.kernel.util.DateUtil;
35  import com.liferay.portal.kernel.util.FileUtil;
36  import com.liferay.portal.kernel.util.GetterUtil;
37  import com.liferay.portal.kernel.util.StringPool;
38  import com.liferay.portal.kernel.util.StringUtil;
39  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
40  import com.liferay.portal.upgrade.StagnantRowException;
41  import com.liferay.portal.upgrade.UpgradeException;
42  import com.liferay.portal.util.PropsUtil;
43  import com.liferay.util.SystemProperties;
44  
45  import java.io.BufferedReader;
46  import java.io.BufferedWriter;
47  import java.io.FileReader;
48  import java.io.FileWriter;
49  
50  import java.sql.Clob;
51  import java.sql.Connection;
52  import java.sql.PreparedStatement;
53  import java.sql.ResultSet;
54  import java.sql.SQLException;
55  import java.sql.Timestamp;
56  import java.sql.Types;
57  
58  import java.text.DateFormat;
59  
60  import java.util.Date;
61  
62  import org.hibernate.usertype.UserType;
63  
64  /**
65   * <a href="Table.java.html"><b><i>View Source</i></b></a>
66   *
67   * @author Alexander Chow
68   * @author Brian Wing Shun Chan
69   * @author Raymond Augé
70   */
71  public class Table {
72  
73      public static final int BATCH_SIZE = GetterUtil.getInteger(
74          PropsUtil.get("hibernate.jdbc.batch_size"));
75  
76      public static final String SAFE_COMMA_CHARACTER =
77          "_SAFE_COMMA_CHARACTER_";
78  
79      public static final String SAFE_NEWLINE_CHARACTER =
80          "_SAFE_NEWLINE_CHARACTER_";
81  
82      public static final String SAFE_RETURN_CHARACTER =
83          "_SAFE_RETURN_CHARACTER_";
84  
85      public static final String[][] SAFE_CHARS = {
86          {StringPool.RETURN, StringPool.COMMA, StringPool.NEW_LINE},
87          {SAFE_RETURN_CHARACTER, SAFE_COMMA_CHARACTER, SAFE_NEWLINE_CHARACTER}
88      };
89  
90      public Table(String tableName) {
91          _tableName = tableName;
92      }
93  
94      public Table(String tableName, Object[][] columns) {
95          _tableName = tableName;
96  
97          setColumns(columns);
98      }
99  
100     public void appendColumn(StringBuilder sb, Object value, boolean last)
101         throws Exception {
102 
103         if (value == null) {
104             throw new UpgradeException(
105                 "Nulls should never be inserted into the database. " +
106                     "Attempted to append column to " + sb.toString() + ".");
107         }
108         else if (value instanceof Clob || value instanceof String) {
109             value = StringUtil.replace(
110                 (String)value, SAFE_CHARS[0], SAFE_CHARS[1]);
111 
112             sb.append(value);
113         }
114         else if (value instanceof Date) {
115             DateFormat df = DateUtil.getISOFormat();
116 
117             sb.append(df.format(value));
118         }
119         else {
120             sb.append(value);
121         }
122 
123         sb.append(StringPool.COMMA);
124 
125         if (last) {
126             sb.append(StringPool.NEW_LINE);
127         }
128     }
129 
130     public void appendColumn(
131             StringBuilder sb, ResultSet rs, String name, Integer type,
132             boolean last)
133         throws Exception {
134 
135         Object value = null;
136 
137         try {
138             value = getValue(rs, name, type);
139         }
140         catch (SQLException sqle) {
141             if (name.equals("uuid_")) {
142                 sb.append(PortalUUIDUtil.generate());
143             }
144 
145             sb.append(StringPool.COMMA);
146 
147             if (last) {
148                 sb.append(StringPool.NEW_LINE);
149             }
150 
151             return;
152         }
153 
154         appendColumn(sb, value, last);
155     }
156 
157     public Object[][] getColumns() {
158         return _columns;
159     }
160 
161     public String getCreateSQL() throws Exception {
162         return _createSQL;
163     }
164 
165     public String getDeleteSQL() throws Exception {
166         return "DELETE FROM " + _tableName;
167     }
168 
169     public String getExportedData(ResultSet rs) throws Exception {
170         StringBuilder sb = new StringBuilder();
171 
172         Object[][] columns = getColumns();
173 
174         for (int i = 0; i < columns.length; i++) {
175             boolean last = false;
176 
177             if ((i + 1) == columns.length) {
178                 last = true;
179             }
180 
181             appendColumn(
182                 sb, rs, (String)columns[i][0], (Integer)columns[i][1], last);
183         }
184 
185         return sb.toString();
186     }
187 
188     public String getInsertSQL() throws Exception {
189         String sql = "INSERT INTO " + _tableName + " (";
190 
191         for (int i = 0; i < _order.length; i++) {
192             int pos = _order[i];
193 
194             sql += _columns[pos][0];
195 
196             if ((i + 1) < _columns.length) {
197                 sql += ", ";
198             }
199             else {
200                 sql += ") VALUES (";
201             }
202         }
203 
204         for (int i = 0; i < _columns.length; i++) {
205             sql += "?";
206 
207             if ((i + 1) < _columns.length) {
208                 sql += ", ";
209             }
210             else {
211                 sql += ")";
212             }
213         }
214 
215         return sql;
216     }
217 
218     public int[] getOrder() {
219         return _order;
220     }
221 
222     public String getSelectSQL() throws Exception {
223         if (_selectSQL == null) {
224             /*String sql = "select ";
225 
226             for (int i = 0; i < _columns.length; i++) {
227                 sql += _columns[i][0];
228 
229                 if ((i + 1) < _columns.length) {
230                     sql += ", ";
231                 }
232                 else {
233                     sql += " from " + _tableName;
234                 }
235             }
236 
237             return sql;*/
238 
239             return "select * from " + _tableName;
240         }
241         else {
242             return _selectSQL;
243         }
244     }
245 
246     public String getTableName() {
247         return _tableName;
248     }
249 
250     public long getTotalRows() {
251         return _totalRows;
252     }
253 
254     public Object getValue(ResultSet rs, String name, Integer type)
255         throws Exception {
256 
257         Object value = null;
258 
259         int t = type.intValue();
260 
261         UserType userType = null;
262 
263         if (t == Types.BIGINT) {
264             userType = new LongType();
265         }
266         else if (t == Types.BOOLEAN) {
267             userType = new BooleanType();
268         }
269         else if (t == Types.CLOB) {
270             try {
271                 Clob clob = rs.getClob(name);
272 
273                 if (clob == null) {
274                     value = StringPool.BLANK;
275                 }
276                 else {
277                     BufferedReader br = new BufferedReader(
278                         clob.getCharacterStream());
279 
280                     StringBuilder sb = new StringBuilder();
281 
282                     String line = null;
283 
284                     while ((line = br.readLine()) != null) {
285                         if (sb.length() != 0) {
286                             sb.append(SAFE_NEWLINE_CHARACTER);
287                         }
288 
289                         sb.append(line);
290                     }
291 
292                     value = sb.toString();
293                 }
294             }
295             catch (Exception e) {
296 
297                 // If the database doesn't allow CLOB types for the column
298                 // value, then try retrieving it as a String
299 
300                 value = GetterUtil.getString(rs.getString(name));
301             }
302         }
303         else if (t == Types.DOUBLE) {
304             userType = new DoubleType();
305         }
306         else if (t == Types.FLOAT) {
307             userType = new FloatType();
308         }
309         else if (t == Types.INTEGER) {
310             userType = new IntegerType();
311         }
312         else if (t == Types.SMALLINT) {
313             userType = new ShortType();
314         }
315         else if (t == Types.TIMESTAMP) {
316             try {
317                 value = rs.getTimestamp(name);
318             }
319             catch (Exception e) {
320             }
321 
322             if (value == null) {
323                 value = StringPool.NULL;
324             }
325         }
326         else if (t == Types.VARCHAR) {
327             value = GetterUtil.getString(rs.getString(name));
328         }
329         else {
330             throw new UpgradeException(
331                 "Upgrade code using unsupported class type " + type);
332         }
333 
334         if (userType != null) {
335             try {
336                 value = userType.nullSafeGet(rs, new String[] {name}, null);
337             }
338             catch (Exception e) {
339                 _log.error(
340                     "Unable to nullSafeGet " + name + " with " +
341                         userType.getClass().getName());
342 
343                 throw e;
344             }
345         }
346 
347         return value;
348     }
349 
350     public String generateTempFile() throws Exception {
351         Connection con = null;
352         PreparedStatement ps = null;
353         ResultSet rs = null;
354 
355         boolean isEmpty = true;
356 
357         String tempFileName =
358             SystemProperties.get(SystemProperties.TMP_DIR) + "/temp-db-" +
359                 _tableName + "-" + System.currentTimeMillis();
360 
361         String selectSQL = getSelectSQL();
362 
363         BufferedWriter bw = new BufferedWriter(new FileWriter(tempFileName));
364 
365         try {
366             con = DataAccess.getConnection();
367 
368             ps = con.prepareStatement(selectSQL);
369 
370             rs = ps.executeQuery();
371 
372             while (rs.next()) {
373                 String data = null;
374 
375                 try {
376                     data = getExportedData(rs);
377 
378                     bw.write(data);
379 
380                     _totalRows++;
381 
382                     isEmpty = false;
383                 }
384                 catch (StagnantRowException sre) {
385                     if (_log.isWarnEnabled()) {
386                         _log.warn(
387                             "Skipping stagnant data in " + _tableName + ": " +
388                                 sre.getMessage());
389                     }
390                 }
391             }
392 
393             if (_log.isDebugEnabled()) {
394                 _log.debug(
395                     _tableName + " table backed up to file " + tempFileName);
396             }
397         }
398         catch (Exception e) {
399             FileUtil.delete(tempFileName);
400 
401             throw e;
402         }
403         finally {
404             DataAccess.cleanUp(con, ps, rs);
405 
406             bw.close();
407         }
408 
409         if (!isEmpty) {
410             return tempFileName;
411         }
412         else {
413             FileUtil.delete(tempFileName);
414 
415             return null;
416         }
417     }
418 
419     public void populateTable(String tempFileName) throws Exception {
420         Connection con = null;
421         PreparedStatement ps = null;
422 
423         String insertSQL = getInsertSQL();
424 
425         BufferedReader br = new BufferedReader(new FileReader(tempFileName));
426 
427         String line = null;
428 
429         try {
430             con = DataAccess.getConnection();
431 
432             boolean useBatch = con.getMetaData().supportsBatchUpdates();
433 
434             if (!useBatch) {
435                 if (_log.isDebugEnabled()) {
436                     _log.debug("Database does not support batch updates");
437                 }
438             }
439 
440             int count = 0;
441 
442             while ((line = br.readLine()) != null) {
443                 String[] values = StringUtil.split(line);
444 
445                 Object[][] columns = getColumns();
446 
447                 if ((values.length) != (columns.length)) {
448                     throw new UpgradeException(
449                         "Column lengths differ between temp file and schema. " +
450                             "Attempted to insert row " + line  + ".");
451                 }
452 
453                 if (count == 0) {
454                     ps = con.prepareStatement(insertSQL);
455                 }
456 
457                 int[] order = getOrder();
458 
459                 for (int i = 0; i < order.length; i++) {
460                     int pos = order[i];
461 
462                     setColumn(ps, i, (Integer)columns[pos][1], values[pos]);
463                 }
464 
465                 if (useBatch) {
466                     ps.addBatch();
467 
468                     if (count == BATCH_SIZE) {
469                         populateTableRows(ps, true);
470 
471                         count = 0;
472                     }
473                     else {
474                         count++;
475                     }
476                 }
477                 else {
478                     populateTableRows(ps, false);
479                 }
480             }
481 
482             if (useBatch) {
483                 if (count != 0) {
484                     populateTableRows(ps, true);
485                 }
486             }
487         }
488         finally {
489             DataAccess.cleanUp(con, ps);
490 
491             br.close();
492         }
493 
494         if (_log.isDebugEnabled()) {
495             _log.debug(getTableName() + " table populated with data");
496         }
497     }
498 
499     public void populateTableRows(PreparedStatement ps, boolean batch)
500         throws Exception {
501 
502         if (_log.isDebugEnabled()) {
503             _log.debug("Updating rows for " + getTableName());
504         }
505 
506         if (batch) {
507             ps.executeBatch();
508         }
509         else {
510             ps.executeUpdate();
511         }
512 
513         ps.close();
514     }
515 
516     public void setColumn(
517             PreparedStatement ps, int index, Integer type, String value)
518         throws Exception {
519 
520         int t = type.intValue();
521 
522         int paramIndex = index + 1;
523 
524         if (t == Types.BIGINT) {
525             ps.setLong(paramIndex, GetterUtil.getLong(value));
526         }
527         else if (t == Types.BOOLEAN) {
528             ps.setBoolean(paramIndex, GetterUtil.getBoolean(value));
529         }
530         else if ((t == Types.CLOB) || (t == Types.VARCHAR)) {
531             value = StringUtil.replace(value, SAFE_CHARS[1], SAFE_CHARS[0]);
532 
533             ps.setString(paramIndex, value);
534         }
535         else if (t == Types.DOUBLE) {
536             ps.setDouble(paramIndex, GetterUtil.getDouble(value));
537         }
538         else if (t == Types.FLOAT) {
539             ps.setFloat(paramIndex, GetterUtil.getFloat(value));
540         }
541         else if (t == Types.INTEGER) {
542             ps.setInt(paramIndex, GetterUtil.getInteger(value));
543         }
544         else if (t == Types.SMALLINT) {
545             ps.setShort(paramIndex, GetterUtil.getShort(value));
546         }
547         else if (t == Types.TIMESTAMP) {
548             if (StringPool.NULL.equals(value)) {
549                 ps.setTimestamp(paramIndex, null);
550             }
551             else {
552                 DateFormat df = DateUtil.getISOFormat();
553 
554                 ps.setTimestamp(
555                     paramIndex, new Timestamp(df.parse(value).getTime()));
556             }
557         }
558         else {
559             throw new UpgradeException(
560                 "Upgrade code using unsupported class type " + type);
561         }
562     }
563 
564     public void setColumns(Object[][] columns) {
565         _columns = columns;
566 
567         // LEP-7331
568 
569         _order = new int[_columns.length];
570 
571         int clobCount = 0;
572 
573         for (int i = 0; i < _columns.length; ++i) {
574             Integer type = (Integer)columns[i][1];
575 
576             if (type.intValue() == Types.CLOB) {
577                 clobCount++;
578 
579                 int pos = _columns.length - clobCount;
580 
581                 _order[pos] = i;
582             }
583             else {
584                 int pos = i - clobCount;
585 
586                 _order[pos] = i;
587             }
588         }
589     }
590 
591     public void setCreateSQL(String createSQL) throws Exception {
592         _createSQL = createSQL;
593     }
594 
595     public void setSelectSQL(String selectSQL) throws Exception {
596         _selectSQL = selectSQL;
597     }
598 
599     private static Log _log = LogFactoryUtil.getLog(Table.class);
600 
601     private Object[][] _columns;
602     private String _createSQL;
603     private int[] _order;
604     private String _selectSQL;
605     private String _tableName;
606     private long _totalRows = 0;
607 
608 }