001    /**
002     * Copyright (c) 2000-2012 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_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.upgrade.util.UpgradeColumn;
022    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
023    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryNameUpgradeColumnImpl;
027    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryTable;
028    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryTitleUpgradeColumnImpl;
029    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryVersionUpgradeColumnImpl;
030    import com.liferay.portal.upgrade.v6_0_0.util.DLFileRankTable;
031    import com.liferay.portal.upgrade.v6_0_0.util.DLFileShortcutTable;
032    import com.liferay.portal.upgrade.v6_0_0.util.DLFileVersionTable;
033    import com.liferay.portlet.documentlibrary.NoSuchFileException;
034    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
035    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
036    
037    import java.sql.Connection;
038    import java.sql.PreparedStatement;
039    import java.sql.ResultSet;
040    import java.sql.Timestamp;
041    
042    /**
043     * @author Jorge Ferrer
044     * @author Alexander Chow
045     */
046    public class UpgradeDocumentLibrary extends UpgradeProcess {
047    
048            protected void addFileVersion(
049                            long groupId, long companyId, long userId, String userName,
050                            long folderId, String name, double version, int size)
051                    throws Exception {
052    
053                    Timestamp now = new Timestamp(System.currentTimeMillis());
054    
055                    Connection con = null;
056                    PreparedStatement ps = null;
057    
058                    try {
059                            con = DataAccess.getConnection();
060    
061                            StringBundler sb = new StringBundler(5);
062    
063                            sb.append("insert into DLFileVersion (fileVersionId, groupId, ");
064                            sb.append("companyId, userId, userName, createDate, folderId, ");
065                            sb.append("name, version, size_, status, statusByUserId, ");
066                            sb.append("statusByUserName, statusDate) values (?, ?, ?, ?, ?, ");
067                            sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?)");
068    
069                            String sql = sb.toString();
070    
071                            ps = con.prepareStatement(sql);
072    
073                            ps.setLong(1, increment());
074                            ps.setLong(2, groupId);
075                            ps.setLong(3, companyId);
076                            ps.setLong(4, userId);
077                            ps.setString(5, userName);
078                            ps.setTimestamp(6, now);
079                            ps.setLong(7, folderId);
080                            ps.setString(8, name);
081                            ps.setDouble(9, version);
082                            ps.setInt(10, size);
083                            ps.setInt(11, WorkflowConstants.STATUS_APPROVED);
084                            ps.setLong(12, userId);
085                            ps.setString(13, userName);
086                            ps.setTimestamp(14, now);
087    
088                            ps.executeUpdate();
089                    }
090                    finally {
091                            DataAccess.cleanUp(con, ps);
092                    }
093            }
094    
095            @Override
096            protected void doUpgrade() throws Exception {
097                    Connection con = null;
098                    PreparedStatement ps = null;
099                    ResultSet rs = null;
100    
101                    try {
102                            con = DataAccess.getConnection();
103    
104                            ps = con.prepareStatement("select * from DLFileEntry");
105    
106                            rs = ps.executeQuery();
107    
108                            while (rs.next()) {
109                                    long companyId = rs.getLong("companyId");
110                                    long groupId = rs.getLong("groupId");
111                                    long userId = rs.getLong("userId");
112                                    String userName = rs.getString("userName");
113                                    long folderId = rs.getLong("folderId");
114                                    String name = rs.getString("name");
115                                    double version = rs.getDouble("version");
116                                    int size = rs.getInt("size_");
117    
118                                    long repositoryId = folderId;
119    
120                                    if (repositoryId ==
121                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
122    
123                                            repositoryId = groupId;
124                                    }
125    
126                                    String newName = DLFileEntryNameUpgradeColumnImpl.getNewName(
127                                            name);
128    
129                                    if (!newName.equals(name)) {
130                                            try {
131                                                    DLStoreUtil.updateFile(
132                                                            companyId, repositoryId, name, newName);
133                                            }
134                                            catch (NoSuchFileException nsfe) {
135                                                    _log.error(nsfe);
136                                            }
137                                    }
138    
139                                    addFileVersion(
140                                            groupId, companyId, userId, userName, folderId, name,
141                                            version, size);
142                            }
143                    }
144                    finally {
145                            DataAccess.cleanUp(con, ps, rs);
146                    }
147    
148                    // DLFileEntry
149    
150                    UpgradeColumn nameColumn = new DLFileEntryNameUpgradeColumnImpl("name");
151                    UpgradeColumn titleColumn = new DLFileEntryTitleUpgradeColumnImpl(
152                            nameColumn, "title");
153                    UpgradeColumn versionColumn = new DLFileEntryVersionUpgradeColumnImpl(
154                            "version");
155    
156                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
157                            DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS,
158                            nameColumn, titleColumn, versionColumn);
159    
160                    upgradeTable.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
161                    upgradeTable.setIndexesSQL(DLFileEntryTable.TABLE_SQL_ADD_INDEXES);
162    
163                    upgradeTable.updateTable();
164    
165                    // DLFileRank
166    
167                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
168                            DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
169                            nameColumn);
170    
171                    upgradeTable.setCreateSQL(DLFileRankTable.TABLE_SQL_CREATE);
172                    upgradeTable.setIndexesSQL(DLFileRankTable.TABLE_SQL_ADD_INDEXES);
173    
174                    upgradeTable.updateTable();
175    
176                    // DLFileShortcut
177    
178                    UpgradeColumn toNameColumn = new DLFileEntryNameUpgradeColumnImpl(
179                            "toName");
180    
181                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
182                            DLFileShortcutTable.TABLE_NAME, DLFileShortcutTable.TABLE_COLUMNS,
183                            toNameColumn);
184    
185                    upgradeTable.setCreateSQL(DLFileShortcutTable.TABLE_SQL_CREATE);
186                    upgradeTable.setIndexesSQL(DLFileShortcutTable.TABLE_SQL_ADD_INDEXES);
187    
188                    upgradeTable.updateTable();
189    
190                    // DLFileVersion
191    
192                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
193                            DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
194                            nameColumn, versionColumn);
195    
196                    upgradeTable.setCreateSQL(DLFileVersionTable.TABLE_SQL_CREATE);
197                    upgradeTable.setIndexesSQL(DLFileVersionTable.TABLE_SQL_ADD_INDEXES);
198    
199                    upgradeTable.updateTable();
200            }
201    
202            private static Log _log = LogFactoryUtil.getLog(
203                    UpgradeDocumentLibrary.class);
204    
205    }