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