1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portal.verify;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
19  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.HtmlUtil;
24  import com.liferay.portal.service.ResourceLocalServiceUtil;
25  import com.liferay.portlet.journal.model.JournalArticle;
26  import com.liferay.portlet.journal.model.JournalStructure;
27  import com.liferay.portlet.journal.model.JournalTemplate;
28  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
29  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
30  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
31  import com.liferay.portlet.tags.NoSuchAssetException;
32  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
33  
34  import java.sql.Connection;
35  import java.sql.PreparedStatement;
36  import java.sql.ResultSet;
37  
38  import java.util.List;
39  
40  /**
41   * <a href="VerifyJournal.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Alexander Chow
44   */
45  public class VerifyJournal extends VerifyProcess {
46  
47      public static final long DEFAULT_GROUP_ID = 14;
48  
49      public static final int NUM_OF_ARTICLES = 5;
50  
51      protected void doVerify() throws Exception {
52  
53          // Oracle new line
54  
55          verifyOracleNewLine();
56  
57          // Structures
58  
59          List<JournalStructure> structures =
60              JournalStructureLocalServiceUtil.getStructures();
61  
62          for (JournalStructure structure : structures) {
63              ResourceLocalServiceUtil.addResources(
64                  structure.getCompanyId(), 0, 0,
65                  JournalStructure.class.getName(), structure.getId(), false,
66                  true, true);
67          }
68  
69          if (_log.isDebugEnabled()) {
70              _log.debug("Permissions verified for Journal structures");
71          }
72  
73          // Templates
74  
75          List<JournalTemplate> templates =
76              JournalTemplateLocalServiceUtil.getTemplates();
77  
78          for (JournalTemplate template : templates) {
79              ResourceLocalServiceUtil.addResources(
80                  template.getCompanyId(), 0, 0,
81                  JournalTemplate.class.getName(), template.getId(), false, true,
82                  true);
83          }
84  
85          if (_log.isDebugEnabled()) {
86              _log.debug("Permissions verified for Journal templates");
87          }
88  
89          // Articles
90  
91          List<JournalArticle> articles =
92              JournalArticleLocalServiceUtil.getArticles();
93  
94          for (JournalArticle article : articles) {
95              long groupId = article.getGroupId();
96              String articleId = article.getArticleId();
97              double version = article.getVersion();
98              //String structureId = article.getStructureId();
99  
100             if (article.getResourcePrimKey() <= 0) {
101                 article =
102                     JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
103                         groupId, articleId, version);
104             }
105 
106             ResourceLocalServiceUtil.addResources(
107                 article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
108                 article.getResourcePrimKey(), false, true, true);
109 
110             try {
111                 TagsAssetLocalServiceUtil.getAsset(
112                     JournalArticle.class.getName(),
113                     article.getResourcePrimKey());
114             }
115             catch (NoSuchAssetException nsae) {
116                 try {
117                     JournalArticleLocalServiceUtil.updateTagsAsset(
118                         article.getUserId(), article, new String[0]);
119                 }
120                 catch (Exception e) {
121                     if (_log.isWarnEnabled()) {
122                         _log.warn(
123                             "Unable to update tags asset for article " +
124                                 article.getId() + ": " + e.getMessage());
125                     }
126                 }
127             }
128 
129             String content = GetterUtil.getString(article.getContent());
130 
131             String newContent = HtmlUtil.replaceMsWordCharacters(content);
132 
133             /*if (Validator.isNotNull(structureId)) {
134                 JournalStructure structure =
135                     JournalStructureLocalServiceUtil.getStructure(
136                         groupId, structureId);
137 
138                 newContent = JournalUtil.removeOldContent(
139                     newContent, structure.getXsd());
140             }*/
141 
142             if (!content.equals(newContent)) {
143                 JournalArticleLocalServiceUtil.updateContent(
144                     groupId, articleId, version, newContent);
145             }
146 
147             JournalArticleLocalServiceUtil.checkStructure(
148                 groupId, articleId, version);
149 
150             //verifyStaleJournalArticle(article);
151         }
152 
153         if (_log.isDebugEnabled()) {
154             _log.debug(
155                 "Permissions and Tags assets verified for Journal articles");
156         }
157     }
158 
159     protected void verifyOracleNewLine() throws Exception {
160         DB db = DBFactoryUtil.getDB();
161 
162         if (!db.getType().equals(DB.TYPE_ORACLE)) {
163             return;
164         }
165 
166         // This is a workaround for a limitation in Oracle sqlldr's inability
167         // insert new line characters for long varchar columns. See
168         // http://forums.liferay.com/index.php?showtopic=2761&hl=oracle for more
169         // information. Check several articles because some articles may not
170         // have new lines.
171 
172         boolean checkNewLine = false;
173 
174         List<JournalArticle> articles = null;
175 
176         if (NUM_OF_ARTICLES <= 0) {
177             checkNewLine = true;
178 
179             articles = JournalArticleLocalServiceUtil.getArticles(
180                 DEFAULT_GROUP_ID);
181         }
182         else {
183             articles = JournalArticleLocalServiceUtil.getArticles(
184                 DEFAULT_GROUP_ID, 0, NUM_OF_ARTICLES);
185         }
186 
187         for (JournalArticle article : articles) {
188             String content = article.getContent();
189 
190             if ((content != null) && (content.indexOf("\\n") != -1)) {
191                 articles = JournalArticleLocalServiceUtil.getArticles(
192                     DEFAULT_GROUP_ID);
193 
194                 for (int j = 0; j < articles.size(); j++) {
195                     article = articles.get(j);
196 
197                     JournalArticleLocalServiceUtil.checkNewLine(
198                         article.getGroupId(), article.getArticleId(),
199                         article.getVersion());
200                 }
201 
202                 checkNewLine = true;
203 
204                 break;
205             }
206         }
207 
208         // Only process this once
209 
210         if (!checkNewLine) {
211             if (_log.isInfoEnabled()) {
212                 _log.debug("Do not fix oracle new line");
213             }
214 
215             return;
216         }
217         else {
218             if (_log.isInfoEnabled()) {
219                 _log.info("Fix oracle new line");
220             }
221         }
222 
223         List<JournalStructure> structures =
224             JournalStructureLocalServiceUtil.getStructures(
225                 DEFAULT_GROUP_ID, 0, 1);
226 
227         if (structures.size() == 1) {
228             JournalStructure structure = structures.get(0);
229 
230             String xsd = structure.getXsd();
231 
232             if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
233                 structures = JournalStructureLocalServiceUtil.getStructures(
234                     DEFAULT_GROUP_ID);
235 
236                 for (int i = 0; i < structures.size(); i++) {
237                     structure = structures.get(i);
238 
239                     JournalStructureLocalServiceUtil.checkNewLine(
240                         structure.getGroupId(), structure.getStructureId());
241                 }
242             }
243         }
244 
245         List<JournalTemplate> templates =
246             JournalTemplateLocalServiceUtil.getTemplates(
247                 DEFAULT_GROUP_ID, 0, 1);
248 
249         if (templates.size() == 1) {
250             JournalTemplate template = templates.get(0);
251 
252             String xsl = template.getXsl();
253 
254             if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
255                 templates = JournalTemplateLocalServiceUtil.getTemplates(
256                     DEFAULT_GROUP_ID);
257 
258                 for (int i = 0; i < templates.size(); i++) {
259                     template = templates.get(i);
260 
261                     JournalTemplateLocalServiceUtil.checkNewLine(
262                         template.getGroupId(), template.getTemplateId());
263                 }
264             }
265         }
266     }
267 
268     protected void verifyStaleJournalArticle(JournalArticle article)
269         throws Exception {
270 
271         long groupId = article.getGroupId();
272         String articleId = article.getArticleId();
273         double version = article.getVersion();
274 
275         if (article.getStructureId().equals("BASIC-RSS-ITEM")) {
276             return;
277         }
278 
279         long count = getPortletPreferencesCount(articleId);
280 
281         if (count == 0) {
282             if (_log.isWarnEnabled()) {
283                 _log.warn(
284                     "Article {groupId=" + groupId + ", articleId=" +
285                         articleId + ", version=" + version +
286                             "} is not used on any layouts");
287             }
288         }
289     }
290 
291     protected long getPortletPreferencesCount(String articleId)
292         throws Exception {
293 
294         Connection con = null;
295         PreparedStatement ps = null;
296         ResultSet rs = null;
297 
298         try {
299             con = DataAccess.getConnection();
300 
301             ps = con.prepareStatement(_GET_PORTLET_PREFERENCES_COUNT);
302 
303             ps.setString(
304                 1, "%<name>article-id</name><value>" + articleId + "</value>%");
305 
306             rs = ps.executeQuery();
307 
308             while (rs.next()) {
309                 long count = rs.getLong("count_value");
310 
311                 return count;
312             }
313         }
314         finally {
315             DataAccess.cleanUp(con, ps, rs);
316         }
317 
318         return 0;
319     }
320 
321     private static final String _GET_PORTLET_PREFERENCES_COUNT =
322         "select count(*) as count_value from PortletPreferences where " +
323             "ownerId = 0 and ownerType = 3 and portletId like " +
324                 "'56_INSTANCE_%' and preferences like ?";
325 
326     private static Log _log = LogFactoryUtil.getLog(VerifyJournal.class);
327 
328 }