001    /**
002     * Copyright (c) 2000-2012 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.RenameUpgradePortletPreferences;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.upgrade.v6_2_0.util.JournalFeedTable;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
026    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
027    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
028    import com.liferay.portlet.journal.model.JournalArticle;
029    import com.liferay.portlet.journal.model.JournalStructure;
030    import com.liferay.portlet.journal.model.JournalTemplate;
031    import com.liferay.portlet.journal.util.JournalConverterUtil;
032    
033    import java.sql.Connection;
034    import java.sql.Date;
035    import java.sql.PreparedStatement;
036    import java.sql.ResultSet;
037    import java.sql.SQLException;
038    
039    import java.util.HashMap;
040    import java.util.Map;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Marcellus Tavares
045     * @author Juan Fernández
046     * @author Bruno Basto
047     */
048    public class UpgradeJournal extends RenameUpgradePortletPreferences {
049    
050            public UpgradeJournal() {
051                    _preferenceNamesMap.put("templateId", "ddmTemplateKey");
052            }
053    
054            protected void addDDMStructure(
055                            String uuid_, long ddmStructureId, long groupId, long companyId,
056                            long userId, String userName, Date createDate, Date modifiedDate,
057                            long parentDDMStructureId, long classNameId, String ddmStructureKey,
058                            String name, String description, String xsd, String storageType,
059                            int type)
060                    throws Exception {
061    
062                    Connection con = null;
063                    PreparedStatement ps = null;
064    
065                    try {
066                            con = DataAccess.getUpgradeOptimizedConnection();
067    
068                            StringBundler sb = new StringBundler(6);
069    
070                            sb.append("insert into DDMStructure(uuid_, structureId, groupId, ");
071                            sb.append("companyId, userId, userName, createDate, ");
072                            sb.append("modifiedDate, parentStructureId, classNameId, ");
073                            sb.append("structureKey, name, description, xsd, storageType, ");
074                            sb.append("type_) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
075                            sb.append("?, ?, ?)");
076    
077                            String sql = sb.toString();
078    
079                            ps = con.prepareStatement(sql);
080    
081                            ps.setString(1, uuid_);
082                            ps.setLong(2, ddmStructureId);
083                            ps.setLong(3, groupId);
084                            ps.setLong(4, companyId);
085                            ps.setLong(5, userId);
086                            ps.setString(6, userName);
087                            ps.setDate(7, createDate);
088                            ps.setDate(8, modifiedDate);
089                            ps.setLong(9, parentDDMStructureId);
090                            ps.setLong(10, classNameId);
091                            ps.setString(11, ddmStructureKey);
092                            ps.setString(12, name);
093                            ps.setString(13, description);
094                            ps.setString(14, JournalConverterUtil.getDDMXSD(xsd));
095                            ps.setString(15, storageType);
096                            ps.setInt(16, type);
097    
098                            ps.executeUpdate();
099                    }
100                    finally {
101                            DataAccess.cleanUp(con, ps);
102                    }
103            }
104    
105            protected void addDDMStructure(
106                            String uuid_, long ddmStructureId, long groupId, long companyId,
107                            long userId, String userName, Date createDate, Date modifiedDate,
108                            String parentStructureId, String ddmStructureKey, String name,
109                            String description, String xsd)
110                    throws Exception {
111    
112                    long parentDDMStructureId = 0;
113    
114                    if (Validator.isNotNull(parentStructureId)) {
115                            parentDDMStructureId = updateStructure(parentStructureId);
116                    }
117    
118                    addDDMStructure(
119                            uuid_, ddmStructureId, groupId, companyId, userId, userName,
120                            createDate, modifiedDate, parentDDMStructureId,
121                            PortalUtil.getClassNameId(JournalArticle.class.getName()),
122                            ddmStructureKey, name, description, xsd,
123                            PropsValues.JOURNAL_ARTICLE_STORAGE_TYPE,
124                            DDMStructureConstants.TYPE_DEFAULT);
125            }
126    
127            protected void addDDMTemplate(
128                            String uuid_, long ddmTemplateId, long groupId, long companyId,
129                            long userId, String userName, Date createDate, Date modifiedDate,
130                            long classNameId, long classPK, String templateKey, String name,
131                            String description, String type, String mode, String language,
132                            String script, boolean cacheable, boolean smallImage,
133                            long smallImageId, String smallImageURL)
134                    throws Exception {
135    
136                    Connection con = null;
137                    PreparedStatement ps = null;
138    
139                    try {
140                            con = DataAccess.getUpgradeOptimizedConnection();
141    
142                            StringBundler sb = new StringBundler(6);
143    
144                            sb.append("insert into DDMTemplate(uuid_, templateId, groupId, ");
145                            sb.append("companyId, userId, userName, createDate, modifiedDate,");
146                            sb.append("classNameId, classPK , templateKey, name, description,");
147                            sb.append("type_, mode_, language, script, cacheable, smallImage,");
148                            sb.append("smallImageId, smallImageURL) values (?, ?, ?, ?, ?, ?,");
149                            sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
150    
151                            String sql = sb.toString();
152    
153                            ps = con.prepareStatement(sql);
154    
155                            ps.setString(1, uuid_);
156                            ps.setLong(2, ddmTemplateId);
157                            ps.setLong(3, groupId);
158                            ps.setLong(4, companyId);
159                            ps.setLong(5, userId);
160                            ps.setString(6, userName);
161                            ps.setDate(7, createDate);
162                            ps.setDate(8, modifiedDate);
163                            ps.setLong(9, classNameId);
164                            ps.setLong(10, classPK);
165                            ps.setString(11, templateKey);
166                            ps.setString(12, name);
167                            ps.setString(13, description);
168                            ps.setString(14, type);
169                            ps.setString(15, mode);
170                            ps.setString(16, language);
171                            ps.setString(17, script);
172                            ps.setBoolean(18, cacheable);
173                            ps.setBoolean(19, smallImage);
174                            ps.setLong(20, smallImageId);
175                            ps.setString(21, smallImageURL);
176    
177                            ps.executeUpdate();
178                    }
179                    finally {
180                            DataAccess.cleanUp(con, ps);
181                    }
182            }
183    
184            @Override
185            protected void doUpgrade() throws Exception {
186                    try {
187                            runSQL(
188                                    "alter_column_name JournalFeed feedType feedFormat " +
189                                            "VARCHAR(75) null");
190                    }
191                    catch (SQLException sqle) {
192                            upgradeTable(
193                                    JournalFeedTable.TABLE_NAME, JournalFeedTable.TABLE_COLUMNS,
194                                    JournalFeedTable.TABLE_SQL_CREATE,
195                                    JournalFeedTable.TABLE_SQL_ADD_INDEXES);
196                    }
197    
198                    updateStructures();
199                    updateTemplates();
200    
201                    updatePortletPreferences();
202            }
203    
204            @Override
205            protected String[] getPortletIds() {
206                    return new String[] {"56_INSTANCE_%"};
207            }
208    
209            @Override
210            protected Map<String, String> getPreferenceNamesMap() {
211                    return _preferenceNamesMap;
212            }
213    
214            protected void updateResourcePermission(
215                            long companyId, String oldClassName, String newClassName,
216                            long oldPrimKey, long newPrimKey)
217                    throws Exception {
218    
219                    StringBundler sb = new StringBundler(10);
220    
221                    sb.append("update ResourcePermission set name = '");
222                    sb.append(newClassName);
223                    sb.append("', primKey = ");
224                    sb.append(newPrimKey);
225                    sb.append(" where companyId = ");
226                    sb.append(companyId);
227                    sb.append(" and name = '");
228                    sb.append(oldClassName);
229                    sb.append("' and primKey = ");
230                    sb.append(oldPrimKey);
231    
232                    runSQL(sb.toString());
233            }
234    
235            protected long updateStructure(String structureId) throws Exception {
236                    Connection con = null;
237                    PreparedStatement ps = null;
238                    ResultSet rs = null;
239    
240                    try {
241                            con = DataAccess.getUpgradeOptimizedConnection();
242    
243                            ps = con.prepareStatement(
244                                    "select * from JournalStructure where structureId = " +
245                                            structureId);
246    
247                            rs = ps.executeQuery();
248    
249                            if (rs.next()) {
250                                    String uuid_ = rs.getString("uuid_");
251                                    long id_ = rs.getLong("id_");
252                                    long groupId = rs.getLong("groupId");
253                                    long companyId = rs.getLong("companyId");
254                                    long userId = rs.getLong("userId");
255                                    String userName = rs.getString("userName");
256                                    Date createDate = rs.getDate("createDate");
257                                    Date modifiedDate = rs.getDate("modifiedDate");
258                                    String parentStructureId = rs.getString("parentStructureId");
259                                    String name = rs.getString("name");
260                                    String description = rs.getString("description");
261                                    String xsd = rs.getString("xsd");
262    
263                                    Long ddmStructureId = _ddmStructureIds.get(
264                                            groupId + "#" + structureId);
265    
266                                    if (ddmStructureId != null) {
267                                            return ddmStructureId;
268                                    }
269    
270                                    ddmStructureId = increment();
271    
272                                    addDDMStructure(
273                                            uuid_, ddmStructureId, groupId, companyId, userId, userName,
274                                            createDate, modifiedDate, parentStructureId, structureId,
275                                            name, description, xsd);
276    
277                                    updateResourcePermission(
278                                            companyId, JournalStructure.class.getName(),
279                                            DDMStructure.class.getName(), id_, ddmStructureId);
280    
281                                    _ddmStructureIds.put(
282                                            groupId + "#" + structureId, ddmStructureId);
283                            }
284    
285                            return 0;
286                    }
287                    finally {
288                            DataAccess.cleanUp(con, ps, rs);
289                    }
290            }
291    
292            protected void updateStructures() throws Exception {
293                    Connection con = null;
294                    PreparedStatement ps = null;
295                    ResultSet rs = null;
296    
297                    try {
298                            con = DataAccess.getUpgradeOptimizedConnection();
299    
300                            ps = con.prepareStatement("select * from JournalStructure");
301    
302                            rs = ps.executeQuery();
303    
304                            while (rs.next()) {
305                                    String uuid_ = rs.getString("uuid_");
306                                    long id_ = rs.getLong("id_");
307                                    long groupId = rs.getLong("groupId");
308                                    long companyId = rs.getLong("companyId");
309                                    long userId = rs.getLong("userId");
310                                    String userName = rs.getString("userName");
311                                    Date createDate = rs.getDate("createDate");
312                                    Date modifiedDate = rs.getDate("modifiedDate");
313                                    String structureId = rs.getString("structureId");
314                                    String parentStructureId = rs.getString("parentStructureId");
315                                    String name = rs.getString("name");
316                                    String description = rs.getString("description");
317                                    String xsd = rs.getString("xsd");
318    
319                                    long ddmStructureId = increment();
320    
321                                    addDDMStructure(
322                                            uuid_, ddmStructureId, groupId, companyId, userId, userName,
323                                            createDate, modifiedDate, parentStructureId, structureId,
324                                            name, description, xsd);
325    
326                                    updateResourcePermission(
327                                            companyId, JournalStructure.class.getName(),
328                                            DDMStructure.class.getName(), id_, ddmStructureId);
329    
330                                    _ddmStructureIds.put(
331                                            groupId + "#" + structureId, ddmStructureId);
332                            }
333                    }
334                    finally {
335                            DataAccess.cleanUp(con, ps, rs);
336                    }
337    
338                    runSQL("drop table JournalStructure");
339            }
340    
341            protected void updateTemplates() throws Exception {
342                    Connection con = null;
343                    PreparedStatement ps = null;
344                    ResultSet rs = null;
345    
346                    try {
347                            con = DataAccess.getUpgradeOptimizedConnection();
348    
349                            ps = con.prepareStatement("select * from JournalTemplate");
350    
351                            rs = ps.executeQuery();
352    
353                            while (rs.next()) {
354                                    String uuid_ = rs.getString("uuid_");
355                                    long id_ = rs.getLong("id_");
356                                    long groupId = rs.getLong("groupId");
357                                    long companyId = rs.getLong("companyId");
358                                    long userId = rs.getLong("userId");
359                                    String userName = rs.getString("userName");
360                                    Date createDate = rs.getDate("createDate");
361                                    Date modifiedDate = rs.getDate("modifiedDate");
362                                    String templateId = rs.getString("templateId");
363                                    String structureId = rs.getString("structureId");
364                                    String name = rs.getString("name");
365                                    String description = rs.getString("description");
366                                    String language = rs.getString("langType");
367                                    String script = rs.getString("xsl");
368                                    boolean cacheable = rs.getBoolean("cacheable");
369                                    boolean smallImage = rs.getBoolean("smallImage");
370                                    long smallImageId = rs.getLong("smallImageId");
371                                    String smallImageURL = rs.getString("smallImageURL");
372    
373                                    long ddmTemplateId = increment();
374    
375                                    long classNameId = PortalUtil.getClassNameId(
376                                            DDMStructure.class.getName());
377    
378                                    long classPK = 0;
379    
380                                    if (Validator.isNotNull(structureId)) {
381                                            classPK = _ddmStructureIds.get(groupId + "#" + structureId);
382                                    }
383    
384                                    addDDMTemplate(
385                                            uuid_, ddmTemplateId, groupId, companyId, userId, userName,
386                                            createDate, modifiedDate, classNameId, classPK, templateId,
387                                            name, description,
388                                            DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY,
389                                            DDMTemplateConstants.TEMPLATE_MODE_CREATE, language, script,
390                                            cacheable, smallImage, smallImageId, smallImageURL);
391    
392                                    updateResourcePermission(
393                                            companyId, JournalTemplate.class.getName(),
394                                            DDMTemplate.class.getName(), id_, ddmTemplateId);
395                            }
396                    }
397                    finally {
398                            DataAccess.cleanUp(con, ps, rs);
399                    }
400    
401                    runSQL("drop table JournalTemplate");
402            }
403    
404            private Map<String, Long> _ddmStructureIds = new HashMap<String, Long>();
405            private Map<String, String> _preferenceNamesMap =
406                    new HashMap<String, String>();
407    
408    }