1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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 elName = el.attributeValue("name", StringPool.BLANK);
129             String elType = el.attributeValue("type", StringPool.BLANK);
130             String elLanguage = "";
131 
132             if (dynamicContent != null) {
133                 elLanguage = dynamicContent.attributeValue(
134                     "language-id", StringPool.BLANK);
135 
136                 if (!elLanguage.equals(StringPool.BLANK)) {
137                     elLanguage = "_" + elLanguage;
138                 }
139             }
140 
141             if (elType.equals("image") || elType.equals("text")) {
142                 String oldImageId = dynamicContent.getText();
143 
144                 if (oldImageId.startsWith(_IMG_ID_PATH) ||
145                     oldImageId.startsWith("@portal_url@" + _IMG_ID_PATH) ||
146                     oldImageId.startsWith(
147                         "http://@portal_url@" + _IMG_ID_PATH) ||
148                     oldImageId.startsWith(
149                         "https://@portal_url@" + _IMG_ID_PATH)) {
150 
151                     int pos = oldImageId.indexOf(_IMG_ID_PATH);
152 
153                     String preOldImageId = oldImageId.substring(0, pos);
154 
155                     oldImageId = oldImageId.substring(
156                         pos + _IMG_ID_PATH.length(), oldImageId.length());
157 
158                     String newImageId = getNewImageId(oldCompanyId, oldImageId);
159 
160                     dynamicContent.setText(
161                         preOldImageId + _IMG_ID_PATH + newImageId);
162 
163                     if (elType.equals("image")) {
164                         dynamicContent.addAttribute("id", newImageId);
165 
166                         long articleImageId = GetterUtil.getLong(newImageId);
167 
168                         JournalArticleImageLocalServiceUtil.addArticleImageId(
169                             articleImageId, groupId, articleId, version, elName,
170                             elLanguage);
171                     }
172                 }
173             }
174 
175             format(oldCompanyId, newCompanyId, groupId, articleId, version, el);
176         }
177     }
178 
179     protected String getNewImageId(String oldCompanyId, String oldImageId)
180         throws Exception {
181 
182         int pos = oldImageId.lastIndexOf("&version=");
183 
184         oldImageId =
185             oldImageId.substring(0, pos) + "." +
186                 oldImageId.substring(pos + 9, oldImageId.length());
187 
188         String newImageId = oldCompanyId + ".journal.article." + oldImageId;
189 
190         return String.valueOf(_imageIdMapper.getNewValue(newImageId));
191     }
192 
193     protected String replaceIds(String content) throws Exception {
194         ValueMapper dlFolderIdMapper =
195             AvailableMappersUtil.getDLFolderIdMapper();
196 
197         content = IdReplacer.replaceLongIds(
198             content, "/document_library/get_file?folderId=", dlFolderIdMapper);
199         content = IdReplacer.replaceLongIds(
200             content,
201             "_20_struts_action=%2Fdocument_library%2Fget_file&_20_folderId=",
202             dlFolderIdMapper);
203         content = IdReplacer.replaceLongIds(
204             content,
205             "_20_struts_action=%2Fdocument_library%2Fget_file&amp;" +
206                 "_20_folderId=",
207             dlFolderIdMapper);
208 
209         ValueMapper imageIdMapper = AvailableMappersUtil.getImageIdMapper();
210 
211         ValueMapper newImageIdMapper = ValueMapperFactoryUtil.getValueMapper();
212 
213         ValueMapper igImageIdMapper = AvailableMappersUtil.getIGImageIdMapper();
214 
215         Iterator<Object> itr = igImageIdMapper.iterator();
216 
217         while (itr.hasNext()) {
218             String oldValue = (String)itr.next();
219 
220             PKParser oldValuePKParser = new PKParser(oldValue);
221 
222             String companyId = oldValuePKParser.getString("companyId");
223             String oldIGImageId = oldValuePKParser.getString("imageId");
224 
225             String oldImageId =
226                 companyId + ".image_gallery." + oldIGImageId + ".large";
227 
228             Long newImageId = (Long)imageIdMapper.getNewValue(oldImageId);
229 
230             newImageIdMapper.mapValue(
231                 new Long(GetterUtil.getLong(oldIGImageId)), newImageId);
232         }
233 
234         content = IdReplacer.replaceLongIds(
235             content, "/image_gallery?img_id=", newImageIdMapper);
236 
237         return content;
238     }
239 
240     private static final String _IMG_ID_PATH =
241         "/image/journal/article?img_id=";
242 
243     private static Log _log = LogFactoryUtil.getLog(
244         JournalArticleContentUpgradeColumnImpl.class);
245 
246     private UpgradeColumn _companyIdColumn;
247     private UpgradeColumn _groupIdColumn;
248     private UpgradeColumn _articleIdColumn;
249     private UpgradeColumn _versionColumn;
250     private UpgradeColumn _structureIdColumn;
251     private ValueMapper _imageIdMapper;
252 
253 }