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.v5_2_8_to_6_0_5;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.upgrade.v5_2_8_to_6_0_5.util.DLFileEntryTable;
026    import com.liferay.portal.upgrade.v5_2_8_to_6_0_5.util.DLFileVersionTable;
027    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryNameUpgradeColumnImpl;
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.portlet.documentlibrary.model.DLFolderConstants;
033    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
034    
035    import java.sql.Connection;
036    import java.sql.PreparedStatement;
037    import java.sql.ResultSet;
038    import java.sql.Timestamp;
039    
040    /**
041     * @author Jorge Ferrer
042     * @author Alexander Chow
043     * @author Douglas Wong
044     */
045    public class UpgradeDocumentLibrary extends UpgradeProcess {
046    
047            protected void addFileVersion(
048                            long groupId, long companyId, long userId, String userName,
049                            long folderId, String name, double version, int size)
050                    throws Exception {
051    
052                    Timestamp now = new Timestamp(System.currentTimeMillis());
053    
054                    Connection con = null;
055                    PreparedStatement ps = null;
056    
057                    try {
058                            con = DataAccess.getUpgradeOptimizedConnection();
059    
060                            StringBundler sb = new StringBundler(5);
061    
062                            sb.append("insert into DLFileVersion (fileVersionId, groupId, ");
063                            sb.append("companyId, userId, userName, createDate, folderId, ");
064                            sb.append("name, version, size_, status, statusByUserId, ");
065                            sb.append("statusByUserName, statusDate) values (?, ?, ?, ?, ?, ");
066                            sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?)");
067    
068                            String sql = sb.toString();
069    
070                            ps = con.prepareStatement(sql);
071    
072                            ps.setLong(1, increment());
073                            ps.setLong(2, groupId);
074                            ps.setLong(3, companyId);
075                            ps.setLong(4, userId);
076                            ps.setString(5, userName);
077                            ps.setTimestamp(6, now);
078                            ps.setLong(7, folderId);
079                            ps.setString(8, name);
080                            ps.setDouble(9, version);
081                            ps.setInt(10, size);
082                            ps.setInt(11, WorkflowConstants.STATUS_APPROVED);
083                            ps.setLong(12, userId);
084                            ps.setString(13, userName);
085                            ps.setTimestamp(14, now);
086    
087                            ps.executeUpdate();
088                    }
089                    finally {
090                            DataAccess.cleanUp(con, ps);
091                    }
092            }
093    
094            @Override
095            protected void doUpgrade() throws Exception {
096                    Connection con = null;
097                    PreparedStatement ps = null;
098                    ResultSet rs = null;
099    
100                    try {
101                            con = DataAccess.getUpgradeOptimizedConnection();
102    
103                            ps = con.prepareStatement("select * from DLFileEntry");
104    
105                            rs = ps.executeQuery();
106    
107                            while (rs.next()) {
108                                    long companyId = rs.getLong("companyId");
109                                    long groupId = rs.getLong("groupId");
110                                    long folderId = rs.getLong("folderId");
111                                    String name = rs.getString("name");
112    
113                                    long repositoryId = folderId;
114    
115                                    if (repositoryId ==
116                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
117    
118                                            repositoryId = groupId;
119                                    }
120    
121                                    String newName = DLFileEntryNameUpgradeColumnImpl.getNewName(
122                                            name);
123    
124                                    if (!newName.equals(name)) {
125                                            DLStoreUtil.updateFile(
126                                                    companyId, repositoryId, name, newName);
127                                    }
128                            }
129                    }
130                    finally {
131                            DataAccess.cleanUp(con, ps, rs);
132                    }
133    
134                    synchronizeFileVersions();
135    
136                    // DLFileEntry
137    
138                    UpgradeColumn nameColumn = new DLFileEntryNameUpgradeColumnImpl("name");
139                    UpgradeColumn titleColumn = new DLFileEntryTitleUpgradeColumnImpl(
140                            nameColumn, "title");
141                    UpgradeColumn versionColumn = new DLFileEntryVersionUpgradeColumnImpl(
142                            "version");
143    
144                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
145                            DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS,
146                            nameColumn, titleColumn, versionColumn);
147    
148                    upgradeTable.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
149                    upgradeTable.setIndexesSQL(DLFileEntryTable.TABLE_SQL_ADD_INDEXES);
150    
151                    upgradeTable.updateTable();
152    
153                    // DLFileRank
154    
155                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
156                            DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
157                            nameColumn);
158    
159                    upgradeTable.setCreateSQL(DLFileRankTable.TABLE_SQL_CREATE);
160                    upgradeTable.setIndexesSQL(DLFileRankTable.TABLE_SQL_ADD_INDEXES);
161    
162                    upgradeTable.updateTable();
163    
164                    // DLFileShortcut
165    
166                    UpgradeColumn toNameColumn = new DLFileEntryNameUpgradeColumnImpl(
167                            "toName");
168    
169                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
170                            DLFileShortcutTable.TABLE_NAME, DLFileShortcutTable.TABLE_COLUMNS,
171                            toNameColumn);
172    
173                    upgradeTable.setCreateSQL(DLFileShortcutTable.TABLE_SQL_CREATE);
174                    upgradeTable.setIndexesSQL(DLFileShortcutTable.TABLE_SQL_ADD_INDEXES);
175    
176                    upgradeTable.updateTable();
177    
178                    // DLFileVersion
179    
180                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
181                            DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
182                            nameColumn, versionColumn);
183    
184                    upgradeTable.setCreateSQL(
185                            StringUtil.replace(
186                                    DLFileVersionTable.TABLE_SQL_CREATE,
187                                    new String[] {
188                                            ",extraSettings VARCHAR(75) null",
189                                            ",title VARCHAR(75) null"
190                                    },
191                                    new String[] {
192                                            ",extraSettings STRING null",
193                                            ",title VARCHAR(255) null"
194                                    }));
195    
196                    upgradeTable.setIndexesSQL(DLFileVersionTable.TABLE_SQL_ADD_INDEXES);
197    
198                    upgradeTable.updateTable();
199            }
200    
201            protected void synchronizeFileVersions() throws Exception {
202                    Connection con = null;
203                    PreparedStatement ps = null;
204                    ResultSet rs = null;
205    
206                    try {
207                            con = DataAccess.getUpgradeOptimizedConnection();
208    
209                            StringBundler sb = new StringBundler(5);
210    
211                            sb.append("select * from DLFileEntry dlFileEntry where version ");
212                            sb.append("not in (select version from DLFileVersion ");
213                            sb.append("dlFileVersion where (dlFileVersion.folderId = ");
214                            sb.append("dlFileEntry.folderId) and (dlFileVersion.name = ");
215                            sb.append("dlFileEntry.name))");
216    
217                            String sql = sb.toString();
218    
219                            ps = con.prepareStatement(sql);
220    
221                            rs = ps.executeQuery();
222    
223                            while (rs.next()) {
224                                    long companyId = rs.getLong("companyId");
225                                    long groupId = rs.getLong("groupId");
226                                    long userId = rs.getLong("userId");
227                                    String userName = rs.getString("userName");
228                                    long folderId = rs.getLong("folderId");
229                                    String name = rs.getString("name");
230                                    double version = rs.getDouble("version");
231                                    int size = rs.getInt("size_");
232    
233                                    addFileVersion(
234                                            groupId, companyId, userId, userName, folderId, name,
235                                            version, size);
236                            }
237                    }
238                    finally {
239                            DataAccess.cleanUp(con, ps);
240                    }
241            }
242    
243    }