1   /**
2    * Copyright (c) 2000-2008 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.portlet.journal.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Group;
31  import com.liferay.portal.model.Layout;
32  import com.liferay.portal.model.LayoutTypePortlet;
33  import com.liferay.portal.model.PortletConstants;
34  import com.liferay.portal.util.PortletKeys;
35  import com.liferay.portlet.journal.model.JournalContentSearch;
36  import com.liferay.portlet.journal.service.base.JournalContentSearchLocalServiceBaseImpl;
37  
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import javax.portlet.PortletPreferences;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  
46  /**
47   * <a href="JournalContentSearchLocalServiceImpl.java.html"><b><i>View Source
48   * </i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class JournalContentSearchLocalServiceImpl
54      extends JournalContentSearchLocalServiceBaseImpl {
55  
56      public void checkContentSearches(long companyId)
57          throws PortalException, SystemException {
58  
59          if (_log.isInfoEnabled()) {
60              _log.info("Checking journal content search for " + companyId);
61          }
62  
63          List<Layout> layouts = new ArrayList<Layout>();
64  
65          List<Group> groups = groupLocalService.search(
66              companyId, null, null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
67  
68          for (Group group : groups) {
69  
70              // Private layouts
71  
72              deleteOwnerContentSearches(group.getGroupId(), true);
73  
74              layouts.addAll(
75                  layoutLocalService.getLayouts(group.getGroupId(), true));
76  
77              // Public layouts
78  
79              deleteOwnerContentSearches(group.getGroupId(), false);
80  
81              layouts.addAll(
82                  layoutLocalService.getLayouts(group.getGroupId(), false));
83          }
84  
85          for (Layout layout : layouts) {
86              LayoutTypePortlet layoutTypePortlet =
87                  (LayoutTypePortlet)layout.getLayoutType();
88  
89              List<String> portletIds = layoutTypePortlet.getPortletIds();
90  
91              for (String portletId : portletIds) {
92                  String rootPortletId = PortletConstants.getRootPortletId(
93                      portletId);
94  
95                  if (rootPortletId.equals(PortletKeys.JOURNAL_CONTENT)) {
96                      PortletPreferences prefs =
97                          portletPreferencesLocalService.getPreferences(
98                              layout.getCompanyId(),
99                              PortletKeys.PREFS_OWNER_ID_DEFAULT,
100                             PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
101                             layout.getPlid(), portletId);
102 
103                     String articleId = prefs.getValue(
104                         "article-id", StringPool.BLANK);
105 
106                     if (Validator.isNotNull(articleId)) {
107                         updateContentSearch(
108                             layout.getGroupId(), layout.isPrivateLayout(),
109                             layout.getLayoutId(), portletId, articleId);
110                     }
111                 }
112             }
113         }
114     }
115 
116     public void deleteArticleContentSearch(
117             long groupId, boolean privateLayout, long layoutId,
118             String portletId, String articleId)
119         throws PortalException, SystemException {
120 
121         journalContentSearchPersistence.removeByG_P_L_P_A(
122             groupId, privateLayout, layoutId, portletId, articleId);
123     }
124 
125     public void deleteArticleContentSearches(long groupId, String articleId)
126         throws SystemException {
127 
128         journalContentSearchPersistence.removeByG_A(groupId, articleId);
129     }
130 
131     public void deleteLayoutContentSearches(
132             long groupId, boolean privateLayout, long layoutId)
133         throws SystemException {
134 
135         journalContentSearchPersistence.removeByG_P_L(
136             groupId, privateLayout, layoutId);
137     }
138 
139     public void deleteOwnerContentSearches(long groupId, boolean privateLayout)
140         throws SystemException {
141 
142         journalContentSearchPersistence.removeByG_P(groupId, privateLayout);
143     }
144 
145     public List<JournalContentSearch> getArticleContentSearches()
146         throws SystemException {
147 
148         return journalContentSearchPersistence.findAll();
149     }
150 
151     public List<JournalContentSearch> getArticleContentSearches(
152             long groupId, String articleId)
153         throws SystemException {
154 
155         return journalContentSearchPersistence.findByG_A(groupId, articleId);
156     }
157 
158     public List<Long> getLayoutIds(
159             long groupId, boolean privateLayout, String articleId)
160         throws SystemException {
161 
162         List<Long> layoutIds = new ArrayList<Long>();
163 
164         List<JournalContentSearch> contentSearches =
165             journalContentSearchPersistence.findByG_P_A(
166                 groupId, privateLayout, articleId);
167 
168         for (JournalContentSearch contentSearch : contentSearches) {
169             layoutIds.add(contentSearch.getLayoutId());
170         }
171 
172         return layoutIds;
173     }
174 
175     public int getLayoutIdsCount(
176             long groupId, boolean privateLayout, String articleId)
177         throws SystemException {
178 
179         return journalContentSearchPersistence.countByG_P_A(
180             groupId, privateLayout, articleId);
181     }
182 
183     public JournalContentSearch updateContentSearch(
184             long groupId, boolean privateLayout, long layoutId,
185             String portletId, String articleId)
186         throws PortalException, SystemException {
187 
188         return updateContentSearch(
189             groupId, privateLayout, layoutId, portletId, articleId, false);
190     }
191 
192     public JournalContentSearch updateContentSearch(
193             long groupId, boolean privateLayout, long layoutId,
194             String portletId, String articleId, boolean purge)
195         throws PortalException, SystemException {
196 
197         if (purge) {
198             journalContentSearchPersistence.removeByG_P_L_P(
199                 groupId, privateLayout, layoutId, portletId);
200         }
201 
202         Group group = groupPersistence.findByPrimaryKey(groupId);
203 
204         JournalContentSearch contentSearch =
205             journalContentSearchPersistence.fetchByG_P_L_P_A(
206                 groupId, privateLayout, layoutId, portletId, articleId);
207 
208         if (contentSearch == null) {
209             long contentSearchId = counterLocalService.increment();
210 
211             contentSearch = journalContentSearchPersistence.create(
212                 contentSearchId);
213 
214             contentSearch.setGroupId(groupId);
215             contentSearch.setCompanyId(group.getCompanyId());
216             contentSearch.setPrivateLayout(privateLayout);
217             contentSearch.setLayoutId(layoutId);
218             contentSearch.setPortletId(portletId);
219             contentSearch.setArticleId(articleId);
220         }
221 
222         journalContentSearchPersistence.update(contentSearch, false);
223 
224         return contentSearch;
225     }
226 
227     public List<JournalContentSearch> updateContentSearch(
228             long groupId, boolean privateLayout, long layoutId,
229             String portletId, String[] articleIds)
230         throws PortalException, SystemException {
231 
232         journalContentSearchPersistence.removeByG_P_L_P(
233             groupId, privateLayout, layoutId, portletId);
234 
235         List<JournalContentSearch> contentSearches =
236             new ArrayList<JournalContentSearch>();
237 
238         for (String articleId : articleIds) {
239             JournalContentSearch contentSearch = updateContentSearch(
240                 groupId, privateLayout, layoutId, portletId, articleId, false);
241 
242             contentSearches.add(contentSearch);
243         }
244 
245         return contentSearches;
246     }
247 
248     private static Log _log =
249         LogFactory.getLog(JournalContentSearchLocalServiceImpl.class);
250 
251 }