1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.v5_2_0;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.kernel.xml.Document;
30  import com.liferay.portal.kernel.xml.Element;
31  import com.liferay.portal.kernel.xml.SAXReaderUtil;
32  import com.liferay.portlet.journal.util.JournalUtil;
33  import com.liferay.util.PwdGenerator;
34  
35  import java.sql.Connection;
36  import java.sql.PreparedStatement;
37  import java.sql.ResultSet;
38  
39  import java.util.Iterator;
40  
41  /**
42   * <a href="UpgradeJournal.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class UpgradeJournal extends UpgradeProcess {
47  
48      protected void doUpgrade() throws Exception {
49          Connection con = null;
50          PreparedStatement ps = null;
51          ResultSet rs = null;
52  
53          try {
54              con = DataAccess.getConnection();
55  
56              ps = con.prepareStatement(
57                  "select id_, content, structureId from JournalArticle");
58  
59              rs = ps.executeQuery();
60  
61              while (rs.next()) {
62                  long id = rs.getLong("id_");
63                  String content = GetterUtil.getString(rs.getString("content"));
64                  String structureId = rs.getString("structureId");
65  
66                  if (Validator.isNull(structureId)) {
67                      continue;
68                  }
69  
70                  String newContent = addDynamicElementInstanceId(content);
71  
72                  if (content.equals(newContent)) {
73                      continue;
74                  }
75  
76                  updateJournalArticleContent(id, newContent);
77              }
78          }
79          finally {
80              DataAccess.cleanUp(con, ps, rs);
81          }
82  
83          deleteJournalArticleImages();
84      }
85  
86      protected String addDynamicElementInstanceId(String content)
87          throws Exception {
88  
89          Document doc = SAXReaderUtil.read(content);
90  
91          Element root = doc.getRootElement();
92  
93          addDynamicElementInstanceId(root);
94  
95          return JournalUtil.formatXML(doc);
96      }
97  
98      protected void addDynamicElementInstanceId(Element root) throws Exception {
99          Iterator<Element> itr = root.elements().iterator();
100 
101         while (itr.hasNext()) {
102             Element element = itr.next();
103 
104             if (!element.getName().equals("dynamic-element")) {
105                 continue;
106             }
107 
108             String instanceId = element.attributeValue("instance-id");
109             String type = element.attributeValue("type");
110 
111             if (Validator.isNull(instanceId)) {
112                 instanceId = PwdGenerator.getPassword();
113 
114                 element.addAttribute("instance-id", instanceId);
115 
116                 if (type.equals("image")) {
117                     updateJournalArticleImageInstanceId(element, instanceId);
118                 }
119             }
120 
121             addDynamicElementInstanceId(element);
122         }
123     }
124 
125     protected void deleteJournalArticleImages() throws Exception {
126         runSQL(
127             "delete from JournalArticleImage where elInstanceId is null or " +
128                 "elInstanceId = ''");
129     }
130 
131     protected void updateJournalArticleContent(long id, String content)
132         throws Exception {
133 
134         Connection con = null;
135         PreparedStatement ps = null;
136 
137         try {
138             con = DataAccess.getConnection();
139 
140             ps = con.prepareStatement(
141                 "update JournalArticle set content = ? where id_ = ?");
142 
143             ps.setString(1, content);
144             ps.setLong(2, id);
145 
146             ps.executeUpdate();
147         }
148         finally {
149             DataAccess.cleanUp(con, ps);
150         }
151     }
152 
153     protected void updateJournalArticleImageInstanceId(
154             Element parentElement, String instanceId)
155         throws Exception {
156 
157         Iterator<Element> itr = parentElement.elements(
158             "dynamic-content").iterator();
159 
160         while (itr.hasNext()) {
161             Element element = itr.next();
162 
163             long articleImageId = GetterUtil.getLong(
164                 element.attributeValue("id"));
165 
166             if (articleImageId <= 0) {
167                 continue;
168             }
169 
170             runSQL(
171                 "update JournalArticleImage set elInstanceId = '" + instanceId +
172                     "' where articleImageId = " + articleImageId);
173         }
174     }
175 
176 }