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