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