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