001    /**
002     * Copyright (c) 2000-2013 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_2_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.LocalizationUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.security.auth.FullNameGenerator;
024    import com.liferay.portal.security.auth.FullNameGeneratorFactory;
025    import com.liferay.portal.upgrade.v6_2_0.util.DLFileEntryTypeTable;
026    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
027    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
028    
029    import java.sql.Connection;
030    import java.sql.PreparedStatement;
031    import java.sql.ResultSet;
032    import java.sql.SQLException;
033    
034    import java.util.HashMap;
035    import java.util.Locale;
036    import java.util.Map;
037    
038    /**
039     * @author Dennis Ju
040     * @author Mate Thurzo
041     * @author Alexander Chow
042     * @author Roberto D??az
043     */
044    public class UpgradeDocumentLibrary extends UpgradeProcess {
045    
046            protected void deleteChecksumDirectory() throws Exception {
047                    Connection con = null;
048                    PreparedStatement ps = null;
049                    ResultSet rs = null;
050    
051                    try {
052                            con = DataAccess.getUpgradeOptimizedConnection();
053    
054                            ps = con.prepareStatement(
055                                    "select distinct companyId from DLFileEntry");
056    
057                            rs = ps.executeQuery();
058    
059                            while (rs.next()) {
060                                    long companyId = rs.getLong("companyId");
061    
062                                    try {
063                                            DLStoreUtil.deleteDirectory(companyId, 0, "checksum");
064                                    }
065                                    catch (Exception e) {
066                                    }
067                            }
068                    }
069                    finally {
070                            DataAccess.cleanUp(con, ps, rs);
071                    }
072            }
073    
074            protected void deleteTempDirectory() {
075                    try {
076                            DLStoreUtil.deleteDirectory(0, 0, "liferay_temp/");
077                    }
078                    catch (Exception e) {
079                    }
080            }
081    
082            @Override
083            protected void doUpgrade() throws Exception {
084    
085                    // DLFileEntryType
086    
087                    try {
088                            runSQL("alter table DLFileEntryType add fileEntryTypeKey STRING");
089    
090                            runSQL("alter_column_type DLFileEntryType name STRING null");
091                    }
092                    catch (SQLException sqle) {
093                            upgradeTable(
094                                    DLFileEntryTypeTable.TABLE_NAME,
095                                    DLFileEntryTypeTable.TABLE_COLUMNS,
096                                    DLFileEntryTypeTable.TABLE_SQL_CREATE,
097                                    DLFileEntryTypeTable.TABLE_SQL_ADD_INDEXES);
098                    }
099    
100                    updateFileEntryTypes();
101    
102                    // Checksum directory
103    
104                    deleteChecksumDirectory();
105    
106                    // Temp directory
107    
108                    deleteTempDirectory();
109            }
110    
111            protected String getUserName(long userId) throws Exception {
112                    Connection con = null;
113                    PreparedStatement ps = null;
114                    ResultSet rs = null;
115    
116                    try {
117                            con = DataAccess.getUpgradeOptimizedConnection();
118    
119                            ps = con.prepareStatement(
120                                    "select firstName, middleName, lastName from User_ where " +
121                                            "userId = ?");
122    
123                            ps.setLong(1, userId);
124    
125                            rs = ps.executeQuery();
126    
127                            if (rs.next()) {
128                                    String firstName = rs.getString("firstName");
129                                    String middleName = rs.getString("middleName");
130                                    String lastName = rs.getString("lastName");
131    
132                                    FullNameGenerator fullNameGenerator =
133                                            FullNameGeneratorFactory.getInstance();
134    
135                                    return fullNameGenerator.getFullName(
136                                            firstName, middleName, lastName);
137                            }
138    
139                            return StringPool.BLANK;
140                    }
141                    finally {
142                            DataAccess.cleanUp(con, ps, rs);
143                    }
144            }
145    
146            protected String localize(String content, String key) {
147                    Locale locale = LocaleUtil.getDefault();
148    
149                    Map<Locale, String> localizationMap = new HashMap<Locale, String>();
150    
151                    localizationMap.put(locale, content);
152    
153                    return LocalizationUtil.updateLocalization(
154                            localizationMap, StringPool.BLANK, key,
155                            LocaleUtil.toLanguageId(locale));
156            }
157    
158            protected void updateFileEntryType(
159                            long fileEntryTypeId, String fileEntryTypeKey, String name,
160                            String description)
161                    throws Exception {
162    
163                    Connection con = null;
164                    PreparedStatement ps = null;
165    
166                    try {
167                            con = DataAccess.getUpgradeOptimizedConnection();
168    
169                            ps = con.prepareStatement(
170                                    "update DLFileEntryType set fileEntryTypeKey = ?, name = ?, " +
171                                            "description = ? where fileEntryTypeId = ?");
172    
173                            ps.setString(1, fileEntryTypeKey);
174                            ps.setString(2, localize(name, "Name"));
175                            ps.setString(3, localize(description, "Description"));
176                            ps.setLong(4, fileEntryTypeId);
177    
178                            ps.executeUpdate();
179                    }
180                    finally {
181                            DataAccess.cleanUp(con, ps);
182                    }
183            }
184    
185            protected void updateFileEntryTypes() throws Exception {
186                    Connection con = null;
187                    PreparedStatement ps = null;
188                    ResultSet rs = null;
189    
190                    try {
191                            con = DataAccess.getUpgradeOptimizedConnection();
192    
193                            ps = con.prepareStatement(
194                                    "select fileEntryTypeId, name, description from " +
195                                            "DLFileEntryType");
196    
197                            rs = ps.executeQuery();
198    
199                            while (rs.next()) {
200                                    long fileEntryTypeId = rs.getLong("fileEntryTypeId");
201                                    String name = GetterUtil.getString(rs.getString("name"));
202                                    String description = rs.getString("description");
203    
204                                    if (fileEntryTypeId ==
205                                                    DLFileEntryTypeConstants.
206                                                            FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) {
207    
208                                            name = DLFileEntryTypeConstants.NAME_BASIC_DOCUMENT;
209                                    }
210    
211                                    updateFileEntryType(
212                                            fileEntryTypeId, name.toUpperCase(), name, description);
213                            }
214                    }
215                    finally {
216                            DataAccess.cleanUp(con, ps, rs);
217                    }
218            }
219    
220    }