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.v5_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.Validator;
021    import com.liferay.portal.kernel.xml.Document;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.kernel.xml.SAXReaderUtil;
024    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
025    import com.liferay.util.PwdGenerator;
026    
027    import java.sql.Connection;
028    import java.sql.PreparedStatement;
029    import java.sql.ResultSet;
030    
031    import java.util.Iterator;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class UpgradeJournal extends UpgradeProcess {
037    
038            protected void addDynamicElementInstanceId(Element root) throws Exception {
039                    Iterator<Element> itr = root.elements().iterator();
040    
041                    while (itr.hasNext()) {
042                            Element element = itr.next();
043    
044                            if (!element.getName().equals("dynamic-element")) {
045                                    continue;
046                            }
047    
048                            String instanceId = element.attributeValue("instance-id");
049                            String type = element.attributeValue("type");
050    
051                            if (Validator.isNull(instanceId)) {
052                                    instanceId = PwdGenerator.getPassword();
053    
054                                    element.addAttribute("instance-id", instanceId);
055    
056                                    if (type.equals("image")) {
057                                            updateJournalArticleImageInstanceId(element, instanceId);
058                                    }
059                            }
060    
061                            addDynamicElementInstanceId(element);
062                    }
063            }
064    
065            protected String addDynamicElementInstanceId(String content)
066                    throws Exception {
067    
068                    Document doc = SAXReaderUtil.read(content);
069    
070                    Element root = doc.getRootElement();
071    
072                    addDynamicElementInstanceId(root);
073    
074                    return DDMXMLUtil.formatXML(doc);
075            }
076    
077            protected void deleteJournalArticleImages() throws Exception {
078                    runSQL(
079                            "delete from JournalArticleImage where elInstanceId is null or " +
080                                    "elInstanceId = ''");
081            }
082    
083            @Override
084            protected void doUpgrade() throws Exception {
085                    Connection con = null;
086                    PreparedStatement ps = null;
087                    ResultSet rs = null;
088    
089                    try {
090                            con = DataAccess.getConnection();
091    
092                            ps = con.prepareStatement(
093                                    "select id_, content, structureId from JournalArticle");
094    
095                            rs = ps.executeQuery();
096    
097                            while (rs.next()) {
098                                    long id = rs.getLong("id_");
099                                    String content = GetterUtil.getString(rs.getString("content"));
100                                    String structureId = rs.getString("structureId");
101    
102                                    if (Validator.isNull(structureId)) {
103                                            continue;
104                                    }
105    
106                                    String newContent = addDynamicElementInstanceId(content);
107    
108                                    if (content.equals(newContent)) {
109                                            continue;
110                                    }
111    
112                                    updateJournalArticleContent(id, newContent);
113                            }
114                    }
115                    finally {
116                            DataAccess.cleanUp(con, ps, rs);
117                    }
118    
119                    deleteJournalArticleImages();
120            }
121    
122            protected void updateJournalArticleContent(long id, String content)
123                    throws Exception {
124    
125                    Connection con = null;
126                    PreparedStatement ps = null;
127    
128                    try {
129                            con = DataAccess.getConnection();
130    
131                            ps = con.prepareStatement(
132                                    "update JournalArticle set content = ? where id_ = ?");
133    
134                            ps.setString(1, content);
135                            ps.setLong(2, id);
136    
137                            ps.executeUpdate();
138                    }
139                    finally {
140                            DataAccess.cleanUp(con, ps);
141                    }
142            }
143    
144            protected void updateJournalArticleImageInstanceId(
145                            Element parentElement, String instanceId)
146                    throws Exception {
147    
148                    Iterator<Element> itr = parentElement.elements(
149                            "dynamic-content").iterator();
150    
151                    while (itr.hasNext()) {
152                            Element element = itr.next();
153    
154                            long articleImageId = GetterUtil.getLong(
155                                    element.attributeValue("id"));
156    
157                            if (articleImageId <= 0) {
158                                    continue;
159                            }
160    
161                            runSQL(
162                                    "update JournalArticleImage set elInstanceId = '" + instanceId +
163                                            "' where articleImageId = " + articleImageId);
164                    }
165            }
166    
167    }