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