001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.model.LayoutTypePortlet;
027    import com.liferay.portal.model.PortletConstants;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portlet.journal.model.JournalContentSearch;
030    import com.liferay.portlet.journal.service.base.JournalContentSearchLocalServiceBaseImpl;
031    
032    import java.util.ArrayList;
033    import java.util.List;
034    
035    import javax.portlet.PortletPreferences;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Wesley Gong
040     */
041    public class JournalContentSearchLocalServiceImpl
042            extends JournalContentSearchLocalServiceBaseImpl {
043    
044            public void checkContentSearches(long companyId)
045                    throws PortalException, SystemException {
046    
047                    if (_log.isInfoEnabled()) {
048                            _log.info("Checking journal content search for " + companyId);
049                    }
050    
051                    List<Layout> layouts = new ArrayList<Layout>();
052    
053                    List<Group> groups = groupLocalService.search(
054                            companyId, null, null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
055    
056                    for (Group group : groups) {
057    
058                            // Private layouts
059    
060                            deleteOwnerContentSearches(group.getGroupId(), true);
061    
062                            layouts.addAll(
063                                    layoutLocalService.getLayouts(group.getGroupId(), true));
064    
065                            // Public layouts
066    
067                            deleteOwnerContentSearches(group.getGroupId(), false);
068    
069                            layouts.addAll(
070                                    layoutLocalService.getLayouts(group.getGroupId(), false));
071                    }
072    
073                    for (Layout layout : layouts) {
074                            LayoutTypePortlet layoutTypePortlet =
075                                    (LayoutTypePortlet)layout.getLayoutType();
076    
077                            List<String> portletIds = layoutTypePortlet.getPortletIds();
078    
079                            for (String portletId : portletIds) {
080                                    String rootPortletId = PortletConstants.getRootPortletId(
081                                            portletId);
082    
083                                    if (rootPortletId.equals(PortletKeys.JOURNAL_CONTENT)) {
084                                            PortletPreferences preferences =
085                                                    portletPreferencesLocalService.getPreferences(
086                                                            layout.getCompanyId(),
087                                                            PortletKeys.PREFS_OWNER_ID_DEFAULT,
088                                                            PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
089                                                            layout.getPlid(), portletId);
090    
091                                            String articleId = preferences.getValue(
092                                                    "articleId", StringPool.BLANK);
093    
094                                            if (Validator.isNotNull(articleId)) {
095                                                    updateContentSearch(
096                                                            layout.getGroupId(), layout.isPrivateLayout(),
097                                                            layout.getLayoutId(), portletId, articleId);
098                                            }
099                                    }
100                            }
101                    }
102            }
103    
104            public void deleteArticleContentSearch(
105                            long groupId, boolean privateLayout, long layoutId,
106                            String portletId, String articleId)
107                    throws PortalException, SystemException {
108    
109                    JournalContentSearch contentSearch =
110                            journalContentSearchPersistence.findByG_P_L_P_A(
111                                    groupId, privateLayout, layoutId, portletId, articleId);
112    
113                    deleteJournalContentSearch(contentSearch);
114            }
115    
116            public void deleteArticleContentSearches(long groupId, String articleId)
117                    throws SystemException {
118    
119                    List<JournalContentSearch> contentSearches =
120                            journalContentSearchPersistence.findByG_A(groupId, articleId);
121    
122                    for (JournalContentSearch contentSearch : contentSearches) {
123                            deleteJournalContentSearch(contentSearch);
124                    }
125            }
126    
127            public void deleteLayoutContentSearches(
128                            long groupId, boolean privateLayout, long layoutId)
129                    throws SystemException {
130    
131                    List<JournalContentSearch> contentSearches =
132                            journalContentSearchPersistence.findByG_P_L(
133                                    groupId, privateLayout, layoutId);
134    
135                    for (JournalContentSearch contentSearch : contentSearches) {
136                            deleteJournalContentSearch(contentSearch);
137                    }
138            }
139    
140            public void deleteOwnerContentSearches(long groupId, boolean privateLayout)
141                    throws SystemException {
142    
143                    List<JournalContentSearch> contentSearches =
144                            journalContentSearchPersistence.findByG_P(groupId, privateLayout);
145    
146                    for (JournalContentSearch contentSearch : contentSearches) {
147                            deleteJournalContentSearch(contentSearch);
148                    }
149            }
150    
151            public List<JournalContentSearch> getArticleContentSearches()
152                    throws SystemException {
153    
154                    return journalContentSearchPersistence.findAll();
155            }
156    
157            public List<JournalContentSearch> getArticleContentSearches(
158                            long groupId, String articleId)
159                    throws SystemException {
160    
161                    return journalContentSearchPersistence.findByG_A(groupId, articleId);
162            }
163    
164            public List<JournalContentSearch> getArticleContentSearches(
165                            String articleId)
166                    throws SystemException {
167    
168                    return journalContentSearchPersistence.findByArticleId(articleId);
169            }
170    
171            public List<Long> getLayoutIds(
172                            long groupId, boolean privateLayout, String articleId)
173                    throws SystemException {
174    
175                    List<Long> layoutIds = new ArrayList<Long>();
176    
177                    List<JournalContentSearch> contentSearches =
178                            journalContentSearchPersistence.findByG_P_A(
179                                    groupId, privateLayout, articleId);
180    
181                    for (JournalContentSearch contentSearch : contentSearches) {
182                            layoutIds.add(contentSearch.getLayoutId());
183                    }
184    
185                    return layoutIds;
186            }
187    
188            public int getLayoutIdsCount(
189                            long groupId, boolean privateLayout, String articleId)
190                    throws SystemException {
191    
192                    return journalContentSearchPersistence.countByG_P_A(
193                            groupId, privateLayout, articleId);
194            }
195    
196            public int getLayoutIdsCount(String articleId) throws SystemException {
197                    return journalContentSearchPersistence.countByArticleId(articleId);
198            }
199    
200            public List<JournalContentSearch> getPortletContentSearches(
201                            String portletId)
202                    throws SystemException {
203    
204                    return journalContentSearchPersistence.findByPortletId(portletId);
205            }
206    
207            public JournalContentSearch updateContentSearch(
208                            long groupId, boolean privateLayout, long layoutId,
209                            String portletId, String articleId)
210                    throws PortalException, SystemException {
211    
212                    return updateContentSearch(
213                            groupId, privateLayout, layoutId, portletId, articleId, false);
214            }
215    
216            public JournalContentSearch updateContentSearch(
217                            long groupId, boolean privateLayout, long layoutId,
218                            String portletId, String articleId, boolean purge)
219                    throws PortalException, SystemException {
220    
221                    if (purge) {
222                            journalContentSearchPersistence.removeByG_P_L_P(
223                                    groupId, privateLayout, layoutId, portletId);
224                    }
225    
226                    Group group = groupPersistence.findByPrimaryKey(groupId);
227    
228                    JournalContentSearch contentSearch =
229                            journalContentSearchPersistence.fetchByG_P_L_P_A(
230                                    groupId, privateLayout, layoutId, portletId, articleId);
231    
232                    if (contentSearch == null) {
233                            long contentSearchId = counterLocalService.increment();
234    
235                            contentSearch = journalContentSearchPersistence.create(
236                                    contentSearchId);
237    
238                            contentSearch.setGroupId(groupId);
239                            contentSearch.setCompanyId(group.getCompanyId());
240                            contentSearch.setPrivateLayout(privateLayout);
241                            contentSearch.setLayoutId(layoutId);
242                            contentSearch.setPortletId(portletId);
243                            contentSearch.setArticleId(articleId);
244                    }
245    
246                    journalContentSearchPersistence.update(contentSearch);
247    
248                    return contentSearch;
249            }
250    
251            public List<JournalContentSearch> updateContentSearch(
252                            long groupId, boolean privateLayout, long layoutId,
253                            String portletId, String[] articleIds)
254                    throws PortalException, SystemException {
255    
256                    journalContentSearchPersistence.removeByG_P_L_P(
257                            groupId, privateLayout, layoutId, portletId);
258    
259                    List<JournalContentSearch> contentSearches =
260                            new ArrayList<JournalContentSearch>();
261    
262                    for (String articleId : articleIds) {
263                            JournalContentSearch contentSearch = updateContentSearch(
264                                    groupId, privateLayout, layoutId, portletId, articleId, false);
265    
266                            contentSearches.add(contentSearch);
267                    }
268    
269                    return contentSearches;
270            }
271    
272            private static Log _log = LogFactoryUtil.getLog(
273                    JournalContentSearchLocalServiceImpl.class);
274    
275    }