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