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.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.upgrade.v6_2_0.util.JournalFeedTable;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portal.util.PropsValues;
030    import com.liferay.portlet.PortletPreferencesFactoryUtil;
031    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
034    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
035    import com.liferay.portlet.journal.model.JournalArticle;
036    import com.liferay.portlet.journal.model.JournalStructure;
037    import com.liferay.portlet.journal.model.JournalTemplate;
038    import com.liferay.portlet.journal.util.JournalConverterUtil;
039    
040    import java.sql.Connection;
041    import java.sql.Date;
042    import java.sql.PreparedStatement;
043    import java.sql.ResultSet;
044    import java.sql.SQLException;
045    
046    import java.util.HashMap;
047    import java.util.Map;
048    
049    import javax.portlet.PortletPreferences;
050    
051    /**
052     * @author Brian Wing Shun Chan
053     * @author Marcellus Tavares
054     * @author Juan Fern??ndez
055     * @author Bruno Basto
056     */
057    public class UpgradeJournal extends BaseUpgradePortletPreferences {
058    
059            protected void addDDMStructure(
060                            String uuid_, long ddmStructureId, long groupId, long companyId,
061                            long userId, String userName, Date createDate, Date modifiedDate,
062                            long parentDDMStructureId, long classNameId, String ddmStructureKey,
063                            String name, String description, String xsd, String storageType,
064                            int type)
065                    throws Exception {
066    
067                    Connection con = null;
068                    PreparedStatement ps = null;
069    
070                    try {
071                            con = DataAccess.getUpgradeOptimizedConnection();
072    
073                            StringBundler sb = new StringBundler(6);
074    
075                            sb.append("insert into DDMStructure(uuid_, structureId, groupId, ");
076                            sb.append("companyId, userId, userName, createDate, ");
077                            sb.append("modifiedDate, parentStructureId, classNameId, ");
078                            sb.append("structureKey, name, description, xsd, storageType, ");
079                            sb.append("type_) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
080                            sb.append("?, ?, ?)");
081    
082                            String sql = sb.toString();
083    
084                            ps = con.prepareStatement(sql);
085    
086                            ps.setString(1, uuid_);
087                            ps.setLong(2, ddmStructureId);
088                            ps.setLong(3, groupId);
089                            ps.setLong(4, companyId);
090                            ps.setLong(5, userId);
091                            ps.setString(6, userName);
092                            ps.setDate(7, createDate);
093                            ps.setDate(8, modifiedDate);
094                            ps.setLong(9, parentDDMStructureId);
095                            ps.setLong(10, classNameId);
096                            ps.setString(11, ddmStructureKey);
097                            ps.setString(12, name);
098                            ps.setString(13, description);
099                            ps.setString(14, JournalConverterUtil.getDDMXSD(xsd));
100                            ps.setString(15, storageType);
101                            ps.setInt(16, type);
102    
103                            ps.executeUpdate();
104                    }
105                    finally {
106                            DataAccess.cleanUp(con, ps);
107                    }
108            }
109    
110            protected void addDDMStructure(
111                            String uuid_, long ddmStructureId, long groupId, long companyId,
112                            long userId, String userName, Date createDate, Date modifiedDate,
113                            String parentStructureId, String ddmStructureKey, String name,
114                            String description, String xsd)
115                    throws Exception {
116    
117                    long parentDDMStructureId = 0;
118    
119                    if (Validator.isNotNull(parentStructureId)) {
120                            parentDDMStructureId = updateStructure(parentStructureId);
121                    }
122    
123                    addDDMStructure(
124                            uuid_, ddmStructureId, groupId, companyId, userId, userName,
125                            createDate, modifiedDate, parentDDMStructureId,
126                            PortalUtil.getClassNameId(JournalArticle.class.getName()),
127                            ddmStructureKey, name, description, xsd,
128                            PropsValues.JOURNAL_ARTICLE_STORAGE_TYPE,
129                            DDMStructureConstants.TYPE_DEFAULT);
130            }
131    
132            protected void addDDMTemplate(
133                            String uuid_, long ddmTemplateId, long groupId, long companyId,
134                            long userId, String userName, Date createDate, Date modifiedDate,
135                            long classNameId, long classPK, String templateKey, String name,
136                            String description, String type, String mode, String language,
137                            String script, boolean cacheable, boolean smallImage,
138                            long smallImageId, String smallImageURL)
139                    throws Exception {
140    
141                    Connection con = null;
142                    PreparedStatement ps = null;
143    
144                    try {
145                            con = DataAccess.getUpgradeOptimizedConnection();
146    
147                            StringBundler sb = new StringBundler(6);
148    
149                            sb.append("insert into DDMTemplate(uuid_, templateId, groupId, ");
150                            sb.append("companyId, userId, userName, createDate, modifiedDate,");
151                            sb.append("classNameId, classPK , templateKey, name, description,");
152                            sb.append("type_, mode_, language, script, cacheable, smallImage,");
153                            sb.append("smallImageId, smallImageURL) values (?, ?, ?, ?, ?, ?,");
154                            sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
155    
156                            String sql = sb.toString();
157    
158                            ps = con.prepareStatement(sql);
159    
160                            ps.setString(1, uuid_);
161                            ps.setLong(2, ddmTemplateId);
162                            ps.setLong(3, groupId);
163                            ps.setLong(4, companyId);
164                            ps.setLong(5, userId);
165                            ps.setString(6, userName);
166                            ps.setDate(7, createDate);
167                            ps.setDate(8, modifiedDate);
168                            ps.setLong(9, classNameId);
169                            ps.setLong(10, classPK);
170                            ps.setString(11, templateKey);
171                            ps.setString(12, name);
172                            ps.setString(13, description);
173                            ps.setString(14, type);
174                            ps.setString(15, mode);
175                            ps.setString(16, language);
176                            ps.setString(17, script);
177                            ps.setBoolean(18, cacheable);
178                            ps.setBoolean(19, smallImage);
179                            ps.setLong(20, smallImageId);
180                            ps.setString(21, smallImageURL);
181    
182                            ps.executeUpdate();
183                    }
184                    finally {
185                            DataAccess.cleanUp(con, ps);
186                    }
187            }
188    
189            @Override
190            protected void doUpgrade() throws Exception {
191                    try {
192                            runSQL(
193                                    "alter_column_name JournalFeed feedType feedFormat " +
194                                            "VARCHAR(75) null");
195                    }
196                    catch (SQLException sqle) {
197                            upgradeTable(
198                                    JournalFeedTable.TABLE_NAME, JournalFeedTable.TABLE_COLUMNS,
199                                    JournalFeedTable.TABLE_SQL_CREATE,
200                                    JournalFeedTable.TABLE_SQL_ADD_INDEXES);
201                    }
202    
203                    updateStructures();
204                    updateTemplates();
205    
206                    super.doUpgrade();
207            }
208    
209            protected long getDDMStructureId(long groupId, String structureId) {
210                    if (Validator.isNull(structureId)) {
211                            return 0;
212                    }
213    
214                    Long ddmStructureId = _ddmStructureIds.get(groupId + "#" + structureId);
215    
216                    if (ddmStructureId == null) {
217                            if (_log.isWarnEnabled()) {
218                                    _log.warn(
219                                            "Unable to get the DDM structure ID for group " +
220                                                    groupId + " and journal structure ID " + structureId);
221                            }
222    
223                            return 0;
224                    }
225    
226                    return ddmStructureId;
227            }
228    
229            @Override
230            protected String[] getPortletIds() {
231                    return new String[] {
232                            "56_INSTANCE_%", "62_INSTANCE_%", "101_INSTANCE_%"
233                    };
234            }
235    
236            protected void updatePreferencesClassPKs(
237                            PortletPreferences preferences, String key)
238                    throws Exception {
239    
240                    String[] oldValues = preferences.getValues(key, null);
241    
242                    if (oldValues == null) {
243                            return;
244                    }
245    
246                    String[] newValues = new String[oldValues.length];
247    
248                    for (int i = 0; i < oldValues.length; i++) {
249                            String oldValue = oldValues[i];
250    
251                            String newValue = oldValue;
252    
253                            String[] oldPrimaryKeys = StringUtil.split(oldValue);
254    
255                            for (String oldPrimaryKey : oldPrimaryKeys) {
256                                    if (!Validator.isNumber(oldPrimaryKey)) {
257                                            break;
258                                    }
259    
260                                    Long newPrimaryKey = _ddmStructurePKs.get(
261                                            GetterUtil.getLong(oldPrimaryKey));
262    
263                                    if (Validator.isNotNull(newPrimaryKey)) {
264                                            newValue = StringUtil.replace(
265                                                    newValue, oldPrimaryKey, String.valueOf(newPrimaryKey));
266                                    }
267                            }
268    
269                            newValues[i] = newValue;
270                    }
271    
272                    preferences.setValues(key, newValues);
273            }
274    
275            protected void updateResourcePermission(
276                            long companyId, String oldClassName, String newClassName,
277                            long oldPrimKey, long newPrimKey)
278                    throws Exception {
279    
280                    StringBundler sb = new StringBundler(11);
281    
282                    sb.append("update ResourcePermission set name = '");
283                    sb.append(newClassName);
284                    sb.append("', primKey = '");
285                    sb.append(newPrimKey);
286                    sb.append("' where companyId = ");
287                    sb.append(companyId);
288                    sb.append(" and name = '");
289                    sb.append(oldClassName);
290                    sb.append("' and primKey = '");
291                    sb.append(oldPrimKey);
292                    sb.append("'");
293    
294                    runSQL(sb.toString());
295            }
296    
297            protected long updateStructure(String structureId) throws Exception {
298                    Connection con = null;
299                    PreparedStatement ps = null;
300                    ResultSet rs = null;
301    
302                    try {
303                            con = DataAccess.getUpgradeOptimizedConnection();
304    
305                            ps = con.prepareStatement(
306                                    "select * from JournalStructure where structureId = " +
307                                            structureId);
308    
309                            rs = ps.executeQuery();
310    
311                            if (rs.next()) {
312                                    String uuid_ = rs.getString("uuid_");
313                                    long id_ = rs.getLong("id_");
314                                    long groupId = rs.getLong("groupId");
315                                    long companyId = rs.getLong("companyId");
316                                    long userId = rs.getLong("userId");
317                                    String userName = rs.getString("userName");
318                                    Date createDate = rs.getDate("createDate");
319                                    Date modifiedDate = rs.getDate("modifiedDate");
320                                    String parentStructureId = rs.getString("parentStructureId");
321                                    String name = rs.getString("name");
322                                    String description = rs.getString("description");
323                                    String xsd = rs.getString("xsd");
324    
325                                    Long ddmStructureId = _ddmStructureIds.get(
326                                            groupId + "#" + structureId);
327    
328                                    if (ddmStructureId != null) {
329                                            return ddmStructureId;
330                                    }
331    
332                                    ddmStructureId = increment();
333    
334                                    addDDMStructure(
335                                            uuid_, ddmStructureId, groupId, companyId, userId, userName,
336                                            createDate, modifiedDate, parentStructureId, structureId,
337                                            name, description, xsd);
338    
339                                    updateResourcePermission(
340                                            companyId, JournalStructure.class.getName(),
341                                            DDMStructure.class.getName(), id_, ddmStructureId);
342    
343                                    _ddmStructureIds.put(
344                                            groupId + "#" + structureId, ddmStructureId);
345                            }
346    
347                            return 0;
348                    }
349                    finally {
350                            DataAccess.cleanUp(con, ps, rs);
351                    }
352            }
353    
354            protected void updateStructures() throws Exception {
355                    Connection con = null;
356                    PreparedStatement ps = null;
357                    ResultSet rs = null;
358    
359                    try {
360                            con = DataAccess.getUpgradeOptimizedConnection();
361    
362                            ps = con.prepareStatement("select * from JournalStructure");
363    
364                            rs = ps.executeQuery();
365    
366                            while (rs.next()) {
367                                    String uuid_ = rs.getString("uuid_");
368                                    long id_ = rs.getLong("id_");
369                                    long groupId = rs.getLong("groupId");
370                                    long companyId = rs.getLong("companyId");
371                                    long userId = rs.getLong("userId");
372                                    String userName = rs.getString("userName");
373                                    Date createDate = rs.getDate("createDate");
374                                    Date modifiedDate = rs.getDate("modifiedDate");
375                                    String structureId = rs.getString("structureId");
376                                    String parentStructureId = rs.getString("parentStructureId");
377                                    String name = rs.getString("name");
378                                    String description = rs.getString("description");
379                                    String xsd = rs.getString("xsd");
380    
381                                    long ddmStructureId = increment();
382    
383                                    addDDMStructure(
384                                            uuid_, ddmStructureId, groupId, companyId, userId, userName,
385                                            createDate, modifiedDate, parentStructureId, structureId,
386                                            name, description, xsd);
387    
388                                    updateResourcePermission(
389                                            companyId, JournalStructure.class.getName(),
390                                            DDMStructure.class.getName(), id_, ddmStructureId);
391    
392                                    _ddmStructureIds.put(
393                                            groupId + "#" + structureId, ddmStructureId);
394                                    _ddmStructurePKs.put(id_, ddmStructureId);
395                            }
396                    }
397                    finally {
398                            DataAccess.cleanUp(con, ps, rs);
399                    }
400    
401                    runSQL("drop table JournalStructure");
402            }
403    
404            protected void updateTemplates() throws Exception {
405                    Connection con = null;
406                    PreparedStatement ps = null;
407                    ResultSet rs = null;
408    
409                    try {
410                            con = DataAccess.getUpgradeOptimizedConnection();
411    
412                            ps = con.prepareStatement("select * from JournalTemplate");
413    
414                            rs = ps.executeQuery();
415    
416                            while (rs.next()) {
417                                    String uuid_ = rs.getString("uuid_");
418                                    long id_ = rs.getLong("id_");
419                                    long groupId = rs.getLong("groupId");
420                                    long companyId = rs.getLong("companyId");
421                                    long userId = rs.getLong("userId");
422                                    String userName = rs.getString("userName");
423                                    Date createDate = rs.getDate("createDate");
424                                    Date modifiedDate = rs.getDate("modifiedDate");
425                                    String templateId = rs.getString("templateId");
426                                    String structureId = rs.getString("structureId");
427                                    String name = rs.getString("name");
428                                    String description = rs.getString("description");
429                                    String language = rs.getString("langType");
430                                    String script = rs.getString("xsl");
431                                    boolean cacheable = rs.getBoolean("cacheable");
432                                    boolean smallImage = rs.getBoolean("smallImage");
433                                    long smallImageId = rs.getLong("smallImageId");
434                                    String smallImageURL = rs.getString("smallImageURL");
435    
436                                    long ddmTemplateId = increment();
437    
438                                    long classNameId = PortalUtil.getClassNameId(
439                                            DDMStructure.class.getName());
440    
441                                    long classPK = getDDMStructureId(groupId, structureId);
442    
443                                    addDDMTemplate(
444                                            uuid_, ddmTemplateId, groupId, companyId, userId, userName,
445                                            createDate, modifiedDate, classNameId, classPK, templateId,
446                                            name, description,
447                                            DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY,
448                                            DDMTemplateConstants.TEMPLATE_MODE_CREATE, language, script,
449                                            cacheable, smallImage, smallImageId, smallImageURL);
450    
451                                    updateResourcePermission(
452                                            companyId, JournalTemplate.class.getName(),
453                                            DDMTemplate.class.getName(), id_, ddmTemplateId);
454                            }
455                    }
456                    finally {
457                            DataAccess.cleanUp(con, ps, rs);
458                    }
459    
460                    runSQL("drop table JournalTemplate");
461            }
462    
463            @Override
464            protected String upgradePreferences(
465                            long companyId, long ownerId, int ownerType, long plid,
466                            String portletId, String xml)
467                    throws Exception {
468    
469                    PortletPreferences preferences = PortletPreferencesFactoryUtil.fromXML(
470                            companyId, ownerId, ownerType, plid, portletId, xml);
471    
472                    if (portletId.startsWith(PortletKeys.ASSET_PUBLISHER)) {
473                            updatePreferencesClassPKs(
474                                    preferences, "anyClassTypeJournalArticleAssetRendererFactory");
475                            updatePreferencesClassPKs(preferences, "classTypeIds");
476                            updatePreferencesClassPKs(
477                                    preferences, "classTypeIdsJournalArticleAssetRendererFactory");
478                    }
479                    else if (portletId.startsWith(PortletKeys.JOURNAL_CONTENT)) {
480                            String templateId = preferences.getValue(
481                                    "templateId", StringPool.BLANK);
482    
483                            if (Validator.isNotNull(templateId)) {
484                                    preferences.reset("templateId");
485    
486                                    preferences.setValue("ddmTemplateKey", templateId);
487                            }
488                    }
489                    else if (portletId.startsWith(PortletKeys.JOURNAL_CONTENT_LIST)) {
490                            String structureId = preferences.getValue(
491                                    "structureId", StringPool.BLANK);
492    
493                            if (Validator.isNotNull(structureId)) {
494                                    preferences.reset("structureId");
495    
496                                    preferences.setValue("ddmStructureKey", structureId);
497                            }
498                    }
499    
500                    return PortletPreferencesFactoryUtil.toXML(preferences);
501            }
502    
503            private static Log _log = LogFactoryUtil.getLog(UpgradeJournal.class);
504    
505            private Map<String, Long> _ddmStructureIds = new HashMap<String, Long>();
506            private Map<Long, Long> _ddmStructurePKs = new HashMap<Long, Long>();
507    
508    }