1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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   * @author Wesley Gong
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 prefs =
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 = prefs.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<JournalContentSearch> getArticleContentSearches(
158             String articleId)
159         throws SystemException {
160 
161         return journalContentSearchPersistence.findByArticleId(articleId);
162     }
163 
164     public List<Long> getLayoutIds(
165             long groupId, boolean privateLayout, String articleId)
166         throws SystemException {
167 
168         List<Long> layoutIds = new ArrayList<Long>();
169 
170         List<JournalContentSearch> contentSearches =
171             journalContentSearchPersistence.findByG_P_A(
172                 groupId, privateLayout, articleId);
173 
174         for (JournalContentSearch contentSearch : contentSearches) {
175             layoutIds.add(contentSearch.getLayoutId());
176         }
177 
178         return layoutIds;
179     }
180 
181     public int getLayoutIdsCount(
182             long groupId, boolean privateLayout, String articleId)
183         throws SystemException {
184 
185         return journalContentSearchPersistence.countByG_P_A(
186             groupId, privateLayout, articleId);
187     }
188 
189     public int getLayoutIdsCount(String articleId) throws SystemException {
190         return journalContentSearchPersistence.countByArticleId(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             journalContentSearchPersistence.removeByG_P_L_P(
209                 groupId, privateLayout, layoutId, portletId);
210         }
211 
212         Group group = groupPersistence.findByPrimaryKey(groupId);
213 
214         JournalContentSearch contentSearch =
215             journalContentSearchPersistence.fetchByG_P_L_P_A(
216                 groupId, privateLayout, layoutId, portletId, articleId);
217 
218         if (contentSearch == null) {
219             long contentSearchId = counterLocalService.increment();
220 
221             contentSearch = journalContentSearchPersistence.create(
222                 contentSearchId);
223 
224             contentSearch.setGroupId(groupId);
225             contentSearch.setCompanyId(group.getCompanyId());
226             contentSearch.setPrivateLayout(privateLayout);
227             contentSearch.setLayoutId(layoutId);
228             contentSearch.setPortletId(portletId);
229             contentSearch.setArticleId(articleId);
230         }
231 
232         journalContentSearchPersistence.update(contentSearch, false);
233 
234         return contentSearch;
235     }
236 
237     public List<JournalContentSearch> updateContentSearch(
238             long groupId, boolean privateLayout, long layoutId,
239             String portletId, String[] articleIds)
240         throws PortalException, SystemException {
241 
242         journalContentSearchPersistence.removeByG_P_L_P(
243             groupId, privateLayout, layoutId, portletId);
244 
245         List<JournalContentSearch> contentSearches =
246             new ArrayList<JournalContentSearch>();
247 
248         for (String articleId : articleIds) {
249             JournalContentSearch contentSearch = updateContentSearch(
250                 groupId, privateLayout, layoutId, portletId, articleId, false);
251 
252             contentSearches.add(contentSearch);
253         }
254 
255         return contentSearches;
256     }
257 
258     private static Log _log =
259         LogFactoryUtil.getLog(JournalContentSearchLocalServiceImpl.class);
260 
261 }