1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
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.PortletConstants;
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  
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  import javax.portlet.PortletPreferences;
41  
42  /**
43   * <a href="JournalContentSearchLocalServiceImpl.java.html"><b><i>View Source
44   * </i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class JournalContentSearchLocalServiceImpl
50      extends JournalContentSearchLocalServiceBaseImpl {
51  
52      public void checkContentSearches(long companyId)
53          throws PortalException, SystemException {
54  
55          if (_log.isInfoEnabled()) {
56              _log.info("Checking journal content search for " + companyId);
57          }
58  
59          List<Layout> layouts = new ArrayList<Layout>();
60  
61          List<Group> groups = groupLocalService.search(
62              companyId, null, null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
63  
64          for (Group group : groups) {
65  
66              // Private layouts
67  
68              deleteOwnerContentSearches(group.getGroupId(), true);
69  
70              layouts.addAll(
71                  layoutLocalService.getLayouts(group.getGroupId(), true));
72  
73              // Public layouts
74  
75              deleteOwnerContentSearches(group.getGroupId(), false);
76  
77              layouts.addAll(
78                  layoutLocalService.getLayouts(group.getGroupId(), false));
79          }
80  
81          for (Layout layout : layouts) {
82              LayoutTypePortlet layoutTypePortlet =
83                  (LayoutTypePortlet)layout.getLayoutType();
84  
85              List<String> portletIds = layoutTypePortlet.getPortletIds();
86  
87              for (String portletId : portletIds) {
88                  String rootPortletId = PortletConstants.getRootPortletId(
89                      portletId);
90  
91                  if (rootPortletId.equals(PortletKeys.JOURNAL_CONTENT)) {
92                      PortletPreferences prefs =
93                          portletPreferencesLocalService.getPreferences(
94                              layout.getCompanyId(),
95                              PortletKeys.PREFS_OWNER_ID_DEFAULT,
96                              PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
97                              layout.getPlid(), portletId);
98  
99                      String articleId = prefs.getValue(
100                         "article-id", StringPool.BLANK);
101 
102                     if (Validator.isNotNull(articleId)) {
103                         updateContentSearch(
104                             layout.getGroupId(), layout.isPrivateLayout(),
105                             layout.getLayoutId(), portletId, articleId);
106                     }
107                 }
108             }
109         }
110     }
111 
112     public void deleteArticleContentSearch(
113             long groupId, boolean privateLayout, long layoutId,
114             String portletId, String articleId)
115         throws PortalException, SystemException {
116 
117         journalContentSearchPersistence.removeByG_P_L_P_A(
118             groupId, privateLayout, layoutId, portletId, articleId);
119     }
120 
121     public void deleteArticleContentSearches(long groupId, String articleId)
122         throws SystemException {
123 
124         journalContentSearchPersistence.removeByG_A(groupId, articleId);
125     }
126 
127     public void deleteLayoutContentSearches(
128             long groupId, boolean privateLayout, long layoutId)
129         throws SystemException {
130 
131         journalContentSearchPersistence.removeByG_P_L(
132             groupId, privateLayout, layoutId);
133     }
134 
135     public void deleteOwnerContentSearches(long groupId, boolean privateLayout)
136         throws SystemException {
137 
138         journalContentSearchPersistence.removeByG_P(groupId, privateLayout);
139     }
140 
141     public List<JournalContentSearch> getArticleContentSearches()
142         throws SystemException {
143 
144         return journalContentSearchPersistence.findAll();
145     }
146 
147     public List<JournalContentSearch> getArticleContentSearches(
148             long groupId, String articleId)
149         throws SystemException {
150 
151         return journalContentSearchPersistence.findByG_A(groupId, articleId);
152     }
153 
154     public List<Long> getLayoutIds(
155             long groupId, boolean privateLayout, String articleId)
156         throws SystemException {
157 
158         List<Long> layoutIds = new ArrayList<Long>();
159 
160         List<JournalContentSearch> contentSearches =
161             journalContentSearchPersistence.findByG_P_A(
162                 groupId, privateLayout, articleId);
163 
164         for (JournalContentSearch contentSearch : contentSearches) {
165             layoutIds.add(contentSearch.getLayoutId());
166         }
167 
168         return layoutIds;
169     }
170 
171     public int getLayoutIdsCount(
172             long groupId, boolean privateLayout, String articleId)
173         throws SystemException {
174 
175         return journalContentSearchPersistence.countByG_P_A(
176             groupId, privateLayout, articleId);
177     }
178 
179     public JournalContentSearch updateContentSearch(
180             long groupId, boolean privateLayout, long layoutId,
181             String portletId, String articleId)
182         throws PortalException, SystemException {
183 
184         return updateContentSearch(
185             groupId, privateLayout, layoutId, portletId, articleId, false);
186     }
187 
188     public JournalContentSearch updateContentSearch(
189             long groupId, boolean privateLayout, long layoutId,
190             String portletId, String articleId, boolean purge)
191         throws PortalException, SystemException {
192 
193         if (purge) {
194             journalContentSearchPersistence.removeByG_P_L_P(
195                 groupId, privateLayout, layoutId, portletId);
196         }
197 
198         Group group = groupPersistence.findByPrimaryKey(groupId);
199 
200         JournalContentSearch contentSearch =
201             journalContentSearchPersistence.fetchByG_P_L_P_A(
202                 groupId, privateLayout, layoutId, portletId, articleId);
203 
204         if (contentSearch == null) {
205             long contentSearchId = counterLocalService.increment();
206 
207             contentSearch = journalContentSearchPersistence.create(
208                 contentSearchId);
209 
210             contentSearch.setGroupId(groupId);
211             contentSearch.setCompanyId(group.getCompanyId());
212             contentSearch.setPrivateLayout(privateLayout);
213             contentSearch.setLayoutId(layoutId);
214             contentSearch.setPortletId(portletId);
215             contentSearch.setArticleId(articleId);
216         }
217 
218         journalContentSearchPersistence.update(contentSearch, false);
219 
220         return contentSearch;
221     }
222 
223     public List<JournalContentSearch> updateContentSearch(
224             long groupId, boolean privateLayout, long layoutId,
225             String portletId, String[] articleIds)
226         throws PortalException, SystemException {
227 
228         journalContentSearchPersistence.removeByG_P_L_P(
229             groupId, privateLayout, layoutId, portletId);
230 
231         List<JournalContentSearch> contentSearches =
232             new ArrayList<JournalContentSearch>();
233 
234         for (String articleId : articleIds) {
235             JournalContentSearch contentSearch = updateContentSearch(
236                 groupId, privateLayout, layoutId, portletId, articleId, false);
237 
238             contentSearches.add(contentSearch);
239         }
240 
241         return contentSearches;
242     }
243 
244     private static Log _log =
245         LogFactoryUtil.getLog(JournalContentSearchLocalServiceImpl.class);
246 
247 }