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