001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upgrade.v4_3_0.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
020    import com.liferay.portal.kernel.upgrade.util.IdReplacer;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
022    import com.liferay.portal.kernel.upgrade.util.ValueMapper;
023    import com.liferay.portal.kernel.upgrade.util.ValueMapperFactoryUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.xml.Document;
029    import com.liferay.portal.kernel.xml.Element;
030    import com.liferay.portal.kernel.xml.SAXReaderUtil;
031    import com.liferay.portal.upgrade.util.Table;
032    import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
033    import com.liferay.portlet.journal.util.JournalUtil;
034    import com.liferay.util.PKParser;
035    
036    import java.util.Iterator;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class JournalArticleContentUpgradeColumnImpl
042            extends BaseUpgradeColumnImpl {
043    
044            public JournalArticleContentUpgradeColumnImpl(
045                    UpgradeColumn companyIdColumn, UpgradeColumn groupIdColumn,
046                    UpgradeColumn articleIdColumn, UpgradeColumn versionColumn,
047                    UpgradeColumn structureIdColumn, ValueMapper imageIdMapper) {
048    
049                    super("content");
050    
051                    _companyIdColumn = companyIdColumn;
052                    _groupIdColumn = groupIdColumn;
053                    _articleIdColumn = articleIdColumn;
054                    _versionColumn = versionColumn;
055                    _structureIdColumn = structureIdColumn;
056                    _imageIdMapper = imageIdMapper;
057            }
058    
059            public Object getNewValue(Object oldValue) throws Exception {
060                    String content = (String)oldValue;
061    
062                    content = StringUtil.replace(
063                            content, Table.SAFE_CHARS[1], Table.SAFE_CHARS[0]);
064    
065                    /*if (content.indexOf("\\n") != -1) {
066                            content = StringUtil.replace(
067                                    content,
068                                    new String[] {"\\n", "\\r"},
069                                    new String[] {"\n", "\r"});
070                    }*/
071    
072                    String structureId = (String)_structureIdColumn.getOldValue();
073    
074                    if (Validator.isNotNull(structureId)) {
075                            content = formatContent(content);
076                    }
077    
078                    content = replaceIds(content);
079    
080                    content = StringUtil.replace(
081                            content, Table.SAFE_CHARS[0], Table.SAFE_CHARS[1]);
082    
083                    return content;
084            }
085    
086            protected String formatContent(String content) throws Exception {
087                    String oldCompanyId = (String)_companyIdColumn.getOldValue();
088                    Long newCompanyId = (Long)_companyIdColumn.getNewValue();
089                    Long groupId = (Long)_groupIdColumn.getNewValue();
090                    String articleId = (String)_articleIdColumn.getNewValue();
091                    Double version = (Double)_versionColumn.getNewValue();
092    
093                    try {
094                            Document doc = SAXReaderUtil.read(content);
095    
096                            Element root = doc.getRootElement();
097    
098                            format(
099                                    oldCompanyId, newCompanyId.longValue(), groupId.longValue(),
100                                    articleId, version.doubleValue(), root);
101    
102                            content = JournalUtil.formatXML(doc);
103                    }
104                    catch (Exception e) {
105                            _log.error(
106                                    "Unable to format content for {articleId=" + articleId +
107                                            ",version=" + version + "}: " + e.getMessage());
108                    }
109    
110                    return content;
111            }
112    
113            protected void format(
114                            String oldCompanyId, long newCompanyId, long groupId,
115                            String articleId, double version, Element root)
116                    throws Exception {
117    
118                    Iterator<Element> itr = root.elements().iterator();
119    
120                    while (itr.hasNext()) {
121                            Element el = itr.next();
122    
123                            Element dynamicContent = el.element("dynamic-content");
124    
125                            String elInstanceId = StringPool.BLANK;
126                            String elName = el.attributeValue("name", StringPool.BLANK);
127                            String elType = el.attributeValue("type", StringPool.BLANK);
128                            String elLanguage = StringPool.BLANK;
129    
130                            if (dynamicContent != null) {
131                                    elLanguage = dynamicContent.attributeValue(
132                                            "language-id", StringPool.BLANK);
133    
134                                    if (!elLanguage.equals(StringPool.BLANK)) {
135                                            elLanguage = "_" + elLanguage;
136                                    }
137                            }
138    
139                            if (elType.equals("image") || elType.equals("text")) {
140                                    String oldImageId = dynamicContent.getText();
141    
142                                    if (oldImageId.startsWith(_IMG_ID_PATH) ||
143                                            oldImageId.startsWith("@portal_url@" + _IMG_ID_PATH) ||
144                                            oldImageId.startsWith(
145                                                    "http://@portal_url@" + _IMG_ID_PATH) ||
146                                            oldImageId.startsWith(
147                                                    "https://@portal_url@" + _IMG_ID_PATH)) {
148    
149                                            int pos = oldImageId.indexOf(_IMG_ID_PATH);
150    
151                                            String preOldImageId = oldImageId.substring(0, pos);
152    
153                                            oldImageId = oldImageId.substring(
154                                                    pos + _IMG_ID_PATH.length(), oldImageId.length());
155    
156                                            String newImageId = getNewImageId(oldCompanyId, oldImageId);
157    
158                                            dynamicContent.setText(
159                                                    preOldImageId + _IMG_ID_PATH + newImageId);
160    
161                                            if (elType.equals("image")) {
162                                                    dynamicContent.addAttribute("id", newImageId);
163    
164                                                    long articleImageId = GetterUtil.getLong(newImageId);
165    
166                                                    JournalArticleImageLocalServiceUtil.addArticleImageId(
167                                                            articleImageId, groupId, articleId, version,
168                                                            elInstanceId, elName, elLanguage);
169                                            }
170                                    }
171                            }
172    
173                            format(oldCompanyId, newCompanyId, groupId, articleId, version, el);
174                    }
175            }
176    
177            protected String getNewImageId(String oldCompanyId, String oldImageId)
178                    throws Exception {
179    
180                    int pos = oldImageId.lastIndexOf("&version=");
181    
182                    oldImageId =
183                            oldImageId.substring(0, pos) + "." +
184                                    oldImageId.substring(pos + 9, oldImageId.length());
185    
186                    String newImageId = oldCompanyId + ".journal.article." + oldImageId;
187    
188                    return String.valueOf(_imageIdMapper.getNewValue(newImageId));
189            }
190    
191            protected String replaceIds(String content) throws Exception {
192                    ValueMapper dlFolderIdMapper =
193                            AvailableMappersUtil.getDLFolderIdMapper();
194    
195                    content = IdReplacer.replaceLongIds(
196                            content, "/document_library/get_file?folderId=", dlFolderIdMapper);
197                    content = IdReplacer.replaceLongIds(
198                            content,
199                            "_20_struts_action=%2Fdocument_library%2Fget_file&_20_folderId=",
200                            dlFolderIdMapper);
201                    content = IdReplacer.replaceLongIds(
202                            content,
203                            "_20_struts_action=%2Fdocument_library%2Fget_file&amp;" +
204                                    "_20_folderId=",
205                            dlFolderIdMapper);
206    
207                    ValueMapper imageIdMapper = AvailableMappersUtil.getImageIdMapper();
208    
209                    ValueMapper newImageIdMapper = ValueMapperFactoryUtil.getValueMapper();
210    
211                    ValueMapper igImageIdMapper = AvailableMappersUtil.getIGImageIdMapper();
212    
213                    Iterator<Object> itr = igImageIdMapper.iterator();
214    
215                    while (itr.hasNext()) {
216                            String oldValue = (String)itr.next();
217    
218                            PKParser oldValuePKParser = new PKParser(oldValue);
219    
220                            String companyId = oldValuePKParser.getString("companyId");
221                            String oldIGImageId = oldValuePKParser.getString("imageId");
222    
223                            String oldImageId =
224                                    companyId + ".image_gallery." + oldIGImageId + ".large";
225    
226                            Long newImageId = (Long)imageIdMapper.getNewValue(oldImageId);
227    
228                            newImageIdMapper.mapValue(
229                                    new Long(GetterUtil.getLong(oldIGImageId)), newImageId);
230                    }
231    
232                    content = IdReplacer.replaceLongIds(
233                            content, "/image_gallery?img_id=", newImageIdMapper);
234    
235                    return content;
236            }
237    
238            private static final String _IMG_ID_PATH =
239                    "/image/journal/article?img_id=";
240    
241            private static Log _log = LogFactoryUtil.getLog(
242                    JournalArticleContentUpgradeColumnImpl.class);
243    
244            private UpgradeColumn _companyIdColumn;
245            private UpgradeColumn _groupIdColumn;
246            private UpgradeColumn _articleIdColumn;
247            private UpgradeColumn _versionColumn;
248            private UpgradeColumn _structureIdColumn;
249            private ValueMapper _imageIdMapper;
250    
251    }