1
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
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 }