1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.verify;
24  
25  import com.liferay.portal.service.ResourceLocalServiceUtil;
26  import com.liferay.portlet.journal.model.JournalArticle;
27  import com.liferay.portlet.journal.model.JournalStructure;
28  import com.liferay.portlet.journal.model.JournalTemplate;
29  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
30  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
31  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
32  import com.liferay.portlet.tags.NoSuchAssetException;
33  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
34  
35  import java.util.List;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * <a href="VerifyJournal.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Alexander Chow
44   *
45   */
46  public class VerifyJournal extends VerifyProcess {
47  
48      public void verify() throws VerifyException {
49          _log.info("Verifying integrity");
50  
51          try {
52              verifyJournal();
53          }
54          catch (Exception e) {
55              throw new VerifyException(e);
56          }
57      }
58  
59      protected void verifyJournal() throws Exception {
60  
61          // Structures
62  
63          List structures = JournalStructureLocalServiceUtil.getStructures();
64  
65          for (int i = 0; i  < structures.size(); i++) {
66              JournalStructure structure = (JournalStructure)structures.get(i);
67  
68              ResourceLocalServiceUtil.addResources(
69                  structure.getCompanyId(), 0, 0,
70                  JournalStructure.class.getName(), structure.getId(), false,
71                  true, true);
72          }
73  
74          if (_log.isDebugEnabled()) {
75              _log.debug("Permissions verified for Journal structures");
76          }
77  
78          // Templates
79  
80          List templates = JournalTemplateLocalServiceUtil.getTemplates();
81  
82          for (int i = 0; i < templates.size(); i++) {
83              JournalTemplate template = (JournalTemplate)templates.get(i);
84  
85              ResourceLocalServiceUtil.addResources(
86                  template.getCompanyId(), 0, 0,
87                  JournalTemplate.class.getName(), template.getId(), false, true,
88                  true);
89          }
90  
91          if (_log.isDebugEnabled()) {
92              _log.debug("Permissions verified for Journal templates");
93          }
94  
95          // Articles
96  
97          List articles = JournalArticleLocalServiceUtil.getArticles();
98  
99          for (int i = 0; i < articles.size(); i++) {
100             JournalArticle article = (JournalArticle)articles.get(i);
101 
102             if (article.getResourcePrimKey() <= 0) {
103                 article =
104                     JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
105                         article.getGroupId(), article.getArticleId(),
106                         article.getVersion());
107             }
108 
109             ResourceLocalServiceUtil.addResources(
110                 article.getCompanyId(), 0, 0,
111                 JournalArticle.class.getName(), article.getResourcePrimKey(),
112                 false, true, true);
113 
114             try {
115                 TagsAssetLocalServiceUtil.getAsset(
116                     JournalArticle.class.getName(),
117                     article.getResourcePrimKey());
118             }
119             catch (NoSuchAssetException nsae) {
120                 try {
121                     JournalArticleLocalServiceUtil.updateTagsAsset(
122                         article.getUserId(), article, new String[0]);
123                 }
124                 catch (Exception e) {
125                     if (_log.isWarnEnabled()) {
126                         _log.warn(
127                             "Unable to update tags asset for article " +
128                                 article.getId(),
129                             e);
130                     }
131                 }
132             }
133         }
134 
135         if (_log.isDebugEnabled()) {
136             _log.debug(
137                 "Permissions and Tags assets verified for Journal articles");
138         }
139     }
140 
141     private static Log _log = LogFactory.getLog(VerifyJournal.class);
142 
143 }