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.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.kernel.xml.Document;
31  import com.liferay.portal.kernel.xml.Element;
32  import com.liferay.portal.kernel.xml.SAXReaderUtil;
33  import com.liferay.portal.upgrade.UpgradeException;
34  import com.liferay.portal.upgrade.UpgradeProcess;
35  import com.liferay.portlet.journal.model.JournalArticle;
36  import com.liferay.portlet.journal.model.JournalArticleImage;
37  import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
38  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
39  import com.liferay.portlet.journal.util.JournalUtil;
40  import com.liferay.util.PwdGenerator;
41  
42  import java.sql.Connection;
43  import java.sql.PreparedStatement;
44  
45  import java.util.Iterator;
46  import java.util.List;
47  
48  /**
49   * <a href="UpgradeJournal.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class UpgradeJournal extends UpgradeProcess {
55  
56      public void upgrade() throws UpgradeException {
57          _log.info("Upgrading");
58  
59          try {
60              doUpgrade();
61          }
62          catch (Exception e) {
63              throw new UpgradeException(e);
64          }
65      }
66  
67      protected void doUpgrade() throws Exception {
68          List<JournalArticle> articles =
69              JournalArticleLocalServiceUtil.getArticles();
70  
71          for (JournalArticle article : articles) {
72              String content = GetterUtil.getString(article.getContent());
73  
74              if (Validator.isNotNull(article.getStructureId())) {
75                  String newContent = addDynamicElementInstanceId(content);
76  
77                  if (!content.equals(newContent)) {
78                      JournalArticleLocalServiceUtil.updateContent(
79                          article.getGroupId(), article.getArticleId(),
80                          article.getVersion(), newContent);
81                  }
82              }
83          }
84  
85          deleteJournalArticleImages();
86      }
87  
88      protected String addDynamicElementInstanceId(String content)
89          throws Exception {
90  
91          Document doc = SAXReaderUtil.read(content);
92  
93          Element root = doc.getRootElement();
94  
95          addDynamicElementInstanceId(root);
96  
97          return JournalUtil.formatXML(doc);
98      }
99  
100     protected void addDynamicElementInstanceId(Element root) throws Exception {
101         Iterator<Element> itr = root.elements().iterator();
102 
103         while (itr.hasNext()) {
104             Element element = itr.next();
105 
106             if (!element.getName().equals("dynamic-element")) {
107                 continue;
108             }
109 
110             String instanceId = element.attributeValue("instance-id");
111             String type = element.attributeValue("type");
112 
113             if (Validator.isNull(instanceId)) {
114                 instanceId = PwdGenerator.getPassword();
115 
116                 element.addAttribute("instance-id", instanceId);
117 
118                 if (type.equals("image")) {
119                     updateJournalArticleImageInstanceId(element, instanceId);
120                 }
121             }
122 
123             addDynamicElementInstanceId(element);
124         }
125     }
126 
127     protected void deleteJournalArticleImages() throws Exception {
128         Connection con = null;
129         PreparedStatement ps = null;
130 
131         try {
132             con = DataAccess.getConnection();
133 
134             ps = con.prepareStatement(
135                 "delete from JournalArticleImage where elInstanceId is null " +
136                     "or elInstanceId = ''");
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             JournalArticleImage articleImage =
163                 JournalArticleImageLocalServiceUtil.getArticleImage(
164                     articleImageId);
165 
166             articleImage.setElInstanceId(instanceId);
167 
168             JournalArticleImageLocalServiceUtil.updateJournalArticleImage(
169                 articleImage);
170         }
171     }
172 
173     private static Log _log = LogFactoryUtil.getLog(UpgradeJournal.class);
174 
175 }