1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class JournalArticleContentUpgradeColumnImpl
53      extends BaseUpgradeColumnImpl {
54  
55      public JournalArticleContentUpgradeColumnImpl(
56          UpgradeColumn companyIdColumn, UpgradeColumn groupIdColumn,
57          UpgradeColumn articleIdColumn, UpgradeColumn versionColumn,
58          UpgradeColumn structureIdColumn, ValueMapper imageIdMapper) {
59  
60          super("content");
61  
62          _companyIdColumn = companyIdColumn;
63          _groupIdColumn = groupIdColumn;
64          _articleIdColumn = articleIdColumn;
65          _versionColumn = versionColumn;
66          _structureIdColumn = structureIdColumn;
67          _imageIdMapper = imageIdMapper;
68      }
69  
70      public Object getNewValue(Object oldValue) throws Exception {
71          String content = (String)oldValue;
72  
73          content = StringUtil.replace(
74              content, Table.SAFE_CHARS[1], Table.SAFE_CHARS[0]);
75  
76          /*if (content.indexOf("\\n") != -1) {
77              content = StringUtil.replace(
78                  content,
79                  new String[] {"\\n", "\\r"},
80                  new String[] {"\n", "\r"});
81          }*/
82  
83          String structureId = (String)_structureIdColumn.getOldValue();
84  
85          if (Validator.isNotNull(structureId)) {
86              content = formatContent(content);
87          }
88  
89          content = replaceIds(content);
90  
91          content = StringUtil.replace(
92              content, Table.SAFE_CHARS[0], Table.SAFE_CHARS[1]);
93  
94          return content;
95      }
96  
97      protected String formatContent(String content) throws Exception {
98          String oldCompanyId = (String)_companyIdColumn.getOldValue();
99          Long newCompanyId = (Long)_companyIdColumn.getNewValue();
100         Long groupId = (Long)_groupIdColumn.getNewValue();
101         String articleId = (String)_articleIdColumn.getNewValue();
102         Double version = (Double)_versionColumn.getNewValue();
103 
104         try {
105             Document doc = SAXReaderUtil.read(content);
106 
107             Element root = doc.getRootElement();
108 
109             format(
110                 oldCompanyId, newCompanyId.longValue(), groupId.longValue(),
111                 articleId, version.doubleValue(), root);
112 
113             content = JournalUtil.formatXML(doc);
114         }
115         catch (Exception e) {
116             _log.error(
117                 "Unable to format content for {articleId=" + articleId +
118                     ",version=" + version + "}: " + e.getMessage());
119         }
120 
121         return content;
122     }
123 
124     protected void format(
125             String oldCompanyId, long newCompanyId, long groupId,
126             String articleId, double version, Element root)
127         throws Exception {
128 
129         Iterator<Element> itr = root.elements().iterator();
130 
131         while (itr.hasNext()) {
132             Element el = itr.next();
133 
134             Element dynamicContent = el.element("dynamic-content");
135 
136             String elName = el.attributeValue("name", StringPool.BLANK);
137             String elType = el.attributeValue("type", StringPool.BLANK);
138             String elLanguage = "";
139 
140             if (dynamicContent != null) {
141                 elLanguage = dynamicContent.attributeValue(
142                     "language-id", StringPool.BLANK);
143 
144                 if (!elLanguage.equals(StringPool.BLANK)) {
145                     elLanguage = "_" + elLanguage;
146                 }
147             }
148 
149             if (elType.equals("image") || elType.equals("text")) {
150                 String oldImageId = dynamicContent.getText();
151 
152                 if (oldImageId.startsWith(_IMG_ID_PATH) ||
153                     oldImageId.startsWith("@portal_url@" + _IMG_ID_PATH) ||
154                     oldImageId.startsWith(
155                         "http://@portal_url@" + _IMG_ID_PATH) ||
156                     oldImageId.startsWith(
157                         "https://@portal_url@" + _IMG_ID_PATH)) {
158 
159                     int pos = oldImageId.indexOf(_IMG_ID_PATH);
160 
161                     String preOldImageId = oldImageId.substring(0, pos);
162 
163                     oldImageId = oldImageId.substring(
164                         pos + _IMG_ID_PATH.length(), oldImageId.length());
165 
166                     String newImageId = getNewImageId(oldCompanyId, oldImageId);
167 
168                     dynamicContent.setText(
169                         preOldImageId + _IMG_ID_PATH + newImageId);
170 
171                     if (elType.equals("image")) {
172                         dynamicContent.addAttribute("id", newImageId);
173 
174                         long articleImageId = GetterUtil.getLong(newImageId);
175 
176                         JournalArticleImageLocalServiceUtil.addArticleImageId(
177                             articleImageId, groupId, articleId, version, elName,
178                             elLanguage);
179                     }
180                 }
181             }
182 
183             format(oldCompanyId, newCompanyId, groupId, articleId, version, el);
184         }
185     }
186 
187     protected String getNewImageId(String oldCompanyId, String oldImageId)
188         throws Exception {
189 
190         int pos = oldImageId.lastIndexOf("&version=");
191 
192         oldImageId =
193             oldImageId.substring(0, pos) + "." +
194                 oldImageId.substring(pos + 9, oldImageId.length());
195 
196         String newImageId = oldCompanyId + ".journal.article." + oldImageId;
197 
198         return String.valueOf(_imageIdMapper.getNewValue(newImageId));
199     }
200 
201     protected String replaceIds(String content) throws Exception {
202         ValueMapper dlFolderIdMapper =
203             AvailableMappersUtil.getDLFolderIdMapper();
204 
205         content = IdReplacer.replaceLongIds(
206             content, "/document_library/get_file?folderId=", dlFolderIdMapper);
207         content = IdReplacer.replaceLongIds(
208             content,
209             "_20_struts_action=%2Fdocument_library%2Fget_file&_20_folderId=",
210             dlFolderIdMapper);
211         content = IdReplacer.replaceLongIds(
212             content,
213             "_20_struts_action=%2Fdocument_library%2Fget_file&amp;" +
214                 "_20_folderId=",
215             dlFolderIdMapper);
216 
217         ValueMapper imageIdMapper = AvailableMappersUtil.getImageIdMapper();
218 
219         ValueMapper newImageIdMapper = ValueMapperFactory.getValueMapper();
220 
221         ValueMapper igImageIdMapper = AvailableMappersUtil.getIGImageIdMapper();
222 
223         Iterator<Object> itr = igImageIdMapper.iterator();
224 
225         while (itr.hasNext()) {
226             String oldValue = (String)itr.next();
227 
228             PKParser oldValuePKParser = new PKParser(oldValue);
229 
230             String companyId = oldValuePKParser.getString("companyId");
231             String oldIGImageId = oldValuePKParser.getString("imageId");
232 
233             String oldImageId =
234                 companyId + ".image_gallery." + oldIGImageId + ".large";
235 
236             Long newImageId = (Long)imageIdMapper.getNewValue(oldImageId);
237 
238             newImageIdMapper.mapValue(
239                 new Long(GetterUtil.getLong(oldIGImageId)), newImageId);
240         }
241 
242         content = IdReplacer.replaceLongIds(
243             content, "/image_gallery?img_id=", newImageIdMapper);
244 
245         return content;
246     }
247 
248     private static final String _IMG_ID_PATH =
249         "/image/journal/article?img_id=";
250 
251     private static Log _log =
252         LogFactoryUtil.getLog(JournalArticleContentUpgradeColumnImpl.class);
253 
254     private UpgradeColumn _companyIdColumn;
255     private UpgradeColumn _groupIdColumn;
256     private UpgradeColumn _articleIdColumn;
257     private UpgradeColumn _versionColumn;
258     private UpgradeColumn _structureIdColumn;
259     private ValueMapper _imageIdMapper;
260 
261 }