1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upgrade.v5_2_0;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.kernel.xml.Document;
22  import com.liferay.portal.kernel.xml.Element;
23  import com.liferay.portal.kernel.xml.SAXReaderUtil;
24  import com.liferay.portlet.journal.util.JournalUtil;
25  import com.liferay.util.PwdGenerator;
26  
27  import java.sql.Connection;
28  import java.sql.PreparedStatement;
29  import java.sql.ResultSet;
30  
31  import java.util.Iterator;
32  
33  /**
34   * <a href="UpgradeJournal.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class UpgradeJournal extends UpgradeProcess {
39  
40      protected void doUpgrade() throws Exception {
41          Connection con = null;
42          PreparedStatement ps = null;
43          ResultSet rs = null;
44  
45          try {
46              con = DataAccess.getConnection();
47  
48              ps = con.prepareStatement(
49                  "select id_, content, structureId from JournalArticle");
50  
51              rs = ps.executeQuery();
52  
53              while (rs.next()) {
54                  long id = rs.getLong("id_");
55                  String content = GetterUtil.getString(rs.getString("content"));
56                  String structureId = rs.getString("structureId");
57  
58                  if (Validator.isNull(structureId)) {
59                      continue;
60                  }
61  
62                  String newContent = addDynamicElementInstanceId(content);
63  
64                  if (content.equals(newContent)) {
65                      continue;
66                  }
67  
68                  updateJournalArticleContent(id, newContent);
69              }
70          }
71          finally {
72              DataAccess.cleanUp(con, ps, rs);
73          }
74  
75          deleteJournalArticleImages();
76      }
77  
78      protected String addDynamicElementInstanceId(String content)
79          throws Exception {
80  
81          Document doc = SAXReaderUtil.read(content);
82  
83          Element root = doc.getRootElement();
84  
85          addDynamicElementInstanceId(root);
86  
87          return JournalUtil.formatXML(doc);
88      }
89  
90      protected void addDynamicElementInstanceId(Element root) throws Exception {
91          Iterator<Element> itr = root.elements().iterator();
92  
93          while (itr.hasNext()) {
94              Element element = itr.next();
95  
96              if (!element.getName().equals("dynamic-element")) {
97                  continue;
98              }
99  
100             String instanceId = element.attributeValue("instance-id");
101             String type = element.attributeValue("type");
102 
103             if (Validator.isNull(instanceId)) {
104                 instanceId = PwdGenerator.getPassword();
105 
106                 element.addAttribute("instance-id", instanceId);
107 
108                 if (type.equals("image")) {
109                     updateJournalArticleImageInstanceId(element, instanceId);
110                 }
111             }
112 
113             addDynamicElementInstanceId(element);
114         }
115     }
116 
117     protected void deleteJournalArticleImages() throws Exception {
118         runSQL(
119             "delete from JournalArticleImage where elInstanceId is null or " +
120                 "elInstanceId = ''");
121     }
122 
123     protected void updateJournalArticleContent(long id, String content)
124         throws Exception {
125 
126         Connection con = null;
127         PreparedStatement ps = null;
128 
129         try {
130             con = DataAccess.getConnection();
131 
132             ps = con.prepareStatement(
133                 "update JournalArticle set content = ? where id_ = ?");
134 
135             ps.setString(1, content);
136             ps.setLong(2, id);
137 
138             ps.executeUpdate();
139         }
140         finally {
141             DataAccess.cleanUp(con, ps);
142         }
143     }
144 
145     protected void updateJournalArticleImageInstanceId(
146             Element parentElement, String instanceId)
147         throws Exception {
148 
149         Iterator<Element> itr = parentElement.elements(
150             "dynamic-content").iterator();
151 
152         while (itr.hasNext()) {
153             Element element = itr.next();
154 
155             long articleImageId = GetterUtil.getLong(
156                 element.attributeValue("id"));
157 
158             if (articleImageId <= 0) {
159                 continue;
160             }
161 
162             runSQL(
163                 "update JournalArticleImage set elInstanceId = '" + instanceId +
164                     "' where articleImageId = " + articleImageId);
165         }
166     }
167 
168 }