001    /**
002     * Copyright (c) 2000-2010 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.documentlibrary.service.DLServiceUtil;
018    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
022    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryNameUpgradeColumnImpl;
026    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryTable;
027    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryTitleUpgradeColumnImpl;
028    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryVersionUpgradeColumnImpl;
029    import com.liferay.portal.upgrade.v6_0_0.util.DLFileRankTable;
030    import com.liferay.portal.upgrade.v6_0_0.util.DLFileShortcutTable;
031    import com.liferay.portal.upgrade.v6_0_0.util.DLFileVersionTable;
032    import com.liferay.portal.util.PortletKeys;
033    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
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     */
044    public class UpgradeDocumentLibrary extends UpgradeProcess {
045    
046            protected void addFileVersion(
047                            long groupId, long companyId, long userId, String userName,
048                            long folderId, String name, double version, int size)
049                    throws Exception {
050    
051                    Timestamp now = new Timestamp(System.currentTimeMillis());
052    
053                    Connection con = null;
054                    PreparedStatement ps = null;
055    
056                    try {
057                            con = DataAccess.getConnection();
058    
059                            StringBundler sb = new StringBundler(5);
060    
061                            sb.append("insert into DLFileVersion (fileVersionId, groupId, ");
062                            sb.append("companyId, userId, userName, createDate, folderId, ");
063                            sb.append("name, version, size_, status, statusByUserId, ");
064                            sb.append("statusByUserName, statusDate) values (?, ?, ?, ?, ?, ");
065                            sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?)");
066    
067                            String sql = sb.toString();
068    
069                            ps = con.prepareStatement(sql);
070    
071                            ps.setLong(1, increment());
072                            ps.setLong(2, groupId);
073                            ps.setLong(3, companyId);
074                            ps.setLong(4, userId);
075                            ps.setString(5, userName);
076                            ps.setTimestamp(6, now);
077                            ps.setLong(7, folderId);
078                            ps.setString(8, name);
079                            ps.setDouble(9, version);
080                            ps.setInt(10, size);
081                            ps.setInt(11, WorkflowConstants.STATUS_APPROVED);
082                            ps.setLong(12, userId);
083                            ps.setString(13, userName);
084                            ps.setTimestamp(14, now);
085    
086                            ps.executeUpdate();
087                    }
088                    finally {
089                            DataAccess.cleanUp(con, ps);
090                    }
091            }
092    
093            protected void doUpgrade() throws Exception {
094                    Connection con = null;
095                    PreparedStatement ps = null;
096                    ResultSet rs = null;
097    
098                    try {
099                            con = DataAccess.getConnection();
100    
101                            ps = con.prepareStatement("select * from DLFileEntry");
102    
103                            rs = ps.executeQuery();
104    
105                            while (rs.next()) {
106                                    long companyId = rs.getLong("companyId");
107                                    long groupId = rs.getLong("groupId");
108                                    long userId = rs.getLong("userId");
109                                    String userName = rs.getString("userName");
110                                    long folderId = rs.getLong("folderId");
111                                    String name = rs.getString("name");
112                                    double version = rs.getDouble("version");
113                                    int size = rs.getInt("size_");
114    
115                                    String portletId = PortletKeys.DOCUMENT_LIBRARY;
116                                    long repositoryId = folderId;
117    
118                                    if (repositoryId ==
119                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
120    
121                                            repositoryId = groupId;
122                                    }
123    
124                                    String newName = DLFileEntryNameUpgradeColumnImpl.getNewName(
125                                            name);
126    
127                                    if (!newName.equals(name)) {
128                                            DLServiceUtil.updateFile(
129                                                    companyId, portletId, groupId, repositoryId, name,
130                                                    newName, false);
131                                    }
132    
133                                    addFileVersion(
134                                            groupId, companyId, userId, userName, folderId, name,
135                                            version, size);
136                            }
137                    }
138                    finally {
139                            DataAccess.cleanUp(con, ps, rs);
140                    }
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    
156                    upgradeTable.updateTable();
157    
158                    // DLFileRank
159    
160                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
161                            DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
162                            nameColumn);
163    
164                    upgradeTable.setCreateSQL(DLFileRankTable.TABLE_SQL_CREATE);
165    
166                    upgradeTable.updateTable();
167    
168                    // DLFileShortcut
169    
170                    UpgradeColumn toNameColumn = new DLFileEntryNameUpgradeColumnImpl(
171                            "toName");
172    
173                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
174                            DLFileShortcutTable.TABLE_NAME, DLFileShortcutTable.TABLE_COLUMNS,
175                            toNameColumn);
176    
177                    upgradeTable.setCreateSQL(DLFileShortcutTable.TABLE_SQL_CREATE);
178    
179                    upgradeTable.updateTable();
180    
181                    // DLFileVersion
182    
183                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
184                            DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
185                            nameColumn, versionColumn);
186    
187                    upgradeTable.setCreateSQL(DLFileVersionTable.TABLE_SQL_CREATE);
188    
189                    upgradeTable.updateTable();
190            }
191    
192    }