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