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.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.CharPool;
020    
021    import java.sql.PreparedStatement;
022    import java.sql.ResultSet;
023    
024    /**
025     * @author Miguel Pastor
026     */
027    public class UpgradeRelease extends UpgradeProcess {
028    
029            @Override
030            protected void doUpgrade() throws Exception {
031                    PreparedStatement ps = null;
032                    ResultSet rs = null;
033    
034                    try {
035                            ps = connection.prepareStatement(
036                                    "select distinct buildNumber from Release_ " +
037                                            "where schemaVersion is null");
038    
039                            rs = ps.executeQuery();
040    
041                            while (rs.next()) {
042                                    String buildNumber = rs.getString("buildNumber");
043    
044                                    String schemaVersion = toSchemaVersion(buildNumber);
045    
046                                    runSQL(
047                                            "update Release_ set schemaVersion = '" + schemaVersion +
048                                                    "' where buildNumber = " + buildNumber +
049                                                            " and schemaVersion is null");
050                            }
051                    }
052                    finally {
053                            DataAccess.cleanUp(ps, rs);
054                    }
055            }
056    
057            protected String toSchemaVersion(String buildNumber) {
058                    StringBuilder sb = new StringBuilder(2 * buildNumber.length());
059    
060                    for (int i = 0; i < buildNumber.length(); i++) {
061                            sb.append(buildNumber.charAt(i));
062                            sb.append(CharPool.PERIOD);
063                    }
064    
065                    if (buildNumber.length() > 0) {
066                            sb.setLength(sb.length() - 1);
067                    }
068    
069                    return sb.toString();
070            }
071    
072    }