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.v4_3_0.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
20  import com.liferay.portal.kernel.upgrade.util.IdReplacer;
21  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
22  import com.liferay.portal.kernel.upgrade.util.ValueMapper;
23  import com.liferay.portal.kernel.upgrade.util.ValueMapperFactoryUtil;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
31  import com.liferay.portal.upgrade.util.Table;
32  import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
33  import com.liferay.portlet.journal.util.JournalUtil;
34  import com.liferay.util.PKParser;
35  
36  import java.util.Iterator;
37  
38  /**
39   * <a href="JournalArticleContentUpgradeColumnImpl.java.html"><b><i>View Source
40   * </i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class JournalArticleContentUpgradeColumnImpl
45      extends BaseUpgradeColumnImpl {
46  
47      public JournalArticleContentUpgradeColumnImpl(
48          UpgradeColumn companyIdColumn, UpgradeColumn groupIdColumn,
49          UpgradeColumn articleIdColumn, UpgradeColumn versionColumn,
50          UpgradeColumn structureIdColumn, ValueMapper imageIdMapper) {
51  
52          super("content");
53  
54          _companyIdColumn = companyIdColumn;
55          _groupIdColumn = groupIdColumn;
56          _articleIdColumn = articleIdColumn;
57          _versionColumn = versionColumn;
58          _structureIdColumn = structureIdColumn;
59          _imageIdMapper = imageIdMapper;
60      }
61  
62      public Object getNewValue(Object oldValue) throws Exception {
63          String content = (String)oldValue;
64  
65          content = StringUtil.replace(
66              content, Table.SAFE_CHARS[1], Table.SAFE_CHARS[0]);
67  
68          /*if (content.indexOf("\\n") != -1) {
69              content = StringUtil.replace(
70                  content,
71                  new String[] {"\\n", "\\r"},
72                  new String[] {"\n", "\r"});
73          }*/
74  
75          String structureId = (String)_structureIdColumn.getOldValue();
76  
77          if (Validator.isNotNull(structureId)) {
78              content = formatContent(content);
79          }
80  
81          content = replaceIds(content);
82  
83          content = StringUtil.replace(
84              content, Table.SAFE_CHARS[0], Table.SAFE_CHARS[1]);
85  
86          return content;
87      }
88  
89      protected String formatContent(String content) throws Exception {
90          String oldCompanyId = (String)_companyIdColumn.getOldValue();
91          Long newCompanyId = (Long)_companyIdColumn.getNewValue();
92          Long groupId = (Long)_groupIdColumn.getNewValue();
93          String articleId = (String)_articleIdColumn.getNewValue();
94          Double version = (Double)_versionColumn.getNewValue();
95  
96          try {
97              Document doc = SAXReaderUtil.read(content);
98  
99              Element root = doc.getRootElement();
100 
101             format(
102                 oldCompanyId, newCompanyId.longValue(), groupId.longValue(),
103                 articleId, version.doubleValue(), root);
104 
105             content = JournalUtil.formatXML(doc);
106         }
107         catch (Exception e) {
108             _log.error(
109                 "Unable to format content for {articleId=" + articleId +
110                     ",version=" + version + "}: " + e.getMessage());
111         }
112 
113         return content;
114     }
115 
116     protected void format(
117             String oldCompanyId, long newCompanyId, long groupId,
118             String articleId, double version, Element root)
119         throws Exception {
120 
121         Iterator<Element> itr = root.elements().iterator();
122 
123         while (itr.hasNext()) {
124             Element el = itr.next();
125 
126             Element dynamicContent = el.element("dynamic-content");
127 
128             String elInstanceId = StringPool.BLANK;
129             String elName = el.attributeValue("name", StringPool.BLANK);
130             String elType = el.attributeValue("type", StringPool.BLANK);
131             String elLanguage = StringPool.BLANK;
132 
133             if (dynamicContent != null) {
134                 elLanguage = dynamicContent.attributeValue(
135                     "language-id", StringPool.BLANK);
136 
137                 if (!elLanguage.equals(StringPool.BLANK)) {
138                     elLanguage = "_" + elLanguage;
139                 }
140             }
141 
142             if (elType.equals("image") || elType.equals("text")) {
143                 String oldImageId = dynamicContent.getText();
144 
145                 if (oldImageId.startsWith(_IMG_ID_PATH) ||
146                     oldImageId.startsWith("@portal_url@" + _IMG_ID_PATH) ||
147                     oldImageId.startsWith(
148                         "http://@portal_url@" + _IMG_ID_PATH) ||
149                     oldImageId.startsWith(
150                         "https://@portal_url@" + _IMG_ID_PATH)) {
151 
152                     int pos = oldImageId.indexOf(_IMG_ID_PATH);
153 
154                     String preOldImageId = oldImageId.substring(0, pos);
155 
156                     oldImageId = oldImageId.substring(
157                         pos + _IMG_ID_PATH.length(), oldImageId.length());
158 
159                     String newImageId = getNewImageId(oldCompanyId, oldImageId);
160 
161                     dynamicContent.setText(
162                         preOldImageId + _IMG_ID_PATH + newImageId);
163 
164                     if (elType.equals("image")) {
165                         dynamicContent.addAttribute("id", newImageId);
166 
167                         long articleImageId = GetterUtil.getLong(newImageId);
168 
169                         JournalArticleImageLocalServiceUtil.addArticleImageId(
170                             articleImageId, groupId, articleId, version,
171                             elInstanceId, elName, elLanguage);
172                     }
173                 }
174             }
175 
176             format(oldCompanyId, newCompanyId, groupId, articleId, version, el);
177         }
178     }
179 
180     protected String getNewImageId(String oldCompanyId, String oldImageId)
181         throws Exception {
182 
183         int pos = oldImageId.lastIndexOf("&version=");
184 
185         oldImageId =
186             oldImageId.substring(0, pos) + "." +
187                 oldImageId.substring(pos + 9, oldImageId.length());
188 
189         String newImageId = oldCompanyId + ".journal.article." + oldImageId;
190 
191         return String.valueOf(_imageIdMapper.getNewValue(newImageId));
192     }
193 
194     protected String replaceIds(String content) throws Exception {
195         ValueMapper dlFolderIdMapper =
196             AvailableMappersUtil.getDLFolderIdMapper();
197 
198         content = IdReplacer.replaceLongIds(
199             content, "/document_library/get_file?folderId=", dlFolderIdMapper);
200         content = IdReplacer.replaceLongIds(
201             content,
202             "_20_struts_action=%2Fdocument_library%2Fget_file&_20_folderId=",
203             dlFolderIdMapper);
204         content = IdReplacer.replaceLongIds(
205             content,
206             "_20_struts_action=%2Fdocument_library%2Fget_file&amp;" +
207                 "_20_folderId=",
208             dlFolderIdMapper);
209 
210         ValueMapper imageIdMapper = AvailableMappersUtil.getImageIdMapper();
211 
212         ValueMapper newImageIdMapper = ValueMapperFactoryUtil.getValueMapper();
213 
214         ValueMapper igImageIdMapper = AvailableMappersUtil.getIGImageIdMapper();
215 
216         Iterator<Object> itr = igImageIdMapper.iterator();
217 
218         while (itr.hasNext()) {
219             String oldValue = (String)itr.next();
220 
221             PKParser oldValuePKParser = new PKParser(oldValue);
222 
223             String companyId = oldValuePKParser.getString("companyId");
224             String oldIGImageId = oldValuePKParser.getString("imageId");
225 
226             String oldImageId =
227                 companyId + ".image_gallery." + oldIGImageId + ".large";
228 
229             Long newImageId = (Long)imageIdMapper.getNewValue(oldImageId);
230 
231             newImageIdMapper.mapValue(
232                 new Long(GetterUtil.getLong(oldIGImageId)), newImageId);
233         }
234 
235         content = IdReplacer.replaceLongIds(
236             content, "/image_gallery?img_id=", newImageIdMapper);
237 
238         return content;
239     }
240 
241     private static final String _IMG_ID_PATH =
242         "/image/journal/article?img_id=";
243 
244     private static Log _log = LogFactoryUtil.getLog(
245         JournalArticleContentUpgradeColumnImpl.class);
246 
247     private UpgradeColumn _companyIdColumn;
248     private UpgradeColumn _groupIdColumn;
249     private UpgradeColumn _articleIdColumn;
250     private UpgradeColumn _versionColumn;
251     private UpgradeColumn _structureIdColumn;
252     private ValueMapper _imageIdMapper;
253 
254 }