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                  ps = con.prepareStatement(
77                      "update JournalArticle set content = ? where id_ = ?");
78  
79                  ps.setString(1, newContent);
80                  ps.setLong(2, id);
81  
82                  ps.executeUpdate();
83  
84                  ps.close();
85              }
86          }
87          finally {
88              DataAccess.cleanUp(con, ps, rs);
89          }
90  
91          deleteJournalArticleImages();
92      }
93  
94      protected String addDynamicElementInstanceId(String content)
95          throws Exception {
96  
97          Document doc = SAXReaderUtil.read(content);
98  
99          Element root = doc.getRootElement();
100 
101         addDynamicElementInstanceId(root);
102 
103         return JournalUtil.formatXML(doc);
104     }
105 
106     protected void addDynamicElementInstanceId(Element root) throws Exception {
107         Iterator<Element> itr = root.elements().iterator();
108 
109         while (itr.hasNext()) {
110             Element element = itr.next();
111 
112             if (!element.getName().equals("dynamic-element")) {
113                 continue;
114             }
115 
116             String instanceId = element.attributeValue("instance-id");
117             String type = element.attributeValue("type");
118 
119             if (Validator.isNull(instanceId)) {
120                 instanceId = PwdGenerator.getPassword();
121 
122                 element.addAttribute("instance-id", instanceId);
123 
124                 if (type.equals("image")) {
125                     updateJournalArticleImageInstanceId(element, instanceId);
126                 }
127             }
128 
129             addDynamicElementInstanceId(element);
130         }
131     }
132 
133     protected void deleteJournalArticleImages() throws Exception {
134         runSQL(
135             "delete from JournalArticleImage where elInstanceId is null or " +
136                 "elInstanceId = ''");
137     }
138 
139     protected void updateJournalArticleImageInstanceId(
140             Element parentElement, String instanceId)
141         throws Exception {
142 
143         Iterator<Element> itr = parentElement.elements(
144             "dynamic-content").iterator();
145 
146         while (itr.hasNext()) {
147             Element element = itr.next();
148 
149             long articleImageId = GetterUtil.getLong(
150                 element.attributeValue("id"));
151 
152             if (articleImageId <= 0) {
153                 continue;
154             }
155 
156             runSQL(
157                 "update JournalArticleImage set elInstanceId = '" + instanceId +
158                     "' where articleImageId = " + articleImageId);
159         }
160     }
161 
162 }