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