1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.service.impl;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryUtil;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.SystemException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.Group;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.model.LayoutTypePortlet;
27  import com.liferay.portal.model.PortletConstants;
28  import com.liferay.portal.util.PortletKeys;
29  import com.liferay.portlet.journal.model.JournalContentSearch;
30  import com.liferay.portlet.journal.service.base.JournalContentSearchLocalServiceBaseImpl;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  import javax.portlet.PortletPreferences;
36  
37  /**
38   * <a href="JournalContentSearchLocalServiceImpl.java.html"><b><i>View Source
39   * </i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Wesley Gong
43   */
44  public class JournalContentSearchLocalServiceImpl
45      extends JournalContentSearchLocalServiceBaseImpl {
46  
47      public void checkContentSearches(long companyId)
48          throws PortalException, SystemException {
49  
50          if (_log.isInfoEnabled()) {
51              _log.info("Checking journal content search for " + companyId);
52          }
53  
54          List<Layout> layouts = new ArrayList<Layout>();
55  
56          List<Group> groups = groupLocalService.search(
57              companyId, null, null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
58  
59          for (Group group : groups) {
60  
61              // Private layouts
62  
63              deleteOwnerContentSearches(group.getGroupId(), true);
64  
65              layouts.addAll(
66                  layoutLocalService.getLayouts(group.getGroupId(), true));
67  
68              // Public layouts
69  
70              deleteOwnerContentSearches(group.getGroupId(), false);
71  
72              layouts.addAll(
73                  layoutLocalService.getLayouts(group.getGroupId(), false));
74          }
75  
76          for (Layout layout : layouts) {
77              LayoutTypePortlet layoutTypePortlet =
78                  (LayoutTypePortlet)layout.getLayoutType();
79  
80              List<String> portletIds = layoutTypePortlet.getPortletIds();
81  
82              for (String portletId : portletIds) {
83                  String rootPortletId = PortletConstants.getRootPortletId(
84                      portletId);
85  
86                  if (rootPortletId.equals(PortletKeys.JOURNAL_CONTENT)) {
87                      PortletPreferences preferences =
88                          portletPreferencesLocalService.getPreferences(
89                              layout.getCompanyId(),
90                              PortletKeys.PREFS_OWNER_ID_DEFAULT,
91                              PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
92                              layout.getPlid(), portletId);
93  
94                      String articleId = preferences.getValue(
95                          "article-id", StringPool.BLANK);
96  
97                      if (Validator.isNotNull(articleId)) {
98                          updateContentSearch(
99                              layout.getGroupId(), layout.isPrivateLayout(),
100                             layout.getLayoutId(), portletId, articleId);
101                     }
102                 }
103             }
104         }
105     }
106 
107     public void deleteArticleContentSearch(
108             long groupId, boolean privateLayout, long layoutId,
109             String portletId, String articleId)
110         throws PortalException, SystemException {
111 
112         journalContentSearchPersistence.removeByG_P_L_P_A(
113             groupId, privateLayout, layoutId, portletId, articleId);
114     }
115 
116     public void deleteArticleContentSearches(long groupId, String articleId)
117         throws SystemException {
118 
119         journalContentSearchPersistence.removeByG_A(groupId, articleId);
120     }
121 
122     public void deleteLayoutContentSearches(
123             long groupId, boolean privateLayout, long layoutId)
124         throws SystemException {
125 
126         journalContentSearchPersistence.removeByG_P_L(
127             groupId, privateLayout, layoutId);
128     }
129 
130     public void deleteOwnerContentSearches(long groupId, boolean privateLayout)
131         throws SystemException {
132 
133         journalContentSearchPersistence.removeByG_P(groupId, privateLayout);
134     }
135 
136     public List<JournalContentSearch> getArticleContentSearches()
137         throws SystemException {
138 
139         return journalContentSearchPersistence.findAll();
140     }
141 
142     public List<JournalContentSearch> getArticleContentSearches(
143             long groupId, String articleId)
144         throws SystemException {
145 
146         return journalContentSearchPersistence.findByG_A(groupId, articleId);
147     }
148 
149     public List<JournalContentSearch> getArticleContentSearches(
150             String articleId)
151         throws SystemException {
152 
153         return journalContentSearchPersistence.findByArticleId(articleId);
154     }
155 
156     public List<Long> getLayoutIds(
157             long groupId, boolean privateLayout, String articleId)
158         throws SystemException {
159 
160         List<Long> layoutIds = new ArrayList<Long>();
161 
162         List<JournalContentSearch> contentSearches =
163             journalContentSearchPersistence.findByG_P_A(
164                 groupId, privateLayout, articleId);
165 
166         for (JournalContentSearch contentSearch : contentSearches) {
167             layoutIds.add(contentSearch.getLayoutId());
168         }
169 
170         return layoutIds;
171     }
172 
173     public int getLayoutIdsCount(
174             long groupId, boolean privateLayout, String articleId)
175         throws SystemException {
176 
177         return journalContentSearchPersistence.countByG_P_A(
178             groupId, privateLayout, articleId);
179     }
180 
181     public int getLayoutIdsCount(String articleId) throws SystemException {
182         return journalContentSearchPersistence.countByArticleId(articleId);
183     }
184 
185     public JournalContentSearch updateContentSearch(
186             long groupId, boolean privateLayout, long layoutId,
187             String portletId, String articleId)
188         throws PortalException, SystemException {
189 
190         return updateContentSearch(
191             groupId, privateLayout, layoutId, portletId, articleId, false);
192     }
193 
194     public JournalContentSearch updateContentSearch(
195             long groupId, boolean privateLayout, long layoutId,
196             String portletId, String articleId, boolean purge)
197         throws PortalException, SystemException {
198 
199         if (purge) {
200             journalContentSearchPersistence.removeByG_P_L_P(
201                 groupId, privateLayout, layoutId, portletId);
202         }
203 
204         Group group = groupPersistence.findByPrimaryKey(groupId);
205 
206         JournalContentSearch contentSearch =
207             journalContentSearchPersistence.fetchByG_P_L_P_A(
208                 groupId, privateLayout, layoutId, portletId, articleId);
209 
210         if (contentSearch == null) {
211             long contentSearchId = counterLocalService.increment();
212 
213             contentSearch = journalContentSearchPersistence.create(
214                 contentSearchId);
215 
216             contentSearch.setGroupId(groupId);
217             contentSearch.setCompanyId(group.getCompanyId());
218             contentSearch.setPrivateLayout(privateLayout);
219             contentSearch.setLayoutId(layoutId);
220             contentSearch.setPortletId(portletId);
221             contentSearch.setArticleId(articleId);
222         }
223 
224         journalContentSearchPersistence.update(contentSearch, false);
225 
226         return contentSearch;
227     }
228 
229     public List<JournalContentSearch> updateContentSearch(
230             long groupId, boolean privateLayout, long layoutId,
231             String portletId, String[] articleIds)
232         throws PortalException, SystemException {
233 
234         journalContentSearchPersistence.removeByG_P_L_P(
235             groupId, privateLayout, layoutId, portletId);
236 
237         List<JournalContentSearch> contentSearches =
238             new ArrayList<JournalContentSearch>();
239 
240         for (String articleId : articleIds) {
241             JournalContentSearch contentSearch = updateContentSearch(
242                 groupId, privateLayout, layoutId, portletId, articleId, false);
243 
244             contentSearches.add(contentSearch);
245         }
246 
247         return contentSearches;
248     }
249 
250     private static Log _log = LogFactoryUtil.getLog(
251         JournalContentSearchLocalServiceImpl.class);
252 
253 }