001    /**
002     * Copyright (c) 2000-2012 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.verify;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.HtmlUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.service.ResourceLocalServiceUtil;
026    import com.liferay.portlet.PortletPreferencesFactoryUtil;
027    import com.liferay.portlet.asset.NoSuchEntryException;
028    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
029    import com.liferay.portlet.journal.model.JournalArticle;
030    import com.liferay.portlet.journal.model.JournalContentSearch;
031    import com.liferay.portlet.journal.model.JournalStructure;
032    import com.liferay.portlet.journal.model.JournalTemplate;
033    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
034    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
035    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
036    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
037    
038    import java.sql.Connection;
039    import java.sql.PreparedStatement;
040    import java.sql.ResultSet;
041    
042    import java.util.List;
043    
044    import javax.portlet.PortletPreferences;
045    
046    /**
047     * @author Alexander Chow
048     * @author Shinn Lok
049     */
050    public class VerifyJournal extends VerifyProcess {
051    
052            public static final long DEFAULT_GROUP_ID = 14;
053    
054            public static final int NUM_OF_ARTICLES = 5;
055    
056            @Override
057            protected void doVerify() throws Exception {
058    
059                    // Oracle new line
060    
061                    verifyOracleNewLine();
062    
063                    // Structures
064    
065                    List<JournalStructure> structures =
066                            JournalStructureLocalServiceUtil.getStructures();
067    
068                    for (JournalStructure structure : structures) {
069                            ResourceLocalServiceUtil.addResources(
070                                    structure.getCompanyId(), 0, 0,
071                                    JournalStructure.class.getName(), structure.getId(), false,
072                                    false, false);
073                    }
074    
075                    if (_log.isDebugEnabled()) {
076                            _log.debug("Permissions verified for structures");
077                    }
078    
079                    // Templates
080    
081                    List<JournalTemplate> templates =
082                            JournalTemplateLocalServiceUtil.getTemplates();
083    
084                    for (JournalTemplate template : templates) {
085                            ResourceLocalServiceUtil.addResources(
086                                    template.getCompanyId(), 0, 0, JournalTemplate.class.getName(),
087                                    template.getId(), false, false, false);
088                    }
089    
090                    if (_log.isDebugEnabled()) {
091                            _log.debug("Permissions verified for templates");
092                    }
093    
094                    // Articles
095    
096                    List<JournalArticle> articles =
097                            JournalArticleLocalServiceUtil.getArticles();
098    
099                    for (JournalArticle article : articles) {
100                            long groupId = article.getGroupId();
101                            String articleId = article.getArticleId();
102                            double version = article.getVersion();
103                            String structureId = article.getStructureId();
104    
105                            if (article.getResourcePrimKey() <= 0) {
106                                    article =
107                                            JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
108                                                    groupId, articleId, version);
109                            }
110    
111                            ResourceLocalServiceUtil.addResources(
112                                    article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
113                                    article.getResourcePrimKey(), false, false, false);
114    
115                            try {
116                                    AssetEntryLocalServiceUtil.getEntry(
117                                            JournalArticle.class.getName(),
118                                            article.getResourcePrimKey());
119                            }
120                            catch (NoSuchEntryException nsee) {
121                                    try {
122                                            JournalArticleLocalServiceUtil.updateAsset(
123                                                    article.getUserId(), article, null, null, null);
124                                    }
125                                    catch (Exception e) {
126                                            if (_log.isWarnEnabled()) {
127                                                    _log.warn(
128                                                            "Unable to update asset for article " +
129                                                                    article.getId() + ": " + e.getMessage());
130                                            }
131                                    }
132                            }
133    
134                            String content = GetterUtil.getString(article.getContent());
135    
136                            String newContent = HtmlUtil.replaceMsWordCharacters(content);
137    
138                            if (Validator.isNotNull(structureId)) {
139                                    /*JournalStructure structure =
140                                            JournalStructureLocalServiceUtil.getStructure(
141                                                    groupId, structureId);
142    
143                                    newContent = JournalUtil.removeOldContent(
144                                            newContent, structure.getXsd());*/
145                            }
146    
147                            if (!content.equals(newContent)) {
148                                    JournalArticleLocalServiceUtil.updateContent(
149                                            groupId, articleId, version, newContent);
150                            }
151    
152                            JournalArticleLocalServiceUtil.checkStructure(
153                                    groupId, articleId, version);
154                    }
155    
156                    if (_log.isDebugEnabled()) {
157                            _log.debug("Permissions and assets verified for articles");
158                    }
159    
160                    // Content searches
161    
162                    verifyContentSearch();
163            }
164    
165            protected void verifyContentSearch() throws Exception {
166                    List<JournalContentSearch> contentSearches =
167                            JournalContentSearchLocalServiceUtil.getArticleContentSearches();
168    
169                    for (JournalContentSearch contentSearch : contentSearches) {
170                            verifyContentSearch(contentSearch);
171                    }
172            }
173    
174            protected void verifyContentSearch(JournalContentSearch contentSearch)
175                    throws Exception {
176    
177                    Connection con = null;
178                    PreparedStatement ps = null;
179                    ResultSet rs = null;
180    
181                    try {
182                            con = DataAccess.getUpgradeOptimizedConnection();
183    
184                            ps = con.prepareStatement(
185                                    "select preferences from PortletPreferences where portletId " +
186                                            "= ?");
187    
188                            ps.setString(1, contentSearch.getPortletId());
189    
190                            rs = ps.executeQuery();
191    
192                            while (rs.next()) {
193                                    String xml = rs.getString("preferences");
194    
195                                    PortletPreferences portletPreferences =
196                                            PortletPreferencesFactoryUtil.fromDefaultXML(xml);
197    
198                                    String articleId = portletPreferences.getValue(
199                                            "articleId", null);
200    
201                                    JournalContentSearchLocalServiceUtil.updateContentSearch(
202                                            contentSearch.getGroupId(), contentSearch.isPrivateLayout(),
203                                            contentSearch.getLayoutId(), contentSearch.getPortletId(),
204                                            articleId, true);
205                            }
206                    }
207                    finally {
208                            DataAccess.cleanUp(con, ps, rs);
209                    }
210            }
211    
212            protected void verifyOracleNewLine() throws Exception {
213                    DB db = DBFactoryUtil.getDB();
214    
215                    String dbType = db.getType();
216    
217                    if (!dbType.equals(DB.TYPE_ORACLE)) {
218                            return;
219                    }
220    
221                    // This is a workaround for a limitation in Oracle sqlldr's inability
222                    // insert new line characters for long varchar columns. See
223                    // http://forums.liferay.com/index.php?showtopic=2761&hl=oracle for more
224                    // information. Check several articles because some articles may not
225                    // have new lines.
226    
227                    boolean checkNewLine = false;
228    
229                    List<JournalArticle> articles =
230                            JournalArticleLocalServiceUtil.getArticles(
231                                    DEFAULT_GROUP_ID, 0, NUM_OF_ARTICLES);
232    
233                    for (JournalArticle article : articles) {
234                            String content = article.getContent();
235    
236                            if ((content != null) && (content.indexOf("\\n") != -1)) {
237                                    articles = JournalArticleLocalServiceUtil.getArticles(
238                                            DEFAULT_GROUP_ID);
239    
240                                    for (int j = 0; j < articles.size(); j++) {
241                                            article = articles.get(j);
242    
243                                            JournalArticleLocalServiceUtil.checkNewLine(
244                                                    article.getGroupId(), article.getArticleId(),
245                                                    article.getVersion());
246                                    }
247    
248                                    checkNewLine = true;
249    
250                                    break;
251                            }
252                    }
253    
254                    // Only process this once
255    
256                    if (!checkNewLine) {
257                            if (_log.isInfoEnabled()) {
258                                    _log.debug("Do not fix oracle new line");
259                            }
260    
261                            return;
262                    }
263                    else {
264                            if (_log.isInfoEnabled()) {
265                                    _log.info("Fix oracle new line");
266                            }
267                    }
268    
269                    List<JournalStructure> structures =
270                            JournalStructureLocalServiceUtil.getStructures(
271                                    DEFAULT_GROUP_ID, 0, 1);
272    
273                    if (structures.size() == 1) {
274                            JournalStructure structure = structures.get(0);
275    
276                            String xsd = structure.getXsd();
277    
278                            if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
279                                    structures = JournalStructureLocalServiceUtil.getStructures(
280                                            DEFAULT_GROUP_ID);
281    
282                                    for (int i = 0; i < structures.size(); i++) {
283                                            structure = structures.get(i);
284    
285                                            JournalStructureLocalServiceUtil.checkNewLine(
286                                                    structure.getGroupId(), structure.getStructureId());
287                                    }
288                            }
289                    }
290    
291                    List<JournalTemplate> templates =
292                            JournalTemplateLocalServiceUtil.getTemplates(
293                                    DEFAULT_GROUP_ID, 0, 1);
294    
295                    if (templates.size() == 1) {
296                            JournalTemplate template = templates.get(0);
297    
298                            String xsl = template.getXsl();
299    
300                            if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
301                                    templates = JournalTemplateLocalServiceUtil.getTemplates(
302                                            DEFAULT_GROUP_ID);
303    
304                                    for (int i = 0; i < templates.size(); i++) {
305                                            template = templates.get(i);
306    
307                                            JournalTemplateLocalServiceUtil.checkNewLine(
308                                                    template.getGroupId(), template.getTemplateId());
309                                    }
310                            }
311                    }
312            }
313    
314            private static Log _log = LogFactoryUtil.getLog(VerifyJournal.class);
315    
316    }