001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.getUpgradeOptimizedConnection();
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.getUpgradeOptimizedConnection();
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 folderId = rs.getLong("folderId");
112                                    String name = rs.getString("name");
113    
114                                    long repositoryId = folderId;
115    
116                                    if (repositoryId ==
117                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
118    
119                                            repositoryId = groupId;
120                                    }
121    
122                                    String newName = DLFileEntryNameUpgradeColumnImpl.getNewName(
123                                            name);
124    
125                                    if (!newName.equals(name)) {
126                                            try {
127                                                    DLStoreUtil.updateFile(
128                                                            companyId, repositoryId, name, newName);
129                                            }
130                                            catch (NoSuchFileException nsfe) {
131                                                    _log.error(nsfe);
132                                            }
133                                    }
134                            }
135                    }
136                    finally {
137                            DataAccess.cleanUp(con, ps, rs);
138                    }
139    
140                    synchronizeFileVersions();
141    
142                    // DLFileEntry
143    
144                    UpgradeColumn nameColumn = new DLFileEntryNameUpgradeColumnImpl("name");
145                    UpgradeColumn titleColumn = new DLFileEntryTitleUpgradeColumnImpl(
146                            nameColumn, "title");
147                    UpgradeColumn versionColumn = new DLFileEntryVersionUpgradeColumnImpl(
148                            "version");
149    
150                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
151                            DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS,
152                            nameColumn, titleColumn, versionColumn);
153    
154                    upgradeTable.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
155                    upgradeTable.setIndexesSQL(DLFileEntryTable.TABLE_SQL_ADD_INDEXES);
156    
157                    upgradeTable.updateTable();
158    
159                    // DLFileRank
160    
161                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
162                            DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
163                            nameColumn);
164    
165                    upgradeTable.setCreateSQL(DLFileRankTable.TABLE_SQL_CREATE);
166                    upgradeTable.setIndexesSQL(DLFileRankTable.TABLE_SQL_ADD_INDEXES);
167    
168                    upgradeTable.updateTable();
169    
170                    // DLFileShortcut
171    
172                    UpgradeColumn toNameColumn = new DLFileEntryNameUpgradeColumnImpl(
173                            "toName");
174    
175                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
176                            DLFileShortcutTable.TABLE_NAME, DLFileShortcutTable.TABLE_COLUMNS,
177                            toNameColumn);
178    
179                    upgradeTable.setCreateSQL(DLFileShortcutTable.TABLE_SQL_CREATE);
180                    upgradeTable.setIndexesSQL(DLFileShortcutTable.TABLE_SQL_ADD_INDEXES);
181    
182                    upgradeTable.updateTable();
183    
184                    // DLFileVersion
185    
186                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
187                            DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
188                            nameColumn, versionColumn);
189    
190                    upgradeTable.setCreateSQL(DLFileVersionTable.TABLE_SQL_CREATE);
191                    upgradeTable.setIndexesSQL(DLFileVersionTable.TABLE_SQL_ADD_INDEXES);
192    
193                    upgradeTable.updateTable();
194            }
195    
196            protected void synchronizeFileVersions() throws Exception {
197                    Connection con = null;
198                    PreparedStatement ps = null;
199                    ResultSet rs = null;
200    
201                    try {
202                            con = DataAccess.getUpgradeOptimizedConnection();
203    
204                            StringBundler sb = new StringBundler(5);
205    
206                            sb.append("select * from DLFileEntry dlFileEntry where version ");
207                            sb.append("not in (select version from DLFileVersion ");
208                            sb.append("dlFileVersion where (dlFileVersion.folderId = ");
209                            sb.append("dlFileEntry.folderId) and (dlFileVersion.name = ");
210                            sb.append("dlFileEntry.name))");
211    
212                            String sql = sb.toString();
213    
214                            ps = con.prepareStatement(sql);
215    
216                            rs = ps.executeQuery();
217    
218                            while (rs.next()) {
219                                    long companyId = rs.getLong("companyId");
220                                    long groupId = rs.getLong("groupId");
221                                    long userId = rs.getLong("userId");
222                                    String userName = rs.getString("userName");
223                                    long folderId = rs.getLong("folderId");
224                                    String name = rs.getString("name");
225                                    double version = rs.getDouble("version");
226                                    int size = rs.getInt("size_");
227    
228                                    addFileVersion(
229                                            groupId, companyId, userId, userName, folderId, name,
230                                            version, size);
231                            }
232                    }
233                    finally {
234                            DataAccess.cleanUp(con, ps);
235                    }
236            }
237    
238            private static Log _log = LogFactoryUtil.getLog(
239                    UpgradeDocumentLibrary.class);
240    
241    }